1#include "scoreworker.h"
2
3#include <base/dbg.h>
4#include <base/log.h>
5#include <base/mem.h>
6#include <base/str.h>
7#include <base/time.h>
8
9#include <engine/server/databases/connection.h>
10#include <engine/server/databases/connection_pool.h>
11#include <engine/server/sql_string_helpers.h>
12#include <engine/shared/config.h>
13
14#include <cmath>
15
16// "6b407e81-8b77-3e04-a207-8da17f37d000"
17// "save-no-save-id@ddnet.tw"
18static const CUuid UUID_NO_SAVE_ID =
19 {.m_aData: {0x6b, 0x40, 0x7e, 0x81, 0x8b, 0x77, 0x3e, 0x04,
20 0xa2, 0x07, 0x8d, 0xa1, 0x7f, 0x37, 0xd0, 0x00}};
21
22CScorePlayerResult::CScorePlayerResult()
23{
24 SetVariant(Variant::DIRECT);
25}
26
27void CScorePlayerResult::SetVariant(Variant v)
28{
29 m_MessageKind = v;
30 switch(v)
31 {
32 case DIRECT:
33 case ALL:
34 for(auto &aMessage : m_Data.m_aaMessages)
35 aMessage[0] = 0;
36 break;
37 case BROADCAST:
38 m_Data.m_aBroadcast[0] = 0;
39 break;
40 case MAP_VOTE:
41 m_Data.m_MapVote.m_aMap[0] = '\0';
42 m_Data.m_MapVote.m_aReason[0] = '\0';
43 m_Data.m_MapVote.m_aServer[0] = '\0';
44 break;
45 case PLAYER_INFO:
46 m_Data.m_Info.m_Birthday = 0;
47 m_Data.m_Info.m_Time.reset();
48 std::fill(first: std::begin(arr&: m_Data.m_Info.m_aTimeCp), last: std::end(arr&: m_Data.m_Info.m_aTimeCp), value: 0.0f);
49 break;
50 case PLAYER_TIMECP:
51 m_Data.m_Info.m_aRequestedPlayer[0] = '\0';
52 m_Data.m_Info.m_Time.reset();
53 std::fill(first: std::begin(arr&: m_Data.m_Info.m_aTimeCp), last: std::end(arr&: m_Data.m_Info.m_aTimeCp), value: 0.0f);
54 break;
55 }
56}
57
58CTeamrank::CTeamrank() :
59 m_NumNames(0)
60{
61 for(auto &aName : m_aaNames)
62 aName[0] = '\0';
63 mem_zero(block: &m_TeamId.m_aData, size: sizeof(m_TeamId));
64}
65
66bool CTeamrank::NextSqlResult(IDbConnection *pSqlServer, bool *pEnd, char *pError, int ErrorSize)
67{
68 pSqlServer->GetBlob(Col: 1, pBuffer: m_TeamId.m_aData, BufferSize: sizeof(m_TeamId.m_aData));
69 pSqlServer->GetString(Col: 2, pBuffer: m_aaNames[0], BufferSize: sizeof(m_aaNames[0]));
70 m_NumNames = 1;
71 bool End = false;
72 while(pSqlServer->Step(pEnd: &End, pError, ErrorSize) && !End)
73 {
74 CUuid TeamId;
75 pSqlServer->GetBlob(Col: 1, pBuffer: TeamId.m_aData, BufferSize: sizeof(TeamId.m_aData));
76 if(m_TeamId != TeamId)
77 {
78 *pEnd = false;
79 return true;
80 }
81 if(m_NumNames < MAX_CLIENTS)
82 {
83 pSqlServer->GetString(Col: 2, pBuffer: m_aaNames[m_NumNames], BufferSize: sizeof(m_aaNames[m_NumNames]));
84 m_NumNames++;
85 }
86 }
87 if(!End)
88 {
89 return false;
90 }
91 *pEnd = true;
92 return true;
93}
94
95bool CTeamrank::SamePlayers(const std::vector<std::string> *pvSortedNames)
96{
97 if(pvSortedNames->size() != m_NumNames)
98 return false;
99 for(unsigned int i = 0; i < m_NumNames; i++)
100 {
101 if(str_comp(a: pvSortedNames->at(n: i).c_str(), b: m_aaNames[i]) != 0)
102 return false;
103 }
104 return true;
105}
106
107void CTeamrank::FormatNames(char *pBuf, int BufSize) const
108{
109 // Reserve room for the longest suffix that could be needed.
110 char aMore[16];
111 str_format(buffer: aMore, buffer_size: sizeof(aMore), format: " & %d more", (int)m_NumNames);
112 const int NamesSize = BufSize - str_length(str: aMore);
113
114 pBuf[0] = '\0';
115 int Length = 0;
116 unsigned int Name = 0;
117 for(; Name < m_NumNames; Name++)
118 {
119 char aName[3 + MAX_NAME_LENGTH];
120 str_format(buffer: aName, buffer_size: sizeof(aName), format: "%s%s",
121 Length == 0 ? "" : (Name == m_NumNames - 1 ? " & " : ", "),
122 m_aaNames[Name]);
123 if(Length + str_length(str: aName) >= NamesSize)
124 break;
125 str_append(dst: pBuf, src: aName, dst_size: NamesSize);
126 Length += str_length(str: aName);
127 }
128 if(Name < m_NumNames)
129 {
130 str_format(buffer: aMore, buffer_size: sizeof(aMore), format: " & %d more", (int)(m_NumNames - Name));
131 str_append(dst: pBuf, src: aMore, dst_size: BufSize);
132 }
133}
134
135void CTeamrank::FormatTeamTopLine(char *pMessage, int MessageSize, int Rank, const char *pTime) const
136{
137 // format without names to measure remaining space for names
138 const int EmptyLength = str_format(buffer: pMessage, buffer_size: MessageSize, format: "%d. %s Team Time: %s", Rank, "", pTime);
139 char aFormattedNames[MAX_TEAM_NAMES_LENGTH];
140 FormatNames(pBuf: aFormattedNames, BufSize: MAX_CHAT_LENGTH - EmptyLength);
141 str_format(buffer: pMessage, buffer_size: MessageSize, format: "%d. %s Team Time: %s", Rank, aFormattedNames, pTime);
142}
143
144bool CTeamrank::GetSqlTop5Team(IDbConnection *pSqlServer, bool *pEnd, char *pError, int ErrorSize, char (*paMessages)[512], int *Line, int Count)
145{
146 char aTime[32];
147 int StartLine = *Line;
148 for(*Line = StartLine; *Line < StartLine + Count; (*Line)++)
149 {
150 bool Last = false;
151 float Time = pSqlServer->GetFloat(Col: 2);
152 str_time_float(secs: Time, format: ETimeFormat::HOURS_CENTISECS, buffer: aTime, buffer_size: sizeof(aTime));
153 int Rank = pSqlServer->GetInt(Col: 3);
154 int TeamSize = pSqlServer->GetInt(Col: 4);
155
156 CTeamrank Teamrank;
157 for(int i = 0; i < TeamSize; i++)
158 {
159 if(Teamrank.m_NumNames < MAX_CLIENTS)
160 {
161 pSqlServer->GetString(Col: 1, pBuffer: Teamrank.m_aaNames[Teamrank.m_NumNames], BufferSize: sizeof(Teamrank.m_aaNames[0]));
162 Teamrank.m_NumNames++;
163 }
164 if(!pSqlServer->Step(pEnd: &Last, pError, ErrorSize))
165 {
166 return false;
167 }
168 if(Last)
169 {
170 break;
171 }
172 }
173 Teamrank.FormatTeamTopLine(pMessage: paMessages[*Line], MessageSize: sizeof(paMessages[*Line]), Rank, pTime: aTime);
174 if(Last)
175 {
176 (*Line)++;
177 break;
178 }
179 }
180 return true;
181}
182
183bool CScoreWorker::LoadBestTime(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize)
184{
185 const auto *pData = dynamic_cast<const CSqlLoadBestTimeRequest *>(pGameData);
186 auto *pResult = dynamic_cast<CScoreLoadBestTimeResult *>(pGameData->m_pResult.get());
187
188 char aBuf[512];
189 // get the best time
190 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
191 format: "SELECT Time FROM %s_race WHERE Map=? ORDER BY `Time` ASC LIMIT 1",
192 pSqlServer->GetPrefix());
193 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
194 {
195 return false;
196 }
197 pSqlServer->BindString(Idx: 1, pString: pData->m_aMap);
198
199 bool End;
200 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
201 {
202 return false;
203 }
204 if(!End)
205 {
206 pResult->m_CurrentRecord = pSqlServer->GetFloat(Col: 1);
207 }
208
209 return true;
210}
211
212// update stuff
213bool CScoreWorker::LoadPlayerData(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize)
214{
215 const auto *pData = dynamic_cast<const CSqlPlayerRequest *>(pGameData);
216 auto *pResult = dynamic_cast<CScorePlayerResult *>(pGameData->m_pResult.get());
217 pResult->SetVariant(CScorePlayerResult::PLAYER_INFO);
218
219 char aBuf[1024];
220 // get best race time
221 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
222 format: "SELECT"
223 " (SELECT Time FROM %s_race WHERE Map = ? AND Name = ? ORDER BY Time ASC LIMIT 1) AS minTime, "
224 " cp1, cp2, cp3, cp4, cp5, cp6, cp7, cp8, cp9, cp10, cp11, cp12, cp13, cp14, "
225 " cp15, cp16, cp17, cp18, cp19, cp20, cp21, cp22, cp23, cp24, cp25, "
226 " (cp1 + cp2 + cp3 + cp4 + cp5 + cp6 + cp7 + cp8 + cp9 + cp10 + cp11 + cp12 + cp13 + cp14 + "
227 " cp15 + cp16 + cp17 + cp18 + cp19 + cp20 + cp21 + cp22 + cp23 + cp24 + cp25 > 0) AS hasCP, Time "
228 "FROM %s_race "
229 "WHERE Map = ? AND Name = ? "
230 "ORDER BY hasCP DESC, Time ASC "
231 "LIMIT 1",
232 pSqlServer->GetPrefix(), pSqlServer->GetPrefix());
233 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
234 {
235 return false;
236 }
237
238 const char *pPlayer = pData->m_aName[0] != '\0' ? pData->m_aName : pData->m_aRequestingPlayer;
239 pSqlServer->BindString(Idx: 1, pString: pData->m_aMap);
240 pSqlServer->BindString(Idx: 2, pString: pData->m_aRequestingPlayer);
241 pSqlServer->BindString(Idx: 3, pString: pData->m_aMap);
242 pSqlServer->BindString(Idx: 4, pString: pPlayer);
243
244 bool End;
245 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
246 {
247 return false;
248 }
249 if(!End)
250 {
251 if(!pSqlServer->IsNull(Col: 1))
252 {
253 // get the best time
254 float Time = pSqlServer->GetFloat(Col: 1);
255 pResult->m_Data.m_Info.m_Time = Time;
256 }
257
258 for(int i = 0; i < NUM_CHECKPOINTS; i++)
259 {
260 pResult->m_Data.m_Info.m_aTimeCp[i] = pSqlServer->GetFloat(Col: i + 2);
261 }
262 }
263
264 // birthday check
265 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
266 format: "SELECT CURRENT_TIMESTAMP AS Current, MIN(Timestamp) AS Stamp "
267 "FROM %s_race "
268 "WHERE Name = ?",
269 pSqlServer->GetPrefix());
270 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
271 {
272 return false;
273 }
274 pSqlServer->BindString(Idx: 1, pString: pData->m_aRequestingPlayer);
275
276 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
277 {
278 return false;
279 }
280 if(!End && !pSqlServer->IsNull(Col: 2))
281 {
282 char aCurrent[TIMESTAMP_STR_LENGTH];
283 pSqlServer->GetString(Col: 1, pBuffer: aCurrent, BufferSize: sizeof(aCurrent));
284 char aStamp[TIMESTAMP_STR_LENGTH];
285 pSqlServer->GetString(Col: 2, pBuffer: aStamp, BufferSize: sizeof(aStamp));
286 int CurrentYear, CurrentMonth, CurrentDay;
287 int StampYear, StampMonth, StampDay;
288 if(sscanf(s: aCurrent, format: "%d-%d-%d", &CurrentYear, &CurrentMonth, &CurrentDay) == 3 && sscanf(s: aStamp, format: "%d-%d-%d", &StampYear, &StampMonth, &StampDay) == 3 && CurrentMonth == StampMonth && CurrentDay == StampDay)
289 pResult->m_Data.m_Info.m_Birthday = CurrentYear - StampYear;
290 }
291 return true;
292}
293
294bool CScoreWorker::LoadPlayerTimeCp(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize)
295{
296 const auto *pData = dynamic_cast<const CSqlPlayerRequest *>(pGameData);
297 auto *pResult = dynamic_cast<CScorePlayerResult *>(pGameData->m_pResult.get());
298 auto *paMessages = pResult->m_Data.m_aaMessages;
299
300 char aBuf[1024];
301 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
302 format: "SELECT"
303 " Time, cp1, cp2, cp3, cp4, cp5, cp6, cp7, cp8, cp9, cp10, cp11, cp12, cp13, "
304 " cp14, cp15, cp16, cp17, cp18, cp19, cp20, cp21, cp22, cp23, cp24, cp25 "
305 "FROM %s_race "
306 "WHERE Map = ? AND Name = ? AND "
307 " (cp1 + cp2 + cp3 + cp4 + cp5 + cp6 + cp7 + cp8 + cp9 + cp10 + cp11 + cp12 + cp13 + cp14 + "
308 " cp15 + cp16 + cp17 + cp18 + cp19 + cp20 + cp21 + cp22 + cp23 + cp24 + cp25) > 0 "
309 "ORDER BY Time ASC "
310 "LIMIT 1",
311 pSqlServer->GetPrefix());
312 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
313 {
314 return false;
315 }
316
317 const char *pPlayer = pData->m_aName[0] != '\0' ? pData->m_aName : pData->m_aRequestingPlayer;
318 pSqlServer->BindString(Idx: 1, pString: pData->m_aMap);
319 pSqlServer->BindString(Idx: 2, pString: pPlayer);
320
321 bool End;
322 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
323 {
324 return false;
325 }
326 if(!End)
327 {
328 pResult->SetVariant(CScorePlayerResult::PLAYER_TIMECP);
329 pResult->m_Data.m_Info.m_Time = pSqlServer->GetFloat(Col: 1);
330 for(int i = 0; i < NUM_CHECKPOINTS; i++)
331 {
332 pResult->m_Data.m_Info.m_aTimeCp[i] = pSqlServer->GetFloat(Col: i + 2);
333 }
334 str_copy(dst&: pResult->m_Data.m_Info.m_aRequestedPlayer, src: pPlayer);
335 }
336 else
337 {
338 pResult->SetVariant(CScorePlayerResult::DIRECT);
339 str_format(buffer: paMessages[0], buffer_size: sizeof(paMessages[0]), format: "'%s' has no checkpoint times available", pPlayer);
340 }
341 return true;
342}
343
344bool CScoreWorker::MapVote(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize)
345{
346 const auto *pData = dynamic_cast<const CSqlPlayerRequest *>(pGameData);
347 auto *pResult = dynamic_cast<CScorePlayerResult *>(pGameData->m_pResult.get());
348 auto *paMessages = pResult->m_Data.m_aaMessages;
349
350 char aFuzzyMap[128];
351 str_copy(dst&: aFuzzyMap, src: pData->m_aName);
352 sqlstr::FuzzyString(pString: aFuzzyMap, Size: sizeof(aFuzzyMap));
353
354 char aMapPrefix[128];
355 str_copy(dst&: aMapPrefix, src: pData->m_aName);
356 str_append(dst&: aMapPrefix, src: "%");
357
358 char aBuf[768];
359 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
360 format: "SELECT Map, Server "
361 "FROM %s_maps "
362 "WHERE Map LIKE %s "
363 "ORDER BY "
364 " CASE WHEN LOWER(Map) = LOWER(?) THEN 0 ELSE 1 END, "
365 " CASE WHEN Map LIKE ? THEN 0 ELSE 1 END, "
366 " LENGTH(Map), Map "
367 "LIMIT 1",
368 pSqlServer->GetPrefix(), pSqlServer->CollateNocase());
369 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
370 {
371 return false;
372 }
373 pSqlServer->BindString(Idx: 1, pString: aFuzzyMap);
374 pSqlServer->BindString(Idx: 2, pString: pData->m_aName);
375 pSqlServer->BindString(Idx: 3, pString: aMapPrefix);
376
377 bool End;
378 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
379 {
380 return false;
381 }
382 if(!End)
383 {
384 pResult->SetVariant(CScorePlayerResult::MAP_VOTE);
385 auto *pMapVote = &pResult->m_Data.m_MapVote;
386 pSqlServer->GetString(Col: 1, pBuffer: pMapVote->m_aMap, BufferSize: sizeof(pMapVote->m_aMap));
387 pSqlServer->GetString(Col: 2, pBuffer: pMapVote->m_aServer, BufferSize: sizeof(pMapVote->m_aServer));
388 str_copy(dst&: pMapVote->m_aReason, src: "/map");
389
390 for(char *p = pMapVote->m_aServer; *p; p++) // lower case server
391 *p = tolower(c: (unsigned char)*p);
392 }
393 else
394 {
395 pResult->SetVariant(CScorePlayerResult::DIRECT);
396 str_format(buffer: paMessages[0], buffer_size: sizeof(paMessages[0]),
397 format: "No map like \"%s\" found. "
398 "Try adding a '%%' at the start if you don't know the first character. "
399 "Example: /map %%castle for \"Out of Castle\"",
400 pData->m_aName);
401 }
402 return true;
403}
404
405bool CScoreWorker::MapInfo(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize)
406{
407 const auto *pData = dynamic_cast<const CSqlPlayerRequest *>(pGameData);
408 auto *pResult = dynamic_cast<CScorePlayerResult *>(pGameData->m_pResult.get());
409
410 char aFuzzyMap[128];
411 str_copy(dst&: aFuzzyMap, src: pData->m_aName);
412 sqlstr::FuzzyString(pString: aFuzzyMap, Size: sizeof(aFuzzyMap));
413
414 char aMapPrefix[128];
415 str_copy(dst&: aMapPrefix, src: pData->m_aName);
416 str_append(dst&: aMapPrefix, src: "%");
417
418 char aCurrentTimestamp[512];
419 pSqlServer->ToUnixTimestamp(pTimestamp: "CURRENT_TIMESTAMP", aBuf: aCurrentTimestamp, BufferSize: sizeof(aCurrentTimestamp));
420 char aTimestamp[512];
421 pSqlServer->ToUnixTimestamp(pTimestamp: "l.Timestamp", aBuf: aTimestamp, BufferSize: sizeof(aTimestamp));
422
423 char aBuf[4096];
424 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
425 format: "SELECT l.Map, l.Server, Mapper, Points, Stars, "
426 " (SELECT COUNT(Name) FROM %s_race WHERE Map = l.Map) AS Finishes, "
427 " (SELECT COUNT(DISTINCT Name) FROM %s_race WHERE Map = l.Map) AS Finishers, "
428 " %s AS Stamp, "
429 " %s-%s AS Ago, "
430 " (SELECT MIN(Time) FROM %s_race WHERE Map = l.Map AND Name = ?) AS OwnTime "
431 "FROM ("
432 " SELECT * FROM %s_maps "
433 " WHERE Map LIKE %s "
434 " ORDER BY "
435 " CASE WHEN LOWER(Map) = LOWER(?) THEN 0 ELSE 1 END, "
436 " CASE WHEN Map LIKE ? THEN 0 ELSE 1 END, "
437 " LENGTH(Map), "
438 " Map "
439 " LIMIT 1"
440 ") as l",
441 pSqlServer->GetPrefix(), pSqlServer->GetPrefix(),
442 aTimestamp, aCurrentTimestamp, aTimestamp,
443 pSqlServer->GetPrefix(), pSqlServer->GetPrefix(),
444 pSqlServer->CollateNocase());
445 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
446 {
447 return false;
448 }
449 pSqlServer->BindString(Idx: 1, pString: pData->m_aRequestingPlayer);
450 pSqlServer->BindString(Idx: 2, pString: aFuzzyMap);
451 pSqlServer->BindString(Idx: 3, pString: pData->m_aName);
452 pSqlServer->BindString(Idx: 4, pString: aMapPrefix);
453
454 bool End;
455 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
456 {
457 return false;
458 }
459 if(!End)
460 {
461 char aMap[MAX_MAP_LENGTH];
462 pSqlServer->GetString(Col: 1, pBuffer: aMap, BufferSize: sizeof(aMap));
463 char aServer[32];
464 pSqlServer->GetString(Col: 2, pBuffer: aServer, BufferSize: sizeof(aServer));
465 char aMapper[128];
466 pSqlServer->GetString(Col: 3, pBuffer: aMapper, BufferSize: sizeof(aMapper));
467 int Points = pSqlServer->GetInt(Col: 4);
468 int Stars = pSqlServer->GetInt(Col: 5);
469 int Finishes = pSqlServer->GetInt(Col: 6);
470 int Finishers = pSqlServer->GetInt(Col: 7);
471 int Stamp = pSqlServer->GetInt(Col: 8);
472 int Ago = pSqlServer->GetInt(Col: 9);
473 float OwnTime = pSqlServer->GetOptionalFloat(Col: 10).value_or(u: -1.0f);
474
475 float Median = -1.0f;
476 if(Finishes > 0)
477 {
478 char aMedianBuf[256];
479 str_format(buffer: aMedianBuf, buffer_size: sizeof(aMedianBuf),
480 format: "SELECT AVG(Time) FROM ("
481 " SELECT Time FROM %s_race WHERE Map = ? ORDER BY Time LIMIT %d OFFSET %d"
482 ") AS m",
483 pSqlServer->GetPrefix(), 2 - (Finishes % 2), (Finishes - 1) / 2);
484 if(!pSqlServer->PrepareStatement(pStmt: aMedianBuf, pError, ErrorSize))
485 {
486 return false;
487 }
488 pSqlServer->BindString(Idx: 1, pString: aMap);
489 bool MedianEnd;
490 if(!pSqlServer->Step(pEnd: &MedianEnd, pError, ErrorSize))
491 {
492 return false;
493 }
494 if(!MedianEnd)
495 {
496 Median = pSqlServer->GetOptionalFloat(Col: 1).value_or(u: -1.0f);
497 }
498 }
499
500 char aAgoString[40] = "\0";
501 char aReleasedString[60] = "\0";
502 if(Stamp != 0)
503 {
504 sqlstr::AgoTimeToString(AgoTime: Ago, pAgoString: aAgoString, Size: sizeof(aAgoString));
505 str_format(buffer: aReleasedString, buffer_size: sizeof(aReleasedString), format: ", released %s ago", aAgoString);
506 }
507
508 char aMedianString[60] = "\0";
509 if(Median > 0)
510 {
511 str_time(centisecs: (int64_t)Median * 100, format: ETimeFormat::HOURS, buffer: aBuf, buffer_size: sizeof(aBuf));
512 str_format(buffer: aMedianString, buffer_size: sizeof(aMedianString), format: " in %s median", aBuf);
513 }
514
515 char aStars[20];
516 switch(Stars)
517 {
518 case 0: str_copy(dst&: aStars, src: "✰✰✰✰✰"); break;
519 case 1: str_copy(dst&: aStars, src: "★✰✰✰✰"); break;
520 case 2: str_copy(dst&: aStars, src: "★★✰✰✰"); break;
521 case 3: str_copy(dst&: aStars, src: "★★★✰✰"); break;
522 case 4: str_copy(dst&: aStars, src: "★★★★✰"); break;
523 case 5: str_copy(dst&: aStars, src: "★★★★★"); break;
524 default: aStars[0] = '\0';
525 }
526
527 char aOwnFinishesString[40] = "\0";
528 if(OwnTime > 0)
529 {
530 str_time_float(secs: OwnTime, format: ETimeFormat::HOURS_CENTISECS, buffer: aBuf, buffer_size: sizeof(aBuf));
531 str_format(buffer: aOwnFinishesString, buffer_size: sizeof(aOwnFinishesString),
532 format: ", your time: %s", aBuf);
533 }
534
535 str_format(buffer: pResult->m_Data.m_aaMessages[0], buffer_size: sizeof(pResult->m_Data.m_aaMessages[0]),
536 format: "\"%s\" by %s on %s, %s, %d %s%s, %d %s by %d %s%s%s",
537 aMap, aMapper, aServer, aStars,
538 Points, Points == 1 ? "point" : "points",
539 aReleasedString,
540 Finishes, Finishes == 1 ? "finish" : "finishes",
541 Finishers, Finishers == 1 ? "tee" : "tees",
542 aMedianString, aOwnFinishesString);
543 }
544 else
545 {
546 str_format(buffer: pResult->m_Data.m_aaMessages[0], buffer_size: sizeof(pResult->m_Data.m_aaMessages[0]),
547 format: "No map like \"%s\" found.", pData->m_aName);
548 }
549 return true;
550}
551
552bool CScoreWorker::SaveScore(IDbConnection *pSqlServer, const ISqlData *pGameData, Write w, char *pError, int ErrorSize)
553{
554 const auto *pData = dynamic_cast<const CSqlScoreData *>(pGameData);
555 auto *pResult = dynamic_cast<CScorePlayerResult *>(pGameData->m_pResult.get());
556 auto *paMessages = pResult->m_Data.m_aaMessages;
557
558 char aBuf[1024];
559
560 if(w == Write::NORMAL_SUCCEEDED)
561 {
562 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
563 format: "DELETE FROM %s_race_backup WHERE GameId=? AND Name=? AND Timestamp=%s",
564 pSqlServer->GetPrefix(), pSqlServer->InsertTimestampAsUtc());
565 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
566 {
567 return false;
568 }
569 pSqlServer->BindString(Idx: 1, pString: pData->m_aGameUuid);
570 pSqlServer->BindString(Idx: 2, pString: pData->m_aName);
571 pSqlServer->BindString(Idx: 3, pString: pData->m_aTimestamp);
572 pSqlServer->Print();
573 int NumDeleted;
574 if(!pSqlServer->ExecuteUpdate(pNumUpdated: &NumDeleted, pError, ErrorSize))
575 {
576 return false;
577 }
578 if(NumDeleted == 0)
579 {
580 log_warn("sql", "Rank got moved out of backup database, will show up as duplicate rank in MySQL");
581 }
582 return true;
583 }
584 if(w == Write::NORMAL_FAILED)
585 {
586 int NumUpdated;
587 // move to non-tmp table succeeded. delete from backup again
588 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
589 format: "INSERT INTO %s_race SELECT * FROM %s_race_backup WHERE GameId=? AND Name=? AND Timestamp=%s",
590 pSqlServer->GetPrefix(), pSqlServer->GetPrefix(), pSqlServer->InsertTimestampAsUtc());
591 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
592 {
593 return false;
594 }
595 pSqlServer->BindString(Idx: 1, pString: pData->m_aGameUuid);
596 pSqlServer->BindString(Idx: 2, pString: pData->m_aName);
597 pSqlServer->BindString(Idx: 3, pString: pData->m_aTimestamp);
598 pSqlServer->Print();
599 if(!pSqlServer->ExecuteUpdate(pNumUpdated: &NumUpdated, pError, ErrorSize))
600 {
601 return false;
602 }
603
604 // move to non-tmp table succeeded. delete from backup again
605 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
606 format: "DELETE FROM %s_race_backup WHERE GameId=? AND Name=? AND Timestamp=%s",
607 pSqlServer->GetPrefix(), pSqlServer->InsertTimestampAsUtc());
608 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
609 {
610 return false;
611 }
612 pSqlServer->BindString(Idx: 1, pString: pData->m_aGameUuid);
613 pSqlServer->BindString(Idx: 2, pString: pData->m_aName);
614 pSqlServer->BindString(Idx: 3, pString: pData->m_aTimestamp);
615 pSqlServer->Print();
616 if(!pSqlServer->ExecuteUpdate(pNumUpdated: &NumUpdated, pError, ErrorSize))
617 {
618 return false;
619 }
620 if(NumUpdated == 0)
621 {
622 log_warn("sql", "Rank got moved out of backup database, will show up as duplicate rank in MySQL");
623 }
624 return true;
625 }
626
627 if(w == Write::NORMAL)
628 {
629 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
630 format: "SELECT COUNT(*) AS NumFinished FROM %s_race WHERE Map=? AND Name=? ORDER BY time ASC LIMIT 1",
631 pSqlServer->GetPrefix());
632 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
633 {
634 return false;
635 }
636 pSqlServer->BindString(Idx: 1, pString: pData->m_aMap);
637 pSqlServer->BindString(Idx: 2, pString: pData->m_aName);
638
639 bool End;
640 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
641 {
642 return false;
643 }
644 int NumFinished = pSqlServer->GetInt(Col: 1);
645 if(NumFinished == 0)
646 {
647 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "SELECT Points FROM %s_maps WHERE Map=?", pSqlServer->GetPrefix());
648 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
649 {
650 return false;
651 }
652 pSqlServer->BindString(Idx: 1, pString: pData->m_aMap);
653
654 bool End2;
655 if(!pSqlServer->Step(pEnd: &End2, pError, ErrorSize))
656 {
657 return false;
658 }
659 if(!End2)
660 {
661 int Points = pSqlServer->GetInt(Col: 1);
662 if(!pSqlServer->AddPoints(pPlayer: pData->m_aName, Points, pError, ErrorSize))
663 {
664 return false;
665 }
666 str_format(buffer: paMessages[0], buffer_size: sizeof(paMessages[0]),
667 format: "You earned %d point%s for finishing this map!",
668 Points, Points == 1 ? "" : "s");
669 }
670 }
671 }
672
673 // save score. Can't fail, because no UNIQUE/PRIMARY KEY constrain is defined.
674 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
675 format: "%s INTO %s_race%s("
676 " Map, Name, Timestamp, Time, Server, "
677 " cp1, cp2, cp3, cp4, cp5, cp6, cp7, cp8, cp9, cp10, cp11, cp12, cp13, "
678 " cp14, cp15, cp16, cp17, cp18, cp19, cp20, cp21, cp22, cp23, cp24, cp25, "
679 " GameId, DDNet7) "
680 "VALUES (?, ?, %s, %.2f, ?, "
681 " %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, "
682 " %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, "
683 " %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, "
684 " ?, %s)",
685 pSqlServer->InsertIgnore(), pSqlServer->GetPrefix(),
686 w == Write::NORMAL ? "" : "_backup",
687 pSqlServer->InsertTimestampAsUtc(), pData->m_Time,
688 pData->m_aCurrentTimeCp[0], pData->m_aCurrentTimeCp[1], pData->m_aCurrentTimeCp[2],
689 pData->m_aCurrentTimeCp[3], pData->m_aCurrentTimeCp[4], pData->m_aCurrentTimeCp[5],
690 pData->m_aCurrentTimeCp[6], pData->m_aCurrentTimeCp[7], pData->m_aCurrentTimeCp[8],
691 pData->m_aCurrentTimeCp[9], pData->m_aCurrentTimeCp[10], pData->m_aCurrentTimeCp[11],
692 pData->m_aCurrentTimeCp[12], pData->m_aCurrentTimeCp[13], pData->m_aCurrentTimeCp[14],
693 pData->m_aCurrentTimeCp[15], pData->m_aCurrentTimeCp[16], pData->m_aCurrentTimeCp[17],
694 pData->m_aCurrentTimeCp[18], pData->m_aCurrentTimeCp[19], pData->m_aCurrentTimeCp[20],
695 pData->m_aCurrentTimeCp[21], pData->m_aCurrentTimeCp[22], pData->m_aCurrentTimeCp[23],
696 pData->m_aCurrentTimeCp[24], pSqlServer->False());
697 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
698 {
699 return false;
700 }
701 pSqlServer->BindString(Idx: 1, pString: pData->m_aMap);
702 pSqlServer->BindString(Idx: 2, pString: pData->m_aName);
703 pSqlServer->BindString(Idx: 3, pString: pData->m_aTimestamp);
704 pSqlServer->BindString(Idx: 4, pString: g_Config.m_SvSqlServerName);
705 pSqlServer->BindString(Idx: 5, pString: pData->m_aGameUuid);
706 pSqlServer->Print();
707 int NumInserted;
708 return pSqlServer->ExecuteUpdate(pNumUpdated: &NumInserted, pError, ErrorSize);
709}
710
711bool CScoreWorker::SaveTeamScore(IDbConnection *pSqlServer, const ISqlData *pGameData, Write w, char *pError, int ErrorSize)
712{
713 const auto *pData = dynamic_cast<const CSqlTeamScoreData *>(pGameData);
714
715 char aBuf[512];
716
717 if(w == Write::NORMAL_SUCCEEDED)
718 {
719 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
720 format: "DELETE FROM %s_teamrace_backup WHERE Id=?",
721 pSqlServer->GetPrefix());
722 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
723 {
724 return false;
725 }
726
727 // copy uuid, because mysql BindBlob doesn't support const buffers
728 CUuid TeamrankId = pData->m_TeamrankUuid;
729 pSqlServer->BindBlob(Idx: 1, pBlob: TeamrankId.m_aData, Size: sizeof(TeamrankId.m_aData));
730 pSqlServer->Print();
731 int NumDeleted;
732 if(!pSqlServer->ExecuteUpdate(pNumUpdated: &NumDeleted, pError, ErrorSize))
733 {
734 return false;
735 }
736 if(NumDeleted == 0)
737 {
738 log_warn("sql", "Teamrank got moved out of backup database, will show up as duplicate teamrank in MySQL");
739 }
740 return true;
741 }
742 if(w == Write::NORMAL_FAILED)
743 {
744 int NumInserted;
745 CUuid TeamrankId = pData->m_TeamrankUuid;
746
747 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
748 format: "INSERT INTO %s_teamrace SELECT * FROM %s_teamrace_backup WHERE Id=?",
749 pSqlServer->GetPrefix(), pSqlServer->GetPrefix());
750 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
751 {
752 return false;
753 }
754 pSqlServer->BindBlob(Idx: 1, pBlob: TeamrankId.m_aData, Size: sizeof(TeamrankId.m_aData));
755 pSqlServer->Print();
756 if(!pSqlServer->ExecuteUpdate(pNumUpdated: &NumInserted, pError, ErrorSize))
757 {
758 return false;
759 }
760
761 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
762 format: "DELETE FROM %s_teamrace_backup WHERE Id=?",
763 pSqlServer->GetPrefix());
764 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
765 {
766 return false;
767 }
768 pSqlServer->BindBlob(Idx: 1, pBlob: TeamrankId.m_aData, Size: sizeof(TeamrankId.m_aData));
769 pSqlServer->Print();
770 return pSqlServer->ExecuteUpdate(pNumUpdated: &NumInserted, pError, ErrorSize);
771 }
772
773 if(w == Write::NORMAL)
774 {
775 // get the names sorted in a tab separated string
776 std::vector<std::string> vNames;
777 vNames.reserve(n: pData->m_Size);
778 for(unsigned int i = 0; i < pData->m_Size; i++)
779 vNames.emplace_back(args: pData->m_aaNames[i]);
780
781 std::sort(first: vNames.begin(), last: vNames.end());
782 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
783 format: "SELECT l.Id, Name, Time "
784 "FROM (" // preselect teams with first name in team
785 " SELECT ID "
786 " FROM %s_teamrace "
787 " WHERE Map = ? AND Name = ? AND DDNet7 = %s"
788 ") as l INNER JOIN %s_teamrace AS r ON l.Id = r.Id "
789 "ORDER BY l.Id, Name COLLATE %s",
790 pSqlServer->GetPrefix(), pSqlServer->False(), pSqlServer->GetPrefix(), pSqlServer->BinaryCollate());
791 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
792 {
793 return false;
794 }
795 pSqlServer->BindString(Idx: 1, pString: pData->m_aMap);
796 pSqlServer->BindString(Idx: 2, pString: pData->m_aaNames[0]);
797
798 bool FoundTeam = false;
799 float Time;
800 CTeamrank Teamrank;
801 bool End;
802 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
803 {
804 return false;
805 }
806 if(!End)
807 {
808 bool SearchTeamEnd = false;
809 while(!SearchTeamEnd)
810 {
811 Time = pSqlServer->GetFloat(Col: 3);
812 if(!Teamrank.NextSqlResult(pSqlServer, pEnd: &SearchTeamEnd, pError, ErrorSize))
813 {
814 return false;
815 }
816 if(Teamrank.SamePlayers(pvSortedNames: &vNames))
817 {
818 FoundTeam = true;
819 break;
820 }
821 }
822 }
823 if(FoundTeam)
824 {
825 dbg_msg(sys: "sql", fmt: "found team rank from same team (old time: %f, new time: %f)", Time, pData->m_Time);
826 if(pData->m_Time < Time)
827 {
828 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
829 format: "UPDATE %s_teamrace SET Time=%.2f, Timestamp=%s, DDNet7=%s, GameId=? WHERE Id = ?",
830 pSqlServer->GetPrefix(), pData->m_Time, pSqlServer->InsertTimestampAsUtc(), pSqlServer->False());
831 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
832 {
833 return false;
834 }
835 pSqlServer->BindString(Idx: 1, pString: pData->m_aTimestamp);
836 pSqlServer->BindString(Idx: 2, pString: pData->m_aGameUuid);
837 pSqlServer->BindBlob(Idx: 3, pBlob: Teamrank.m_TeamId.m_aData, Size: sizeof(Teamrank.m_TeamId.m_aData));
838 pSqlServer->Print();
839 int NumUpdated;
840 if(!pSqlServer->ExecuteUpdate(pNumUpdated: &NumUpdated, pError, ErrorSize))
841 {
842 return false;
843 }
844 // return error if we didn't update any rows
845 return NumUpdated != 0;
846 }
847 return true;
848 }
849 }
850
851 for(unsigned int i = 0; i < pData->m_Size; i++)
852 {
853 // if no entry found... create a new one
854 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
855 format: "%s INTO %s_teamrace%s(Map, Name, Timestamp, Time, Id, GameId, DDNet7) "
856 "VALUES (?, ?, %s, %.2f, ?, ?, %s)",
857 pSqlServer->InsertIgnore(), pSqlServer->GetPrefix(),
858 w == Write::NORMAL ? "" : "_backup",
859 pSqlServer->InsertTimestampAsUtc(), pData->m_Time, pSqlServer->False());
860 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
861 {
862 return false;
863 }
864 pSqlServer->BindString(Idx: 1, pString: pData->m_aMap);
865 pSqlServer->BindString(Idx: 2, pString: pData->m_aaNames[i]);
866 pSqlServer->BindString(Idx: 3, pString: pData->m_aTimestamp);
867 // copy uuid, because mysql BindBlob doesn't support const buffers
868 CUuid TeamrankId = pData->m_TeamrankUuid;
869 pSqlServer->BindBlob(Idx: 4, pBlob: TeamrankId.m_aData, Size: sizeof(TeamrankId.m_aData));
870 pSqlServer->BindString(Idx: 5, pString: pData->m_aGameUuid);
871 pSqlServer->Print();
872 int NumInserted;
873 if(!pSqlServer->ExecuteUpdate(pNumUpdated: &NumInserted, pError, ErrorSize))
874 {
875 return false;
876 }
877 }
878 return true;
879}
880
881bool CScoreWorker::ShowRank(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize)
882{
883 const auto *pData = dynamic_cast<const CSqlPlayerRequest *>(pGameData);
884 auto *pResult = dynamic_cast<CScorePlayerResult *>(pGameData->m_pResult.get());
885
886 char aServerLike[16];
887 str_format(buffer: aServerLike, buffer_size: sizeof(aServerLike), format: "%%%s%%", pData->m_aServer);
888
889 // Counting how many players are faster than the requesting player is cheaper
890 // than ranking every player with a window function. Best holds the best time
891 // of every player, Own the best time of the requesting player.
892 char aBuf[1024];
893 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
894 format: "SELECT SUM(Best.Time < Own.Time) + 1 AS Ranking, Own.Time AS Time, "
895 " CASE WHEN COUNT(*) > 1 THEN SUM(Best.Time < Own.Time) * 1.0 / (COUNT(*) - 1) ELSE 0 END AS PercentRank "
896 "FROM ("
897 " SELECT MIN(Time) AS Time FROM %s_race WHERE Map = ? AND Server LIKE ? GROUP BY Name"
898 ") AS Best "
899 "CROSS JOIN ("
900 " SELECT MIN(Time) AS Time FROM %s_race WHERE Map = ? AND Server LIKE ? AND Name = ?"
901 ") AS Own "
902 "WHERE Own.Time IS NOT NULL "
903 "GROUP BY Own.Time",
904 pSqlServer->GetPrefix(), pSqlServer->GetPrefix());
905
906 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
907 {
908 return false;
909 }
910 pSqlServer->BindString(Idx: 1, pString: pData->m_aMap);
911 pSqlServer->BindString(Idx: 2, pString: aServerLike);
912 pSqlServer->BindString(Idx: 3, pString: pData->m_aMap);
913 pSqlServer->BindString(Idx: 4, pString: aServerLike);
914 pSqlServer->BindString(Idx: 5, pString: pData->m_aName);
915
916 bool End;
917 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
918 {
919 return false;
920 }
921
922 char aRegionalRank[16];
923 if(End)
924 {
925 str_copy(dst&: aRegionalRank, src: "unranked");
926 }
927 else
928 {
929 str_format(buffer: aRegionalRank, buffer_size: sizeof(aRegionalRank), format: "rank %d", pSqlServer->GetInt(Col: 1));
930 }
931
932 const char *pAny = "%";
933
934 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
935 {
936 return false;
937 }
938 pSqlServer->BindString(Idx: 1, pString: pData->m_aMap);
939 pSqlServer->BindString(Idx: 2, pString: pAny);
940 pSqlServer->BindString(Idx: 3, pString: pData->m_aMap);
941 pSqlServer->BindString(Idx: 4, pString: pAny);
942 pSqlServer->BindString(Idx: 5, pString: pData->m_aName);
943
944 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
945 {
946 return false;
947 }
948
949 if(!End)
950 {
951 int Rank = pSqlServer->GetInt(Col: 1);
952 float Time = pSqlServer->GetFloat(Col: 2);
953 str_time_float(secs: Time, format: ETimeFormat::HOURS_CENTISECS, buffer: aBuf, buffer_size: sizeof(aBuf));
954
955 if(g_Config.m_SvHideScore)
956 {
957 str_format(buffer: pResult->m_Data.m_aaMessages[0], buffer_size: sizeof(pResult->m_Data.m_aaMessages[0]),
958 format: "Your time: %s", aBuf);
959 }
960 else
961 {
962 pResult->m_MessageKind = CScorePlayerResult::ALL;
963 // CEIL and FLOOR are not supported in SQLite
964 int BetterThanPercent = std::floor(x: 100.0f - 100.0f * pSqlServer->GetFloat(Col: 3));
965
966 if(str_comp_nocase(a: pData->m_aRequestingPlayer, b: pData->m_aName) == 0)
967 {
968 str_format(buffer: pResult->m_Data.m_aaMessages[0], buffer_size: sizeof(pResult->m_Data.m_aaMessages[0]),
969 format: "%s - %s - better than %d%%",
970 pData->m_aName, aBuf, BetterThanPercent);
971 }
972 else
973 {
974 str_format(buffer: pResult->m_Data.m_aaMessages[0], buffer_size: sizeof(pResult->m_Data.m_aaMessages[0]),
975 format: "%s - %s - better than %d%% - requested by %s",
976 pData->m_aName, aBuf, BetterThanPercent, pData->m_aRequestingPlayer);
977 }
978
979 if(g_Config.m_SvRegionalRankings)
980 {
981 str_format(buffer: pResult->m_Data.m_aaMessages[1], buffer_size: sizeof(pResult->m_Data.m_aaMessages[1]),
982 format: "Global rank %d - %s %s",
983 Rank, pData->m_aServer, aRegionalRank);
984 }
985 else
986 {
987 str_format(buffer: pResult->m_Data.m_aaMessages[1], buffer_size: sizeof(pResult->m_Data.m_aaMessages[1]),
988 format: "Global rank %d", Rank);
989 }
990 }
991 }
992 else
993 {
994 str_format(buffer: pResult->m_Data.m_aaMessages[0], buffer_size: sizeof(pResult->m_Data.m_aaMessages[0]),
995 format: "%s is not ranked", pData->m_aName);
996 }
997 return true;
998}
999
1000bool CScoreWorker::ShowTeamRank(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize)
1001{
1002 const auto *pData = dynamic_cast<const CSqlPlayerRequest *>(pGameData);
1003 auto *pResult = dynamic_cast<CScorePlayerResult *>(pGameData->m_pResult.get());
1004
1005 // check sort method
1006 char aBuf[2400];
1007
1008 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1009 format: "SELECT l.Id, Name, Time, Ranking, PercentRank "
1010 "FROM (" // teamrank score board
1011 " SELECT RANK() OVER w AS Ranking, PERCENT_RANK() OVER w AS PercentRank, Id "
1012 " FROM %s_teamrace "
1013 " WHERE Map = ? "
1014 " GROUP BY ID "
1015 " WINDOW w AS (ORDER BY Min(Time))"
1016 ") AS TeamRank INNER JOIN (" // select rank with Name in team
1017 " SELECT ID "
1018 " FROM %s_teamrace "
1019 " WHERE Map = ? AND Name = ? "
1020 " ORDER BY Time "
1021 " LIMIT 1"
1022 ") AS l ON TeamRank.Id = l.Id "
1023 "INNER JOIN %s_teamrace AS r ON l.Id = r.Id ",
1024 pSqlServer->GetPrefix(), pSqlServer->GetPrefix(), pSqlServer->GetPrefix());
1025 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1026 {
1027 return false;
1028 }
1029 pSqlServer->BindString(Idx: 1, pString: pData->m_aMap);
1030 pSqlServer->BindString(Idx: 2, pString: pData->m_aMap);
1031 pSqlServer->BindString(Idx: 3, pString: pData->m_aName);
1032
1033 bool End;
1034 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
1035 {
1036 return false;
1037 }
1038 if(!End)
1039 {
1040 float Time = pSqlServer->GetFloat(Col: 3);
1041 str_time_float(secs: Time, format: ETimeFormat::HOURS_CENTISECS, buffer: aBuf, buffer_size: sizeof(aBuf));
1042 int Rank = pSqlServer->GetInt(Col: 4);
1043 // CEIL and FLOOR are not supported in SQLite
1044 int BetterThanPercent = std::floor(x: 100.0f - 100.0f * pSqlServer->GetFloat(Col: 5));
1045 CTeamrank Teamrank;
1046 if(!Teamrank.NextSqlResult(pSqlServer, pEnd: &End, pError, ErrorSize))
1047 {
1048 return false;
1049 }
1050
1051 if(g_Config.m_SvHideScore)
1052 {
1053 str_format(buffer: pResult->m_Data.m_aaMessages[0], buffer_size: sizeof(pResult->m_Data.m_aaMessages[0]),
1054 format: "Your team time: %s, better than %d%%", aBuf, BetterThanPercent);
1055 }
1056 else
1057 {
1058 pResult->m_MessageKind = CScorePlayerResult::ALL;
1059
1060 // format without names to measure remaining space for names
1061 const int EmptyLength = str_format(buffer: pResult->m_Data.m_aaMessages[0], buffer_size: sizeof(pResult->m_Data.m_aaMessages[0]),
1062 format: "%d. %s Team time: %s, better than %d%%, requested by %s",
1063 Rank, "", aBuf, BetterThanPercent, pData->m_aRequestingPlayer);
1064 char aFormattedNames[MAX_TEAM_NAMES_LENGTH];
1065 Teamrank.FormatNames(pBuf: aFormattedNames, BufSize: MAX_CHAT_LENGTH - EmptyLength);
1066 str_format(buffer: pResult->m_Data.m_aaMessages[0], buffer_size: sizeof(pResult->m_Data.m_aaMessages[0]),
1067 format: "%d. %s Team time: %s, better than %d%%, requested by %s",
1068 Rank, aFormattedNames, aBuf, BetterThanPercent, pData->m_aRequestingPlayer);
1069 }
1070 }
1071 else
1072 {
1073 str_format(buffer: pResult->m_Data.m_aaMessages[0], buffer_size: sizeof(pResult->m_Data.m_aaMessages[0]),
1074 format: "%s has no team ranks", pData->m_aName);
1075 }
1076 return true;
1077}
1078
1079bool CScoreWorker::ShowTop(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize)
1080{
1081 const auto *pData = dynamic_cast<const CSqlPlayerRequest *>(pGameData);
1082 auto *pResult = dynamic_cast<CScorePlayerResult *>(pGameData->m_pResult.get());
1083
1084 int LimitStart = std::max(a: absolute(a: pData->m_Offset) - 1, b: 0);
1085 const char *pAny = "%";
1086
1087 const bool Ascending = pData->m_Offset >= 0;
1088 char aBuf[1024];
1089 if(Ascending)
1090 {
1091 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1092 format: "SELECT Name, Time, RANK() OVER (ORDER BY Time) AS Ranking "
1093 "FROM ("
1094 " SELECT r1.Name AS Name, r1.Time AS Time "
1095 " FROM %s_race r1 "
1096 " WHERE r1.Map = ? AND r1.Server LIKE ? AND NOT EXISTS ("
1097 " SELECT 1 FROM %s_race r2 "
1098 " WHERE r2.Map = r1.Map AND r2.Name = r1.Name AND r2.Server LIKE ? "
1099 " AND (r2.Time, r2.Timestamp, r2.Server) < (r1.Time, r1.Timestamp, r1.Server)) "
1100 " ORDER BY r1.Time LIMIT %d"
1101 ") AS a "
1102 "ORDER BY Ranking ASC LIMIT %d, ?",
1103 pSqlServer->GetPrefix(), pSqlServer->GetPrefix(), LimitStart + 5, LimitStart);
1104 }
1105 else
1106 {
1107 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1108 format: "SELECT Name, Time, Ranking "
1109 "FROM ("
1110 " SELECT RANK() OVER w AS Ranking, MIN(Time) AS Time, Name "
1111 " FROM %s_race "
1112 " WHERE Map = ? "
1113 " AND Server LIKE ? "
1114 " GROUP BY Name "
1115 " WINDOW w AS (ORDER BY MIN(Time))"
1116 ") as a "
1117 "ORDER BY Ranking DESC "
1118 "LIMIT %d, ?",
1119 pSqlServer->GetPrefix(),
1120 LimitStart);
1121 }
1122
1123 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1124 {
1125 return false;
1126 }
1127 pSqlServer->BindString(Idx: 1, pString: pData->m_aMap);
1128 pSqlServer->BindString(Idx: 2, pString: pAny);
1129 if(Ascending)
1130 {
1131 pSqlServer->BindString(Idx: 3, pString: pAny);
1132 pSqlServer->BindInt(Idx: 4, Value: 5);
1133 }
1134 else
1135 {
1136 pSqlServer->BindInt(Idx: 3, Value: 5);
1137 }
1138
1139 // show top
1140 int Line = 0;
1141 str_copy(dst&: pResult->m_Data.m_aaMessages[Line], src: "------------ Global Top ------------");
1142 Line++;
1143
1144 char aTime[32];
1145 bool End = false;
1146
1147 while(pSqlServer->Step(pEnd: &End, pError, ErrorSize) && !End)
1148 {
1149 char aName[MAX_NAME_LENGTH];
1150 pSqlServer->GetString(Col: 1, pBuffer: aName, BufferSize: sizeof(aName));
1151 float Time = pSqlServer->GetFloat(Col: 2);
1152 str_time_float(secs: Time, format: ETimeFormat::HOURS_CENTISECS, buffer: aTime, buffer_size: sizeof(aTime));
1153 int Rank = pSqlServer->GetInt(Col: 3);
1154 str_format(buffer: pResult->m_Data.m_aaMessages[Line], buffer_size: sizeof(pResult->m_Data.m_aaMessages[Line]),
1155 format: "%d. %s Time: %s", Rank, aName, aTime);
1156
1157 Line++;
1158 }
1159
1160 if(!g_Config.m_SvRegionalRankings)
1161 {
1162 str_copy(dst&: pResult->m_Data.m_aaMessages[Line], src: "-----------------------------------------");
1163 return End;
1164 }
1165
1166 char aServerLike[16];
1167 str_format(buffer: aServerLike, buffer_size: sizeof(aServerLike), format: "%%%s%%", pData->m_aServer);
1168
1169 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1170 {
1171 return false;
1172 }
1173 pSqlServer->BindString(Idx: 1, pString: pData->m_aMap);
1174 pSqlServer->BindString(Idx: 2, pString: aServerLike);
1175 if(Ascending)
1176 {
1177 pSqlServer->BindString(Idx: 3, pString: aServerLike);
1178 pSqlServer->BindInt(Idx: 4, Value: 3);
1179 }
1180 else
1181 {
1182 pSqlServer->BindInt(Idx: 3, Value: 3);
1183 }
1184
1185 str_format(buffer: pResult->m_Data.m_aaMessages[Line], buffer_size: sizeof(pResult->m_Data.m_aaMessages[Line]),
1186 format: "------------ %s Top ------------", pData->m_aServer);
1187 Line++;
1188
1189 // show top
1190 while(pSqlServer->Step(pEnd: &End, pError, ErrorSize) && !End)
1191 {
1192 char aName[MAX_NAME_LENGTH];
1193 pSqlServer->GetString(Col: 1, pBuffer: aName, BufferSize: sizeof(aName));
1194 float Time = pSqlServer->GetFloat(Col: 2);
1195 str_time_float(secs: Time, format: ETimeFormat::HOURS_CENTISECS, buffer: aTime, buffer_size: sizeof(aTime));
1196 int Rank = pSqlServer->GetInt(Col: 3);
1197 str_format(buffer: pResult->m_Data.m_aaMessages[Line], buffer_size: sizeof(pResult->m_Data.m_aaMessages[Line]),
1198 format: "%d. %s Time: %s", Rank, aName, aTime);
1199 Line++;
1200 }
1201
1202 return End;
1203}
1204
1205bool CScoreWorker::ShowTeamTop5(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize)
1206{
1207 const auto *pData = dynamic_cast<const CSqlPlayerRequest *>(pGameData);
1208 auto *pResult = dynamic_cast<CScorePlayerResult *>(pGameData->m_pResult.get());
1209 auto *paMessages = pResult->m_Data.m_aaMessages;
1210
1211 int LimitStart = std::max(a: absolute(a: pData->m_Offset) - 1, b: 0);
1212 const char *pOrder = pData->m_Offset >= 0 ? "ASC" : "DESC";
1213 const char *pAny = "%";
1214
1215 // check sort method
1216 char aBuf[1024];
1217
1218 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1219 format: "SELECT Name, Time, Ranking, TeamSize "
1220 "FROM ("
1221 " SELECT TeamSize, Ranking, Id, Server "
1222 " FROM (" // teamrank score board
1223 " SELECT RANK() OVER w AS Ranking, COUNT(*) AS Teamsize, Id, Server "
1224 " FROM ("
1225 " SELECT tr.Map, tr.Time, tr.Id, ("
1226 " SELECT rr.Server FROM %s_race AS rr "
1227 " WHERE rr.Map = tr.Map AND rr.Name = tr.Name AND rr.Time = tr.Time "
1228 " LIMIT 1"
1229 " ) AS Server "
1230 " FROM %s_teamrace AS tr "
1231 " WHERE tr.Map = ? "
1232 " ) AS ll "
1233 " GROUP BY ID "
1234 " WINDOW w AS (ORDER BY Min(Time))"
1235 " ) as l1 "
1236 " WHERE Server LIKE ? "
1237 " ORDER BY Ranking %s "
1238 " LIMIT %d, ?"
1239 ") as l2 "
1240 "INNER JOIN %s_teamrace as r ON l2.Id = r.Id "
1241 "ORDER BY Ranking %s, r.Id, Name ASC",
1242 pSqlServer->GetPrefix(), pSqlServer->GetPrefix(), pOrder, LimitStart, pSqlServer->GetPrefix(), pOrder);
1243 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1244 {
1245 return false;
1246 }
1247 pSqlServer->BindString(Idx: 1, pString: pData->m_aMap);
1248 pSqlServer->BindString(Idx: 2, pString: pAny);
1249 pSqlServer->BindInt(Idx: 3, Value: 5);
1250
1251 int Line = 0;
1252 str_copy(dst: paMessages[Line++], src: "------- Team Top 5 -------", dst_size: sizeof(paMessages[Line]));
1253
1254 bool End;
1255 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
1256 {
1257 return false;
1258 }
1259 if(!End)
1260 {
1261 if(!CTeamrank::GetSqlTop5Team(pSqlServer, pEnd: &End, pError, ErrorSize, paMessages, Line: &Line, Count: 5))
1262 {
1263 return false;
1264 }
1265 }
1266
1267 if(!g_Config.m_SvRegionalRankings)
1268 {
1269 str_copy(dst&: paMessages[Line], src: "-------------------------------");
1270 return true;
1271 }
1272
1273 char aServerLike[16];
1274 str_format(buffer: aServerLike, buffer_size: sizeof(aServerLike), format: "%%%s%%", pData->m_aServer);
1275
1276 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1277 {
1278 return false;
1279 }
1280 pSqlServer->BindString(Idx: 1, pString: pData->m_aMap);
1281 pSqlServer->BindString(Idx: 2, pString: aServerLike);
1282 pSqlServer->BindInt(Idx: 3, Value: 3);
1283
1284 str_format(buffer: pResult->m_Data.m_aaMessages[Line], buffer_size: sizeof(pResult->m_Data.m_aaMessages[Line]),
1285 format: "----- %s Team Top -----", pData->m_aServer);
1286 Line++;
1287
1288 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
1289 {
1290 return false;
1291 }
1292 if(!End)
1293 {
1294 if(!CTeamrank::GetSqlTop5Team(pSqlServer, pEnd: &End, pError, ErrorSize, paMessages, Line: &Line, Count: 3))
1295 {
1296 return false;
1297 }
1298 }
1299 return true;
1300}
1301
1302bool CScoreWorker::ShowPlayerTeamTop5(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize)
1303{
1304 const auto *pData = dynamic_cast<const CSqlPlayerRequest *>(pGameData);
1305 auto *pResult = dynamic_cast<CScorePlayerResult *>(pGameData->m_pResult.get());
1306 auto *paMessages = pResult->m_Data.m_aaMessages;
1307
1308 int LimitStart = std::max(a: absolute(a: pData->m_Offset) - 1, b: 0);
1309 const char *pOrder = pData->m_Offset >= 0 ? "ASC" : "DESC";
1310
1311 // check sort method
1312 char aBuf[2400];
1313
1314 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1315 format: "SELECT l.Id, Name, Time, Ranking "
1316 "FROM (" // teamrank score board
1317 " SELECT RANK() OVER w AS Ranking, Id "
1318 " FROM %s_teamrace "
1319 " WHERE Map = ? "
1320 " GROUP BY ID "
1321 " WINDOW w AS (ORDER BY Min(Time))"
1322 ") AS TeamRank INNER JOIN (" // select rank with Name in team
1323 " SELECT ID "
1324 " FROM %s_teamrace "
1325 " WHERE Map = ? AND Name = ? "
1326 " ORDER BY Time %s "
1327 " LIMIT %d, 5 "
1328 ") AS l ON TeamRank.Id = l.Id "
1329 "INNER JOIN %s_teamrace AS r ON l.Id = r.Id "
1330 "ORDER BY Time %s, l.Id, Name ASC",
1331 pSqlServer->GetPrefix(), pSqlServer->GetPrefix(), pOrder, LimitStart, pSqlServer->GetPrefix(), pOrder);
1332 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1333 {
1334 return false;
1335 }
1336 pSqlServer->BindString(Idx: 1, pString: pData->m_aMap);
1337 pSqlServer->BindString(Idx: 2, pString: pData->m_aMap);
1338 pSqlServer->BindString(Idx: 3, pString: pData->m_aName);
1339
1340 bool End;
1341 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
1342 {
1343 return false;
1344 }
1345 if(!End)
1346 {
1347 // show teamtop5
1348 int Line = 0;
1349 str_copy(dst: paMessages[Line++], src: "------- Team Top 5 -------", dst_size: sizeof(paMessages[Line]));
1350
1351 for(Line = 1; Line < 6; Line++) // print
1352 {
1353 float Time = pSqlServer->GetFloat(Col: 3);
1354 str_time_float(secs: Time, format: ETimeFormat::HOURS_CENTISECS, buffer: aBuf, buffer_size: sizeof(aBuf));
1355 int Rank = pSqlServer->GetInt(Col: 4);
1356 CTeamrank Teamrank;
1357 bool Last;
1358 if(!Teamrank.NextSqlResult(pSqlServer, pEnd: &Last, pError, ErrorSize))
1359 {
1360 return false;
1361 }
1362
1363 Teamrank.FormatTeamTopLine(pMessage: paMessages[Line], MessageSize: sizeof(paMessages[Line]), Rank, pTime: aBuf);
1364 if(Last)
1365 {
1366 Line++;
1367 break;
1368 }
1369 }
1370 str_copy(dst&: paMessages[Line], src: "---------------------------------");
1371 }
1372 else
1373 {
1374 if(pData->m_Offset == 0)
1375 str_format(buffer: paMessages[0], buffer_size: sizeof(paMessages[0]), format: "%s has no team ranks", pData->m_aName);
1376 else
1377 str_format(buffer: paMessages[0], buffer_size: sizeof(paMessages[0]), format: "%s has no team ranks in the specified range", pData->m_aName);
1378 }
1379 return true;
1380}
1381
1382bool CScoreWorker::ShowTimes(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize)
1383{
1384 const auto *pData = dynamic_cast<const CSqlPlayerRequest *>(pGameData);
1385 auto *pResult = dynamic_cast<CScorePlayerResult *>(pGameData->m_pResult.get());
1386 auto *paMessages = pResult->m_Data.m_aaMessages;
1387
1388 int LimitStart = std::max(a: absolute(a: pData->m_Offset) - 1, b: 0);
1389 const char *pOrder = pData->m_Offset >= 0 ? "DESC" : "ASC";
1390
1391 char aCurrentTimestamp[512];
1392 pSqlServer->ToUnixTimestamp(pTimestamp: "CURRENT_TIMESTAMP", aBuf: aCurrentTimestamp, BufferSize: sizeof(aCurrentTimestamp));
1393 char aTimestamp[512];
1394 pSqlServer->ToUnixTimestamp(pTimestamp: "Timestamp", aBuf: aTimestamp, BufferSize: sizeof(aTimestamp));
1395 char aBuf[512];
1396 if(pData->m_aName[0] != '\0') // last 5 times of a player
1397 {
1398 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1399 format: "SELECT Time, (%s-%s) as Ago, %s as Stamp, Server "
1400 "FROM %s_race "
1401 "WHERE Map = ? AND Name = ? "
1402 "ORDER BY Timestamp %s "
1403 "LIMIT ?, 5",
1404 aCurrentTimestamp, aTimestamp, aTimestamp,
1405 pSqlServer->GetPrefix(), pOrder);
1406 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1407 {
1408 return false;
1409 }
1410 pSqlServer->BindString(Idx: 1, pString: pData->m_aMap);
1411 pSqlServer->BindString(Idx: 2, pString: pData->m_aName);
1412 pSqlServer->BindInt(Idx: 3, Value: LimitStart);
1413 }
1414 else // last 5 times of server
1415 {
1416 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1417 format: "SELECT Time, (%s-%s) as Ago, %s as Stamp, Server, Name "
1418 "FROM %s_race "
1419 "WHERE Map = ? "
1420 "ORDER BY Timestamp %s "
1421 "LIMIT ?, 5",
1422 aCurrentTimestamp, aTimestamp, aTimestamp,
1423 pSqlServer->GetPrefix(), pOrder);
1424 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1425 {
1426 return false;
1427 }
1428 pSqlServer->BindString(Idx: 1, pString: pData->m_aMap);
1429 pSqlServer->BindInt(Idx: 2, Value: LimitStart);
1430 }
1431
1432 // show top5
1433 bool End;
1434 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
1435 {
1436 return false;
1437 }
1438 if(End)
1439 {
1440 str_copy(dst&: paMessages[0], src: "There are no times in the specified range");
1441 return true;
1442 }
1443
1444 str_copy(dst&: paMessages[0], src: "------------- Last Times -------------");
1445 int Line = 1;
1446
1447 do
1448 {
1449 float Time = pSqlServer->GetFloat(Col: 1);
1450 str_time_float(secs: Time, format: ETimeFormat::HOURS_CENTISECS, buffer: aBuf, buffer_size: sizeof(aBuf));
1451 int Ago = pSqlServer->GetInt(Col: 2);
1452 int Stamp = pSqlServer->GetInt(Col: 3);
1453 char aServer[5];
1454 pSqlServer->GetString(Col: 4, pBuffer: aServer, BufferSize: sizeof(aServer));
1455 char aServerFormatted[8] = "\0";
1456 if(str_comp(a: aServer, b: "UNK") != 0)
1457 str_format(buffer: aServerFormatted, buffer_size: sizeof(aServerFormatted), format: "[%s] ", aServer);
1458
1459 char aAgoString[40] = "\0";
1460 sqlstr::AgoTimeToString(AgoTime: Ago, pAgoString: aAgoString, Size: sizeof(aAgoString));
1461
1462 if(pData->m_aName[0] != '\0') // last 5 times of a player
1463 {
1464 if(Stamp == 0) // stamp is 00:00:00 cause it's an old entry from old times where there where no stamps yet
1465 str_format(buffer: paMessages[Line], buffer_size: sizeof(paMessages[Line]),
1466 format: "%s%s, don't know how long ago", aServerFormatted, aBuf);
1467 else
1468 str_format(buffer: paMessages[Line], buffer_size: sizeof(paMessages[Line]),
1469 format: "%s%s ago, %s", aServerFormatted, aAgoString, aBuf);
1470 }
1471 else // last 5 times of the server
1472 {
1473 char aName[MAX_NAME_LENGTH];
1474 pSqlServer->GetString(Col: 5, pBuffer: aName, BufferSize: sizeof(aName));
1475 if(Stamp == 0) // stamp is 00:00:00 cause it's an old entry from old times where there where no stamps yet
1476 {
1477 str_format(buffer: paMessages[Line], buffer_size: sizeof(paMessages[Line]),
1478 format: "%s%s, %s, don't know when", aServerFormatted, aName, aBuf);
1479 }
1480 else
1481 {
1482 str_format(buffer: paMessages[Line], buffer_size: sizeof(paMessages[Line]),
1483 format: "%s%s, %s ago, %s", aServerFormatted, aName, aAgoString, aBuf);
1484 }
1485 }
1486 Line++;
1487 } while(pSqlServer->Step(pEnd: &End, pError, ErrorSize) && !End);
1488 if(!End)
1489 {
1490 return false;
1491 }
1492 str_copy(dst&: paMessages[Line], src: "-------------------------------------------");
1493
1494 return true;
1495}
1496
1497bool CScoreWorker::ShowPoints(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize)
1498{
1499 const auto *pData = dynamic_cast<const CSqlPlayerRequest *>(pGameData);
1500 auto *pResult = dynamic_cast<CScorePlayerResult *>(pGameData->m_pResult.get());
1501 auto *paMessages = pResult->m_Data.m_aaMessages;
1502
1503 char aBuf[512];
1504 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1505 format: "SELECT ("
1506 " SELECT COUNT(Name) + 1 FROM %s_points WHERE Points > ("
1507 " SELECT Points FROM %s_points WHERE Name = ?"
1508 ")) as Ranking, Points, Name "
1509 "FROM %s_points WHERE Name = ?",
1510 pSqlServer->GetPrefix(), pSqlServer->GetPrefix(), pSqlServer->GetPrefix());
1511 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1512 {
1513 return false;
1514 }
1515 pSqlServer->BindString(Idx: 1, pString: pData->m_aName);
1516 pSqlServer->BindString(Idx: 2, pString: pData->m_aName);
1517
1518 bool End;
1519 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
1520 {
1521 return false;
1522 }
1523 if(!End)
1524 {
1525 int Rank = pSqlServer->GetInt(Col: 1);
1526 int Count = pSqlServer->GetInt(Col: 2);
1527 char aName[MAX_NAME_LENGTH];
1528 pSqlServer->GetString(Col: 3, pBuffer: aName, BufferSize: sizeof(aName));
1529 pResult->m_MessageKind = CScorePlayerResult::ALL;
1530 str_format(buffer: paMessages[0], buffer_size: sizeof(paMessages[0]),
1531 format: "%d. %s Points: %d, requested by %s",
1532 Rank, aName, Count, pData->m_aRequestingPlayer);
1533 }
1534 else
1535 {
1536 str_format(buffer: paMessages[0], buffer_size: sizeof(paMessages[0]),
1537 format: "%s has not collected any points so far", pData->m_aName);
1538 }
1539 return true;
1540}
1541
1542bool CScoreWorker::ShowTopPoints(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize)
1543{
1544 const auto *pData = dynamic_cast<const CSqlPlayerRequest *>(pGameData);
1545 auto *pResult = dynamic_cast<CScorePlayerResult *>(pGameData->m_pResult.get());
1546 auto *paMessages = pResult->m_Data.m_aaMessages;
1547
1548 int LimitStart = std::max(a: pData->m_Offset - 1, b: 0);
1549
1550 char aBuf[512];
1551 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1552 format: "SELECT RANK() OVER (ORDER BY a.Points DESC) as Ranking, Points, Name "
1553 "FROM ("
1554 " SELECT Points, Name "
1555 " FROM %s_points "
1556 " ORDER BY Points DESC LIMIT ?"
1557 ") as a "
1558 "ORDER BY Ranking ASC, Name ASC LIMIT ?, 5",
1559 pSqlServer->GetPrefix());
1560 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1561 {
1562 return false;
1563 }
1564 pSqlServer->BindInt(Idx: 1, Value: LimitStart + 5);
1565 pSqlServer->BindInt(Idx: 2, Value: LimitStart);
1566
1567 // show top points
1568 str_copy(dst&: paMessages[0], src: "-------- Top Points --------");
1569
1570 bool End = false;
1571 int Line = 1;
1572 while(pSqlServer->Step(pEnd: &End, pError, ErrorSize) && !End)
1573 {
1574 int Rank = pSqlServer->GetInt(Col: 1);
1575 int Points = pSqlServer->GetInt(Col: 2);
1576 char aName[MAX_NAME_LENGTH];
1577 pSqlServer->GetString(Col: 3, pBuffer: aName, BufferSize: sizeof(aName));
1578 str_format(buffer: paMessages[Line], buffer_size: sizeof(paMessages[Line]),
1579 format: "%d. %s Points: %d", Rank, aName, Points);
1580 Line++;
1581 }
1582 if(!End)
1583 {
1584 return false;
1585 }
1586 str_copy(dst&: paMessages[Line], src: "-------------------------------");
1587
1588 return true;
1589}
1590
1591bool CScoreWorker::RandomMap(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize)
1592{
1593 const auto *pData = dynamic_cast<const CSqlRandomMapRequest *>(pGameData);
1594 auto *pResult = dynamic_cast<CScoreRandomMapResult *>(pGameData->m_pResult.get());
1595
1596 char aBuf[512];
1597 if(in_range(a: pData->m_MinStars, lower: 0, upper: 5) && in_range(a: pData->m_MaxStars, lower: 0, upper: 5))
1598 {
1599 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1600 format: "SELECT Map FROM %s_maps "
1601 "WHERE Server = ? AND Map != ? AND Stars BETWEEN ? AND ? "
1602 "ORDER BY %s LIMIT 1",
1603 pSqlServer->GetPrefix(), pSqlServer->Random());
1604 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1605 {
1606 return false;
1607 }
1608 pSqlServer->BindInt(Idx: 3, Value: pData->m_MinStars);
1609 pSqlServer->BindInt(Idx: 4, Value: pData->m_MaxStars);
1610 }
1611 else
1612 {
1613 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1614 format: "SELECT Map FROM %s_maps "
1615 "WHERE Server = ? AND Map != ? "
1616 "ORDER BY %s LIMIT 1",
1617 pSqlServer->GetPrefix(), pSqlServer->Random());
1618 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1619 {
1620 return false;
1621 }
1622 }
1623 pSqlServer->BindString(Idx: 1, pString: pData->m_aServerType);
1624 pSqlServer->BindString(Idx: 2, pString: pData->m_aCurrentMap);
1625
1626 bool End;
1627 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
1628 {
1629 return false;
1630 }
1631 if(!End)
1632 {
1633 pSqlServer->GetString(Col: 1, pBuffer: pResult->m_aMap, BufferSize: sizeof(pResult->m_aMap));
1634 }
1635 else
1636 {
1637 str_copy(dst&: pResult->m_aMessage, src: "No maps found on this server!");
1638 }
1639 return true;
1640}
1641
1642bool CScoreWorker::RandomUnfinishedMap(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize)
1643{
1644 const auto *pData = dynamic_cast<const CSqlRandomMapRequest *>(pGameData);
1645 auto *pResult = dynamic_cast<CScoreRandomMapResult *>(pGameData->m_pResult.get());
1646
1647 char aBuf[512];
1648 if(in_range(a: pData->m_MinStars, lower: 0, upper: 5) && in_range(a: pData->m_MaxStars, lower: 0, upper: 5))
1649 {
1650 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1651 format: "SELECT Map "
1652 "FROM %s_maps "
1653 "WHERE Server = ? AND Map != ? AND Stars BETWEEN ? AND ? AND Map NOT IN ("
1654 " SELECT Map "
1655 " FROM %s_race "
1656 " WHERE Name = ?"
1657 ") ORDER BY %s "
1658 "LIMIT 1",
1659 pSqlServer->GetPrefix(), pSqlServer->GetPrefix(), pSqlServer->Random());
1660 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1661 {
1662 return false;
1663 }
1664 pSqlServer->BindString(Idx: 1, pString: pData->m_aServerType);
1665 pSqlServer->BindString(Idx: 2, pString: pData->m_aCurrentMap);
1666 pSqlServer->BindInt(Idx: 3, Value: pData->m_MinStars);
1667 pSqlServer->BindInt(Idx: 4, Value: pData->m_MaxStars);
1668 pSqlServer->BindString(Idx: 5, pString: pData->m_aRequestingPlayer);
1669 }
1670 else
1671 {
1672 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1673 format: "SELECT Map "
1674 "FROM %s_maps AS maps "
1675 "WHERE Server = ? AND Map != ? AND Map NOT IN ("
1676 " SELECT Map "
1677 " FROM %s_race as race "
1678 " WHERE Name = ?"
1679 ") ORDER BY %s "
1680 "LIMIT 1",
1681 pSqlServer->GetPrefix(), pSqlServer->GetPrefix(), pSqlServer->Random());
1682 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1683 {
1684 return false;
1685 }
1686 pSqlServer->BindString(Idx: 1, pString: pData->m_aServerType);
1687 pSqlServer->BindString(Idx: 2, pString: pData->m_aCurrentMap);
1688 pSqlServer->BindString(Idx: 3, pString: pData->m_aRequestingPlayer);
1689 }
1690
1691 bool End;
1692 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
1693 {
1694 return false;
1695 }
1696 if(!End)
1697 {
1698 pSqlServer->GetString(Col: 1, pBuffer: pResult->m_aMap, BufferSize: sizeof(pResult->m_aMap));
1699 }
1700 else
1701 {
1702 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s has no more unfinished maps on this server!", pData->m_aRequestingPlayer);
1703 str_copy(dst&: pResult->m_aMessage, src: aBuf);
1704 }
1705 return true;
1706}
1707
1708bool CScoreWorker::SaveTeam(IDbConnection *pSqlServer, const ISqlData *pGameData, Write w, char *pError, int ErrorSize)
1709{
1710 const auto *pData = dynamic_cast<const CSqlTeamSaveData *>(pGameData);
1711 auto *pResult = dynamic_cast<CScoreSaveResult *>(pGameData->m_pResult.get());
1712
1713 if(w == Write::NORMAL_SUCCEEDED)
1714 {
1715 // write succeeded on mysql server. delete from sqlite again
1716 char aBuf[128] = {0};
1717 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1718 format: "DELETE FROM %s_saves_backup WHERE Code = ?",
1719 pSqlServer->GetPrefix());
1720 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1721 {
1722 return false;
1723 }
1724 pSqlServer->BindString(Idx: 1, pString: pData->m_aGeneratedCode);
1725 bool End;
1726 return pSqlServer->Step(pEnd: &End, pError, ErrorSize);
1727 }
1728 if(w == Write::NORMAL_FAILED)
1729 {
1730 char aBuf[256] = {0};
1731 bool End;
1732 // move to non-tmp table succeeded. delete from backup again
1733 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1734 format: "INSERT INTO %s_saves SELECT * FROM %s_saves_backup WHERE Code = ?",
1735 pSqlServer->GetPrefix(), pSqlServer->GetPrefix());
1736 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1737 {
1738 return false;
1739 }
1740 pSqlServer->BindString(Idx: 1, pString: pData->m_aGeneratedCode);
1741 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
1742 {
1743 return false;
1744 }
1745
1746 // move to non-tmp table succeeded. delete from backup again
1747 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1748 format: "DELETE FROM %s_saves_backup WHERE Code = ?",
1749 pSqlServer->GetPrefix());
1750 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1751 {
1752 return false;
1753 }
1754 pSqlServer->BindString(Idx: 1, pString: pData->m_aGeneratedCode);
1755 return pSqlServer->Step(pEnd: &End, pError, ErrorSize);
1756 }
1757
1758 char aSaveId[UUID_MAXSTRSIZE];
1759 FormatUuid(Uuid: pResult->m_SaveId, pBuffer: aSaveId, BufferLength: UUID_MAXSTRSIZE);
1760
1761 const char *pSaveState = pResult->m_SavedTeam.GetString();
1762 if(!pSaveState)
1763 {
1764 pResult->m_Status = CScoreSaveResult::SAVE_FAILED;
1765 str_copy(dst&: pResult->m_aMessage, src: "Your team is too large to save");
1766 return true;
1767 }
1768 char aBuf[65536];
1769
1770 dbg_msg(sys: "score/dbg", fmt: "code=%s failure=%d", pData->m_aCode, (int)w);
1771 bool UseGeneratedCode = pData->m_aCode[0] == '\0' || w != Write::NORMAL;
1772
1773 str_copy(dst&: pResult->m_aGeneratedCode, src: pData->m_aGeneratedCode);
1774 str_copy(dst&: pResult->m_aCode, src: UseGeneratedCode ? "" : pData->m_aCode);
1775
1776 bool Retry = false;
1777 // two tries, first use the user provided code, then the autogenerated
1778 do
1779 {
1780 Retry = false;
1781 char aCode[128] = {0};
1782 if(UseGeneratedCode)
1783 str_copy(dst&: aCode, src: pData->m_aGeneratedCode);
1784 else
1785 str_copy(dst&: aCode, src: pData->m_aCode);
1786
1787 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1788 format: "%s INTO %s_saves%s(Savegame, Map, Code, Timestamp, Server, SaveId, DDNet7) "
1789 "VALUES (?, ?, ?, CURRENT_TIMESTAMP, ?, ?, %s)",
1790 pSqlServer->InsertIgnore(), pSqlServer->GetPrefix(),
1791 w == Write::NORMAL ? "" : "_backup", pSqlServer->False());
1792 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1793 {
1794 return false;
1795 }
1796 pSqlServer->BindString(Idx: 1, pString: pSaveState);
1797 pSqlServer->BindString(Idx: 2, pString: pData->m_aMap);
1798 pSqlServer->BindString(Idx: 3, pString: aCode);
1799 pSqlServer->BindString(Idx: 4, pString: pData->m_aServer);
1800 pSqlServer->BindString(Idx: 5, pString: aSaveId);
1801 pSqlServer->Print();
1802 int NumInserted;
1803 if(!pSqlServer->ExecuteUpdate(pNumUpdated: &NumInserted, pError, ErrorSize))
1804 {
1805 return false;
1806 }
1807 if(NumInserted == 1)
1808 {
1809 pResult->m_Status = CScoreSaveResult::SAVE_SUCCESS;
1810 if(UseGeneratedCode)
1811 {
1812 pResult->m_aCode[0] = '\0';
1813 }
1814 if(w != Write::NORMAL)
1815 {
1816 if(str_comp(a: pData->m_aServer, b: g_Config.m_SvSqlServerName) == 0)
1817 {
1818 pResult->m_aServer[0] = '\0';
1819 }
1820 pResult->m_Status = CScoreSaveResult::SAVE_FALLBACKFILE;
1821 }
1822 }
1823 else if(!UseGeneratedCode)
1824 {
1825 UseGeneratedCode = true;
1826 Retry = true;
1827 }
1828 } while(Retry);
1829
1830 if(
1831 pResult->m_Status != CScoreSaveResult::SAVE_SUCCESS &&
1832 pResult->m_Status != CScoreSaveResult::SAVE_WARNING &&
1833 pResult->m_Status != CScoreSaveResult::SAVE_FALLBACKFILE)
1834 {
1835 dbg_msg(sys: "sql", fmt: "ERROR: This save-code already exists");
1836 pResult->m_Status = CScoreSaveResult::SAVE_FAILED;
1837 str_copy(dst&: pResult->m_aMessage, src: "This save-code already exists");
1838 }
1839 return true;
1840}
1841
1842bool CScoreWorker::LoadTeam(IDbConnection *pSqlServer, const ISqlData *pGameData, Write w, char *pError, int ErrorSize)
1843{
1844 if(w == Write::NORMAL_SUCCEEDED || w == Write::BACKUP_FIRST)
1845 return true;
1846 const auto *pData = dynamic_cast<const CSqlTeamLoadRequest *>(pGameData);
1847 auto *pResult = dynamic_cast<CScoreSaveResult *>(pGameData->m_pResult.get());
1848 pResult->m_Status = CScoreSaveResult::LOAD_FAILED;
1849
1850 char aCurrentTimestamp[512];
1851 pSqlServer->ToUnixTimestamp(pTimestamp: "CURRENT_TIMESTAMP", aBuf: aCurrentTimestamp, BufferSize: sizeof(aCurrentTimestamp));
1852 char aTimestamp[512];
1853 pSqlServer->ToUnixTimestamp(pTimestamp: "Timestamp", aBuf: aTimestamp, BufferSize: sizeof(aTimestamp));
1854
1855 char aBuf[512];
1856 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1857 format: "SELECT Savegame, %s-%s AS Ago, SaveId "
1858 "FROM %s_saves "
1859 "where Code = ? AND Map = ? AND DDNet7 = %s",
1860 aCurrentTimestamp, aTimestamp,
1861 pSqlServer->GetPrefix(), pSqlServer->False());
1862 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1863 {
1864 return false;
1865 }
1866 pSqlServer->BindString(Idx: 1, pString: pData->m_aCode);
1867 pSqlServer->BindString(Idx: 2, pString: pData->m_aMap);
1868
1869 bool End;
1870 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
1871 {
1872 return false;
1873 }
1874 if(End)
1875 {
1876 str_copy(dst&: pResult->m_aMessage, src: "No such savegame for this map");
1877 return true;
1878 }
1879
1880 pResult->m_SaveId = UUID_NO_SAVE_ID;
1881 if(!pSqlServer->IsNull(Col: 3))
1882 {
1883 char aSaveId[UUID_MAXSTRSIZE];
1884 pSqlServer->GetString(Col: 3, pBuffer: aSaveId, BufferSize: sizeof(aSaveId));
1885 if(ParseUuid(pUuid: &pResult->m_SaveId, pBuffer: aSaveId) || pResult->m_SaveId == UUID_NO_SAVE_ID)
1886 {
1887 str_copy(dst&: pResult->m_aMessage, src: "Unable to load savegame: SaveId corrupted");
1888 return true;
1889 }
1890 }
1891
1892 char aSaveString[MAX_SAVE_STRING_LENGTH];
1893 pSqlServer->GetString(Col: 1, pBuffer: aSaveString, BufferSize: sizeof(aSaveString));
1894 int Num = pResult->m_SavedTeam.FromString(pString: aSaveString);
1895
1896 if(Num != 0)
1897 {
1898 str_copy(dst&: pResult->m_aMessage, src: "Unable to load savegame: data corrupted");
1899 return true;
1900 }
1901
1902 bool Found = false;
1903 for(int i = 0; i < pResult->m_SavedTeam.GetMembersCount(); i++)
1904 {
1905 if(str_comp(a: pResult->m_SavedTeam.m_pSavedTees[i].GetName(), b: pData->m_aRequestingPlayer) == 0)
1906 {
1907 Found = true;
1908 break;
1909 }
1910 }
1911 if(!Found)
1912 {
1913 str_copy(dst: pResult->m_aMessage, src: "This save exists, but you are not part of it. "
1914 "Make sure you use the same name as you had when saving. "
1915 "If you saved with an already used code, you get a new random save code, "
1916 "check ddnet-saves.txt in config_directory.",
1917 dst_size: sizeof(pResult->m_aMessage));
1918 return true;
1919 }
1920
1921 int Since = pSqlServer->GetInt(Col: 2);
1922 if(Since < g_Config.m_SvSaveSwapGamesDelay)
1923 {
1924 str_format(buffer: pResult->m_aMessage, buffer_size: sizeof(pResult->m_aMessage),
1925 format: "You have to wait %d seconds until you can load this savegame",
1926 g_Config.m_SvSaveSwapGamesDelay - Since);
1927 return true;
1928 }
1929
1930 bool CanLoad = pResult->m_SavedTeam.MatchPlayers(
1931 paNames: pData->m_aClientNames, pClientId: pData->m_aClientId, NumPlayer: pData->m_NumPlayer,
1932 pMessage: pResult->m_aMessage, MessageLen: sizeof(pResult->m_aMessage));
1933
1934 if(!CanLoad)
1935 return true;
1936
1937 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1938 format: "DELETE FROM %s_saves "
1939 "WHERE Code = ? AND Map = ? AND SaveId %s",
1940 pSqlServer->GetPrefix(),
1941 pResult->m_SaveId != UUID_NO_SAVE_ID ? "= ?" : "IS NULL");
1942 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1943 {
1944 return false;
1945 }
1946 pSqlServer->BindString(Idx: 1, pString: pData->m_aCode);
1947 pSqlServer->BindString(Idx: 2, pString: pData->m_aMap);
1948 char aUuid[UUID_MAXSTRSIZE];
1949 if(pResult->m_SaveId != UUID_NO_SAVE_ID)
1950 {
1951 FormatUuid(Uuid: pResult->m_SaveId, pBuffer: aUuid, BufferLength: sizeof(aUuid));
1952 pSqlServer->BindString(Idx: 3, pString: aUuid);
1953 }
1954 pSqlServer->Print();
1955 int NumDeleted;
1956 if(!pSqlServer->ExecuteUpdate(pNumUpdated: &NumDeleted, pError, ErrorSize))
1957 {
1958 return false;
1959 }
1960
1961 if(NumDeleted != 1)
1962 {
1963 str_copy(dst&: pResult->m_aMessage, src: "Unable to load savegame: loaded on a different server");
1964 return true;
1965 }
1966
1967 pResult->m_Status = CScoreSaveResult::LOAD_SUCCESS;
1968 str_copy(dst&: pResult->m_aMessage, src: "Loading successfully done");
1969 return true;
1970}
1971
1972bool CScoreWorker::GetSaves(IDbConnection *pSqlServer, const ISqlData *pGameData, char *pError, int ErrorSize)
1973{
1974 const auto *pData = dynamic_cast<const CSqlPlayerRequest *>(pGameData);
1975 auto *pResult = dynamic_cast<CScorePlayerResult *>(pGameData->m_pResult.get());
1976 auto *paMessages = pResult->m_Data.m_aaMessages;
1977
1978 char aSaveLike[128] = "";
1979 str_append(dst&: aSaveLike, src: "%\n");
1980 sqlstr::EscapeLike(pDst: aSaveLike + str_length(str: aSaveLike),
1981 pSrc: pData->m_aRequestingPlayer,
1982 DstSize: sizeof(aSaveLike) - str_length(str: aSaveLike));
1983 str_append(dst&: aSaveLike, src: "\t%");
1984
1985 char aCurrentTimestamp[512];
1986 pSqlServer->ToUnixTimestamp(pTimestamp: "CURRENT_TIMESTAMP", aBuf: aCurrentTimestamp, BufferSize: sizeof(aCurrentTimestamp));
1987 char aMaxTimestamp[512];
1988 pSqlServer->ToUnixTimestamp(pTimestamp: "MAX(Timestamp)", aBuf: aMaxTimestamp, BufferSize: sizeof(aMaxTimestamp));
1989
1990 char aBuf[512];
1991 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1992 format: "SELECT COUNT(*) AS NumSaves, %s-%s AS Ago "
1993 "FROM %s_saves "
1994 "WHERE Map = ? AND Savegame LIKE ?",
1995 aCurrentTimestamp, aMaxTimestamp,
1996 pSqlServer->GetPrefix());
1997 if(!pSqlServer->PrepareStatement(pStmt: aBuf, pError, ErrorSize))
1998 {
1999 return false;
2000 }
2001 pSqlServer->BindString(Idx: 1, pString: pData->m_aMap);
2002 pSqlServer->BindString(Idx: 2, pString: aSaveLike);
2003
2004 bool End;
2005 if(!pSqlServer->Step(pEnd: &End, pError, ErrorSize))
2006 {
2007 return false;
2008 }
2009 if(!End)
2010 {
2011 int NumSaves = pSqlServer->GetInt(Col: 1);
2012 char aLastSavedString[60] = "\0";
2013 if(!pSqlServer->IsNull(Col: 2))
2014 {
2015 int Ago = pSqlServer->GetInt(Col: 2);
2016 char aAgoString[40] = "\0";
2017 sqlstr::AgoTimeToString(AgoTime: Ago, pAgoString: aAgoString, Size: sizeof(aAgoString));
2018 str_format(buffer: aLastSavedString, buffer_size: sizeof(aLastSavedString), format: ", last saved %s ago", aAgoString);
2019 }
2020
2021 str_format(buffer: paMessages[0], buffer_size: sizeof(paMessages[0]),
2022 format: "%s has %d save%s on %s%s",
2023 pData->m_aRequestingPlayer,
2024 NumSaves, NumSaves == 1 ? "" : "s",
2025 pData->m_aMap, aLastSavedString);
2026 }
2027 return true;
2028}
2029