Skip to content

Commit

Permalink
Merge pull request #29 from CHR15cs/18-backup-wrong-order
Browse files Browse the repository at this point in the history
18 backup wrong order
  • Loading branch information
CHR15cs authored Dec 12, 2023
2 parents 319a53b + 21bc1b0 commit 7894a5c
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CSPracc/CommandHandler/MatchCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public override bool PlayerChat(EventPlayerChat @event, GameEventInfo info)
MatchMode.RestoreBackup(player);
break;
}
case PRACC_COMMAND.RESTORE:
{
MatchMode.RestoreLastRound(player);
break;
}
case PRACC_COMMAND.FORCEUNPAUSE:
{
MatchMode.ForceUnpause(player);
Expand Down
145 changes: 141 additions & 4 deletions CSPracc/Managers/RoundRestoreManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,134 @@ public static void CleanupOldFiles()
}
private static List<FileInfo> GetBackupFiles()
{
return CSPraccPlugin.Cs2Dir.GetFiles("backup_round*").ToList();
List<FileInfo> unsortedBackups = CSPraccPlugin.Cs2Dir.GetFiles("backup_round*").ToList();
List<FileInfo> sortedbackupFiles = new List<FileInfo>();

while (unsortedBackups.Count > 0)
{
FileInfo earliestBackup = unsortedBackups.First();
int round = -1;
try
{
round = Convert.ToInt32(earliestBackup.Name.Substring(earliestBackup.Name.Length - 6, 2));
}
catch (Exception ex) { }

foreach (FileInfo file in unsortedBackups)
{
int round2 = -1;
try
{
round2 = Convert.ToInt32(file.Name.Substring(file.Name.Length - 6, 2));
}
catch (Exception ex) { }
if(round2 != -1 && round2 < round) { earliestBackup = file; }
}
unsortedBackups.Remove(earliestBackup);
sortedbackupFiles.Add(earliestBackup);
}
return sortedbackupFiles;
}

public static void LoadLastBackup(CCSPlayerController player)
{
if (player == null) return;
if (!player.PlayerPawn.IsValid) return;
if (!player.IsAdmin())
{
player.PrintToCenter("Only admins can execute this command!");
return;
}

List<FileInfo> Backupfiles = GetBackupFiles();
int highestRound = 0;
foreach (FileInfo file in Backupfiles)
{
try
{
int round = Convert.ToInt32(file.Name.Substring(file.Name.Length - 6, 2));
if (round > highestRound)
{
highestRound = round;
}
}
catch { }
}
string sround = highestRound >= 10 ? highestRound.ToString() : "0"+highestRound;
foreach (FileInfo backup in Backupfiles)
{
if(backup.Name.Contains(sround))
{
Server.ExecuteCommand($"mp_backup_restore_load_file {backup.Name}");
}
}
CSPracc.DataModules.Constants.Methods.MsgToServer("Restored round, to continue match both teams need to use .unpause");

}

private static string getScoreOfBackupFile(FileInfo fileInfo)
{
string score = "0-0";

string content = File.ReadAllText(fileInfo.FullName);
int startOfScore = content.IndexOf('{', content.IndexOf("FirstHalfScore"))+1;
int endOfScore = content.IndexOf('}', startOfScore)-1;
int length = endOfScore - startOfScore;
string firsthalf = content.Substring(startOfScore, length);
int team1 = 0;
int team2 = 0;
List<string> firsthalfScore = parseBackupInfo(firsthalf);
if (firsthalfScore.Count != 4)
{
Server.PrintToConsole("Invalid backupfile");
return "invalid";
}
team1 += Convert.ToInt32(firsthalfScore[1]);
team2 += Convert.ToInt32(firsthalfScore[3]);
if (content.Contains("SecondHalfScore"))
{
Server.PrintToChatAll("contains second half score");
startOfScore = content.IndexOf('{', content.IndexOf("SecondHalfScore")) + 1;
endOfScore = content.IndexOf('}', startOfScore) - 1;
length = endOfScore - startOfScore;
string secondhalf = content.Substring(startOfScore, length);
List<string> secondhalfScore = parseBackupInfo(secondhalf);
if(secondhalfScore.Count != 4)
{
Server.PrintToConsole("Invalid backupfile");
return "invalid";
}
team1 += Convert.ToInt32(secondhalfScore[1]);
team2 += Convert.ToInt32(secondhalfScore[3]);
}
return $"{team1}:{team2}";
}

private static List<string> parseBackupInfo(string backupInfo)
{
bool beginning = false;
List<string> lines = new List<string>();
string laststring = "";
for (int i = 0; i < backupInfo.Length; i++)
{
if (backupInfo[i] == '"')
{
if (!beginning)
{
beginning = true;
continue;
}
else
{
lines.Add(laststring);
laststring = "";
beginning = false;
continue;
}
}
laststring += backupInfo[i];
}
return lines;
}

public static void OpenBackupMenu(CCSPlayerController player)
Expand All @@ -41,7 +168,7 @@ public static void OpenBackupMenu(CCSPlayerController player)

foreach (var file in Backupfiles)
{
string round = file.Name.Substring(file.Name.Length - 6, 2);
string round = getScoreOfBackupFile(file);
backupMenu.AddMenuOption(round, handleGive);
}
ChatMenus.OpenMenu(player, backupMenu);
Expand All @@ -50,14 +177,24 @@ public static void OpenBackupMenu(CCSPlayerController player)
private static void LoadSelectedBackup(string Backup)
{
List<FileInfo> backups = GetBackupFiles();
bool FileFound = false;
foreach (var backup in backups)
{
if(backup.Name.Contains(Backup))
if(getScoreOfBackupFile(backup) == Backup)
{
Server.ExecuteCommand($"mp_backup_restore_load_file {backup.Name}");
FileFound = true;
}
}
CSPracc.DataModules.Constants.Methods.MsgToServer("Restored round, to continue match both teams need to use .unpause");
if (!FileFound)
{
CSPracc.DataModules.Constants.Methods.MsgToServer("Failed to restore round. Please select another backup file.");
}
else
{
CSPracc.DataModules.Constants.Methods.MsgToServer("Restored round, to continue match both teams need to use .unpause");
}

}


Expand Down
12 changes: 12 additions & 0 deletions CSPracc/Modes/MatchMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,25 @@ public void RestoreBackup(CCSPlayerController player)
RoundRestoreManager.OpenBackupMenu(player);
}

public void RestoreLastRound(CCSPlayerController player)
{
if (player == null) { return; }
if (!player.IsValid) { return; }
if (!player.IsAdmin()) { player.PrintToCenter("Only admins can execute this command!"); return; }
Pause();
Methods.MsgToServer("Admin is using round restore manager.");
RoundRestoreManager.LoadLastBackup(player);
}

public void ForceUnpause(CCSPlayerController player)
{
if (player == null) { return; }
if (!player.IsValid) { return; }
if (!player.IsAdmin()) { player.PrintToCenter("Only admins can execute this command!"); return; }
ReadyTeamCT = true;
ReadyTeamT = true;
Methods.MsgToServer("Both Teams are now ready. Unpausing match!");
Server.ExecuteCommand(DataModules.Constants.COMMANDS.UNPAUSE_MATCH);
}
public virtual HookResult OnPlayerSpawnHandler(EventPlayerSpawn @event,GameEventInfo info)
{
Expand Down

0 comments on commit 7894a5c

Please sign in to comment.