1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-05 16:02:56 +08:00

Further safety checking.

This commit is contained in:
smoogipooo 2017-03-03 15:59:33 +09:00
parent 777996d884
commit 1d1c08125b
3 changed files with 56 additions and 21 deletions

View File

@ -24,6 +24,7 @@ using osu.Framework.Configuration;
using osu.Framework.Graphics.Textures; using osu.Framework.Graphics.Textures;
using osu.Framework.IO.Stores; using osu.Framework.IO.Stores;
using osu.Framework.Platform; using osu.Framework.Platform;
using osu.Framework.Logging;
namespace osu.Game.Screens.Tournament namespace osu.Game.Screens.Tournament
{ {
@ -250,11 +251,18 @@ namespace osu.Game.Screens.Tournament
{ {
Action writeAction = () => Action writeAction = () =>
{ {
// Write to drawings_results try
using (Stream stream = storage.GetStream(results_filename, FileAccess.Write, FileMode.Create))
using (StreamWriter sw = new StreamWriter(stream))
{ {
sw.Write(text); // Write to drawings_results
using (Stream stream = storage.GetStream(results_filename, FileAccess.Write, FileMode.Create))
using (StreamWriter sw = new StreamWriter(stream))
{
sw.Write(text);
}
}
catch (Exception ex)
{
Logger.Error(ex, "Failed to write results.");
} }
}; };
@ -269,6 +277,8 @@ namespace osu.Game.Screens.Tournament
teamsContainer.ClearTeams(); teamsContainer.ClearTeams();
allTeams.Clear(); allTeams.Clear();
List<Team> newTeams = new List<Team>();
try try
{ {
using (Stream stream = storage.GetStream(teams_filename, FileAccess.Read, FileMode.Open)) using (Stream stream = storage.GetStream(teams_filename, FileAccess.Read, FileMode.Open))
@ -276,32 +286,46 @@ namespace osu.Game.Screens.Tournament
{ {
while (sr.Peek() != -1) while (sr.Peek() != -1)
{ {
string line = sr.ReadLine(); string line = sr.ReadLine().Trim();
if (string.IsNullOrEmpty(line))
continue;
string[] split = line.Split(':'); string[] split = line.Split(':');
if (split.Length < 2)
{
Logger.Log($"Invalid team definition: {line}. Expected \"flag_name : team_name : team_acronym\".");
continue;
}
string flagName = split[0].Trim(); string flagName = split[0].Trim();
string name = split[1].Trim(); string teamName = split[1].Trim();
string acronym = name.Substring(0, Math.Min(3, name.Length)); string acronym = split.Length >= 3 ? split[2].Trim() : teamName;
if (split.Length >= 3) acronym = acronym.Substring(0, Math.Min(3, acronym.Length));
acronym = split[2].Trim();
if (groupsContainer.ContainsTeam(name)) if (groupsContainer.ContainsTeam(teamName))
continue; continue;
Team t = new Team() Team t = new Team()
{ {
FlagName = flagName, FlagName = flagName,
FullName = name, FullName = teamName,
Acronym = acronym Acronym = acronym
}; };
allTeams.Add(t); newTeams.Add(t);
teamsContainer.AddTeam(t);
} }
} }
allTeams = newTeams;
teamsContainer.AddTeams(allTeams);
}
catch (Exception ex)
{
Logger.Error(ex, "Failed to read teams.");
} }
catch { }
} }
private void reset(bool loadLastResults = false) private void reset(bool loadLastResults = false)
@ -310,9 +334,9 @@ namespace osu.Game.Screens.Tournament
reloadTeams(); reloadTeams();
try if (loadLastResults)
{ {
if (loadLastResults) try
{ {
// Read from drawings_results // Read from drawings_results
using (Stream stream = storage.GetStream(results_filename, FileAccess.Read, FileMode.Open)) using (Stream stream = storage.GetStream(results_filename, FileAccess.Read, FileMode.Open))
@ -320,7 +344,7 @@ namespace osu.Game.Screens.Tournament
{ {
while (sr.Peek() != -1) while (sr.Peek() != -1)
{ {
string line = sr.ReadLine(); string line = sr.ReadLine().Trim();
if (string.IsNullOrEmpty(line)) if (string.IsNullOrEmpty(line))
continue; continue;
@ -338,13 +362,15 @@ namespace osu.Game.Screens.Tournament
} }
} }
} }
else catch (Exception ex)
{ {
writeResults(string.Empty); Logger.Error(ex, "Failed to read last drawings results.");
} }
} }
catch else
{ {
writeResults(string.Empty);
} }
} }
} }

View File

@ -20,7 +20,7 @@ namespace osu.Game.Screens.Tournament
{ {
public class Group : Container public class Group : Container
{ {
public string GroupName; public readonly string GroupName;
public int TeamsCount { get; private set; } public int TeamsCount { get; private set; }

View File

@ -163,6 +163,15 @@ namespace osu.Game.Screens.Tournament
scrollState = ScrollState.Idle; scrollState = ScrollState.Idle;
} }
public void AddTeams(IEnumerable<Team> teams)
{
if (teams == null)
return;
foreach (Team t in teams)
AddTeam(t);
}
public void ClearTeams() public void ClearTeams()
{ {
availableTeams.Clear(); availableTeams.Clear();