1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 16:27:26 +08:00
osu-lazer/osu.Game/Screens/Tournament/Drawings.cs

336 lines
12 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
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.Primitives;
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;
using osu.Game.Graphics.UserInterface;
using osu.Game.Screens.Backgrounds;
using osu.Game.Screens.Tournament.Components;
using OpenTK;
using OpenTK.Graphics;
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";
2017-02-27 22:38:29 +08:00
internal override bool ShowOverlays => 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;
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;
public ITeamList TeamList;
2017-03-03 17:47:56 +08:00
[BackgroundDependencyLoader]
2017-03-03 13:11:47 +08:00
private void load(TextureStore textures, Storage storage)
{
this.storage = storage;
if (TeamList == null)
TeamList = new FileTeamList(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
{
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-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
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"
},
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
}
}
},
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();
foreach (Team t in TeamList.Teams)
2017-02-27 13:19:07 +08:00
{
2017-03-03 17:47:56 +08:00
if (groupsContainer.ContainsTeam(t.FullName))
continue;
2017-03-03 14:59:33 +08:00
2017-03-03 17:47:56 +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();
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
}
}
}