This commit is contained in:
Dean Herbert
2018-09-16 05:35:51 +09:00
parent ff125f4c71
commit 74014bec40
7 changed files with 148 additions and 96 deletions

View File

@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using Newtonsoft.Json; using Newtonsoft.Json;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Game.Tests.Visual; using osu.Game.Tests.Visual;
using osu.Game.Tournament.Components; using osu.Game.Tournament.Components;
using osu.Game.Tournament.Screens.Ladder.Components; using osu.Game.Tournament.Screens.Ladder.Components;
@ -16,6 +17,9 @@ namespace osu.Game.Tournament.Tests
[Cached] [Cached]
private readonly LadderManager manager; private readonly LadderManager manager;
[Cached]
private Bindable<TournamentConditions> conditions = new Bindable<TournamentConditions>(new TournamentConditions { BestOf = 9 });
public TestCaseLadderManager() public TestCaseLadderManager()
{ {
var teams = JsonConvert.DeserializeObject<List<TournamentTeam>>(File.ReadAllText(@"teams.json")); var teams = JsonConvert.DeserializeObject<List<TournamentTeam>>(File.ReadAllText(@"teams.json"));
@ -28,7 +32,7 @@ namespace osu.Game.Tournament.Tests
{ {
base.Dispose(isDisposing); base.Dispose(isDisposing);
File.WriteAllText(@"bracket.json", JsonConvert.SerializeObject(manager.Info, File.WriteAllText(@"bracket.json", JsonConvert.SerializeObject(manager.CreateInfo(),
new JsonSerializerSettings new JsonSerializerSettings
{ {
NullValueHandling = NullValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore,

View File

@ -75,12 +75,12 @@ namespace osu.Game.Tournament.Tests
} }
}; };
level1.Children[0].Progression = level2.Children[0]; level1.Children[0].Pairing.Progression.Value = level2.Children[0].Pairing;
level1.Children[1].Progression = level2.Children[0]; level1.Children[1].Pairing.Progression.Value = level2.Children[0].Pairing;
AddRepeatStep("change scores", () => pairing1.Team2Score.Value++, 4); AddRepeatStep("change scores", () => pairing1.Team2Score.Value++, 4);
AddStep("add new team", () => pairing2.Team2.Value = new TournamentTeam { FlagName = "PT", FullName = "Portugal" }); AddStep("add new team", () => pairing2.Team2.Value = new TournamentTeam { FlagName = "PT", FullName = "Portugal" });
AddStep("Add progression", () => level1.Children[2].Progression = level2.Children[1]); AddStep("Add progression", () => level1.Children[2].Pairing.Progression.Value = level2.Children[1].Pairing);
AddStep("start match", () => pairing2.StartMatch()); AddStep("start match", () => pairing2.StartMatch());

View File

@ -1,15 +1,12 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>. // Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System.Collections.Generic;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Configuration; using osu.Framework.Configuration;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Lines;
using osu.Framework.Input.EventArgs; using osu.Framework.Input.EventArgs;
using osu.Framework.Input.States; using osu.Framework.Input.States;
using osu.Framework.MathUtils;
using OpenTK; using OpenTK;
using OpenTK.Input; using OpenTK.Input;
using SixLabors.Primitives; using SixLabors.Primitives;
@ -20,32 +17,8 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
{ {
public readonly MatchPairing Pairing; public readonly MatchPairing Pairing;
private readonly FillFlowContainer<DrawableMatchTeam> flow; private readonly FillFlowContainer<DrawableMatchTeam> flow;
private DrawableMatchPairing progression;
private readonly Bindable<TournamentConditions> conditions = new Bindable<TournamentConditions>(); private readonly Bindable<TournamentConditions> conditions = new Bindable<TournamentConditions>();
private readonly Path path;
public DrawableMatchPairing Progression
{
get => progression;
set
{
if (progression == value) return;
progression = value;
if (LoadState == LoadState.Loaded)
updateProgression();
path.FadeInFromZero(200);
}
}
private Vector2 progressionStart;
private Vector2 progressionEnd;
private const float line_width = 2;
public DrawableMatchPairing(MatchPairing pairing) public DrawableMatchPairing(MatchPairing pairing)
{ {
Pairing = pairing; Pairing = pairing;
@ -63,14 +36,7 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
AutoSizeAxes = Axes.Both, AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical, Direction = FillDirection.Vertical,
Spacing = new Vector2(2) Spacing = new Vector2(2)
}, }
path = new Path
{
Alpha = 0,
BypassAutoSizeAxes = Axes.Both,
Anchor = Anchor.CentreRight,
PathWidth = line_width,
},
}; };
pairing.Team1.BindValueChanged(_ => updateTeams()); pairing.Team1.BindValueChanged(_ => updateTeams());
@ -80,6 +46,7 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
pairing.Team2Score.BindValueChanged(_ => updateWinConditions()); pairing.Team2Score.BindValueChanged(_ => updateWinConditions());
pairing.Completed.BindValueChanged(_ => updateProgression()); pairing.Completed.BindValueChanged(_ => updateProgression());
pairing.Progression.BindValueChanged(_ => updateProgression());
updateTeams(); updateTeams();
} }
@ -95,58 +62,16 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
private void updateProgression() private void updateProgression()
{ {
if (progression == null) var progression = Pairing.Progression?.Value;
{
path.Positions = new List<Vector2>();
return;
}
Vector2 getCenteredVector(Vector2 top, Vector2 bottom) => new Vector2(top.X, top.Y + (bottom.Y - top.Y) / 2); if (progression == null) return;
const float padding = 5; bool progressionAbove = progression.ID < Pairing.ID;
var start = getCenteredVector(ScreenSpaceDrawQuad.TopRight, ScreenSpaceDrawQuad.BottomRight);
var end = getCenteredVector(progression.ScreenSpaceDrawQuad.TopLeft, progression.ScreenSpaceDrawQuad.BottomLeft);
bool progressionAbove = progression.ScreenSpaceDrawQuad.TopLeft.Y < ScreenSpaceDrawQuad.TopLeft.Y;
bool progressionToRight = progression.ScreenSpaceDrawQuad.TopLeft.X > ScreenSpaceDrawQuad.TopLeft.X;
if (!Precision.AlmostEquals(progressionStart, start) || !Precision.AlmostEquals(progressionEnd, end))
{
progressionStart = start;
progressionEnd = end;
path.Origin = progressionAbove ? Anchor.y2 : Anchor.y0;
path.Y = progressionAbove ? line_width : -line_width;
path.Origin |= progressionToRight ? Anchor.x0 : Anchor.x2;
//path.X = progressionToRight ? line_width : -line_width;
Vector2 startPosition = path.ToLocalSpace(start) + new Vector2(padding, 0);
Vector2 endPosition = path.ToLocalSpace(end) + new Vector2(-padding, 0);
Vector2 intermediate1 = startPosition + new Vector2(padding, 0);
Vector2 intermediate2 = new Vector2(intermediate1.X, endPosition.Y);
path.Positions = new List<Vector2>
{
startPosition,
intermediate1,
intermediate2,
endPosition
};
}
var destinationForWinner = progressionAbove ? progression.Pairing.Team2 : progression.Pairing.Team1;
var destinationForWinner = progressionAbove ? progression.Team2 : progression.Team1;
destinationForWinner.Value = Pairing.Winner; destinationForWinner.Value = Pairing.Winner;
} }
protected override void UpdateAfterAutoSize()
{
base.UpdateAfterAutoSize();
updateProgression();
}
private void updateWinConditions() private void updateWinConditions()
{ {
if (conditions.Value == null) return; if (conditions.Value == null) return;

View File

@ -160,6 +160,7 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
public MenuItem[] ContextMenuItems => new[] public MenuItem[] ContextMenuItems => new[]
{ {
new MenuItem("Populate team", () => team.Value = manager.Teams.Random()), new MenuItem("Populate team", () => team.Value = manager.Teams.Random()),
new MenuItem("Join with", () => manager.JoinRequest(pairing)),
}; };
} }

View File

@ -5,5 +5,6 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
public class LadderInfo public class LadderInfo
{ {
public List<MatchPairing> Pairings = new List<MatchPairing>(); public List<MatchPairing> Pairings = new List<MatchPairing>();
public List<(int, int)> Progressions = new List<(int, int)>();
} }
} }

View File

@ -1,51 +1,166 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Lines;
using osu.Framework.Input.States; using osu.Framework.Input.States;
using osu.Game.Graphics.Cursor; using osu.Game.Graphics.Cursor;
using osu.Game.Tournament.Components; using osu.Game.Tournament.Components;
using OpenTK;
using SixLabors.Primitives; using SixLabors.Primitives;
namespace osu.Game.Tournament.Screens.Ladder.Components namespace osu.Game.Tournament.Screens.Ladder.Components
{ {
public class LadderManager : CompositeDrawable public class LadderManager : CompositeDrawable
{ {
public readonly LadderInfo Info;
public readonly List<TournamentTeam> Teams; public readonly List<TournamentTeam> Teams;
private readonly OsuContextMenuContainer content; private readonly Container<DrawableMatchPairing> pairingsContainer;
private readonly Container<Path> paths;
public LadderManager(LadderInfo info, List<TournamentTeam> teams) public LadderManager(LadderInfo info, List<TournamentTeam> teams)
{ {
Info = info;
Teams = teams; Teams = teams;
RelativeSizeAxes = Axes.Both; RelativeSizeAxes = Axes.Both;
InternalChild = content = new OsuContextMenuContainer InternalChild = new OsuContextMenuContainer
{ {
RelativeSizeAxes = Axes.Both RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
pairingsContainer = new Container<DrawableMatchPairing> { RelativeSizeAxes = Axes.Both },
paths = new Container<Path> { RelativeSizeAxes = Axes.Both },
}
}; };
foreach (var pair in info.Progressions)
info.Pairings.Single(p => p.ID == pair.Item1).Progression.Value = info.Pairings.Single(p => p.ID == pair.Item2);
foreach (var pairing in info.Pairings) foreach (var pairing in info.Pairings)
addPairing(pairing); addPairing(pairing);
} }
protected void AddPairing(MatchPairing pairing) public LadderInfo CreateInfo()
{ {
Info.Pairings.Add(pairing); var pairings = pairingsContainer.Select(p => p.Pairing).ToList();
addPairing(pairing);
return new LadderInfo
{
Pairings = pairings,
Progressions = pairings
.Where(p => p.Progression.Value != null)
.Select(p => (p.ID, p.Progression.Value.ID))
.ToList()
};
} }
private void addPairing(MatchPairing pairing) => content.Add(new DrawableMatchPairing(pairing)); private void addPairing(MatchPairing pairing) => pairingsContainer.Add(new DrawableMatchPairing(pairing));
protected override bool OnClick(InputState state) protected override bool OnClick(InputState state)
{ {
AddPairing(new MatchPairing addPairing(new MatchPairing
{ {
Position = new Point((int)state.Mouse.Position.X, (int)state.Mouse.Position.Y) Position = new Point((int)state.Mouse.Position.X, (int)state.Mouse.Position.Y)
}); });
return true; return true;
} }
protected override void Update()
{
base.Update();
paths.Clear();
int id = 0;
foreach (var pairing in pairingsContainer.OrderBy(d => d.Y).ThenBy(d => d.X))
{
pairing.Pairing.ID = id++;
if (pairing.Pairing.Progression.Value != null)
{
var progression = pairingsContainer.Single(p => p.Pairing == pairing.Pairing.Progression.Value);
const float line_width = 2;
var path = new Path
{
BypassAutoSizeAxes = Axes.Both,
PathWidth = line_width,
};
paths.Add(path);
Vector2 getCenteredVector(Vector2 top, Vector2 bottom) => new Vector2(top.X, top.Y + (bottom.Y - top.Y) / 2);
const float padding = 5;
var start = getCenteredVector(pairing.ScreenSpaceDrawQuad.TopRight, pairing.ScreenSpaceDrawQuad.BottomRight);
var end = getCenteredVector(progression.ScreenSpaceDrawQuad.TopLeft, progression.ScreenSpaceDrawQuad.BottomLeft);
bool progressionAbove = progression.ScreenSpaceDrawQuad.TopLeft.Y < pairing.ScreenSpaceDrawQuad.TopLeft.Y;
bool progressionToRight = progression.ScreenSpaceDrawQuad.TopLeft.X > pairing.ScreenSpaceDrawQuad.TopLeft.X;
//if (!Precision.AlmostEquals(progressionStart, start) || !Precision.AlmostEquals(progressionEnd, end))
{
// var progressionStart = start;
// var progressionEnd = end;
Vector2 startPosition = path.ToLocalSpace(start) + new Vector2(padding, 0);
Vector2 endPosition = path.ToLocalSpace(end) + new Vector2(-padding, 0);
Vector2 intermediate1 = startPosition + new Vector2(padding, 0);
Vector2 intermediate2 = new Vector2(intermediate1.X, endPosition.Y);
path.Positions = new List<Vector2>
{
startPosition,
intermediate1,
intermediate2,
endPosition
};
}
}
}
}
public void JoinRequest(MatchPairing pairing)
{
AddInternal(new JoinRequestHandler(pairing, handleProgression));
}
private bool handleProgression(JoinRequestHandler handler, InputState state)
{
var found = pairingsContainer.FirstOrDefault(d => d.ReceiveMouseInputAt(state.Mouse.NativeState.Position));
if (found != null)
{
handler.Source.Progression.Value = found.Pairing;
return true;
}
return false;
}
private class JoinRequestHandler : CompositeDrawable
{
public readonly MatchPairing Source;
private readonly Func<JoinRequestHandler, InputState, bool> onClick;
public JoinRequestHandler(MatchPairing source, Func<JoinRequestHandler, InputState, bool> onClick)
{
Source = source;
this.onClick = onClick;
RelativeSizeAxes = Axes.Both;
}
protected override bool OnClick(InputState state)
{
if (onClick(this, state))
Expire();
return true;
}
}
} }
} }

View File

@ -13,6 +13,8 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
/// </summary> /// </summary>
public class MatchPairing public class MatchPairing
{ {
public int ID;
public readonly Bindable<TournamentTeam> Team1 = new Bindable<TournamentTeam>(); public readonly Bindable<TournamentTeam> Team1 = new Bindable<TournamentTeam>();
public readonly Bindable<int?> Team1Score = new Bindable<int?>(); public readonly Bindable<int?> Team1Score = new Bindable<int?>();
@ -23,6 +25,9 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
public readonly Bindable<bool> Completed = new Bindable<bool>(); public readonly Bindable<bool> Completed = new Bindable<bool>();
[JsonIgnore]
public readonly Bindable<MatchPairing> Progression = new Bindable<MatchPairing>();
[JsonProperty] [JsonProperty]
public Point Position; public Point Position;
@ -36,7 +41,8 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
Team2.Value = team2; Team2.Value = team2;
} }
public TournamentTeam Winner => !Completed.Value ? null : (Team1Score.Value > Team2Score.Value ? Team1.Value : Team2.Value); [JsonIgnore]
public TournamentTeam Winner => !Completed.Value ? null : Team1Score.Value > Team2Score.Value ? Team1.Value : Team2.Value;
/// <summary> /// <summary>
/// Remove scores from the match, in case of a false click or false start. /// Remove scores from the match, in case of a false click or false start.