mirror of
https://github.com/ppy/osu
synced 2025-02-18 03:16:57 +00:00
Simplify and extract complex method
This commit is contained in:
parent
5b81de7663
commit
8bf49830d5
@ -27,7 +27,8 @@ namespace osu.Game.Tournament
|
|||||||
{
|
{
|
||||||
private const string bracket_filename = "bracket.json";
|
private const string bracket_filename = "bracket.json";
|
||||||
|
|
||||||
protected LadderInfo Ladder;
|
private LadderInfo ladder;
|
||||||
|
|
||||||
private Storage storage;
|
private Storage storage;
|
||||||
|
|
||||||
private DependencyContainer dependencies;
|
private DependencyContainer dependencies;
|
||||||
@ -57,42 +58,61 @@ namespace osu.Game.Tournament
|
|||||||
|
|
||||||
windowSize = frameworkConfig.GetBindable<Size>(FrameworkSetting.WindowedSize);
|
windowSize = frameworkConfig.GetBindable<Size>(FrameworkSetting.WindowedSize);
|
||||||
|
|
||||||
string content = null;
|
readBracket();
|
||||||
if (storage.Exists(bracket_filename))
|
|
||||||
using (Stream stream = storage.GetStream(bracket_filename, FileAccess.Read, FileMode.Open))
|
|
||||||
using (var sr = new StreamReader(stream))
|
|
||||||
{
|
|
||||||
content = sr.ReadToEnd();
|
|
||||||
}
|
|
||||||
|
|
||||||
Ladder = content != null ? JsonConvert.DeserializeObject<LadderInfo>(content) : new LadderInfo();
|
ladder.CurrentMatch.Value = ladder.Pairings.FirstOrDefault(p => p.Current.Value);
|
||||||
|
|
||||||
dependencies.Cache(Ladder);
|
|
||||||
|
|
||||||
dependencies.CacheAs<MatchIPCInfo>(ipc = new FileBasedIPC());
|
dependencies.CacheAs<MatchIPCInfo>(ipc = new FileBasedIPC());
|
||||||
Add(ipc);
|
Add(ipc);
|
||||||
|
|
||||||
|
Add(new OsuButton
|
||||||
|
{
|
||||||
|
Text = "Save Changes",
|
||||||
|
Width = 140,
|
||||||
|
Height = 50,
|
||||||
|
Anchor = Anchor.BottomRight,
|
||||||
|
Origin = Anchor.BottomRight,
|
||||||
|
Padding = new MarginPadding(10),
|
||||||
|
Action = SaveChanges,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void readBracket()
|
||||||
|
{
|
||||||
|
if (storage.Exists(bracket_filename))
|
||||||
|
{
|
||||||
|
using (Stream stream = storage.GetStream(bracket_filename, FileAccess.Read, FileMode.Open))
|
||||||
|
using (var sr = new StreamReader(stream))
|
||||||
|
ladder = JsonConvert.DeserializeObject<LadderInfo>(sr.ReadToEnd());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ladder = new LadderInfo();
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies.Cache(ladder);
|
||||||
|
|
||||||
bool addedInfo = false;
|
bool addedInfo = false;
|
||||||
|
|
||||||
// assign teams
|
// assign teams
|
||||||
foreach (var pairing in Ladder.Pairings)
|
foreach (var pairing in ladder.Pairings)
|
||||||
{
|
{
|
||||||
pairing.Team1.Value = Ladder.Teams.FirstOrDefault(t => t.Acronym == pairing.Team1Acronym);
|
pairing.Team1.Value = ladder.Teams.FirstOrDefault(t => t.Acronym == pairing.Team1Acronym);
|
||||||
pairing.Team2.Value = Ladder.Teams.FirstOrDefault(t => t.Acronym == pairing.Team2Acronym);
|
pairing.Team2.Value = ladder.Teams.FirstOrDefault(t => t.Acronym == pairing.Team2Acronym);
|
||||||
|
|
||||||
foreach (var conditional in pairing.ConditionalPairings)
|
foreach (var conditional in pairing.ConditionalPairings)
|
||||||
{
|
{
|
||||||
conditional.Team1.Value = Ladder.Teams.FirstOrDefault(t => t.Acronym == conditional.Team1Acronym);
|
conditional.Team1.Value = ladder.Teams.FirstOrDefault(t => t.Acronym == conditional.Team1Acronym);
|
||||||
conditional.Team2.Value = Ladder.Teams.FirstOrDefault(t => t.Acronym == conditional.Team2Acronym);
|
conditional.Team2.Value = ladder.Teams.FirstOrDefault(t => t.Acronym == conditional.Team2Acronym);
|
||||||
conditional.Grouping.Value = pairing.Grouping.Value;
|
conditional.Grouping.Value = pairing.Grouping.Value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// assign progressions
|
// assign progressions
|
||||||
foreach (var pair in Ladder.Progressions)
|
foreach (var pair in ladder.Progressions)
|
||||||
{
|
{
|
||||||
var src = Ladder.Pairings.FirstOrDefault(p => p.ID == pair.Item1);
|
var src = ladder.Pairings.FirstOrDefault(p => p.ID == pair.Item1);
|
||||||
var dest = Ladder.Pairings.FirstOrDefault(p => p.ID == pair.Item2);
|
var dest = ladder.Pairings.FirstOrDefault(p => p.ID == pair.Item2);
|
||||||
|
|
||||||
if (src == null) throw new InvalidOperationException();
|
if (src == null) throw new InvalidOperationException();
|
||||||
|
|
||||||
@ -106,10 +126,10 @@ namespace osu.Game.Tournament
|
|||||||
}
|
}
|
||||||
|
|
||||||
// link pairings to groupings
|
// link pairings to groupings
|
||||||
foreach (var group in Ladder.Groupings)
|
foreach (var group in ladder.Groupings)
|
||||||
foreach (var id in group.Pairings)
|
foreach (var id in group.Pairings)
|
||||||
{
|
{
|
||||||
var found = Ladder.Pairings.FirstOrDefault(p => p.ID == id);
|
var found = ladder.Pairings.FirstOrDefault(p => p.ID == id);
|
||||||
if (found != null)
|
if (found != null)
|
||||||
{
|
{
|
||||||
found.Grouping.Value = group;
|
found.Grouping.Value = group;
|
||||||
@ -118,10 +138,23 @@ namespace osu.Game.Tournament
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ladder.CurrentMatch.Value = Ladder.Pairings.FirstOrDefault(p => p.Current.Value);
|
addedInfo |= addPlayers();
|
||||||
|
addedInfo |= addBeatmaps();
|
||||||
|
addedInfo |= addCountries();
|
||||||
|
|
||||||
// add full player info based on user IDs
|
if (addedInfo)
|
||||||
foreach (var t in Ladder.Teams)
|
SaveChanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add missing player info based on user IDs.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
private bool addPlayers()
|
||||||
|
{
|
||||||
|
bool addedInfo = false;
|
||||||
|
|
||||||
|
foreach (var t in ladder.Teams)
|
||||||
foreach (var p in t.Players)
|
foreach (var p in t.Players)
|
||||||
if (string.IsNullOrEmpty(p.Username))
|
if (string.IsNullOrEmpty(p.Username))
|
||||||
{
|
{
|
||||||
@ -132,8 +165,16 @@ namespace osu.Game.Tournament
|
|||||||
addedInfo = true;
|
addedInfo = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// add full beatmap info based on beatmap IDs
|
return addedInfo;
|
||||||
foreach (var g in Ladder.Groupings)
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add missing beatmap info based on beatmap IDs
|
||||||
|
/// </summary>
|
||||||
|
private bool addBeatmaps()
|
||||||
|
{
|
||||||
|
bool addedInfo = false;
|
||||||
|
foreach (var g in ladder.Groupings)
|
||||||
foreach (var b in g.Beatmaps)
|
foreach (var b in g.Beatmaps)
|
||||||
if (b.BeatmapInfo == null)
|
if (b.BeatmapInfo == null)
|
||||||
{
|
{
|
||||||
@ -144,36 +185,37 @@ namespace osu.Game.Tournament
|
|||||||
addedInfo = true;
|
addedInfo = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return addedInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add missing country info based on acronyms.
|
||||||
|
/// </summary>
|
||||||
|
private bool addCountries()
|
||||||
|
{
|
||||||
|
bool addedInfo = false;
|
||||||
|
|
||||||
List<TournamentTeam> countries;
|
List<TournamentTeam> countries;
|
||||||
using (Stream stream = Resources.GetStream("Resources/countries.json"))
|
using (Stream stream = Resources.GetStream("Resources/countries.json"))
|
||||||
using (var sr = new StreamReader(stream))
|
using (var sr = new StreamReader(stream))
|
||||||
countries = JsonConvert.DeserializeObject<List<TournamentTeam>>(sr.ReadToEnd());
|
countries = JsonConvert.DeserializeObject<List<TournamentTeam>>(sr.ReadToEnd());
|
||||||
|
|
||||||
foreach (var t in Ladder.Teams)
|
foreach (var t in ladder.Teams)
|
||||||
if (string.IsNullOrEmpty(t.FullName))
|
|
||||||
{
|
|
||||||
var result = countries.FirstOrDefault(c => c.Acronym == t.Acronym);
|
|
||||||
if (result != null)
|
|
||||||
{
|
|
||||||
t.Acronym = result.Acronym;
|
|
||||||
t.FlagName = result.FlagName;
|
|
||||||
t.FullName = result.FullName;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (addedInfo)
|
|
||||||
SaveChanges();
|
|
||||||
|
|
||||||
Add(new OsuButton
|
|
||||||
{
|
{
|
||||||
Text = "Save Changes",
|
if (!string.IsNullOrEmpty(t.FullName))
|
||||||
Width = 140,
|
continue;
|
||||||
Height = 50,
|
|
||||||
Anchor = Anchor.BottomRight,
|
var result = countries.FirstOrDefault(c => c.Acronym == t.Acronym);
|
||||||
Origin = Anchor.BottomRight,
|
|
||||||
Padding = new MarginPadding(10),
|
if (result == null) continue;
|
||||||
Action = SaveChanges,
|
|
||||||
});
|
t.Acronym = result.Acronym;
|
||||||
|
t.FlagName = result.FlagName;
|
||||||
|
t.FullName = result.FullName;
|
||||||
|
addedInfo = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return addedInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void LoadComplete()
|
protected override void LoadComplete()
|
||||||
@ -198,7 +240,7 @@ namespace osu.Game.Tournament
|
|||||||
using (var stream = storage.GetStream(bracket_filename, FileAccess.Write, FileMode.Create))
|
using (var stream = storage.GetStream(bracket_filename, FileAccess.Write, FileMode.Create))
|
||||||
using (var sw = new StreamWriter(stream))
|
using (var sw = new StreamWriter(stream))
|
||||||
{
|
{
|
||||||
sw.Write(JsonConvert.SerializeObject(Ladder,
|
sw.Write(JsonConvert.SerializeObject(ladder,
|
||||||
new JsonSerializerSettings
|
new JsonSerializerSettings
|
||||||
{
|
{
|
||||||
NullValueHandling = NullValueHandling.Ignore,
|
NullValueHandling = NullValueHandling.Ignore,
|
||||||
|
Loading…
Reference in New Issue
Block a user