Files
Embeddium/Embeddium/Backend/FriendStore.cpp
T
ApfelTeeSaft 446e6a5b7f Initial Release
theres some bugs related to crashes, idk what causes them....
2026-03-11 21:40:17 +01:00

92 lines
2.4 KiB
C++

#include "FriendStore.h"
#include "../Assets/AssetManager.h"
#include <chrono>
#include <sstream>
#include <iomanip>
namespace Embeddium {
Json FriendStore::sFriendList_;
Json FriendStore::sFriendList2_;
static std::string NowIso() {
auto now = std::chrono::system_clock::now();
auto t = std::chrono::system_clock::to_time_t(now);
std::tm tm{};
#ifdef _WIN32
gmtime_s(&tm, &t);
#else
gmtime_r(&t, &tm);
#endif
std::ostringstream ss;
ss << std::put_time(&tm, "%Y-%m-%dT%H:%M:%S.000Z");
return ss.str();
}
void FriendStore::Init() {
sFriendList_ = JsonUtils::ParseSafe(AssetManager::GetJSON("friendslist.json"));
sFriendList2_ = JsonUtils::ParseSafe(AssetManager::GetJSON("friendslist2.json"));
if (!sFriendList_.is_array()) sFriendList_ = Json::array();
if (!sFriendList2_.is_object()) sFriendList2_ = Json::object();
if (!sFriendList2_.contains("friends"))
sFriendList2_["friends"] = Json::array();
}
bool FriendStore::HasFriend(std::string_view accountId) {
for (const auto& f : sFriendList_) {
if (f.value("accountId", "") == accountId) return true;
}
return false;
}
void FriendStore::AddFriendV1(std::string_view accountId) {
std::string now = NowIso();
sFriendList_.push_back({
{"accountId", std::string(accountId)},
{"status", "ACCEPTED"},
{"direction", "OUTBOUND"},
{"created", now},
{"favorite", false}
});
}
void FriendStore::AddFriendV2(std::string_view accountId) {
std::string now = NowIso();
sFriendList2_["friends"].push_back({
{"accountId", std::string(accountId)},
{"groups", Json::array()},
{"mutual", 0},
{"alias", ""},
{"note", ""},
{"favorite", false},
{"created", now}
});
}
Json FriendStore::GetFriendList(std::string_view accountId) {
if (!HasFriend(accountId)) {
AddFriendV1(accountId);
AddFriendV2(accountId);
}
return sFriendList_;
}
Json FriendStore::GetFriendSummary(std::string_view accountId) {
// Check presence in v2 list
bool found = false;
for (const auto& f : sFriendList2_["friends"]) {
if (f.value("accountId", "") == accountId) { found = true; break; }
}
if (!found) {
AddFriendV2(accountId);
if (!HasFriend(accountId)) AddFriendV1(accountId);
}
return sFriendList2_;
}
} // namespace Embeddium