Skip to content

Commit

Permalink
Ensure correct MapNumber logic when restoring from backup in case of …
Browse files Browse the repository at this point in the history
…draws

Read round number from valve backup on restore event
Use g_MapNumber in more places and reduce use of Get5_GetMapNumber where not needed
Properly calculate series results when ties are included
Remove bo2 tie message (it was used when not bo2)
  • Loading branch information
nickdnk committed Aug 8, 2022
1 parent 3dadefc commit 6899ebc
Show file tree
Hide file tree
Showing 20 changed files with 98 additions and 137 deletions.
5 changes: 3 additions & 2 deletions documentation/docs/event_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -489,14 +489,15 @@ paths:
tags:
- Series Flow
description: |
Fired when a round is restored from a backup.
Fired when a round is restored from a backup. Note that the map and round numbers indicate the round being
restored **to**, not the round the backup was requested during.
requestBody:
content:
application/json:
schema:
title: Get5BackupRestoredEvent
allOf:
- "$ref": "#/components/schemas/Get5MapEvent"
- "$ref": "#/components/schemas/Get5RoundEvent"
properties:
event:
enum:
Expand Down
93 changes: 42 additions & 51 deletions scripting/get5.sp
Original file line number Diff line number Diff line change
Expand Up @@ -1181,70 +1181,61 @@ public Action Event_MatchOver(Event event, const char[] name, bool dontBroadcast

float minDelay = float(GetTvDelay()) + MATCH_END_DELAY_AFTER_TV;

if (t1maps == g_MapsToWin) {
// Team 1 won
SeriesEndMessage(Get5Team_1);
DelayFunction(minDelay, EndSeries);

} else if (t2maps == g_MapsToWin) {
// Team 2 won
SeriesEndMessage(Get5Team_2);
DelayFunction(minDelay, EndSeries);

} else if (t1maps == t2maps && t1maps + tiedMaps == g_MapsToWin) {
// The whole series was a tie
SeriesEndMessage(Get5Team_None);
DelayFunction(minDelay, EndSeries);

} else if (g_BO2Match && Get5_GetMapNumber() == 2) {
// It was a bo2, and none of the teams got to 2
SeriesEndMessage(Get5Team_None);
DelayFunction(minDelay, EndSeries);

if (t1maps == t2maps) {
// As long as team scores are equal, we play until there are no maps left.
if (t1maps + t2maps + tiedMaps == g_MapsToPlay.Length) {
SeriesEndMessage(Get5Team_None);
DelayFunction(minDelay, EndSeries);
return Plugin_Continue;
}
} else {
if (t1maps > t2maps) {
Get5_MessageToAll("%t", "TeamWinningSeriesInfoMessage", g_FormattedTeamNames[Get5Team_1],
t1maps, t2maps);

} else if (t2maps > t1maps) {
Get5_MessageToAll("%t", "TeamWinningSeriesInfoMessage", g_FormattedTeamNames[Get5Team_2],
t2maps, t1maps);

} else {
Get5_MessageToAll("%t", "SeriesTiedInfoMessage", t1maps, t2maps);
// This adjusts for ties!
int actualMapsToWin = ((g_MapsToPlay.Length - tiedMaps) / 2) + 1;
if (t1maps == actualMapsToWin) {
// Team 1 won
SeriesEndMessage(Get5Team_1);
DelayFunction(minDelay, EndSeries);
return Plugin_Continue;
} else if (t2maps == actualMapsToWin) {
// Team 2 won
SeriesEndMessage(Get5Team_2);
DelayFunction(minDelay, EndSeries);
return Plugin_Continue;
}
}

if (t1maps > t2maps) {
Get5_MessageToAll("%t", "TeamWinningSeriesInfoMessage", g_FormattedTeamNames[Get5Team_1],
t1maps, t2maps);

int index = Get5_GetMapNumber();
char nextMap[PLATFORM_MAX_PATH];
g_MapsToPlay.GetString(index, nextMap, sizeof(nextMap));
} else if (t2maps > t1maps) {
Get5_MessageToAll("%t", "TeamWinningSeriesInfoMessage", g_FormattedTeamNames[Get5Team_2],
t2maps, t1maps);

g_MapChangePending = true;
Get5_MessageToAll("%t", "NextSeriesMapInfoMessage", nextMap);
ChangeState(Get5State_PostGame);
CreateTimer(minDelay, Timer_NextMatchMap);
} else {
Get5_MessageToAll("%t", "SeriesTiedInfoMessage", t1maps, t2maps);
}

char nextMap[PLATFORM_MAX_PATH];
g_MapsToPlay.GetString(Get5_GetMapNumber(), nextMap, sizeof(nextMap));

g_MapChangePending = true;
Get5_MessageToAll("%t", "NextSeriesMapInfoMessage", nextMap);
ChangeState(Get5State_PostGame);
CreateTimer(minDelay, Timer_NextMatchMap);
}

return Plugin_Continue;
}

static void SeriesEndMessage(Get5Team team) {
if (g_MapsToWin == 1) {
if (team == Get5Team_None) {
Get5_MessageToAll("%t", "TeamTiedMatchInfoMessage", g_FormattedTeamNames[Get5Team_1],
g_FormattedTeamNames[Get5Team_2]);
} else {
Get5_MessageToAll("%t", "TeamWonMatchInfoMessage", g_FormattedTeamNames[team]);
}
if (team == Get5Team_None) {
Get5_MessageToAll("%t", "TeamTiedMatchInfoMessage", g_FormattedTeamNames[Get5Team_1], g_FormattedTeamNames[Get5Team_2]);
} else {
if (team == Get5Team_None) {
// BO2 split.
Get5_MessageToAll("%t", "TeamsSplitSeriesBO2InfoMessage", g_FormattedTeamNames[Get5Team_1],
g_FormattedTeamNames[Get5Team_2]);

if (g_MapsToPlay.Length == 1) {
Get5_MessageToAll("%t", "TeamWonMatchInfoMessage", g_FormattedTeamNames[team]);
} else {
Get5_MessageToAll("%t", "TeamWonSeriesInfoMessage", g_FormattedTeamNames[team],
g_TeamSeriesScores[team], g_TeamSeriesScores[OtherMatchTeam(team)]);
Get5_MessageToAll("%t", "TeamWonSeriesInfoMessage", g_FormattedTeamNames[team], g_TeamSeriesScores[team], g_TeamSeriesScores[OtherMatchTeam(team)]);
}
}
}
Expand Down
16 changes: 13 additions & 3 deletions scripting/get5/backups.sp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ public void WriteBackStructure(const char[] path) {
kv.SetNum("team1_series_score", g_TeamSeriesScores[Get5Team_1]);
kv.SetNum("team2_series_score", g_TeamSeriesScores[Get5Team_2]);

kv.SetNum("series_draw", g_TeamSeriesScores[Get5Team_None]);

// Write original maplist.
kv.JumpToKey("maps", true);
for (int i = 0; i < g_MapsToPlay.Length; i++) {
Expand Down Expand Up @@ -251,6 +253,12 @@ public bool RestoreFromBackup(const char[] path) {
g_TeamSeriesScores[Get5Team_1] = kv.GetNum("team1_series_score");
g_TeamSeriesScores[Get5Team_2] = kv.GetNum("team2_series_score");

// This ensures that the MapNumber logic correctly calculates the map number when there have been draws.
g_TeamSeriesScores[Get5Team_None] = kv.GetNum("series_draw", 0);

// Immediately set map number global var to ensure anything below doesn't break.
g_MapNumber = Get5_GetMapNumber();

char mapName[PLATFORM_MAX_PATH];
if (g_GameState > Get5State_Veto) {
if (kv.JumpToKey("maps")) {
Expand Down Expand Up @@ -292,11 +300,14 @@ public bool RestoreFromBackup(const char[] path) {
kv.GoBack();
}

// When loading round 0, there is no valve backup, so we assume round 0 if the game is live, otherwise -1
int roundNumberRestoredTo = g_GameState == Get5State_Live ? 0 : -1;
char tempValveBackup[PLATFORM_MAX_PATH];
GetTempFilePath(tempValveBackup, sizeof(tempValveBackup), TEMP_VALVE_BACKUP_PATTERN);
if (kv.JumpToKey("valve_backup")) {
g_SavedValveBackup = true;
kv.ExportToFile(tempValveBackup);
roundNumberRestoredTo = kv.GetNum("round", 0);
kv.GoBack();
} else {
g_SavedValveBackup = false;
Expand All @@ -306,7 +317,7 @@ public bool RestoreFromBackup(const char[] path) {
GetCurrentMap(currentMap, sizeof(currentMap));

char currentSeriesMap[PLATFORM_MAX_PATH];
g_MapsToPlay.GetString(Get5_GetMapNumber(), currentSeriesMap, sizeof(currentSeriesMap));
g_MapsToPlay.GetString(g_MapNumber, currentSeriesMap, sizeof(currentSeriesMap));

if (!StrEqual(currentMap, currentSeriesMap)) {
ChangeMap(currentSeriesMap, 1.0);
Expand All @@ -320,8 +331,7 @@ public bool RestoreFromBackup(const char[] path) {

LogDebug("Calling Get5_OnBackupRestore()");

Get5BackupRestoredEvent backupEvent =
new Get5BackupRestoredEvent(g_MatchID, Get5_GetMapNumber(), path);
Get5BackupRestoredEvent backupEvent = new Get5BackupRestoredEvent(g_MatchID, g_MapNumber, roundNumberRestoredTo, path);

Call_StartForward(g_OnBackupRestore);
Call_PushCell(backupEvent);
Expand Down
2 changes: 1 addition & 1 deletion scripting/get5/goinglive.sp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Action StartGoingLive(Handle timer) {
float delay = float(5 + g_LiveCountdownTimeCvar.IntValue);
CreateTimer(delay, MatchLive);

Get5GoingLiveEvent liveEvent = new Get5GoingLiveEvent(g_MatchID, Get5_GetMapNumber());
Get5GoingLiveEvent liveEvent = new Get5GoingLiveEvent(g_MatchID, g_MapNumber);

LogDebug("Calling Get5_OnGoingLive()");

Expand Down
4 changes: 2 additions & 2 deletions scripting/get5/kniferounds.sp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ static void PerformSideSwap(bool swap) {
// that way set starting teams won't swap on round 0,
// since a temp valve backup does not exist.
if (g_TeamSide[Get5Team_1] == CS_TEAM_CT)
g_MapSides.Set(Get5_GetMapNumber(), SideChoice_Team1CT);
g_MapSides.Set(g_MapNumber, SideChoice_Team1CT);
else
g_MapSides.Set(Get5_GetMapNumber(), SideChoice_Team1T);
g_MapSides.Set(g_MapNumber, SideChoice_Team1T);
} else {
g_TeamSide[Get5Team_1] = TEAM1_STARTING_SIDE;
g_TeamSide[Get5Team_2] = TEAM2_STARTING_SIDE;
Expand Down
15 changes: 8 additions & 7 deletions scripting/get5/matchconfig.sp
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,20 @@ stock bool LoadMatchConfig(const char[] config, bool restoreBackup = false) {
}
}

g_MapPoolList.GetString(Get5_GetMapNumber(), mapName, sizeof(mapName));
ChangeState(Get5State_Warmup);

char currentMap[PLATFORM_MAX_PATH];
GetCurrentMap(currentMap, sizeof(currentMap));
if (!StrEqual(mapName, currentMap) && !restoreBackup) {
ChangeMap(mapName);
if (!restoreBackup) {
// When restoring from backup, changelevel is called after loading the match config.
g_MapPoolList.GetString(Get5_GetMapNumber(), mapName, sizeof(mapName));
char currentMap[PLATFORM_MAX_PATH];
GetCurrentMap(currentMap, sizeof(currentMap));
if (!StrEqual(mapName, currentMap)) {
ChangeMap(mapName);
}
}
} else {
ChangeState(Get5State_PreVeto);
}


// We need to ensure our match team CVARs are set
// before calling the event so we can grab values
// that are set in the OnSeriesInit event.
Expand Down
2 changes: 1 addition & 1 deletion scripting/get5/readysystem.sp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ static void HandleReadyMessage(Get5Team team) {
if (g_GameState == Get5State_PreVeto) {
Get5_MessageToAll("%t", "TeamReadyToVetoInfoMessage", g_FormattedTeamNames[team]);
} else if (g_GameState == Get5State_Warmup) {
SideChoice sides = view_as<SideChoice>(g_MapSides.Get(Get5_GetMapNumber()));
SideChoice sides = view_as<SideChoice>(g_MapSides.Get(g_MapNumber));
if (g_WaitingForRoundBackup) {
Get5_MessageToAll("%t", "TeamReadyToRestoreBackupInfoMessage", g_FormattedTeamNames[team]);
} else if (sides == SideChoice_KnifeRound) {
Expand Down
11 changes: 5 additions & 6 deletions scripting/get5_apistats.sp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public void Get5_OnGoingLive(const Get5GoingLiveEvent event) {
Get5_AddLiveCvar("get5_web_api_url", g_APIURL);
}

public void UpdateRoundStats(const char[] matchId, int mapNumber) {
public void UpdateRoundStats(const char[] matchId, const int mapNumber) {
int t1score = CS_GetTeamScore(Get5_Get5TeamToCSTeam(Get5Team_1));
int t2score = CS_GetTeamScore(Get5_Get5TeamToCSTeam(Get5Team_2));

Expand All @@ -252,11 +252,11 @@ public void UpdateRoundStats(const char[] matchId, int mapNumber) {
Format(mapKey, sizeof(mapKey), "map%d", mapNumber);
if (kv.JumpToKey(mapKey)) {
if (kv.JumpToKey("team1")) {
UpdatePlayerStats(matchId, kv, Get5Team_1);
UpdatePlayerStats(matchId, mapNumber, kv, Get5Team_1);
kv.GoBack();
}
if (kv.JumpToKey("team2")) {
UpdatePlayerStats(matchId, kv, Get5Team_2);
UpdatePlayerStats(matchId, mapNumber, kv, Get5Team_2);
kv.GoBack();
}
kv.GoBack();
Expand Down Expand Up @@ -284,10 +284,9 @@ static void AddIntStat(Handle req, KeyValues kv, const char[] field) {
AddIntParam(req, field, kv.GetNum(field));
}

public void UpdatePlayerStats(const char[] matchId, KeyValues kv, Get5Team team) {
public void UpdatePlayerStats(const char[] matchId, const int mapNumber, const KeyValues kv, const Get5Team team) {
char name[MAX_NAME_LENGTH];
char auth[AUTH_LENGTH];
int mapNumber = Get5_GetMapNumber();

if (kv.GotoFirstSubKey()) {
do {
Expand Down Expand Up @@ -382,6 +381,6 @@ public void Get5_OnRoundStatsUpdated(const Get5RoundStatsUpdatedEvent event) {
if (Get5_GetGameState() == Get5State_Live) {
char matchId[64];
event.GetMatchId(matchId, sizeof(matchId));
UpdateRoundStats(matchId, Get5_GetMapNumber());
UpdateRoundStats(matchId, event.MapNumber);
}
}
11 changes: 5 additions & 6 deletions scripting/get5_mysqlstats.sp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public void Get5_OnGoingLive(const Get5GoingLiveEvent event) {
db.Query(SQLErrorCheckCallback, queryBuffer);
}

public void UpdateRoundStats(const char[] matchId, int mapNumber) {
public void UpdateRoundStats(const char[] matchId, const int mapNumber) {
// Update team scores
int t1score = CS_GetTeamScore(Get5_Get5TeamToCSTeam(Get5Team_1));
int t2score = CS_GetTeamScore(Get5_Get5TeamToCSTeam(Get5Team_2));
Expand All @@ -178,11 +178,11 @@ public void UpdateRoundStats(const char[] matchId, int mapNumber) {
Format(mapKey, sizeof(mapKey), "map%d", mapNumber);
if (kv.JumpToKey(mapKey)) {
if (kv.JumpToKey("team1")) {
AddPlayerStats(matchId, kv, Get5Team_1);
AddPlayerStats(matchId, mapNumber, kv, Get5Team_1);
kv.GoBack();
}
if (kv.JumpToKey("team2")) {
AddPlayerStats(matchId, kv, Get5Team_2);
AddPlayerStats(matchId, mapNumber, kv, Get5Team_2);
kv.GoBack();
}
kv.GoBack();
Expand Down Expand Up @@ -223,12 +223,11 @@ public void Get5_OnMapResult(const Get5MapResultEvent event) {
db.Query(SQLErrorCheckCallback, queryBuffer);
}

public void AddPlayerStats(const char[] matchId, KeyValues kv, Get5Team team) {
public void AddPlayerStats(const char[] matchId, const int mapNumber, const KeyValues kv, const Get5Team team) {
char name[MAX_NAME_LENGTH];
char auth[AUTH_LENGTH];
char nameSz[MAX_NAME_LENGTH * 2 + 1];
char authSz[AUTH_LENGTH * 2 + 1];
int mapNumber = Get5_GetMapNumber();

char matchIdSz[64];
db.Escape(matchId, matchIdSz, sizeof(matchIdSz));
Expand Down Expand Up @@ -381,6 +380,6 @@ public void Get5_OnRoundStatsUpdated(const Get5RoundStatsUpdatedEvent event) {
if (Get5_GetGameState() == Get5State_Live && !g_DisableStats) {
char matchId[64];
event.GetMatchId(matchId, sizeof(matchId));
UpdateRoundStats(matchId, Get5_GetMapNumber());
UpdateRoundStats(matchId, event.MapNumber);
}
}
35 changes: 18 additions & 17 deletions scripting/include/get5.inc
Original file line number Diff line number Diff line change
Expand Up @@ -977,25 +977,25 @@ methodmap Get5SeriesStartedEvent < Get5MatchEvent {
}
}

methodmap Get5BackupRestoredEvent < Get5MapEvent {
methodmap Get5BackupRestoredEvent < Get5RoundEvent {

public bool SetFileName(const char[] file)
{
return this.SetString("filename", file);
}
public bool SetFileName(const char[] file) {
return this.SetString("filename", file);
}

public bool GetFileName(char[] buffer, const int maxSize) {
return this.GetString("filename", buffer, maxSize);
}
public bool GetFileName(char[] buffer, const int maxSize) {
return this.GetString("filename", buffer, maxSize);
}

public Get5BackupRestoredEvent(const char[] matchId, const int mapNumber, const char[] file) {
Get5BackupRestoredEvent self = view_as<Get5BackupRestoredEvent>(new JSON_Object());
self.SetEvent("backup_loaded");
self.SetMatchId(matchId);
self.MapNumber = mapNumber;
self.SetFileName(file);
return self;
}
public Get5BackupRestoredEvent(const char[] matchId, const int mapNumber, const int roundNumber, const char[] file) {
Get5BackupRestoredEvent self = view_as<Get5BackupRestoredEvent>(new JSON_Object());
self.SetEvent("backup_loaded");
self.SetMatchId(matchId);
self.MapNumber = mapNumber;
self.RoundNumber = roundNumber;
self.SetFileName(file);
return self;
}
}

methodmap Get5RoundStartedEvent < Get5RoundEvent {
Expand Down Expand Up @@ -1779,7 +1779,8 @@ forward void Get5_OnMatchPaused(const Get5MatchPausedEvent event);
forward void Get5_OnMatchUnpaused(const Get5MatchUnpausedEvent event);

// Called when a match backup is restored.
// Note that the match ID and map number is the one being restored *to*, not the current game state and the time the backup is loaded.
// Note that the match ID, map number and round number is the one being restored *to*, not the current game state at the
// time the backup is loaded.
forward void Get5_OnBackupRestore(const Get5BackupRestoredEvent event);

// Series stats (root section)
Expand Down
4 changes: 0 additions & 4 deletions translations/chi/get5.phrases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,6 @@
{
"chi" "{1}与{2}打成了平局。"
}
"TeamsSplitSeriesBO2InfoMessage"
{
"chi" "{1}和{2}在此系列比赛中平局 1-1。"
}
"TeamWonSeriesInfoMessage"
{
"chi" "{1}赢得了此系列比赛 {2}-{3}."
Expand Down
Loading

0 comments on commit 6899ebc

Please sign in to comment.