| 1 | #ifndef GAME_SERVER_SCOREWORKER_H |
| 2 | #define GAME_SERVER_SCOREWORKER_H |
| 3 | |
| 4 | #include <base/str.h> |
| 5 | |
| 6 | #include <engine/map.h> |
| 7 | #include <engine/server/databases/connection_pool.h> |
| 8 | #include <engine/shared/protocol.h> |
| 9 | #include <engine/shared/uuid_manager.h> |
| 10 | |
| 11 | #include <game/server/save.h> |
| 12 | #include <game/voting.h> |
| 13 | |
| 14 | #include <memory> |
| 15 | #include <optional> |
| 16 | #include <string> |
| 17 | #include <utility> |
| 18 | #include <vector> |
| 19 | |
| 20 | class IDbConnection; |
| 21 | class IGameController; |
| 22 | |
| 23 | enum |
| 24 | { |
| 25 | NUM_CHECKPOINTS = MAX_CHECKPOINTS, |
| 26 | TIMESTAMP_STR_LENGTH = 20, // 2019-04-02 19:38:36 |
| 27 | // Every team member name at maximum length, joined by ", " and " & ". |
| 28 | MAX_TEAM_NAMES_LENGTH = MAX_CLIENTS * (MAX_NAME_LENGTH + 2), |
| 29 | }; |
| 30 | |
| 31 | struct CScorePlayerResult : ISqlResult |
| 32 | { |
| 33 | CScorePlayerResult(); |
| 34 | |
| 35 | enum |
| 36 | { |
| 37 | MAX_MESSAGES = 10, |
| 38 | }; |
| 39 | |
| 40 | enum Variant |
| 41 | { |
| 42 | DIRECT, |
| 43 | ALL, |
| 44 | BROADCAST, |
| 45 | MAP_VOTE, |
| 46 | PLAYER_INFO, |
| 47 | PLAYER_TIMECP, |
| 48 | } m_MessageKind; |
| 49 | union |
| 50 | { |
| 51 | char m_aaMessages[MAX_MESSAGES][512]; |
| 52 | char m_aBroadcast[1024]; |
| 53 | struct |
| 54 | { |
| 55 | std::optional<float> m_Time; |
| 56 | float m_aTimeCp[NUM_CHECKPOINTS]; |
| 57 | int m_Birthday; // 0 indicates no birthday |
| 58 | char m_aRequestedPlayer[MAX_NAME_LENGTH]; |
| 59 | } m_Info = {}; |
| 60 | struct |
| 61 | { |
| 62 | char m_aReason[VOTE_REASON_LENGTH]; |
| 63 | char m_aServer[32 + 1]; |
| 64 | char m_aMap[MAX_MAP_LENGTH + 1]; |
| 65 | } m_MapVote; |
| 66 | } m_Data = {}; // PLAYER_INFO |
| 67 | |
| 68 | void SetVariant(Variant v); |
| 69 | }; |
| 70 | |
| 71 | struct CScoreLoadBestTimeResult : ISqlResult |
| 72 | { |
| 73 | std::optional<float> m_CurrentRecord = std::nullopt; |
| 74 | }; |
| 75 | |
| 76 | struct CSqlLoadBestTimeRequest : ISqlData |
| 77 | { |
| 78 | CSqlLoadBestTimeRequest(std::shared_ptr<CScoreLoadBestTimeResult> pResult) : |
| 79 | ISqlData(std::move(pResult)) |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | // current map |
| 84 | char m_aMap[MAX_MAP_LENGTH]; |
| 85 | }; |
| 86 | |
| 87 | struct CSqlPlayerRequest : ISqlData |
| 88 | { |
| 89 | CSqlPlayerRequest(std::shared_ptr<CScorePlayerResult> pResult) : |
| 90 | ISqlData(std::move(pResult)) |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | // object being requested, either map (128 bytes) or player (16 bytes) |
| 95 | char m_aName[MAX_MAP_LENGTH]; |
| 96 | // current map |
| 97 | char m_aMap[MAX_MAP_LENGTH]; |
| 98 | char m_aRequestingPlayer[MAX_NAME_LENGTH]; |
| 99 | // relevant for /top5 kind of requests |
| 100 | int m_Offset; |
| 101 | char m_aServer[5]; |
| 102 | }; |
| 103 | |
| 104 | struct CScoreRandomMapResult : ISqlResult |
| 105 | { |
| 106 | CScoreRandomMapResult(int ClientId) : |
| 107 | m_ClientId(ClientId) |
| 108 | { |
| 109 | m_aMap[0] = '\0'; |
| 110 | m_aMessage[0] = '\0'; |
| 111 | } |
| 112 | int m_ClientId; |
| 113 | char m_aMap[MAX_MAP_LENGTH]; |
| 114 | char m_aMessage[512]; |
| 115 | }; |
| 116 | |
| 117 | struct CSqlRandomMapRequest : ISqlData |
| 118 | { |
| 119 | CSqlRandomMapRequest(std::shared_ptr<CScoreRandomMapResult> pResult) : |
| 120 | ISqlData(std::move(pResult)) |
| 121 | { |
| 122 | } |
| 123 | |
| 124 | char m_aServerType[32]; |
| 125 | char m_aCurrentMap[MAX_MAP_LENGTH]; |
| 126 | char m_aRequestingPlayer[MAX_NAME_LENGTH]; |
| 127 | int m_MinStars; |
| 128 | int m_MaxStars; |
| 129 | }; |
| 130 | |
| 131 | struct CSqlScoreData : ISqlData |
| 132 | { |
| 133 | CSqlScoreData(std::shared_ptr<CScorePlayerResult> pResult) : |
| 134 | ISqlData(std::move(pResult)) |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | char m_aMap[MAX_MAP_LENGTH]; |
| 139 | char m_aGameUuid[UUID_MAXSTRSIZE]; |
| 140 | char m_aName[MAX_MAP_LENGTH]; |
| 141 | |
| 142 | int m_ClientId; |
| 143 | float m_Time; |
| 144 | char m_aTimestamp[TIMESTAMP_STR_LENGTH]; |
| 145 | float m_aCurrentTimeCp[NUM_CHECKPOINTS]; |
| 146 | int m_Num; |
| 147 | bool m_Search; |
| 148 | char m_aRequestingPlayer[MAX_NAME_LENGTH]; |
| 149 | }; |
| 150 | |
| 151 | struct CScoreSaveResult : ISqlResult |
| 152 | { |
| 153 | CScoreSaveResult(int PlayerId, const char *pPlayerName, const char *pServer) : |
| 154 | m_Status(SAVE_FAILED), |
| 155 | m_RequestingPlayer(PlayerId) |
| 156 | { |
| 157 | m_aMessage[0] = '\0'; |
| 158 | m_aBroadcast[0] = '\0'; |
| 159 | m_aCode[0] = '\0'; |
| 160 | m_aGeneratedCode[0] = '\0'; |
| 161 | str_copy(dst&: m_aRequestingPlayer, src: pPlayerName); |
| 162 | str_copy(dst&: m_aServer, src: pServer); |
| 163 | } |
| 164 | enum |
| 165 | { |
| 166 | SAVE_SUCCESS, |
| 167 | SAVE_WARNING, |
| 168 | SAVE_FALLBACKFILE, |
| 169 | // load team in the following two cases |
| 170 | SAVE_FAILED, |
| 171 | LOAD_SUCCESS, |
| 172 | LOAD_FAILED, |
| 173 | } m_Status; |
| 174 | char m_aMessage[512]; |
| 175 | char m_aBroadcast[512]; |
| 176 | CSaveTeam m_SavedTeam; |
| 177 | int m_RequestingPlayer; |
| 178 | char m_aRequestingPlayer[MAX_NAME_LENGTH]; |
| 179 | CUuid m_SaveId; |
| 180 | char m_aServer[5]; |
| 181 | char m_aCode[128]; |
| 182 | char m_aGeneratedCode[128]; |
| 183 | }; |
| 184 | |
| 185 | struct CSqlTeamScoreData : ISqlData |
| 186 | { |
| 187 | CSqlTeamScoreData() : |
| 188 | ISqlData(nullptr) |
| 189 | { |
| 190 | } |
| 191 | |
| 192 | char m_aGameUuid[UUID_MAXSTRSIZE]; |
| 193 | char m_aMap[MAX_MAP_LENGTH]; |
| 194 | float m_Time; |
| 195 | char m_aTimestamp[TIMESTAMP_STR_LENGTH]; |
| 196 | unsigned int m_Size; |
| 197 | char m_aaNames[MAX_CLIENTS][MAX_NAME_LENGTH]; |
| 198 | CUuid m_TeamrankUuid; |
| 199 | }; |
| 200 | |
| 201 | struct CSqlTeamSaveData : ISqlData |
| 202 | { |
| 203 | CSqlTeamSaveData(std::shared_ptr<CScoreSaveResult> pResult) : |
| 204 | ISqlData(std::move(pResult)) |
| 205 | { |
| 206 | } |
| 207 | |
| 208 | char m_aClientName[MAX_NAME_LENGTH]; |
| 209 | char m_aMap[MAX_MAP_LENGTH]; |
| 210 | char m_aCode[128]; |
| 211 | char m_aGeneratedCode[128]; |
| 212 | char m_aServer[5]; |
| 213 | }; |
| 214 | |
| 215 | struct CSqlTeamLoadRequest : ISqlData |
| 216 | { |
| 217 | CSqlTeamLoadRequest(std::shared_ptr<CScoreSaveResult> pResult) : |
| 218 | ISqlData(std::move(pResult)) |
| 219 | { |
| 220 | } |
| 221 | |
| 222 | char m_aCode[128]; |
| 223 | char m_aMap[MAX_MAP_LENGTH]; |
| 224 | char m_aRequestingPlayer[MAX_NAME_LENGTH]; |
| 225 | // struct holding all player names in the team or an empty string |
| 226 | char m_aClientNames[MAX_CLIENTS][MAX_NAME_LENGTH]; |
| 227 | int m_aClientId[MAX_CLIENTS]; |
| 228 | int m_NumPlayer; |
| 229 | }; |
| 230 | |
| 231 | class CPlayerData |
| 232 | { |
| 233 | public: |
| 234 | CPlayerData() |
| 235 | { |
| 236 | Reset(); |
| 237 | } |
| 238 | |
| 239 | void Reset() |
| 240 | { |
| 241 | m_BestTime.reset(); |
| 242 | std::fill(first: std::begin(arr&: m_aBestTimeCp), last: std::end(arr&: m_aBestTimeCp), value: 0.0f); |
| 243 | m_RecordStopTick = -1; |
| 244 | } |
| 245 | |
| 246 | void Set(float Time, const float aTimeCp[NUM_CHECKPOINTS]) |
| 247 | { |
| 248 | m_BestTime = Time; |
| 249 | for(int i = 0; i < NUM_CHECKPOINTS; i++) |
| 250 | m_aBestTimeCp[i] = aTimeCp[i]; |
| 251 | } |
| 252 | |
| 253 | void SetBestTimeCp(const float aTimeCp[NUM_CHECKPOINTS]) |
| 254 | { |
| 255 | for(int i = 0; i < NUM_CHECKPOINTS; i++) |
| 256 | m_aBestTimeCp[i] = aTimeCp[i]; |
| 257 | } |
| 258 | |
| 259 | std::optional<float> m_BestTime; |
| 260 | float m_aBestTimeCp[NUM_CHECKPOINTS]; |
| 261 | |
| 262 | int m_RecordStopTick; |
| 263 | float m_RecordFinishTime; |
| 264 | }; |
| 265 | |
| 266 | struct CTeamrank |
| 267 | { |
| 268 | CUuid m_TeamId; |
| 269 | char m_aaNames[MAX_CLIENTS][MAX_NAME_LENGTH]; |
| 270 | unsigned int m_NumNames; |
| 271 | CTeamrank(); |
| 272 | |
| 273 | // Assumes that a database query equivalent to |
| 274 | // |
| 275 | // SELECT TeamId, Name [, ...] -- the order is important |
| 276 | // FROM record_teamrace |
| 277 | // ORDER BY TeamId, Name |
| 278 | // |
| 279 | // was executed and that the result line of the first team member is already selected. |
| 280 | // Afterwards the team member of the next team is selected. |
| 281 | // |
| 282 | // Returns true on SQL failure |
| 283 | // |
| 284 | // if another team can be extracted |
| 285 | bool NextSqlResult(IDbConnection *pSqlServer, bool *pEnd, char *pError, int ErrorSize); |
| 286 | |
| 287 | bool SamePlayers(const std::vector<std::string> *pvSortedNames); |
| 288 | |
| 289 | // Formats the names into pBuf, joined by ", " and " & ". Names that don't |
| 290 | // fit are summarized as " & %d more". |
| 291 | void FormatNames(char *pBuf, int BufSize) const; |
| 292 | |
| 293 | void FormatTeamTopLine(char *pMessage, int MessageSize, int Rank, const char *pTime) const; |
| 294 | |
| 295 | static bool GetSqlTop5Team(IDbConnection *pSqlServer, bool *pEnd, char *pError, int ErrorSize, char (*paMessages)[512], int *StartLine, int Count); |
| 296 | }; |
| 297 | |
| 298 | struct CScoreWorker |
| 299 | { |
| 300 | static bool LoadBestTime(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize); |
| 301 | |
| 302 | static bool RandomMap(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize); |
| 303 | static bool RandomUnfinishedMap(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize); |
| 304 | static bool MapVote(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize); |
| 305 | |
| 306 | static bool LoadPlayerData(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize); |
| 307 | static bool LoadPlayerTimeCp(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize); |
| 308 | static bool MapInfo(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize); |
| 309 | static bool ShowRank(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize); |
| 310 | static bool ShowTeamRank(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize); |
| 311 | static bool ShowTop(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize); |
| 312 | static bool ShowTeamTop5(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize); |
| 313 | static bool ShowPlayerTeamTop5(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize); |
| 314 | static bool ShowTimes(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize); |
| 315 | static bool ShowPoints(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize); |
| 316 | static bool ShowTopPoints(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize); |
| 317 | static bool GetSaves(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize); |
| 318 | |
| 319 | static bool SaveTeam(IDbConnection *pSqlServer, const ISqlData *pGameData, Write w, char *pError, int ErrorSize); |
| 320 | static bool LoadTeam(IDbConnection *pSqlServer, const ISqlData *pGameData, Write w, char *pError, int ErrorSize); |
| 321 | |
| 322 | static bool SaveScore(IDbConnection *pSqlServer, const ISqlData *pGameData, Write w, char *pError, int ErrorSize); |
| 323 | static bool SaveTeamScore(IDbConnection *pSqlServer, const ISqlData *pGameData, Write w, char *pError, int ErrorSize); |
| 324 | }; |
| 325 | |
| 326 | #endif // GAME_SERVER_SCOREWORKER_H |
| 327 | |