3 #include <mysql/mysql_time.h>
7 #include <browsing/constants.h>
9 #include <battlefield/gameserver.h>
11 std::unordered_map<std::string, Battlefield::GameServer::SetterFunc> Battlefield::GameServer::SetterMap = {
12 {
"localip0", &Battlefield::GameServer::SetLocalIp },
13 {
"localport", &Battlefield::GameServer::SetLocalPort },
14 {
"natneg", &Battlefield::GameServer::SetNatNeg },
15 {
"gamename", &Battlefield::GameServer::SetGameName },
16 {
"hostname", &Battlefield::GameServer::SetHostName },
17 {
"hostport", &Battlefield::GameServer::SetHostPort },
18 {
"gamever", &Battlefield::GameServer::SetGameVersion },
19 {
"cl", &Battlefield::GameServer::SetClientVersion },
20 {
"rv", &Battlefield::GameServer::SetRV },
21 {
"map", &Battlefield::GameServer::SetMap },
22 {
"mc", &Battlefield::GameServer::SetMapCycling },
23 {
"mapname", &Battlefield::GameServer::SetMapName },
24 {
"gc", &Battlefield::GameServer::SetGC },
25 {
"gametype", &Battlefield::GameServer::SetGameType },
26 {
"gamevariant", &Battlefield::GameServer::SetGameVariant },
27 {
"numplayers", &Battlefield::GameServer::SetNumPlayers },
28 {
"maxplayers", &Battlefield::GameServer::SetMaxPlayers },
29 {
"numteams", &Battlefield::GameServer::SetNumTeams },
30 {
"gamemode", &Battlefield::GameServer::SetGameMode },
31 {
"teamplay", &Battlefield::GameServer::SetTeamplay },
32 {
"fraglimit", &Battlefield::GameServer::SetFlagLimit },
33 {
"teamfraglimit", &Battlefield::GameServer::SetTeamFragLimit },
34 {
"timelimit", &Battlefield::GameServer::SetTimeLimit },
35 {
"timeelapsed", &Battlefield::GameServer::SetTimeElapsed },
36 {
"password", &Battlefield::GameServer::SetPassword },
37 {
"nr", &Battlefield::GameServer::SetMinRank },
38 {
"xr", &Battlefield::GameServer::SetMaxRank },
39 {
"ff", &Battlefield::GameServer::SetFriendlyFire },
40 {
"sr", &Battlefield::GameServer::SetStatsTracking },
41 {
"rc", &Battlefield::GameServer::SetReconfigurable },
42 {
"ni", &Battlefield::GameServer::SetMinIpRange },
43 {
"xi", &Battlefield::GameServer::SetMaxIpRange },
44 {
"qm", &Battlefield::GameServer::SetQM },
45 {
"region", &Battlefield::GameServer::SetRegion },
46 {
"c0", &Battlefield::GameServer::SetClan1Id },
47 {
"c1", &Battlefield::GameServer::SetClan2Id },
48 {
"n0", &Battlefield::GameServer::SetClan1Name },
49 {
"n1", &Battlefield::GameServer::SetClan2Name },
50 {
"c0c", &Battlefield::GameServer::SetClan1Claimed },
51 {
"c1c", &Battlefield::GameServer::SetClan2Claimed },
56 this->SetIp(
"168.119.189.149");
58 this->SetFlag(FLAG_UNSOLICITED_UDP | FLAG_PRIVATE_IP | FLAG_ICMP_IP | FLAG_NONSTANDARD_PORT | FLAG_NONSTANDARD_PRIVATE_PORT);
61 bool Battlefield::GameServer::SetId(
int id)
67 void Battlefield::GameServer::GetIpArray(uint8_t* ip)
const
69 inet_pton(AF_INET, this->_ip.c_str(), ip);
72 bool Battlefield::GameServer::SetIp(
const std::string& ip)
78 bool Battlefield::GameServer::SetPort(uint16_t port)
84 bool Battlefield::GameServer::SetFlag(uint8_t flag)
90 bool Battlefield::GameServer::SetLocalIp(
const std::string& localip)
92 this->_localip0 = localip;
96 bool Battlefield::GameServer::SetLocalPort(uint16_t localport)
98 this->_localport = localport;
102 bool Battlefield::GameServer::SetLocalPort(
const std::string& str_localport)
106 uint16_t localport =
static_cast<uint16_t
>(std::stoul(str_localport));
108 return this->SetLocalPort(localport);
115 bool Battlefield::GameServer::SetNatNeg(uint8_t natneg)
117 this->_natneg = natneg;
121 bool Battlefield::GameServer::SetNatNeg(
const std::string& str_natneg)
125 uint8_t natneg =
static_cast<uint8_t
>(std::stoul(str_natneg));
127 return this->SetNatNeg(natneg);
134 bool Battlefield::GameServer::SetGameName(
const std::string& gamename)
136 this->_gamename = gamename;
140 bool Battlefield::GameServer::SetHostName(
const std::string& hostname)
142 this->_hostname = hostname;
146 bool Battlefield::GameServer::SetHostPort(uint16_t hostport)
148 this->_hostport = hostport;
152 bool Battlefield::GameServer::SetHostPort(
const std::string& str_hostport)
156 uint16_t hostport =
static_cast<uint16_t
>(std::stoul(str_hostport));
158 return this->SetHostPort(hostport);
165 bool Battlefield::GameServer::SetGameVersion(
const std::string& gamever)
167 this->_gamever = gamever;
171 bool Battlefield::GameServer::SetClientVersion(
const std::string& clientver)
173 this->_cl = clientver;
177 bool Battlefield::GameServer::SetRV(
const std::string& rv)
183 bool Battlefield::GameServer::SetMap(
const std::string& map)
189 bool Battlefield::GameServer::SetMapCycling(uint8_t mc)
195 bool Battlefield::GameServer::SetMapCycling(
const std::string& str_mc)
199 uint8_t mc =
static_cast<uint8_t
>(std::stoul(str_mc));
201 return this->SetMapCycling(mc);
208 bool Battlefield::GameServer::SetMapName(uint8_t mapname)
210 this->_mapname = mapname;
214 bool Battlefield::GameServer::SetMapName(
const std::string& str_mapname)
218 uint8_t mapname =
static_cast<uint8_t
>(std::stoul(str_mapname));
220 return this->SetMapName(mapname);
227 bool Battlefield::GameServer::SetGC(uint8_t gc)
233 bool Battlefield::GameServer::SetGC(
const std::string& str_gc)
237 uint8_t gc =
static_cast<uint8_t
>(std::stoul(str_gc));
239 return this->SetGC(gc);
246 bool Battlefield::GameServer::SetGameType(
const std::string& gametype)
248 this->_gametype = gametype;
252 bool Battlefield::GameServer::SetGameVariant(
const std::string& gamevariant)
254 this->_gamevariant = gamevariant;
258 bool Battlefield::GameServer::SetNumPlayers(uint8_t numplayers)
260 this->_numplayers = numplayers;
264 bool Battlefield::GameServer::SetNumPlayers(
const std::string& str_numplayers)
268 uint8_t numplayers =
static_cast<uint8_t
>(std::stoul(str_numplayers));
270 return this->SetNumPlayers(numplayers);
277 bool Battlefield::GameServer::SetMaxPlayers(uint8_t maxplayers)
279 this->_maxplayers = maxplayers;
283 bool Battlefield::GameServer::SetMaxPlayers(
const std::string& str_maxplayers)
287 uint8_t maxplayers =
static_cast<uint8_t
>(std::stoul(str_maxplayers));
289 return this->SetMaxPlayers(maxplayers);
296 bool Battlefield::GameServer::SetNumTeams(uint8_t numteams)
298 this->_numteams = numteams;
302 bool Battlefield::GameServer::SetNumTeams(
const std::string& str_numteams)
306 uint8_t numteams =
static_cast<uint8_t
>(std::stoul(str_numteams));
308 return this->SetNumTeams(numteams);
315 bool Battlefield::GameServer::SetGameMode(
const std::string& gamemode)
317 this->_gamemode = gamemode;
321 bool Battlefield::GameServer::SetTeamplay(uint8_t teamplay)
323 this->_teamplay = teamplay;
327 bool Battlefield::GameServer::SetTeamplay(
const std::string& str_teamplay)
331 uint8_t teamplay =
static_cast<uint8_t
>(std::stoul(str_teamplay));
333 return this->SetTeamplay(teamplay);
340 bool Battlefield::GameServer::SetFlagLimit(uint8_t fraglimit)
342 this->_fraglimit = fraglimit;
346 bool Battlefield::GameServer::SetFlagLimit(
const std::string& str_fraglimit)
350 uint8_t fraglimit =
static_cast<uint8_t
>(std::stoul(str_fraglimit));
352 return this->SetFlagLimit(fraglimit);
359 bool Battlefield::GameServer::SetTeamFragLimit(uint8_t teamfraglimit)
361 this->_teamfraglimit = teamfraglimit;
365 bool Battlefield::GameServer::SetTeamFragLimit(
const std::string& str_teamfraglimit)
369 uint8_t teamfraglimit =
static_cast<uint8_t
>(std::stoul(str_teamfraglimit));
371 return this->SetTeamFragLimit(teamfraglimit);
378 bool Battlefield::GameServer::SetTimeLimit(uint16_t timelimit)
380 this->_timelimit = timelimit;
384 bool Battlefield::GameServer::SetTimeLimit(
const std::string& str_timelimit)
388 uint16_t timelimit =
static_cast<uint16_t
>(std::stoul(str_timelimit));
390 return this->SetTimeLimit(timelimit);
397 bool Battlefield::GameServer::SetTimeElapsed(uint16_t timeelapsed)
399 this->_timeelapsed = timeelapsed;
403 bool Battlefield::GameServer::SetTimeElapsed(
const std::string& str_timeelapsed)
407 uint16_t timeelapsed =
static_cast<uint16_t
>(std::stoul(str_timeelapsed));
409 return this->SetTimeElapsed(timeelapsed);
416 bool Battlefield::GameServer::SetPassword(uint8_t password)
418 this->_password = password;
422 bool Battlefield::GameServer::SetPassword(
const std::string& str_password)
426 uint8_t password =
static_cast<uint8_t
>(std::stoul(str_password));
428 return this->SetPassword(password);
435 bool Battlefield::GameServer::SetMinRank(uint8_t nr)
441 bool Battlefield::GameServer::SetMinRank(
const std::string& str_nr)
445 uint8_t nr =
static_cast<uint8_t
>(std::stoul(str_nr));
447 return this->SetMinRank(nr);
454 bool Battlefield::GameServer::SetMaxRank(uint8_t xr)
460 bool Battlefield::GameServer::SetMaxRank(
const std::string& str_xr)
464 uint8_t xr =
static_cast<uint8_t
>(std::stoul(str_xr));
466 return this->SetMaxRank(xr);
473 bool Battlefield::GameServer::SetFriendlyFire(uint8_t ff)
479 bool Battlefield::GameServer::SetFriendlyFire(
const std::string& str_ff)
483 uint8_t ff =
static_cast<uint8_t
>(std::stoul(str_ff));
485 return this->SetFriendlyFire(ff);
492 bool Battlefield::GameServer::SetStatsTracking(uint8_t sr)
498 bool Battlefield::GameServer::SetStatsTracking(
const std::string& str_sr)
502 uint8_t sr =
static_cast<uint8_t
>(std::stoul(str_sr));
504 return this->SetStatsTracking(sr);
511 bool Battlefield::GameServer::SetReconfigurable(uint8_t rc)
517 bool Battlefield::GameServer::SetReconfigurable(
const std::string& str_rc)
521 uint8_t rc =
static_cast<uint8_t
>(std::stoul(str_rc));
523 return this->SetReconfigurable(rc);
530 bool Battlefield::GameServer::SetMinIpRange(int64_t ni)
536 bool Battlefield::GameServer::SetMinIpRange(
const std::string& str_ni)
540 int64_t ni =
static_cast<int64_t
>(std::stoll(str_ni));
542 return this->SetMinIpRange(ni);
549 bool Battlefield::GameServer::SetMaxIpRange(int64_t xi)
555 bool Battlefield::GameServer::SetMaxIpRange(
const std::string& str_xi)
559 int64_t xi =
static_cast<int64_t
>(std::stoll(str_xi));
561 return this->SetMaxIpRange(xi);
568 bool Battlefield::GameServer::SetQM(uint8_t qm)
574 bool Battlefield::GameServer::SetQM(
const std::string& str_qm)
578 uint8_t qm =
static_cast<uint8_t
>(std::stoul(str_qm));
580 return this->SetQM(qm);
587 bool Battlefield::GameServer::SetRegion(uint64_t region)
589 this->_region = region;
593 bool Battlefield::GameServer::SetRegion(
const std::string& str_region)
597 uint64_t region =
static_cast<uint64_t
>(std::stoull(str_region));
599 return this->SetRegion(region);
607 bool Battlefield::GameServer::SetClan1Id(
int c0)
613 bool Battlefield::GameServer::SetClan1Id(
const std::string& str_c0)
617 int c0 = std::stoi(str_c0);
619 return this->SetClan1Id(c0);
626 bool Battlefield::GameServer::SetClan2Id(
int c1)
632 bool Battlefield::GameServer::SetClan2Id(
const std::string& str_c1)
636 int c1 = std::stoi(str_c1);
638 return this->SetClan2Id(c1);
645 bool Battlefield::GameServer::SetClan1Name(
const std::string& n0)
651 bool Battlefield::GameServer::SetClan2Name(
const std::string& n1)
657 bool Battlefield::GameServer::SetClan1Claimed(uint8_t c0c)
663 bool Battlefield::GameServer::SetClan1Claimed(
const std::string& str_c0c)
667 uint8_t c0c =
static_cast<uint8_t
>(std::stoul(str_c0c));
669 return this->SetClan1Claimed(c0c);
676 bool Battlefield::GameServer::SetClan2Claimed(uint8_t c1c)
682 bool Battlefield::GameServer::SetClan2Claimed(
const std::string& str_c1c)
686 uint8_t c1c =
static_cast<uint8_t
>(std::stoul(str_c1c));
688 return this->SetClan2Claimed(c1c);
695 bool Battlefield::GameServer::SetTeam1Name(
const std::string& team0)
697 this->_team0 = team0;
701 bool Battlefield::GameServer::SetTeam2Name(
const std::string& team1)
703 this->_team1 = team1;
707 bool Battlefield::GameServer::SetTeam1Score(int16_t score0)
709 this->_score0 = score0;
713 bool Battlefield::GameServer::SetTeam1Score(
const std::string& str_score0)
717 int16_t score0 =
static_cast<int16_t
>(std::stoi(str_score0));
719 return this->SetTeam1Score(score0);
726 bool Battlefield::GameServer::SetTeam2Score(int16_t score1)
728 this->_score1 = score1;
732 bool Battlefield::GameServer::SetTeam2Score(
const std::string& str_score1)
736 int16_t score1 =
static_cast<int16_t
>(std::stoi(str_score1));
738 return this->SetTeam2Score(score1);
745 bool Battlefield::GameServer::SetUpdatedAt(MYSQL_TIME updated_at)
747 this->_updated_at = Util::Time::GetDateTime(updated_at);
752 bool Battlefield::GameServer::SetVerified(
bool verified)
754 this->_verified = verified;
758 bool Battlefield::GameServer::SetVerified(uint8_t verified)
760 return this->SetVerified(verified == 1);
765 this->_players.push_back(gsplayer);
770 struct tm timeinfo = {};
772 if (strptime(this->GetUpdatedAt().c_str(),
"%Y-%m-%d %H:%M:%S %Z", &timeinfo) !=
nullptr)
774 std::chrono::system_clock::time_point current_time_point = std::chrono::system_clock::now();
775 std::chrono::system_clock::time_point two_minutes_ago = current_time_point - std::chrono::minutes(2);
778 std::time_t current_time = std::chrono::system_clock::to_time_t(current_time_point);
779 std::time_t two_minutes_ago_time = std::chrono::system_clock::to_time_t(two_minutes_ago);
780 std::time_t target_time = std::mktime(&timeinfo);
786 return (std::difftime(target_time, current_time) <= 0 && std::difftime(target_time, two_minutes_ago_time) >= 0);
792 void Battlefield::GameServer::Debug()
794 Logger::debug(
"============================");
795 Logger::debug(
"id = " + std::to_string(this->GetId()));
796 Logger::debug(
"ip = " + this->GetIp());
797 Logger::debug(
"port = " + std::to_string(this->GetPort()));
798 Logger::debug(
"flag = " + std::to_string(this->GetFlag()));
799 Logger::debug(
"localip0 = " + this->GetLocalIp());
800 Logger::debug(
"localport = " + std::to_string(this->GetLocalPort()));
801 Logger::debug(
"natneg = " + std::to_string(this->GetNatNeg()));
802 Logger::debug(
"gamename = " + this->GetGameName());
803 Logger::debug(
"hostname = " + this->GetHostName());
804 Logger::debug(
"hostport = " + std::to_string(this->GetHostPort()));
805 Logger::debug(
"gamever = " + this->GetGameVersion());
806 Logger::debug(
"cl = " + this->GetClientVersion());
807 Logger::debug(
"rv = " + this->GetRV());
808 Logger::debug(
"map = " + this->GetMap());
809 Logger::debug(
"mc = " + std::to_string(this->GetMapCycling()));
810 Logger::debug(
"mapname = " + std::to_string(this->GetMapName()));
811 Logger::debug(
"gc = " + std::to_string(this->GetGC()));
812 Logger::debug(
"gametype = " + this->GetGameType());
813 Logger::debug(
"gamevariant = " + this->GetGameVariant());
814 Logger::debug(
"numplayers = " + std::to_string(this->GetNumPlayers()));
815 Logger::debug(
"maxplayers = " + std::to_string(this->GetMaxPlayers()));
816 Logger::debug(
"numteams = " + std::to_string(this->GetNumTeams()));
817 Logger::debug(
"gamemode = " + this->GetGameMode());
818 Logger::debug(
"teamplay = " + std::to_string(this->GetTeamplay()));
819 Logger::debug(
"fraglimit = " + std::to_string(this->GetFlagLimit()));
820 Logger::debug(
"teamfraglimit = " + std::to_string(this->GetTeamFragLimit()));
821 Logger::debug(
"timelimit = " + std::to_string(this->GetTimeLimit()));
822 Logger::debug(
"timeelapsed = " + std::to_string(this->GetTimeElapsed()));
823 Logger::debug(
"password = " + std::to_string(this->GetPassword()));
824 Logger::debug(
"nr = " + std::to_string(this->GetMinRank()));
825 Logger::debug(
"xr = " + std::to_string(this->GetMaxRank()));
826 Logger::debug(
"ff = " + std::to_string(this->GetFriendlyFire()));
827 Logger::debug(
"sr = " + std::to_string(this->GetStatsTracking()));
828 Logger::debug(
"rc = " + std::to_string(this->GetReconfigurable()));
829 Logger::debug(
"ni = " + std::to_string(this->GetMinIpRange()));
830 Logger::debug(
"xi = " + std::to_string(this->GetMaxIpRange()));
831 Logger::debug(
"qm = " + std::to_string(this->GetQM()));
832 Logger::debug(
"region = " + std::to_string(this->GetRegion()));
833 Logger::debug(
"c0 = " + std::to_string(this->GetClan1Id()));
834 Logger::debug(
"c1 = " + std::to_string(this->GetClan2Id()));
835 Logger::debug(
"n0 = " + this->GetClan1Name());
836 Logger::debug(
"n1 = " + this->GetClan2Name());
837 Logger::debug(
"c0c = " + std::to_string(this->GetClan1Claimed()));
838 Logger::debug(
"c1c = " + std::to_string(this->GetClan2Claimed()));
839 Logger::debug(
"team0 = " + this->GetTeam1Name());
840 Logger::debug(
"score0 = " + std::to_string(this->GetTeam1Score()));
841 Logger::debug(
"team1 = " + this->GetTeam2Name());
842 Logger::debug(
"score1 = " + std::to_string(this->GetTeam2Score()));
847 bool Battlefield::GameServerPlayer::SetId(
int id)
853 bool Battlefield::GameServerPlayer::SetName(
const std::string& name)
859 bool Battlefield::GameServerPlayer::SetScore(int16_t score)
861 this->_score = score;
865 bool Battlefield::GameServerPlayer::SetScore(
const std::string& str_score)
869 int16_t score =
static_cast<int16_t
>(std::stoi(str_score));
871 return this->SetScore(score);
878 bool Battlefield::GameServerPlayer::SetSkill(
const std::string& skill)
880 this->_skill = skill;
884 bool Battlefield::GameServerPlayer::SetPing(uint8_t ping)
890 bool Battlefield::GameServerPlayer::SetPing(
const std::string& str_ping)
894 uint8_t ping =
static_cast<uint8_t
>(std::stoul(str_ping));
896 return this->SetPing(ping);
903 bool Battlefield::GameServerPlayer::SetTeam(uint8_t team)
909 bool Battlefield::GameServerPlayer::SetTeam(
const std::string& str_team)
913 uint8_t team =
static_cast<uint8_t
>(std::stoul(str_team));
915 return this->SetTeam(team);
922 bool Battlefield::GameServerPlayer::SetDeaths(uint16_t deaths)
924 this->_deaths = deaths;
928 bool Battlefield::GameServerPlayer::SetDeaths(
const std::string& str_deaths)
932 uint16_t deaths =
static_cast<uint16_t
>(std::stoul(str_deaths));
934 return this->SetDeaths(deaths);
941 bool Battlefield::GameServerPlayer::SetProfileId(
int profileid)
943 this->_profileid = profileid;
947 bool Battlefield::GameServerPlayer::SetProfileId(
const std::string& str_profileid)
951 int profileid = std::stoi(str_profileid);
953 return this->SetProfileId(profileid);
Represents a player in a game server.
void useExample()
Performs an example operation using the GameServer class.
bool IsAlive()
Checks if the game server is alive.
void AddPlayer(const GameServerPlayer &gsplayer)
Adds a player to the game server.