1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 21:57:55 +08:00
osu-lazer/osu.Game.Tournament/Screens/Ladder/LadderManager.cs

273 lines
9.9 KiB
C#
Raw Normal View History

2018-09-22 04:52:25 +08:00
using System;
using System.Collections.Generic;
2018-09-16 04:35:51 +08:00
using System.Linq;
2018-09-22 04:52:25 +08:00
using osu.Framework.Allocation;
2018-09-24 23:57:44 +08:00
using osu.Framework.Caching;
2018-09-22 04:52:25 +08:00
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-09-21 18:58:47 +08:00
using osu.Framework.Graphics.Cursor;
2018-09-16 04:35:51 +08:00
using osu.Framework.Graphics.Lines;
2018-09-21 18:58:47 +08:00
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.States;
2018-09-21 18:58:47 +08:00
using osu.Game.Graphics.UserInterface;
using osu.Game.Tournament.Components;
2018-09-21 17:51:37 +08:00
using osu.Game.Tournament.Screens.Ladder.Components;
using SixLabors.Primitives;
2018-09-21 17:51:37 +08:00
namespace osu.Game.Tournament.Screens.Ladder
{
2018-09-22 04:52:25 +08:00
public class LadderEditorInfo
{
public readonly BindableBool EditingEnabled = new BindableBool();
public List<TournamentTeam> Teams = new List<TournamentTeam>();
2018-09-24 22:30:37 +08:00
public List<TournamentGrouping> Groupings = new List<TournamentGrouping>();
2018-09-22 04:52:25 +08:00
public readonly Bindable<MatchPairing> Selected = new Bindable<MatchPairing>();
}
2018-09-21 18:58:47 +08:00
public class LadderManager : CompositeDrawable, IHasContextMenu
{
public readonly List<TournamentTeam> Teams;
2018-09-16 04:35:51 +08:00
private readonly Container<DrawableMatchPairing> pairingsContainer;
private readonly Container<Path> paths;
2018-09-24 23:57:44 +08:00
private readonly Container headings;
2018-09-22 04:52:25 +08:00
[Cached]
private readonly LadderEditorInfo editorInfo = new LadderEditorInfo();
2018-09-25 02:14:30 +08:00
public LadderManager(LadderInfo info)
{
2018-09-25 02:14:30 +08:00
editorInfo.Teams = Teams = info.Teams;
2018-09-24 22:30:37 +08:00
editorInfo.Groupings = info.Groupings;
RelativeSizeAxes = Axes.Both;
2018-09-21 18:58:47 +08:00
InternalChild = new Container
{
2018-09-16 04:35:51 +08:00
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
2018-09-24 15:34:46 +08:00
new ScrollableContainer
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
paths = new Container<Path> { RelativeSizeAxes = Axes.Both },
2018-09-25 01:31:48 +08:00
headings = new Container { RelativeSizeAxes = Axes.Both },
2018-09-24 15:34:46 +08:00
pairingsContainer = new Container<DrawableMatchPairing> { RelativeSizeAxes = Axes.Both },
}
},
2018-09-22 04:52:25 +08:00
new LadderEditorSettings
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Margin = new MarginPadding(5)
}
2018-09-16 04:35:51 +08:00
}
};
2018-09-25 02:14:30 +08:00
// assign teams
foreach (var pairing in info.Pairings)
{
pairing.Team1.Value = info.Teams.FirstOrDefault(t => t.Acronym == pairing.Team1Acronym);
pairing.Team2.Value = info.Teams.FirstOrDefault(t => t.Acronym == pairing.Team2Acronym);
}
// assign progressions
2018-09-16 04:35:51 +08:00
foreach (var pair in info.Progressions)
2018-09-22 04:52:25 +08:00
{
var src = info.Pairings.FirstOrDefault(p => p.ID == pair.Item1);
var dest = info.Pairings.FirstOrDefault(p => p.ID == pair.Item2);
if (src == null) throw new InvalidOperationException();
if (dest != null)
2018-09-25 01:31:48 +08:00
{
if (pair.Losers)
src.LosersProgression.Value = dest;
else
src.Progression.Value = dest;
}
2018-09-22 04:52:25 +08:00
}
2018-09-16 04:35:51 +08:00
foreach (var pairing in info.Pairings)
addPairing(pairing);
2018-09-24 22:30:37 +08:00
foreach (var group in info.Groupings)
foreach (var id in group.Pairings)
info.Pairings.Single(p => p.ID == id).Grouping.Value = group;
2018-09-24 23:57:44 +08:00
// todo: fix this
Scheduler.AddDelayed(() => layout.Invalidate(), 100, true);
}
2018-09-16 04:35:51 +08:00
public LadderInfo CreateInfo()
{
2018-09-16 04:35:51 +08:00
var pairings = pairingsContainer.Select(p => p.Pairing).ToList();
2018-09-24 22:30:37 +08:00
foreach (var g in editorInfo.Groupings)
g.Pairings = pairings.Where(p => p.Grouping.Value == g).Select(p => p.ID).ToList();
2018-09-16 04:35:51 +08:00
return new LadderInfo
{
Pairings = pairings,
2018-09-25 01:31:48 +08:00
Progressions = pairings.Where(p => p.Progression.Value != null).Select(p => new TournamentProgression(p.ID, p.Progression.Value.ID)).Concat(
pairings.Where(p => p.LosersProgression.Value != null).Select(p => new TournamentProgression(p.ID, p.LosersProgression.Value.ID, true)))
.ToList(),
2018-09-25 02:14:30 +08:00
Teams = editorInfo.Teams,
2018-09-24 22:30:37 +08:00
Groupings = editorInfo.Groupings
2018-09-16 04:35:51 +08:00
};
}
2018-09-16 04:35:51 +08:00
private void addPairing(MatchPairing pairing) => pairingsContainer.Add(new DrawableMatchPairing(pairing));
2018-09-22 04:52:25 +08:00
public MenuItem[] ContextMenuItems
{
2018-09-22 04:52:25 +08:00
get
{
2018-09-22 04:52:25 +08:00
if (!editorInfo.EditingEnabled)
return new MenuItem[0];
return new MenuItem[]
{
new OsuMenuItem("Create new match", MenuItemType.Highlighted, () =>
{
2018-09-25 01:31:48 +08:00
var pos = pairingsContainer.ToLocalSpace(GetContainingInputManager().CurrentState.Mouse.Position);
2018-09-22 04:52:25 +08:00
addPairing(new MatchPairing { Position = new Point((int)pos.X, (int)pos.Y) });
}),
};
}
}
2018-09-16 04:35:51 +08:00
2018-09-24 23:57:44 +08:00
private Cached layout = new Cached();
2018-09-16 04:35:51 +08:00
protected override void Update()
{
base.Update();
2018-09-24 23:57:44 +08:00
if (!layout.IsValid)
updateLayout();
}
private void updateLayout()
{
2018-09-16 04:35:51 +08:00
paths.Clear();
2018-09-24 23:57:44 +08:00
headings.Clear();
2018-09-16 04:35:51 +08:00
2018-09-22 05:36:45 +08:00
int id = 1;
2018-09-16 04:35:51 +08:00
foreach (var pairing in pairingsContainer.OrderBy(d => d.Y).ThenBy(d => d.X))
{
pairing.Pairing.ID = id++;
if (pairing.Pairing.Progression.Value != null)
2018-09-22 04:52:25 +08:00
{
var dest = pairingsContainer.FirstOrDefault(p => p.Pairing == pairing.Pairing.Progression.Value);
if (dest == null)
// clean up outdated progressions.
pairing.Pairing.Progression.Value = null;
else
paths.Add(new ProgressionPath(pairing, dest));
}
2018-09-16 04:35:51 +08:00
}
2018-09-24 23:57:44 +08:00
foreach (var group in editorInfo.Groupings)
{
2018-09-25 01:31:48 +08:00
var topPairing = pairingsContainer.Where(p => !p.Pairing.Losers && p.Pairing.Grouping.Value == group).OrderBy(p => p.Y).FirstOrDefault();
2018-09-24 23:57:44 +08:00
if (topPairing == null) continue;
headings.Add(new DrawableTournamentGrouping(group)
{
Position = headings.ToLocalSpace((topPairing.ScreenSpaceDrawQuad.TopLeft + topPairing.ScreenSpaceDrawQuad.TopRight) / 2),
Margin = new MarginPadding { Bottom = 10 },
Origin = Anchor.BottomCentre,
});
}
2018-09-25 01:31:48 +08:00
foreach (var group in editorInfo.Groupings)
{
var topPairing = pairingsContainer.Where(p => p.Pairing.Losers && p.Pairing.Grouping.Value == group).OrderBy(p => p.Y).FirstOrDefault();
if (topPairing == null) continue;
headings.Add(new DrawableTournamentGrouping(group, true)
{
Position = headings.ToLocalSpace((topPairing.ScreenSpaceDrawQuad.TopLeft + topPairing.ScreenSpaceDrawQuad.TopRight) / 2),
Margin = new MarginPadding { Bottom = 10 },
Origin = Anchor.BottomCentre,
});
}
2018-09-24 23:57:44 +08:00
layout.Validate();
2018-09-16 04:35:51 +08:00
}
2018-09-25 01:31:48 +08:00
public void RequestJoin(MatchPairing pairing, bool losers) => AddInternal(new JoinRequestHandler(pairingsContainer, pairing, losers));
2018-09-16 04:35:51 +08:00
private class JoinRequestHandler : CompositeDrawable
{
2018-09-19 01:00:16 +08:00
private readonly Container<DrawableMatchPairing> pairingsContainer;
2018-09-16 04:35:51 +08:00
public readonly MatchPairing Source;
2018-09-25 01:31:48 +08:00
private readonly bool losers;
2018-09-16 04:35:51 +08:00
2018-09-21 17:51:37 +08:00
private ProgressionPath path;
2018-09-25 01:31:48 +08:00
public JoinRequestHandler(Container<DrawableMatchPairing> pairingsContainer, MatchPairing source, bool losers)
2018-09-16 04:35:51 +08:00
{
2018-09-19 01:00:16 +08:00
this.pairingsContainer = pairingsContainer;
2018-09-16 04:35:51 +08:00
RelativeSizeAxes = Axes.Both;
2018-09-21 17:51:37 +08:00
Source = source;
2018-09-25 01:31:48 +08:00
this.losers = losers;
if (losers)
Source.LosersProgression.Value = null;
else
Source.Progression.Value = null;
2018-09-21 17:51:37 +08:00
}
private DrawableMatchPairing findTarget(InputState state) => pairingsContainer.FirstOrDefault(d => d.ReceiveMouseInputAt(state.Mouse.NativeState.Position));
protected override bool OnMouseMove(InputState state)
{
var found = findTarget(state);
if (found == path?.Destination)
return false;
path?.Expire();
path = null;
if (found == null)
return false;
AddInternal(path = new ProgressionPath(pairingsContainer.First(c => c.Pairing == Source), found) { Alpha = 0.4f });
return base.OnMouseMove(state);
2018-09-16 04:35:51 +08:00
}
protected override bool OnClick(InputState state)
{
2018-09-21 17:51:37 +08:00
var found = findTarget(state);
2018-09-19 01:00:16 +08:00
if (found != null)
{
2018-09-21 18:58:47 +08:00
if (found.Pairing != Source)
2018-09-25 01:31:48 +08:00
{
if (losers)
Source.LosersProgression.Value = found.Pairing;
else
Source.Progression.Value = found.Pairing;
}
2018-09-16 04:35:51 +08:00
Expire();
2018-09-19 01:00:16 +08:00
return true;
}
2018-09-16 04:35:51 +08:00
2018-09-19 01:00:16 +08:00
return false;
2018-09-16 04:35:51 +08:00
}
}
2018-09-21 18:58:47 +08:00
public void Remove(MatchPairing pairing) => pairingsContainer.FirstOrDefault(p => p.Pairing == pairing)?.Remove();
}
}