Skip to content

Commit ece1ec0

Browse files
committed
Record Player and Team finishes in Teehistorian
1 parent 783f4e3 commit ece1ec0

File tree

11 files changed

+109
-18
lines changed

11 files changed

+109
-18
lines changed

src/engine/server.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ class IGameServer : public IInterface
357357
virtual void TeehistorianRecordPlayerDrop(int ClientId, const char *pReason) = 0;
358358
virtual void TeehistorianRecordPlayerRejoin(int ClientId) = 0;
359359
virtual void TeehistorianRecordPlayerName(int ClientId, const char *pName) = 0;
360+
virtual void TeehistorianRecordPlayerFinish(int ClientId, int TimeTicks) = 0;
361+
virtual void TeehistorianRecordTeamFinish(int TeamId, int TimeTicks) = 0;
360362

361363
virtual void FillAntibot(CAntibotRoundData *pData) = 0;
362364

src/engine/shared/teehistorian_ex_chunks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ UUID(TEEHISTORIAN_PLAYER_READY, "[email protected]")
1919
UUID(TEEHISTORIAN_PLAYER_REJOIN, "[email protected]")
2020
UUID(TEEHISTORIAN_ANTIBOT, "[email protected]")
2121
UUID(TEEHISTORIAN_PLAYER_NAME, "[email protected]")
22+
UUID(TEEHISTORIAN_PLAYER_FINISH, "[email protected]")
23+
UUID(TEEHISTORIAN_TEAM_FINISH, "[email protected]")

src/game/server/gamecontext.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,22 @@ void CGameContext::TeehistorianRecordPlayerName(int ClientId, const char *pName)
17751775
}
17761776
}
17771777

1778+
void CGameContext::TeehistorianRecordPlayerFinish(int ClientId, int TimeTicks)
1779+
{
1780+
if(m_TeeHistorianActive)
1781+
{
1782+
m_TeeHistorian.RecordPlayerFinish(ClientId, TimeTicks);
1783+
}
1784+
}
1785+
1786+
void CGameContext::TeehistorianRecordTeamFinish(int TeamId, int TimeTicks)
1787+
{
1788+
if(m_TeeHistorianActive)
1789+
{
1790+
m_TeeHistorian.RecordTeamFinish(TeamId, TimeTicks);
1791+
}
1792+
}
1793+
17781794
bool CGameContext::OnClientDDNetVersionKnown(int ClientId)
17791795
{
17801796
IServer::CClientInfo Info;

src/game/server/gamecontext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ class CGameContext : public IGameServer
329329
void TeehistorianRecordPlayerDrop(int ClientId, const char *pReason) override;
330330
void TeehistorianRecordPlayerRejoin(int ClientId) override;
331331
void TeehistorianRecordPlayerName(int ClientId, const char *pName) override;
332+
void TeehistorianRecordPlayerFinish(int ClientId, int TimeTicks) override;
333+
void TeehistorianRecordTeamFinish(int TeamId, int TimeTicks) override;
332334

333335
bool IsClientReady(int ClientId) const override;
334336
bool IsClientPlayer(int ClientId) const override;

src/game/server/score.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,14 @@ void CScore::MapInfo(int ClientId, const char *pMapName)
145145
ExecPlayerThread(CScoreWorker::MapInfo, "map info", ClientId, pMapName, 0);
146146
}
147147

148-
void CScore::SaveScore(int ClientId, float Time, const char *pTimestamp, const float aTimeCp[NUM_CHECKPOINTS], bool NotEligible)
148+
void CScore::SaveScore(int ClientId, int TimeTicks, const char *pTimestamp, const float aTimeCp[NUM_CHECKPOINTS], bool NotEligible)
149149
{
150150
CConsole *pCon = (CConsole *)GameServer()->Console();
151151
if(pCon->Cheated() || NotEligible)
152152
return;
153153

154+
GameServer()->TeehistorianRecordPlayerFinish(ClientId, TimeTicks);
155+
154156
CPlayer *pCurPlayer = GameServer()->m_apPlayers[ClientId];
155157
if(pCurPlayer->m_ScoreFinishResult != nullptr)
156158
dbg_msg("sql", "WARNING: previous save score result didn't complete, overwriting it now");
@@ -160,15 +162,15 @@ void CScore::SaveScore(int ClientId, float Time, const char *pTimestamp, const f
160162
FormatUuid(GameServer()->GameUuid(), Tmp->m_aGameUuid, sizeof(Tmp->m_aGameUuid));
161163
Tmp->m_ClientId = ClientId;
162164
str_copy(Tmp->m_aName, Server()->ClientName(ClientId), sizeof(Tmp->m_aName));
163-
Tmp->m_Time = Time;
165+
Tmp->m_Time = (float)(TimeTicks) / (float)Server()->TickSpeed();
164166
str_copy(Tmp->m_aTimestamp, pTimestamp, sizeof(Tmp->m_aTimestamp));
165167
for(int i = 0; i < NUM_CHECKPOINTS; i++)
166168
Tmp->m_aCurrentTimeCp[i] = aTimeCp[i];
167169

168170
m_pPool->ExecuteWrite(CScoreWorker::SaveScore, std::move(Tmp), "save score");
169171
}
170172

171-
void CScore::SaveTeamScore(int *pClientIds, unsigned int Size, float Time, const char *pTimestamp)
173+
void CScore::SaveTeamScore(int Team, int *pClientIds, unsigned int Size, int TimeTicks, const char *pTimestamp)
172174
{
173175
CConsole *pCon = (CConsole *)GameServer()->Console();
174176
if(pCon->Cheated())
@@ -178,11 +180,14 @@ void CScore::SaveTeamScore(int *pClientIds, unsigned int Size, float Time, const
178180
if(GameServer()->m_apPlayers[pClientIds[i]]->m_NotEligibleForFinish)
179181
return;
180182
}
183+
184+
GameServer()->TeehistorianRecordTeamFinish(Team, TimeTicks);
185+
181186
auto Tmp = std::make_unique<CSqlTeamScoreData>();
182187
for(unsigned int i = 0; i < Size; i++)
183188
str_copy(Tmp->m_aaNames[i], Server()->ClientName(pClientIds[i]), sizeof(Tmp->m_aaNames[i]));
184189
Tmp->m_Size = Size;
185-
Tmp->m_Time = Time;
190+
Tmp->m_Time = (float)TimeTicks / (float)Server()->TickSpeed();
186191
str_copy(Tmp->m_aTimestamp, pTimestamp, sizeof(Tmp->m_aTimestamp));
187192
FormatUuid(GameServer()->GameUuid(), Tmp->m_aGameUuid, sizeof(Tmp->m_aGameUuid));
188193
str_copy(Tmp->m_aMap, g_Config.m_SvMap, sizeof(Tmp->m_aMap));

src/game/server/score.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ class CScore
4949
void MapVote(int ClientId, const char *pMapName);
5050
void LoadPlayerData(int ClientId, const char *pName = "");
5151
void LoadPlayerTimeCp(int ClientId, const char *pName = "");
52-
void SaveScore(int ClientId, float Time, const char *pTimestamp, const float aTimeCp[NUM_CHECKPOINTS], bool NotEligible);
52+
void SaveScore(int ClientId, int TimeTicks, const char *pTimestamp, const float aTimeCp[NUM_CHECKPOINTS], bool NotEligible);
5353

54-
void SaveTeamScore(int *pClientIds, unsigned int Size, float Time, const char *pTimestamp);
54+
void SaveTeamScore(int Team, int *pClientIds, unsigned int Size, int TimeTicks, const char *pTimestamp);
5555

5656
void ShowTop(int ClientId, int Offset = 1);
5757
void ShowRank(int ClientId, const char *pName);

src/game/server/teams.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,9 @@ void CGameTeams::CheckTeamFinished(int Team)
335335

336336
if(PlayersCount > 0)
337337
{
338-
float Time = (float)(Server()->Tick() - GetStartTime(apTeamPlayers[0])) / ((float)Server()->TickSpeed());
339-
if(Time < 0.000001f)
338+
int TimeTicks = Server()->Tick() - GetStartTime(apTeamPlayers[0]);
339+
float Time = (float)TimeTicks / (float)Server()->TickSpeed();
340+
if(TimeTicks <= 0)
340341
{
341342
return;
342343
}
@@ -348,7 +349,7 @@ void CGameTeams::CheckTeamFinished(int Team)
348349
char aBuf[256];
349350
str_format(aBuf, sizeof(aBuf),
350351
"Your team would've finished in: %d minute(s) %5.2f second(s). Since you had practice mode enabled your rank doesn't count.",
351-
(int)Time / 60, Time - ((int)Time / 60 * 60));
352+
(int)Time / 50 / 60, Time - ((int)Time / 60 * 60));
352353
GameServer()->SendChatTeam(Team, aBuf);
353354

354355
for(unsigned int i = 0; i < PlayersCount; ++i)
@@ -365,7 +366,7 @@ void CGameTeams::CheckTeamFinished(int Team)
365366
for(unsigned int i = 0; i < PlayersCount; ++i)
366367
OnFinish(apTeamPlayers[i], Time, aTimestamp);
367368
ChangeTeamState(Team, TEAMSTATE_FINISHED); // TODO: Make it better
368-
OnTeamFinish(apTeamPlayers, PlayersCount, Time, aTimestamp);
369+
OnTeamFinish(Team, apTeamPlayers, PlayersCount, TimeTicks, aTimestamp);
369370
}
370371
}
371372
}
@@ -666,7 +667,7 @@ float *CGameTeams::GetCurrentTimeCp(CPlayer *Player)
666667
return NULL;
667668
}
668669

669-
void CGameTeams::OnTeamFinish(CPlayer **Players, unsigned int Size, float Time, const char *pTimestamp)
670+
void CGameTeams::OnTeamFinish(int Team, CPlayer **Players, unsigned int Size, int TimeTicks, const char *pTimestamp)
670671
{
671672
int aPlayerCids[MAX_CLIENTS];
672673

@@ -685,13 +686,16 @@ void CGameTeams::OnTeamFinish(CPlayer **Players, unsigned int Size, float Time,
685686
}
686687

687688
if(Size >= (unsigned int)g_Config.m_SvMinTeamSize)
688-
GameServer()->Score()->SaveTeamScore(aPlayerCids, Size, Time, pTimestamp);
689+
GameServer()->Score()->SaveTeamScore(Team, aPlayerCids, Size, TimeTicks, pTimestamp);
689690
}
690691

691-
void CGameTeams::OnFinish(CPlayer *Player, float Time, const char *pTimestamp)
692+
void CGameTeams::OnFinish(CPlayer *Player, int TimeTicks, const char *pTimestamp)
692693
{
693694
if(!Player || !Player->IsPlaying())
694695
return;
696+
697+
float Time = TimeTicks / (float)Server()->TickSpeed();
698+
695699
// TODO:DDRace:btd: this ugly
696700
const int ClientId = Player->GetCid();
697701
CPlayerData *pData = GameServer()->Score()->PlayerData(ClientId);
@@ -804,7 +808,7 @@ void CGameTeams::OnFinish(CPlayer *Player, float Time, const char *pTimestamp)
804808

805809
if(CallSaveScore)
806810
if(g_Config.m_SvNamelessScore || !str_startswith(Server()->ClientName(ClientId), "nameless tee"))
807-
GameServer()->Score()->SaveScore(ClientId, Time, pTimestamp,
811+
GameServer()->Score()->SaveScore(ClientId, TimeTicks, pTimestamp,
808812
GetCurrentTimeCp(Player), Player->m_NotEligibleForFinish);
809813

810814
bool NeedToSendNewServerRecord = false;

src/game/server/teams.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ class CGameTeams
4747
*/
4848
void KillTeam(int Team, int NewStrongId, int ExceptId = -1);
4949
bool TeamFinished(int Team);
50-
void OnTeamFinish(CPlayer **Players, unsigned int Size, float Time, const char *pTimestamp);
51-
void OnFinish(CPlayer *Player, float Time, const char *pTimestamp);
50+
void OnTeamFinish(int Team, CPlayer **Players, unsigned int Size, int TimeTicks, const char *pTimestamp);
51+
void OnFinish(CPlayer *Player, int TimeTicks, const char *pTimestamp);
5252

5353
public:
5454
enum

src/game/server/teehistorian.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,34 @@ void CTeeHistorian::RecordAntibot(const void *pData, int DataSize)
811811
WriteExtra(UUID_TEEHISTORIAN_ANTIBOT, pData, DataSize);
812812
}
813813

814+
void CTeeHistorian::RecordPlayerFinish(int ClientId, int TimeTicks)
815+
{
816+
CPacker Buffer;
817+
Buffer.Reset();
818+
Buffer.AddInt(ClientId);
819+
Buffer.AddInt(TimeTicks);
820+
if(m_Debug)
821+
{
822+
dbg_msg("teehistorian", "player_finish cid=%d Time=%d", ClientId, TimeTicks);
823+
}
824+
825+
WriteExtra(UUID_TEEHISTORIAN_PLAYER_FINISH, Buffer.Data(), Buffer.Size());
826+
}
827+
828+
void CTeeHistorian::RecordTeamFinish(int TeamId, int TimeTicks)
829+
{
830+
CPacker Buffer;
831+
Buffer.Reset();
832+
Buffer.AddInt(TeamId);
833+
Buffer.AddInt(TimeTicks);
834+
if(m_Debug)
835+
{
836+
dbg_msg("teehistorian", "player_finish cid=%d Time=%d", TeamId, TimeTicks);
837+
}
838+
839+
WriteExtra(UUID_TEEHISTORIAN_TEAM_FINISH, Buffer.Data(), Buffer.Size());
840+
}
841+
814842
void CTeeHistorian::Finish()
815843
{
816844
dbg_assert(m_State == STATE_START || m_State == STATE_INPUTS || m_State == STATE_BEFORE_ENDTICK || m_State == STATE_BEFORE_TICK, "invalid teehistorian state");

src/game/server/teehistorian.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ class CTeeHistorian
9191

9292
void RecordAntibot(const void *pData, int DataSize);
9393

94+
void RecordPlayerFinish(int ClientId, int TimeTicks);
95+
void RecordTeamFinish(int TeamId, int TimeTicks);
96+
9497
int m_Debug; // Possible values: 0, 1, 2.
9598

9699
private:

0 commit comments

Comments
 (0)