Favourites.

This commit is contained in:
momo5502 2016-01-08 15:31:26 +01:00
parent b83306cc20
commit dfc18268af
2 changed files with 71 additions and 1 deletions

View File

@ -204,7 +204,65 @@ namespace Components
}
else if (ServerList::IsFavouriteList())
{
// TODO: Whatever
ServerList::LoadFavourties();
}
}
void ServerList::StoreFavourite(std::string server)
{
//json11::Json::parse()
std::vector<std::string> servers;
if (Utils::FileExists("players/favourites.json"))
{
std::string data = Utils::ReadFile("players/favourites.json");
json11::Json object = json11::Json::parse(data, data);
if (!object.is_array())
{
Logger::Print("Favourites storage file is invalid!\n");
return;
}
auto storedServers = object.array_items();
for (unsigned int i = 0; i < storedServers.size(); i++)
{
if (!storedServers[i].is_string()) continue;
if (storedServers[i].string_value() == server) return;
servers.push_back(storedServers[i].string_value());
}
}
servers.push_back(server);
json11::Json data = json11::Json(servers);
Utils::WriteFile("players/favourites.json", data.dump());
}
void ServerList::LoadFavourties()
{
if (ServerList::IsFavouriteList() && Utils::FileExists("players/favourites.json"))
{
ServerList::GetList().clear();
std::string data = Utils::ReadFile("players/favourites.json");
json11::Json object = json11::Json::parse(data, data);
if (!object.is_array())
{
Logger::Print("Favourites storage file is invalid!\n");
return;
}
auto servers = object.array_items();
for (unsigned int i = 0; i < servers.size(); i++)
{
if(!servers[i].is_string()) continue;
ServerList::InsertRequest(servers[i].string_value(), true);
}
}
}
@ -508,6 +566,15 @@ namespace Components
Logger::Print("Sorting server list by token: %d\n", ServerList::SortKey);
ServerList::SortList();
});
UIScript::Add("CreateListFavorite", [] ()
{
ServerList::ServerInfo* info = ServerList::GetCurrentServer();
ServerList::StoreFavourite(info->Addr.GetString());
});
UIScript::Add("CreateFavorite", [] ()
{
ServerList::StoreFavourite(Dvar::Var("ui_favoriteAddress").Get<std::string>());
});
// Add required ownerDraws
UIScript::AddOwnerDraw(220, ServerList::UpdateSource);

View File

@ -105,6 +105,9 @@ namespace Components
static void SortList();
static void LoadFavourties();
static void StoreFavourite(std::string server);
static ServerInfo* GetServer(int index);
static std::vector<ServerInfo>& GetList();