1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 02:07:24 +08:00
osu-lazer/osu.Game/Screens/Tournament/Drawings.cs

378 lines
14 KiB
C#
Raw Normal View History

2017-03-03 12:17:24 +08:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using OpenTK;
2017-02-27 14:02:38 +08:00
using OpenTK.Graphics;
2017-02-27 13:19:07 +08:00
using osu.Framework.Graphics;
2017-02-27 14:02:38 +08:00
using osu.Framework.Graphics.Colour;
2017-02-27 13:19:07 +08:00
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
2017-02-27 14:02:38 +08:00
using osu.Framework.Graphics.Sprites;
using osu.Framework.MathUtils;
using osu.Game.Graphics.UserInterface;
2017-02-27 13:19:07 +08:00
using osu.Game.Screens.Backgrounds;
2017-02-27 14:02:38 +08:00
using osu.Game.Screens.Tournament.Components;
2017-02-27 13:19:07 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using osu.Framework.Input;
using System.IO;
using osu.Framework.Allocation;
using osu.Framework.Configuration;
2017-02-28 09:26:37 +08:00
using osu.Framework.Graphics.Textures;
using osu.Framework.IO.Stores;
using osu.Framework.Platform;
2017-03-03 14:59:33 +08:00
using osu.Framework.Logging;
2017-02-27 13:19:07 +08:00
namespace osu.Game.Screens.Tournament
{
public class Drawings : OsuScreen
{
2017-03-03 15:33:40 +08:00
public const string TEAMS_FILENAME = "drawings.txt";
private const string results_filename = "drawings_results.txt";
2017-02-27 13:19:07 +08:00
protected override BackgroundScreen CreateBackground() => new BackgroundScreenDefault();
2017-02-27 22:38:29 +08:00
internal override bool ShowOverlays => false;
2017-02-27 13:19:07 +08:00
private ScrollingTeamContainer teamsContainer;
private GroupsContainer groupsContainer;
2017-03-03 13:51:19 +08:00
private SpriteText fullTeamNameText;
private List<Team> allTeams = new List<Team>();
2017-02-27 22:09:26 +08:00
private DrawingsConfigManager drawingsConfig;
private Task writeOp;
private Storage storage;
[BackgroundDependencyLoader]
2017-03-03 13:11:47 +08:00
private void load(TextureStore textures, Storage storage)
{
this.storage = storage;
2017-03-03 15:33:40 +08:00
if (!storage.Exists(TEAMS_FILENAME))
2017-03-03 11:43:48 +08:00
{
Exit();
return;
}
drawingsConfig = new DrawingsConfigManager(storage);
2017-02-27 22:09:26 +08:00
2017-02-27 14:02:38 +08:00
Children = new Drawable[]
2017-02-27 13:19:07 +08:00
{
2017-02-28 09:26:37 +08:00
new Box()
{
RelativeSizeAxes = Axes.Both,
Colour = new Color4(77, 77, 77, 255)
},
new Sprite()
{
FillMode = FillMode.Fill,
Texture = textures.Get(@"Backgrounds/Drawings/background.png")
},
2017-03-03 12:17:06 +08:00
new FillFlowContainer()
2017-02-27 14:02:38 +08:00
{
RelativeSizeAxes = Axes.Both,
2017-03-03 12:17:06 +08:00
Direction = FillDirection.Right,
2017-02-27 14:02:38 +08:00
Children = new Drawable[]
{
// Main container
new Container()
{
RelativeSizeAxes = Axes.Both,
Width = 0.85f,
2017-02-27 14:02:38 +08:00
Children = new Drawable[]
{
// Visualiser
new VisualiserContainer()
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
2017-02-27 13:19:07 +08:00
RelativeSizeAxes = Axes.X,
Size = new Vector2(1, 10),
2017-02-27 13:19:07 +08:00
Colour = new Color4(255, 204, 34, 255),
Lines = 6
},
// Groups
2017-02-27 22:09:26 +08:00
groupsContainer = new GroupsContainer(drawingsConfig.Get<int>(DrawingsConfig.Groups), drawingsConfig.Get<int>(DrawingsConfig.TeamsPerGroup))
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
RelativeSizeAxes = Axes.Y,
AutoSizeAxes = Axes.X,
Padding = new MarginPadding()
{
Top = 35f,
Bottom = 35f
}
},
// Scrolling teams
teamsContainer = new ScrollingTeamContainer()
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.X,
},
// Scrolling team name
2017-03-03 13:51:19 +08:00
fullTeamNameText = new SpriteText()
{
Anchor = Anchor.Centre,
Origin = Anchor.TopCentre,
Position = new Vector2(0, 45f),
Alpha = 0,
Font = "Exo2.0-Light",
TextSize = 42f
}
}
},
// Control panel container
new Container()
{
RelativeSizeAxes = Axes.Both,
Width = 0.15f,
Children = new Drawable[]
{
new Box()
{
RelativeSizeAxes = Axes.Both,
Colour = new Color4(54, 54, 54, 255)
},
new SpriteText()
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Text = "Control Panel",
TextSize = 22f,
Font = "Exo2.0-Boldd"
},
2017-03-03 12:17:06 +08:00
new FillFlowContainer()
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Width = 0.75f,
Position = new Vector2(0, 35f),
2017-03-03 12:17:06 +08:00
Direction = FillDirection.Down,
Spacing = new Vector2(0, 5f),
Children = new Drawable[]
{
new OsuButton()
{
RelativeSizeAxes = Axes.X,
2017-02-27 14:02:38 +08:00
Text = "Begin random",
Action = teamsContainer.StartScrolling,
},
new OsuButton()
{
RelativeSizeAxes = Axes.X,
Text = "Stop random",
Action = teamsContainer.StopScrolling,
},
new OsuButton()
{
RelativeSizeAxes = Axes.X,
Text = "Reload",
Action = reloadTeams
}
}
},
2017-03-03 12:17:06 +08:00
new FillFlowContainer()
{
Anchor = Anchor.BottomCentre,
Origin = Anchor.BottomCentre,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Width = 0.75f,
Position = new Vector2(0, -5f),
2017-03-03 12:17:06 +08:00
Direction = FillDirection.Down,
Spacing = new Vector2(0, 5f),
Children = new Drawable[]
{
new OsuButton()
{
RelativeSizeAxes = Axes.X,
Text = "Reset",
Action = () => reset(false)
}
}
}
}
}
}
}
2017-02-27 13:19:07 +08:00
};
2017-03-03 13:51:19 +08:00
teamsContainer.OnSelected += onTeamSelected;
teamsContainer.OnScrollStarted += () => fullTeamNameText.FadeOut(200);
2017-03-03 13:51:19 +08:00
reset(true);
}
2017-03-03 13:51:19 +08:00
private void onTeamSelected(Team team)
{
groupsContainer.AddTeam(team);
2017-02-27 13:19:07 +08:00
2017-03-03 13:51:19 +08:00
fullTeamNameText.Text = team.FullName;
fullTeamNameText.FadeIn(200);
2017-02-27 13:19:07 +08:00
2017-03-03 13:51:19 +08:00
writeResults(groupsContainer.ToStringRepresentation());
}
private void writeResults(string text)
{
2017-03-03 13:51:19 +08:00
Action writeAction = () =>
{
2017-03-03 14:59:33 +08:00
try
{
2017-03-03 14:59:33 +08:00
// 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.");
}
2017-03-03 13:51:19 +08:00
};
if (writeOp == null)
writeOp = Task.Run(writeAction);
else
writeOp = writeOp.ContinueWith(t => { writeAction(); });
}
private void reloadTeams()
{
teamsContainer.ClearTeams();
allTeams.Clear();
2017-03-03 14:59:33 +08:00
List<Team> newTeams = new List<Team>();
try
2017-02-27 13:19:07 +08:00
{
2017-03-03 15:33:40 +08:00
using (Stream stream = storage.GetStream(TEAMS_FILENAME, FileAccess.Read, FileMode.Open))
using (StreamReader sr = new StreamReader(stream))
{
while (sr.Peek() != -1)
{
2017-03-03 14:59:33 +08:00
string line = sr.ReadLine().Trim();
if (string.IsNullOrEmpty(line))
continue;
string[] split = line.Split(':');
2017-03-03 14:59:33 +08:00
if (split.Length < 2)
{
Logger.Log($"Invalid team definition: {line}. Expected \"flag_name : team_name : team_acronym\".");
continue;
}
string flagName = split[0].Trim();
2017-03-03 14:59:33 +08:00
string teamName = split[1].Trim();
2017-03-03 14:59:33 +08:00
string acronym = split.Length >= 3 ? split[2].Trim() : teamName;
acronym = acronym.Substring(0, Math.Min(3, acronym.Length));
2017-03-03 14:59:33 +08:00
if (groupsContainer.ContainsTeam(teamName))
continue;
Team t = new Team()
{
FlagName = flagName,
2017-03-03 14:59:33 +08:00
FullName = teamName,
Acronym = acronym
};
2017-03-03 14:59:33 +08:00
newTeams.Add(t);
}
}
2017-03-03 14:59:33 +08:00
allTeams = newTeams;
teamsContainer.AddTeams(allTeams);
}
catch (Exception ex)
{
Logger.Error(ex, "Failed to read teams.");
2017-02-27 13:19:07 +08:00
}
}
2017-02-27 13:19:07 +08:00
private void reset(bool loadLastResults = false)
{
groupsContainer.ClearTeams();
2017-02-27 13:19:07 +08:00
reloadTeams();
2017-03-03 14:59:33 +08:00
if (loadLastResults)
{
2017-03-03 14:59:33 +08:00
try
{
// Read from drawings_results
using (Stream stream = storage.GetStream(results_filename, FileAccess.Read, FileMode.Open))
using (StreamReader sr = new StreamReader(stream))
{
while (sr.Peek() != -1)
{
2017-03-03 14:59:33 +08:00
string line = sr.ReadLine().Trim();
if (string.IsNullOrEmpty(line))
continue;
if (line.ToUpper().StartsWith("GROUP"))
continue;
Team teamToAdd = allTeams.FirstOrDefault(t => t.FullName == line);
if (teamToAdd == null)
continue;
groupsContainer.AddTeam(teamToAdd);
teamsContainer.RemoveTeam(teamToAdd);
}
}
}
2017-03-03 14:59:33 +08:00
catch (Exception ex)
{
2017-03-03 14:59:33 +08:00
Logger.Error(ex, "Failed to read last drawings results.");
}
2017-03-03 14:59:33 +08:00
}
2017-03-03 14:59:33 +08:00
else
{
2017-03-03 14:59:33 +08:00
writeResults(string.Empty);
}
2017-02-27 13:19:07 +08:00
}
}
}