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

352 lines
13 KiB
C#
Raw Normal View History

2018-01-05 19:21:19 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
2017-03-03 12:17:24 +08:00
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2017-02-27 13:19:07 +08:00
using System;
using System.Collections.Generic;
using System.IO;
2017-02-27 13:19:07 +08:00
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
2017-02-28 09:26:37 +08:00
using osu.Framework.Graphics.Textures;
2017-03-03 14:59:33 +08:00
using osu.Framework.Logging;
using osu.Framework.Platform;
2017-03-03 19:27:53 +08:00
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Screens.Backgrounds;
using osu.Game.Screens.Tournament.Components;
2017-03-03 19:42:22 +08:00
using osu.Game.Screens.Tournament.Teams;
using OpenTK;
using OpenTK.Graphics;
using osu.Framework.IO.Stores;
using osu.Framework.Graphics.Shapes;
2017-02-27 13:19:07 +08:00
namespace osu.Game.Screens.Tournament
{
public class Drawings : OsuScreen
{
private const string results_filename = "drawings_results.txt";
2018-01-04 23:42:48 +08:00
public override bool ShowOverlaysOnEnter => false;
2017-02-27 13:19:07 +08:00
2017-03-03 17:47:56 +08:00
protected override BackgroundScreen CreateBackground() => new BackgroundScreenDefault();
private ScrollingTeamContainer teamsContainer;
2017-03-03 19:46:07 +08:00
private GroupContainer groupsContainer;
2017-03-03 19:27:53 +08:00
private OsuSpriteText fullTeamNameText;
2017-05-02 17:00:37 +08:00
private readonly List<DrawingsTeam> allTeams = new List<DrawingsTeam>();
2017-02-27 22:09:26 +08:00
private DrawingsConfigManager drawingsConfig;
private Task writeOp;
private Storage storage;
public ITeamList TeamList;
2017-03-03 17:47:56 +08:00
private DependencyContainer dependencies;
protected override IReadOnlyDependencyContainer CreateLocalDependencies(IReadOnlyDependencyContainer parent) =>
dependencies = new DependencyContainer(base.CreateLocalDependencies(parent));
2017-05-02 13:21:22 +08:00
[BackgroundDependencyLoader]
private void load(TextureStore textures, Storage storage)
{
this.storage = storage;
TextureStore flagStore = new TextureStore();
// Local flag store
flagStore.AddStore(new RawTextureLoaderStore(new NamespacedResourceStore<byte[]>(new StorageBackedResourceStore(storage), "Drawings")));
2017-05-02 16:09:16 +08:00
// Default texture store
flagStore.AddStore(textures);
2017-05-02 13:21:22 +08:00
dependencies.Cache(flagStore);
if (TeamList == null)
2017-03-03 19:42:22 +08:00
TeamList = new StorageBackedTeamList(storage);
if (!TeamList.Teams.Any())
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
{
new Box
2017-02-28 09:26:37 +08:00
{
RelativeSizeAxes = Axes.Both,
Colour = new Color4(77, 77, 77, 255)
},
new Sprite
2017-02-28 09:26:37 +08:00
{
RelativeSizeAxes = Axes.Both,
2017-02-28 09:26:37 +08:00
FillMode = FillMode.Fill,
Texture = textures.Get(@"Backgrounds/Drawings/background.png")
},
new FillFlowContainer
2017-02-27 14:02:38 +08:00
{
RelativeSizeAxes = Axes.Both,
2017-03-04 18:00:17 +08:00
Direction = FillDirection.Horizontal,
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-05-02 13:21:22 +08:00
groupsContainer = new GroupContainer(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
2017-05-02 13:21:22 +08:00
teamsContainer = new ScrollingTeamContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.X,
},
// Scrolling team name
2017-03-03 19:27:53 +08:00
fullTeamNameText = new OsuSpriteText
{
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)
},
2017-03-03 19:27:53 +08:00
new OsuSpriteText
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Text = "Control Panel",
TextSize = 22f,
2017-03-21 21:51:49 +08:00
Font = "Exo2.0-Bold"
},
new FillFlowContainer
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Width = 0.75f,
Position = new Vector2(0, 35f),
2017-03-04 18:00:17 +08:00
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 5f),
Children = new Drawable[]
{
new TriangleButton
{
RelativeSizeAxes = Axes.X,
2017-02-27 14:02:38 +08:00
Text = "Begin random",
Action = teamsContainer.StartScrolling,
},
new TriangleButton
{
RelativeSizeAxes = Axes.X,
Text = "Stop random",
Action = teamsContainer.StopScrolling,
},
new TriangleButton
{
RelativeSizeAxes = Axes.X,
Text = "Reload",
Action = reloadTeams
}
}
},
new FillFlowContainer
{
Anchor = Anchor.BottomCentre,
Origin = Anchor.BottomCentre,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Width = 0.75f,
Position = new Vector2(0, -5f),
2017-03-04 18:00:17 +08:00
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 5f),
Children = new Drawable[]
{
new TriangleButton
{
RelativeSizeAxes = Axes.X,
Text = "Reset",
Action = () => reset()
}
}
}
}
}
}
}
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-05-02 17:00:37 +08:00
private void onTeamSelected(DrawingsTeam team)
2017-03-03 13:51:19 +08:00
{
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 19:42:22 +08:00
writeResults(groupsContainer.GetStringRepresentation());
}
private void writeResults(string text)
{
2018-01-16 02:42:17 +08:00
void 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.");
}
2018-01-16 02:42:17 +08:00
}
2017-03-03 13:51:19 +08:00
2018-01-16 02:42:17 +08:00
writeOp = writeOp?.ContinueWith(t => { writeAction(); }) ?? Task.Run((Action)writeAction);
}
private void reloadTeams()
{
teamsContainer.ClearTeams();
allTeams.Clear();
2017-05-02 17:00:37 +08:00
foreach (DrawingsTeam t in TeamList.Teams)
2017-02-27 13:19:07 +08:00
{
2017-03-18 05:16:59 +08:00
if (groupsContainer.ContainsTeam(t.FullName))
2017-03-03 17:47:56 +08:00
continue;
2017-03-03 14:59:33 +08:00
2017-03-18 05:16:59 +08:00
allTeams.Add(t);
teamsContainer.AddTeam(t);
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();
if (!storage.Exists(results_filename))
return;
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))
{
2017-03-03 19:24:34 +08:00
string line;
while ((line = sr.ReadLine()?.Trim()) != null)
{
if (string.IsNullOrEmpty(line))
continue;
if (line.ToUpper().StartsWith("GROUP"))
continue;
// ReSharper disable once AccessToModifiedClosure
2017-05-02 17:00:37 +08:00
DrawingsTeam 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
else
{
2017-03-03 14:59:33 +08:00
writeResults(string.Empty);
}
2017-02-27 13:19:07 +08:00
}
}
}