Skip to content

Commit

Permalink
0.4.1 | fix: Coach and Backup fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shobhit-pathak committed Nov 20, 2023
1 parent 61737b4 commit 31a831c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
10 changes: 6 additions & 4 deletions BackupManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public partial class MatchZy
public bool pauseAfterRoundRestore = true;
public string lastBackupFileName = "";

public bool isRoundRestoring = false;

public Dictionary<string, bool> stopData = new Dictionary<string, bool> {
{ "ct", false },
{ "t", false }
Expand Down Expand Up @@ -132,17 +134,17 @@ private void RestoreRoundBackup(CCSPlayerController? player, string fileName, st
backupData = new Dictionary<string, string>();
}
}

isRoundRestoring = true;

foreach (var kvp in backupData) {

if (kvp.Key == "team1_side") {
// This means round is being restored after sides were swapped, hence we swap sides in our records as well!
if (kvp.Value == "CT" && teamSides[matchzyTeam1] != "CT") {
(teamSides[matchzyTeam1], teamSides[matchzyTeam2]) = (teamSides[matchzyTeam2], teamSides[matchzyTeam1]);
(reverseTeamSides["CT"], reverseTeamSides["TERRORIST"]) = (reverseTeamSides["TERRORIST"], reverseTeamSides["CT"]);
SwapSidesInTeamData(false);
} else if (kvp.Value == "TERRORIST" && teamSides[matchzyTeam1] != "TERRORIST") {
(teamSides[matchzyTeam1], teamSides[matchzyTeam2]) = (teamSides[matchzyTeam2], teamSides[matchzyTeam1]);
(reverseTeamSides["CT"], reverseTeamSides["TERRORIST"]) = (reverseTeamSides["TERRORIST"], reverseTeamSides["CT"]);
SwapSidesInTeamData(false);
}
Server.ExecuteCommand($"mp_teamname_1 {matchzyTeam1.teamName}");
Server.ExecuteCommand($"mp_teamname_2 {matchzyTeam2.teamName}");
Expand Down
2 changes: 1 addition & 1 deletion Teams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void HandleCoaches()
if (coach == null) continue;
Log($"Found coach: {coach.PlayerName}");
coach.InGameMoneyServices!.Account = 0;
AddTimer(0.5f, () => HandleCoachTeam(coach));
AddTimer(0.5f, () => HandleCoachTeam(coach, true));
// AddTimer(1, () => {
// Server.ExecuteCommand("mp_suicide_penalty 0; mp_death_drop_gun 0");
// coach.PlayerPawn.Value.CommitSuicide(false, true);
Expand Down
54 changes: 34 additions & 20 deletions Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ public void HandlePostRoundFreezeEndEvent(EventRoundFreezeEnd @event)
}
}

private void HandleCoachTeam(CCSPlayerController playerController)
private void HandleCoachTeam(CCSPlayerController playerController, bool isFreezeTime = false)
{
CsTeam oldTeam = CsTeam.Spectator;
if (matchzyTeam1.coach == playerController) {
Expand All @@ -638,8 +638,10 @@ private void HandleCoachTeam(CCSPlayerController playerController)
oldTeam = CsTeam.Terrorist;
}
}
playerController.ChangeTeam(CsTeam.Spectator);
playerController.ChangeTeam(oldTeam);
if (!(isFreezeTime && playerController.TeamNum == (int)oldTeam)) {
playerController.ChangeTeam(CsTeam.Spectator);
playerController.ChangeTeam(oldTeam);
}
if (playerController.InGameMoneyServices != null) playerController.InGameMoneyServices.Account = 0;
}

Expand All @@ -666,29 +668,41 @@ private void HandlePostRoundEndEvent(EventRoundEnd @event) {
stopData["ct"] = false;
stopData["t"] = false;

// Handling OTs and side swaps (Referred from Get5)
var gameRules = Utilities.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules").First().GameRules!;
int roundsPlayed = gameRules.TotalRoundsPlayed;
bool swapRequired = IsTeamSwapRequired();

int roundsPerHalf = ConVar.Find("mp_maxrounds").GetPrimitiveValue<int>() / 2;
int roundsPerOTHalf = ConVar.Find("mp_overtime_maxrounds").GetPrimitiveValue<int>() / 2;
// If isRoundRestoring is true, sides will be swapped from round restore if required!
if (swapRequired && !isRoundRestoring) {
SwapSidesInTeamData(false);
}

bool halftimeEnabled = ConVar.Find("mp_halftime").GetPrimitiveValue<bool>();
isRoundRestoring = false;
}
}

if (halftimeEnabled) {
if (roundsPlayed == roundsPerHalf) {
SwapSidesInTeamData(false);
}
// Now in OT.
if (roundsPlayed >= 2 * roundsPerHalf) {
int otround = roundsPlayed - 2 * roundsPerHalf; // round 33 -> round 3, etc.
// Do side swaps at OT halves (rounds 3, 9, ...)
if ((otround + roundsPerOTHalf) % (2 * roundsPerOTHalf) == 0) {
SwapSidesInTeamData(false);
}
public bool IsTeamSwapRequired() {
// Handling OTs and side swaps (Referred from Get5)
var gameRules = Utilities.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules").First().GameRules!;
int roundsPlayed = gameRules.TotalRoundsPlayed;

int roundsPerHalf = ConVar.Find("mp_maxrounds").GetPrimitiveValue<int>() / 2;
int roundsPerOTHalf = ConVar.Find("mp_overtime_maxrounds").GetPrimitiveValue<int>() / 2;

bool halftimeEnabled = ConVar.Find("mp_halftime").GetPrimitiveValue<bool>();

if (halftimeEnabled) {
if (roundsPlayed == roundsPerHalf) {
return true;
}
// Now in OT.
if (roundsPlayed >= 2 * roundsPerHalf) {
int otround = roundsPlayed - 2 * roundsPerHalf; // round 33 -> round 3, etc.
// Do side swaps at OT halves (rounds 3, 9, ...)
if ((otround + roundsPerOTHalf) % (2 * roundsPerOTHalf) == 0) {
return true;
}
}
}
return false;
}

private void ReplyToUserCommand(CCSPlayerController? player, string message, bool console = false)
Expand Down

0 comments on commit 31a831c

Please sign in to comment.