2018-07-06 14:11:38 +00:00
|
|
|
#ifndef GAME_SERVER_SCORE_H
|
|
|
|
#define GAME_SERVER_SCORE_H
|
2010-07-29 05:21:18 +00:00
|
|
|
|
2019-05-19 22:12:59 +00:00
|
|
|
#include <memory>
|
2020-05-26 21:22:16 +00:00
|
|
|
#include <atomic>
|
2019-05-19 22:12:59 +00:00
|
|
|
|
2020-06-24 21:14:09 +00:00
|
|
|
#include <engine/map.h>
|
2020-07-04 17:53:27 +00:00
|
|
|
#include <engine/server/databases/connection_pool.h>
|
|
|
|
#include <game/voting.h>
|
|
|
|
#include <game/prng.h>
|
|
|
|
|
2020-05-26 21:22:16 +00:00
|
|
|
#include "save.h"
|
2010-08-20 17:45:09 +00:00
|
|
|
|
2020-07-04 17:53:27 +00:00
|
|
|
struct ISqlData;
|
|
|
|
class IDbConnection;
|
|
|
|
class IServer;
|
|
|
|
class CGameContext;
|
|
|
|
|
2019-04-02 17:53:37 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
NUM_CHECKPOINTS = 25,
|
|
|
|
TIMESTAMP_STR_LENGTH = 20, // 2019-04-02 19:38:36
|
|
|
|
};
|
2010-07-29 05:21:18 +00:00
|
|
|
|
2020-06-24 21:14:09 +00:00
|
|
|
struct CScorePlayerResult
|
|
|
|
{
|
|
|
|
std::atomic_bool m_Done;
|
|
|
|
CScorePlayerResult();
|
|
|
|
|
|
|
|
enum {
|
|
|
|
MAX_MESSAGES = 7,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Variant
|
|
|
|
{
|
|
|
|
DIRECT,
|
|
|
|
ALL,
|
|
|
|
BROADCAST,
|
|
|
|
MAP_VOTE,
|
|
|
|
PLAYER_INFO,
|
|
|
|
} m_MessageKind;
|
|
|
|
union {
|
|
|
|
char m_aaMessages[MAX_MESSAGES][512];
|
|
|
|
char m_Broadcast[1024];
|
|
|
|
struct {
|
|
|
|
float m_Time;
|
|
|
|
float m_CpTime[NUM_CHECKPOINTS];
|
|
|
|
int m_Score;
|
|
|
|
int m_HasFinishScore;
|
|
|
|
int m_Birthday; // 0 indicates no birthday
|
|
|
|
} m_Info;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
char m_Reason[VOTE_REASON_LENGTH];
|
|
|
|
char m_Server[32+1];
|
|
|
|
char m_Map[MAX_MAP_LENGTH+1];
|
|
|
|
} m_MapVote;
|
|
|
|
} m_Data; // PLAYER_INFO
|
|
|
|
|
|
|
|
void SetVariant(Variant v);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CScoreRandomMapResult
|
|
|
|
{
|
|
|
|
std::atomic_bool m_Done;
|
|
|
|
CScoreRandomMapResult(int ClientID) :
|
|
|
|
m_Done(false),
|
|
|
|
m_ClientID(ClientID)
|
|
|
|
{
|
|
|
|
m_Map[0] = '\0';
|
|
|
|
m_aMessage[0] = '\0';
|
|
|
|
}
|
|
|
|
int m_ClientID;
|
|
|
|
char m_Map[MAX_MAP_LENGTH];
|
|
|
|
char m_aMessage[512];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CScoreSaveResult
|
|
|
|
{
|
|
|
|
CScoreSaveResult(int PlayerID, IGameController* Controller) :
|
|
|
|
m_Status(SAVE_FAILED),
|
|
|
|
m_SavedTeam(CSaveTeam(Controller)),
|
|
|
|
m_RequestingPlayer(PlayerID)
|
|
|
|
{
|
|
|
|
m_aMessage[0] = '\0';
|
|
|
|
m_aBroadcast[0] = '\0';
|
|
|
|
}
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
SAVE_SUCCESS,
|
|
|
|
// load team in the following two cases
|
|
|
|
SAVE_FAILED,
|
|
|
|
LOAD_SUCCESS,
|
|
|
|
LOAD_FAILED,
|
|
|
|
} m_Status;
|
|
|
|
char m_aMessage[512];
|
|
|
|
char m_aBroadcast[512];
|
|
|
|
CSaveTeam m_SavedTeam;
|
|
|
|
int m_RequestingPlayer;
|
|
|
|
CUuid m_SaveID;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CScoreInitResult
|
|
|
|
{
|
|
|
|
CScoreInitResult() :
|
|
|
|
m_Done(false),
|
|
|
|
m_CurrentRecord(0)
|
|
|
|
{ }
|
|
|
|
std::atomic_bool m_Done;
|
|
|
|
float m_CurrentRecord;
|
|
|
|
};
|
|
|
|
|
2010-08-23 19:37:27 +00:00
|
|
|
class CPlayerData
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
|
|
|
public:
|
2010-08-23 19:37:27 +00:00
|
|
|
CPlayerData()
|
|
|
|
{
|
|
|
|
Reset();
|
|
|
|
}
|
2020-06-04 15:33:10 +00:00
|
|
|
~CPlayerData() {}
|
2014-10-10 23:01:32 +00:00
|
|
|
|
2010-08-23 19:37:27 +00:00
|
|
|
void Reset()
|
|
|
|
{
|
|
|
|
m_BestTime = 0;
|
|
|
|
m_CurrentTime = 0;
|
2010-11-04 18:10:51 +00:00
|
|
|
for(int i = 0; i < NUM_CHECKPOINTS; i++)
|
2010-08-23 19:37:27 +00:00
|
|
|
m_aBestCpTime[i] = 0;
|
|
|
|
}
|
2014-10-10 23:01:32 +00:00
|
|
|
|
2010-11-04 18:10:51 +00:00
|
|
|
void Set(float Time, float CpTime[NUM_CHECKPOINTS])
|
2010-08-23 19:37:27 +00:00
|
|
|
{
|
|
|
|
m_BestTime = Time;
|
2020-06-04 15:33:10 +00:00
|
|
|
m_CurrentTime = Time;
|
2010-11-04 18:10:51 +00:00
|
|
|
for(int i = 0; i < NUM_CHECKPOINTS; i++)
|
2010-08-23 19:37:27 +00:00
|
|
|
m_aBestCpTime[i] = CpTime[i];
|
|
|
|
}
|
2014-10-10 23:01:32 +00:00
|
|
|
|
2010-08-23 19:37:27 +00:00
|
|
|
float m_BestTime;
|
|
|
|
float m_CurrentTime;
|
2010-11-04 18:10:51 +00:00
|
|
|
float m_aBestCpTime[NUM_CHECKPOINTS];
|
2010-07-29 05:21:18 +00:00
|
|
|
};
|
|
|
|
|
2020-07-04 17:53:27 +00:00
|
|
|
struct CSqlInitData : ISqlData
|
|
|
|
{
|
|
|
|
CSqlInitData(std::shared_ptr<CScoreInitResult> pResult) :
|
|
|
|
m_pResult(pResult)
|
|
|
|
{}
|
|
|
|
std::shared_ptr<CScoreInitResult> m_pResult;
|
|
|
|
|
|
|
|
// current map
|
|
|
|
char m_Map[MAX_MAP_LENGTH];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CSqlPlayerRequest : ISqlData
|
|
|
|
{
|
|
|
|
CSqlPlayerRequest(std::shared_ptr<CScorePlayerResult> pResult) :
|
|
|
|
m_pResult(pResult)
|
|
|
|
{}
|
|
|
|
std::shared_ptr<CScorePlayerResult> m_pResult;
|
|
|
|
// object being requested, either map (128 bytes) or player (16 bytes)
|
|
|
|
char m_Name[MAX_MAP_LENGTH];
|
|
|
|
// current map
|
|
|
|
char m_Map[MAX_MAP_LENGTH];
|
|
|
|
char m_RequestingPlayer[MAX_NAME_LENGTH];
|
|
|
|
// relevant for /top5 kind of requests
|
|
|
|
int m_Offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CSqlRandomMapRequest : ISqlData
|
|
|
|
{
|
|
|
|
CSqlRandomMapRequest(std::shared_ptr<CScoreRandomMapResult> pResult) :
|
|
|
|
m_pResult(pResult)
|
|
|
|
{}
|
|
|
|
std::shared_ptr<CScoreRandomMapResult> m_pResult;
|
|
|
|
|
|
|
|
char m_ServerType[32];
|
|
|
|
char m_CurrentMap[MAX_MAP_LENGTH];
|
|
|
|
char m_RequestingPlayer[MAX_NAME_LENGTH];
|
|
|
|
int m_Stars;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CSqlScoreData : ISqlData
|
|
|
|
{
|
|
|
|
CSqlScoreData(std::shared_ptr<CScorePlayerResult> pResult) :
|
|
|
|
m_pResult(pResult)
|
|
|
|
{}
|
|
|
|
virtual ~CSqlScoreData() {};
|
|
|
|
|
|
|
|
std::shared_ptr<CScorePlayerResult> m_pResult;
|
|
|
|
|
|
|
|
char m_Map[MAX_MAP_LENGTH];
|
|
|
|
char m_GameUuid[UUID_MAXSTRSIZE];
|
|
|
|
char m_Name[MAX_MAP_LENGTH];
|
|
|
|
|
|
|
|
int m_ClientID;
|
|
|
|
float m_Time;
|
|
|
|
char m_aTimestamp[TIMESTAMP_STR_LENGTH];
|
|
|
|
float m_aCpCurrent[NUM_CHECKPOINTS];
|
|
|
|
int m_Num;
|
|
|
|
bool m_Search;
|
|
|
|
char m_aRequestingPlayer[MAX_NAME_LENGTH];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CSqlTeamScoreData : ISqlData
|
|
|
|
{
|
|
|
|
char m_GameUuid[UUID_MAXSTRSIZE];
|
|
|
|
char m_Map[MAX_MAP_LENGTH];
|
|
|
|
float m_Time;
|
|
|
|
char m_aTimestamp[TIMESTAMP_STR_LENGTH];
|
|
|
|
unsigned int m_Size;
|
|
|
|
char m_aNames[MAX_CLIENTS][MAX_NAME_LENGTH];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CSqlTeamSave : ISqlData
|
|
|
|
{
|
|
|
|
CSqlTeamSave(std::shared_ptr<CScoreSaveResult> pResult) :
|
|
|
|
m_pResult(pResult)
|
|
|
|
{}
|
|
|
|
virtual ~CSqlTeamSave() {};
|
|
|
|
|
|
|
|
std::shared_ptr<CScoreSaveResult> m_pResult;
|
|
|
|
|
|
|
|
char m_ClientName[MAX_NAME_LENGTH];
|
|
|
|
char m_Map[MAX_MAP_LENGTH];
|
|
|
|
char m_Code[128];
|
|
|
|
char m_aGeneratedCode[128];
|
|
|
|
char m_Server[5];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CSqlTeamLoad : ISqlData
|
|
|
|
{
|
|
|
|
CSqlTeamLoad(std::shared_ptr<CScoreSaveResult> pResult) :
|
|
|
|
m_pResult(pResult)
|
|
|
|
{}
|
|
|
|
virtual ~CSqlTeamLoad() {};
|
|
|
|
|
|
|
|
std::shared_ptr<CScoreSaveResult> m_pResult;
|
|
|
|
|
|
|
|
char m_Code[128];
|
|
|
|
char m_Map[MAX_MAP_LENGTH];
|
|
|
|
char m_RequestingPlayer[MAX_NAME_LENGTH];
|
|
|
|
int m_ClientID;
|
|
|
|
// struct holding all player names in the team or an empty string
|
|
|
|
char m_aClientNames[MAX_CLIENTS][MAX_NAME_LENGTH];
|
|
|
|
int m_aClientID[MAX_CLIENTS];
|
|
|
|
int m_NumPlayer;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CScore
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2010-08-23 19:37:27 +00:00
|
|
|
CPlayerData m_aPlayerData[MAX_CLIENTS];
|
2020-07-04 17:53:27 +00:00
|
|
|
CDbConnectionPool *m_pPool;
|
|
|
|
|
|
|
|
static bool Init(IDbConnection *pSqlServer, const ISqlData *pGameData);
|
|
|
|
|
|
|
|
static bool RandomMapThread(IDbConnection *pSqlServer, const ISqlData *pGameData);
|
|
|
|
static bool RandomUnfinishedMapThread(IDbConnection *pSqlServer, const ISqlData *pGameData);
|
|
|
|
static bool MapVoteThread(IDbConnection *pSqlServer, const ISqlData *pGameData);
|
|
|
|
|
|
|
|
static bool LoadPlayerDataThread(IDbConnection *pSqlServer, const ISqlData *pGameData);
|
|
|
|
static bool MapInfoThread(IDbConnection *pSqlServer, const ISqlData *pGameData);
|
|
|
|
static bool ShowRankThread(IDbConnection *pSqlServer, const ISqlData *pGameData);
|
|
|
|
static bool ShowTeamRankThread(IDbConnection *pSqlServer, const ISqlData *pGameData);
|
|
|
|
static bool ShowTop5Thread(IDbConnection *pSqlServer, const ISqlData *pGameData);
|
|
|
|
static bool ShowTeamTop5Thread(IDbConnection *pSqlServer, const ISqlData *pGameData);
|
|
|
|
static bool ShowTimesThread(IDbConnection *pSqlServer, const ISqlData *pGameData);
|
|
|
|
static bool ShowPointsThread(IDbConnection *pSqlServer, const ISqlData *pGameData);
|
|
|
|
static bool ShowTopPointsThread(IDbConnection *pSqlServer, const ISqlData *pGameData);
|
|
|
|
static bool GetSavesThread(IDbConnection *pSqlServer, const ISqlData *pGameData);
|
|
|
|
|
|
|
|
static bool SaveTeamThread(IDbConnection *pSqlServer, const ISqlData *pGameData, bool Failure);
|
|
|
|
static bool LoadTeamThread(IDbConnection *pSqlServer, const ISqlData *pGameData);
|
|
|
|
|
|
|
|
static bool SaveScoreThread(IDbConnection *pSqlServer, const ISqlData *pGameData, bool Failure);
|
|
|
|
static bool SaveTeamScoreThread(IDbConnection *pSqlServer, const ISqlData *pGameData, bool Failure);
|
|
|
|
|
|
|
|
CGameContext *GameServer() const { return m_pGameServer; }
|
|
|
|
IServer *Server() const { return m_pServer; }
|
|
|
|
CGameContext *m_pGameServer;
|
|
|
|
IServer *m_pServer;
|
|
|
|
|
|
|
|
std::vector<std::string> m_aWordlist;
|
|
|
|
CPrng m_Prng;
|
|
|
|
void GeneratePassphrase(char *pBuf, int BufSize);
|
|
|
|
|
|
|
|
// returns new SqlResult bound to the player, if no current Thread is active for this player
|
|
|
|
std::shared_ptr<CScorePlayerResult> NewSqlPlayerResult(int ClientID);
|
|
|
|
// Creates for player database requests
|
|
|
|
void ExecPlayerThread(
|
|
|
|
bool (*pFuncPtr) (IDbConnection *, const ISqlData *),
|
|
|
|
const char *pThreadName,
|
|
|
|
int ClientID,
|
|
|
|
const char *pName,
|
|
|
|
int Offset);
|
|
|
|
|
|
|
|
// returns true if the player should be rate limited
|
|
|
|
bool RateLimitPlayer(int ClientID);
|
2014-10-10 23:01:32 +00:00
|
|
|
|
2010-07-29 05:21:18 +00:00
|
|
|
public:
|
2020-07-04 17:53:27 +00:00
|
|
|
CScore(CGameContext *pGameServer, CDbConnectionPool *pPool);
|
|
|
|
~CScore() {}
|
2014-10-10 23:01:32 +00:00
|
|
|
|
2010-08-23 19:37:27 +00:00
|
|
|
CPlayerData *PlayerData(int ID) { return &m_aPlayerData[ID]; }
|
2014-10-10 23:01:32 +00:00
|
|
|
|
2020-07-04 17:53:27 +00:00
|
|
|
void MapInfo(int ClientID, const char *pMapName);
|
|
|
|
void MapVote(int ClientID, const char *pMapName);
|
|
|
|
void LoadPlayerData(int ClientID);
|
|
|
|
void SaveScore(int ClientID, float Time, const char *pTimestamp, float aCpTime[NUM_CHECKPOINTS], bool NotEligible);
|
2014-10-10 23:01:32 +00:00
|
|
|
|
2020-07-04 17:53:27 +00:00
|
|
|
void SaveTeamScore(int *pClientIDs, unsigned int Size, float Time, const char *pTimestamp);
|
2013-07-21 02:52:23 +00:00
|
|
|
|
2020-07-04 17:53:27 +00:00
|
|
|
void ShowTop5(int ClientID, int Offset=1);
|
|
|
|
void ShowRank(int ClientID, const char *pName);
|
2013-07-21 06:46:52 +00:00
|
|
|
|
2020-07-04 17:53:27 +00:00
|
|
|
void ShowTeamTop5(int ClientID, int Offset=1);
|
|
|
|
void ShowTeamRank(int ClientID, const char *pName);
|
2014-06-20 20:40:23 +00:00
|
|
|
|
2020-07-04 17:53:27 +00:00
|
|
|
void ShowTopPoints(int ClientID, int Offset=1);
|
|
|
|
void ShowPoints(int ClientID, const char *pName);
|
2020-06-24 21:14:09 +00:00
|
|
|
|
2020-07-04 17:53:27 +00:00
|
|
|
void ShowTimes(int ClientID, const char *pName, int Offset = 1);
|
|
|
|
void ShowTimes(int ClientID, int Offset = 1);
|
2014-10-10 23:01:32 +00:00
|
|
|
|
2020-07-04 17:53:27 +00:00
|
|
|
void RandomMap(int ClientID, int Stars);
|
|
|
|
void RandomUnfinishedMap(int ClientID, int Stars);
|
2016-05-04 13:32:24 +00:00
|
|
|
|
2020-07-04 17:53:27 +00:00
|
|
|
void SaveTeam(int ClientID, const char *pCode, const char *pServer);
|
|
|
|
void LoadTeam(const char *pCode, int ClientID);
|
|
|
|
void GetSaves(int ClientID);
|
2010-07-29 05:21:18 +00:00
|
|
|
};
|
|
|
|
|
2018-07-06 14:11:38 +00:00
|
|
|
#endif // GAME_SERVER_SCORE_H
|