From 06e3c0e3c3dae9fdd356b1e5c18235c85bc4051e Mon Sep 17 00:00:00 2001 From: Kai Philipp Date: Tue, 10 Sep 2019 20:47:10 +0200 Subject: [PATCH] lastgames from sessiondata to playerdata --- .gitignore | 2 +- src/net/common/servergame.cpp | 2 + src/net/common/serverlobbythread.cpp | 4 +- src/net/common/sessiondata.cpp | 59 ---------------------------- src/net/sessiondata.h | 7 ---- src/playerdata.cpp | 52 ++++++++++++++++++++++++ src/playerdata.h | 8 ++++ 7 files changed, 65 insertions(+), 69 deletions(-) diff --git a/.gitignore b/.gitignore index 29b09046..6b77edfe 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,4 @@ uics/ make.sh gitpush_serverstuff_stable pokerth_server - +.vscode/ diff --git a/src/net/common/servergame.cpp b/src/net/common/servergame.cpp index 63028e9d..50e31edf 100644 --- a/src/net/common/servergame.cpp +++ b/src/net/common/servergame.cpp @@ -348,6 +348,8 @@ ServerGame::InternalStartGame() //if (GetGameData().gameType == GAME_TYPE_RANKING) if(true) StoreLastGames(); + + // @TODO: StoreWECData() ;) GetDatabase().AsyncCreateGame(GetId(), GetName()); diff --git a/src/net/common/serverlobbythread.cpp b/src/net/common/serverlobbythread.cpp index b68ad25f..06831b95 100644 --- a/src/net/common/serverlobbythread.cpp +++ b/src/net/common/serverlobbythread.cpp @@ -1439,8 +1439,8 @@ ServerLobbyThread::HandleNetPacketJoinGame(boost::shared_ptr sessio // SendJoinGameFailed(session, joinGame.gameid(), NTF_NET_JOIN_IP_BLOCKED); } else { // @FIXME: debug only - final position is gamestart - session->AddPlayerLastGame((long)time(NULL)); - LOG_ERROR("TimeStamp stored: " << *session->GetPlayerLastGames().end()); + session->GetPlayerData()->AddPlayerLastGame((long)time(NULL)); + LOG_ERROR("TimeStamp stored: " << *session->GetPlayerData()->GetPlayerLastGames().end()); MoveSessionToGame(game, session, joinGame.autoleave(), false); } } diff --git a/src/net/common/sessiondata.cpp b/src/net/common/sessiondata.cpp index f809d4c3..549f79ca 100644 --- a/src/net/common/sessiondata.cpp +++ b/src/net/common/sessiondata.cpp @@ -38,8 +38,6 @@ #include #include -#include - using namespace std; using boost::asio::ip::tcp; @@ -49,9 +47,6 @@ using namespace std::chrono; using namespace boost::chrono; #endif -#define SERVER_ALLOWED_RANKING_GAMES_PER_MINUTES 5 -#define SERVER_ALLOWED_RANKING_GAMES_MINUTES 60 - SessionData::SessionData(boost::shared_ptr sock, SessionId id, SessionDataCallback &cb, boost::asio::io_service &ioService) : m_socket(sock), m_id(id), m_state(SessionData::Init), m_readyFlag(false), m_wantsLobbyMsg(true), m_activityTimeoutSec(0), m_activityWarningRemainingSec(0), m_initTimeoutTimer(ioService), m_globalTimeoutTimer(ioService), @@ -403,60 +398,6 @@ SessionData::GetPlayerData() return m_playerData; } -void -SessionData::SetPlayerLastGames(std::vector lastGames) -{ - boost::mutex::scoped_lock lock(m_dataMutex); - m_lastGames = lastGames; -} - -void -SessionData::AddPlayerLastGame(long lastGame) -{ - boost::mutex::scoped_lock lock(m_dataMutex); - - m_lastGames.push_back(lastGame); - - // @TODO: remove overdued entries -} - -std::vector -SessionData::GetPlayerLastGames() -{ - boost::mutex::scoped_lock lock(m_dataMutex); - return m_lastGames; -} - -bool -SessionData::IsPlayerAllowedToJoinLimitRank() -{ - bool retVal = false; - -// #define SERVER_ALLOWED_RANKING_GAMES_PER_MINUTES 5 -// #define SERVER_ALLOWED_RANKING_GAMES_MINUTES 60 - - boost::mutex::scoped_lock lock(m_dataMutex); - - // @TODO: iterate m_lastGames - long then = (long)time(NULL) - (long)(SERVER_ALLOWED_RANKING_GAMES_MINUTES * 10); - - long num = (long)SERVER_ALLOWED_RANKING_GAMES_PER_MINUTES; - - long count = 0; - - for(std::vector::iterator timeStamp = m_lastGames.begin(); timeStamp != m_lastGames.end(); ++timeStamp) { - if(*timeStamp > then) - count++; - else - m_lastGames.erase(timeStamp); - } - - if(count < num) - retVal = true; - - return retVal; -} - string SessionData::GetRemoteIPAddressFromSocket() const { diff --git a/src/net/sessiondata.h b/src/net/sessiondata.h index f2bc96c6..417cba7a 100644 --- a/src/net/sessiondata.h +++ b/src/net/sessiondata.h @@ -127,11 +127,6 @@ public: void SetPlayerData(boost::shared_ptr player); boost::shared_ptr GetPlayerData(); - void AddPlayerLastGame(long lastGames); - void SetPlayerLastGames(std::vector lastGames); - std::vector GetPlayerLastGames(); - bool IsPlayerAllowedToJoinLimitRank(); - std::string GetRemoteIPAddressFromSocket() const; protected: @@ -165,8 +160,6 @@ private: std::string m_password; boost::shared_ptr m_playerData; - std::vector m_lastGames; - mutable boost::mutex m_dataMutex; }; diff --git a/src/playerdata.cpp b/src/playerdata.cpp index 8cd5cedf..db71a196 100644 --- a/src/playerdata.cpp +++ b/src/playerdata.cpp @@ -29,6 +29,10 @@ * as that of the covered work. * *****************************************************************************/ #include +#include + +#define SERVER_ALLOWED_RANKING_GAMES_PER_MINUTES 5 +#define SERVER_ALLOWED_RANKING_GAMES_MINUTES 60 using namespace std; @@ -242,3 +246,51 @@ PlayerData::operator<(const PlayerData &other) const return m_number < other.GetNumber(); } +void +PlayerData::SetPlayerLastGames(std::vector lastGames) +{ + boost::mutex::scoped_lock lock(m_dataMutex); + m_lastGames = lastGames; +} + +void +PlayerData::AddPlayerLastGame(long lastGame) +{ + boost::mutex::scoped_lock lock(m_dataMutex); + + m_lastGames.push_back(lastGame); +} + +std::vector +PlayerData::GetPlayerLastGames() +{ + boost::mutex::scoped_lock lock(m_dataMutex); + return m_lastGames; +} + +bool +PlayerData::IsPlayerAllowedToJoinLimitRank() +{ + bool retVal = false; + +// #define SERVER_ALLOWED_RANKING_GAMES_PER_MINUTES 5 +// #define SERVER_ALLOWED_RANKING_GAMES_MINUTES 60 + + boost::mutex::scoped_lock lock(m_dataMutex); + + long then = (long)time(NULL) - (long)(SERVER_ALLOWED_RANKING_GAMES_MINUTES * 10); + + int count = 0; + + for(std::vector::iterator timeStamp = m_lastGames.begin(); timeStamp != m_lastGames.end(); ++timeStamp) { + if(*timeStamp > then) + count++; + else + m_lastGames.erase(timeStamp); // erase overdued entries + } + + if(count < SERVER_ALLOWED_RANKING_GAMES_PER_MINUTES) + retVal = true; + + return retVal; +} diff --git a/src/playerdata.h b/src/playerdata.h index 6a0ed8d3..62a0c763 100644 --- a/src/playerdata.h +++ b/src/playerdata.h @@ -116,6 +116,12 @@ public: int GetStartCash() const; void SetStartCash(int cash); + // @TODO: lastGames here + void AddPlayerLastGame(long lastGames); + void SetPlayerLastGames(std::vector lastGames); + std::vector GetPlayerLastGames(); + bool IsPlayerAllowedToJoinLimitRank(); + bool operator<(const PlayerData &other) const; private: @@ -135,6 +141,8 @@ private: bool m_isGameAdmin; boost::shared_ptr m_netAvatarFile; + std::vector m_lastGames; + mutable boost::mutex m_dataMutex; };