mirror of
https://github.com/ppy/osu.git
synced 2025-01-14 19:22:56 +08:00
Better way to initialize team lists.
This commit is contained in:
parent
890066dae4
commit
5a4d07d770
@ -19,31 +19,78 @@ namespace osu.Desktop.VisualTests.Tests
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(Storage storage)
|
||||
{
|
||||
string[] testTeams =
|
||||
{
|
||||
"GB:United Kingdom:UK",
|
||||
"FR:France:FRA",
|
||||
"CN:China:CHN",
|
||||
"AU:Australia:AUS",
|
||||
"JP:Japan:JPN",
|
||||
"RO:Romania",
|
||||
"IT:Italy",
|
||||
"VE:Venezuela:VNZ"
|
||||
};
|
||||
|
||||
using (Stream stream = storage.GetStream(Drawings.TEAMS_FILENAME, FileAccess.Write, FileMode.Create))
|
||||
using (StreamWriter sw = new StreamWriter(stream))
|
||||
{
|
||||
foreach (string line in testTeams)
|
||||
sw.WriteLine(line);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
|
||||
Add(new Drawings());
|
||||
Add(new Drawings(new TestTeamList()));
|
||||
}
|
||||
|
||||
class TestTeamList : ITeamList
|
||||
{
|
||||
public IEnumerable<Team> Teams
|
||||
{
|
||||
get
|
||||
{
|
||||
return new Team[]
|
||||
{
|
||||
new Team()
|
||||
{
|
||||
FlagName = "GB",
|
||||
FullName = "United Kingdom",
|
||||
Acronym = "UK"
|
||||
},
|
||||
new Team()
|
||||
{
|
||||
FlagName = "FR",
|
||||
FullName = "France",
|
||||
Acronym = "FRA"
|
||||
},
|
||||
new Team()
|
||||
{
|
||||
FlagName = "CN",
|
||||
FullName = "China",
|
||||
Acronym = "CHN"
|
||||
},
|
||||
new Team()
|
||||
{
|
||||
FlagName = "AU",
|
||||
FullName = "Australia",
|
||||
Acronym = "AUS"
|
||||
},
|
||||
new Team()
|
||||
{
|
||||
FlagName = "JP",
|
||||
FullName = "Japan",
|
||||
Acronym = "JPN"
|
||||
},
|
||||
new Team()
|
||||
{
|
||||
FlagName = "RO",
|
||||
FullName = "Romania",
|
||||
},
|
||||
new Team()
|
||||
{
|
||||
FlagName = "IT",
|
||||
FullName = "Italy",
|
||||
},
|
||||
new Team()
|
||||
{
|
||||
FlagName = "VE",
|
||||
FullName = "Venezuela",
|
||||
Acronym = "VNZ"
|
||||
},
|
||||
new Team()
|
||||
{
|
||||
FlagName = "US",
|
||||
FullName = "United States of America",
|
||||
Acronym = "USA"
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -105,7 +105,7 @@ namespace osu.Game.Screens.Menu
|
||||
{
|
||||
if (!args.Repeat && state.Keyboard.ControlPressed && state.Keyboard.ShiftPressed && args.Key == Key.D)
|
||||
{
|
||||
Push(new Drawings());
|
||||
Push(new Drawings(new FileTeamList(Game.Host.Storage)));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -30,12 +30,12 @@ namespace osu.Game.Screens.Tournament
|
||||
{
|
||||
public class Drawings : OsuScreen
|
||||
{
|
||||
public const string TEAMS_FILENAME = "drawings.txt";
|
||||
private const string results_filename = "drawings_results.txt";
|
||||
|
||||
protected override BackgroundScreen CreateBackground() => new BackgroundScreenDefault();
|
||||
internal override bool ShowOverlays => false;
|
||||
|
||||
protected override BackgroundScreen CreateBackground() => new BackgroundScreenDefault();
|
||||
|
||||
private ScrollingTeamContainer teamsContainer;
|
||||
private GroupsContainer groupsContainer;
|
||||
private SpriteText fullTeamNameText;
|
||||
@ -48,12 +48,19 @@ namespace osu.Game.Screens.Tournament
|
||||
|
||||
private Storage storage;
|
||||
|
||||
private ITeamList teamList;
|
||||
|
||||
public Drawings(ITeamList teamList)
|
||||
{
|
||||
this.teamList = teamList;
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(TextureStore textures, Storage storage)
|
||||
{
|
||||
this.storage = storage;
|
||||
|
||||
if (!storage.Exists(TEAMS_FILENAME))
|
||||
if (teamList.Teams.Count() == 0)
|
||||
{
|
||||
Exit();
|
||||
return;
|
||||
@ -277,54 +284,15 @@ namespace osu.Game.Screens.Tournament
|
||||
teamsContainer.ClearTeams();
|
||||
allTeams.Clear();
|
||||
|
||||
List<Team> newTeams = new List<Team>();
|
||||
List<Team> newTeams = teamList.Teams.ToList();
|
||||
|
||||
try
|
||||
foreach (Team t in newTeams)
|
||||
{
|
||||
using (Stream stream = storage.GetStream(TEAMS_FILENAME, FileAccess.Read, FileMode.Open))
|
||||
using (StreamReader sr = new StreamReader(stream))
|
||||
{
|
||||
while (sr.Peek() != -1)
|
||||
{
|
||||
string line = sr.ReadLine().Trim();
|
||||
if (groupsContainer.ContainsTeam(t.FullName))
|
||||
continue;
|
||||
|
||||
if (string.IsNullOrEmpty(line))
|
||||
continue;
|
||||
|
||||
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 teamName = split[1].Trim();
|
||||
|
||||
string acronym = split.Length >= 3 ? split[2].Trim() : teamName;
|
||||
acronym = acronym.Substring(0, Math.Min(3, acronym.Length));
|
||||
|
||||
if (groupsContainer.ContainsTeam(teamName))
|
||||
continue;
|
||||
|
||||
Team t = new Team()
|
||||
{
|
||||
FlagName = flagName,
|
||||
FullName = teamName,
|
||||
Acronym = acronym
|
||||
};
|
||||
|
||||
newTeams.Add(t);
|
||||
}
|
||||
}
|
||||
|
||||
allTeams = newTeams;
|
||||
teamsContainer.AddTeams(allTeams);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex, "Failed to read teams.");
|
||||
allTeams.Add(t);
|
||||
teamsContainer.AddTeam(t);
|
||||
}
|
||||
}
|
||||
|
||||
|
76
osu.Game/Screens/Tournament/FileTeamList.cs
Normal file
76
osu.Game/Screens/Tournament/FileTeamList.cs
Normal file
@ -0,0 +1,76 @@
|
||||
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
||||
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||
|
||||
using osu.Framework.Logging;
|
||||
using osu.Framework.Platform;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace osu.Game.Screens.Tournament
|
||||
{
|
||||
public class FileTeamList : ITeamList
|
||||
{
|
||||
private const string teams_filename = "drawings.txt";
|
||||
|
||||
private Storage storage;
|
||||
|
||||
public FileTeamList(Storage storage)
|
||||
{
|
||||
this.storage = storage;
|
||||
}
|
||||
|
||||
public IEnumerable<Team> Teams
|
||||
{
|
||||
get
|
||||
{
|
||||
List<Team> teams = new List<Team>();
|
||||
|
||||
try
|
||||
{
|
||||
using (Stream stream = storage.GetStream(teams_filename, FileAccess.Read, FileMode.Open))
|
||||
using (StreamReader sr = new StreamReader(stream))
|
||||
{
|
||||
while (sr.Peek() != -1)
|
||||
{
|
||||
string line = sr.ReadLine().Trim();
|
||||
|
||||
if (string.IsNullOrEmpty(line))
|
||||
continue;
|
||||
|
||||
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 teamName = split[1].Trim();
|
||||
|
||||
string acronym = split.Length >= 3 ? split[2].Trim() : teamName;
|
||||
acronym = acronym.Substring(0, Math.Min(3, acronym.Length));
|
||||
|
||||
teams.Add(new Team()
|
||||
{
|
||||
FlagName = flagName,
|
||||
FullName = teamName,
|
||||
Acronym = acronym
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex, "Failed to read teams.");
|
||||
}
|
||||
|
||||
return teams;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
16
osu.Game/Screens/Tournament/ITeamList.cs
Normal file
16
osu.Game/Screens/Tournament/ITeamList.cs
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
||||
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace osu.Game.Screens.Tournament
|
||||
{
|
||||
public interface ITeamList
|
||||
{
|
||||
IEnumerable<Team> Teams { get; }
|
||||
}
|
||||
}
|
@ -216,8 +216,10 @@
|
||||
<Compile Include="Screens\Tournament\Drawings.cs" />
|
||||
<Compile Include="Screens\Tournament\Group.cs" />
|
||||
<Compile Include="Screens\Tournament\GroupsContainer.cs" />
|
||||
<Compile Include="Screens\Tournament\ITeamList.cs" />
|
||||
<Compile Include="Screens\Tournament\ScrollingTeamContainer.cs" />
|
||||
<Compile Include="Screens\Tournament\Team.cs" />
|
||||
<Compile Include="Screens\Tournament\FileTeamList.cs" />
|
||||
<Compile Include="Users\User.cs" />
|
||||
<Compile Include="Graphics\UserInterface\Volume\VolumeControl.cs" />
|
||||
<Compile Include="Database\BeatmapDatabase.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user