1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 14:12:55 +08:00

Add basic ladder manager and state retention

This commit is contained in:
Dean Herbert 2018-09-10 04:51:38 +09:00
parent e6baf418fb
commit 234b04dfc5
10 changed files with 199 additions and 24 deletions

View File

@ -0,0 +1,34 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using osu.Framework.Allocation;
using osu.Game.Tests.Visual;
using osu.Game.Tournament.Components;
using osu.Game.Tournament.Screens.Ladder.Components;
namespace osu.Game.Tournament.Tests
{
public class TestCaseLadderManager : OsuTestCase
{
[Cached]
private readonly LadderManager manager;
public TestCaseLadderManager()
{
var teams = JsonConvert.DeserializeObject<List<TournamentTeam>>(File.ReadAllText(@"teams.json"));
var ladder = JsonConvert.DeserializeObject<LadderInfo>(File.ReadAllText(@"bracket.json")) ?? new LadderInfo();
Child = manager = new LadderManager(ladder, teams);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
File.WriteAllText(@"bracket.json", JsonConvert.SerializeObject(manager.Info));
}
}
}

View File

@ -28,8 +28,8 @@ namespace osu.Game.Tournament.Tests
public TestCaseMatchPairings()
{
FillFlowContainer<DrawableMatchPairing> level1;
FillFlowContainer<DrawableMatchPairing> level2;
Container<DrawableMatchPairing> level1;
Container<DrawableMatchPairing> level2;
var pairing1 = new MatchPairing(
new TournamentTeam { FlagName = "AU", FullName = "Australia", },
@ -47,16 +47,14 @@ namespace osu.Game.Tournament.Tests
}
);
Child = new FillFlowContainer
Child = new Container
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Children = new Drawable[]
{
level1 = new FillFlowContainer<DrawableMatchPairing>
level1 = new Container<DrawableMatchPairing>
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Children = new[]
{
new DrawableMatchPairing(pairing1),
@ -64,10 +62,9 @@ namespace osu.Game.Tournament.Tests
new DrawableMatchPairing(new MatchPairing()),
}
},
level2 = new FillFlowContainer<DrawableMatchPairing>
level2 = new Container<DrawableMatchPairing>
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Margin = new MarginPadding(20),
Children = new[]
{

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,9 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System.Collections.Generic;
using osu.Game.Users;
namespace osu.Game.Tournament.Components
{
public class TournamentTeam
@ -10,10 +13,16 @@ namespace osu.Game.Tournament.Components
/// </summary>
public string FullName;
private string flagName;
/// <summary>
/// Name of the file containing the flag.
/// </summary>
public string FlagName;
public string FlagName
{
get { return flagName ?? Acronym.Substring(0, 2); }
set { flagName = value; }
}
private string acronym;
@ -25,5 +34,7 @@ namespace osu.Game.Tournament.Components
get { return acronym ?? FullName.Substring(0, 3); }
set { acronym = value; }
}
public List<User> Players { get; set; }
}
}

View File

@ -50,7 +50,6 @@ namespace osu.Game.Tournament.Screens.Drawings.Components
teams.Add(new TournamentTeam
{
FlagName = split[0].Trim(),
FullName = split[1].Trim(),
Acronym = split.Length >= 3 ? split[2].Trim() : null
});

View File

@ -7,8 +7,12 @@ using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Lines;
using osu.Framework.Input.EventArgs;
using osu.Framework.Input.States;
using osu.Framework.MathUtils;
using OpenTK;
using OpenTK.Input;
using SixLabors.Primitives;
namespace osu.Game.Tournament.Screens.Ladder.Components
{
@ -46,6 +50,8 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
{
Pairing = pairing;
Position = new Vector2(pairing.Position.X, pairing.Position.Y);
AutoSizeAxes = Axes.Both;
Margin = new MarginPadding(5);
@ -103,15 +109,19 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
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.BottomLeft : Anchor.TopLeft;
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);
@ -131,6 +141,12 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
destinationForWinner.Value = Pairing.Winner;
}
protected override void UpdateAfterAutoSize()
{
base.UpdateAfterAutoSize();
updateProgression();
}
private void updateWinConditions()
{
if (conditions.Value == null) return;
@ -163,5 +179,20 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
SchedulerAfterChildren.Add(() => Scheduler.Add(updateProgression));
updateWinConditions();
}
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args) => args.Button == MouseButton.Left;
protected override bool OnDragStart(InputState state) => true;
protected override bool OnDrag(InputState state)
{
if (base.OnDrag(state)) return true;
this.MoveToOffset(state.Mouse.Delta);
var pos = Position;
Pairing.Position = new Point((int)pos.X, (int)pos.Y);
return true;
}
}
}

View File

@ -2,11 +2,15 @@
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.EventArgs;
using osu.Framework.Input.States;
using osu.Game.Graphics;
@ -18,8 +22,9 @@ using OpenTK.Input;
namespace osu.Game.Tournament.Screens.Ladder.Components
{
public class DrawableMatchTeam : DrawableTournamentTeam
public class DrawableMatchTeam : DrawableTournamentTeam, IHasContextMenu
{
private readonly Bindable<TournamentTeam> team;
private readonly MatchPairing pairing;
private OsuSpriteText scoreText;
private Box background;
@ -31,10 +36,12 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
private Color4 colourNormal;
private readonly Func<bool> isWinner;
private LadderManager manager;
public DrawableMatchTeam(TournamentTeam team, MatchPairing pairing)
public DrawableMatchTeam(Bindable<TournamentTeam> team, MatchPairing pairing)
: base(team)
{
this.team = team.GetBoundCopy();
this.pairing = pairing;
Size = new Vector2(150, 40);
@ -53,14 +60,16 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
isWinner = () => pairing.Winner == Team;
completed.BindTo(pairing.Completed);
if (team != null)
score.BindTo(team == pairing.Team1.Value ? pairing.Team1Score : pairing.Team2Score);
if (team.Value != null)
score.BindTo(team.Value == pairing.Team1.Value ? pairing.Team1Score : pairing.Team2Score);
}
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
[BackgroundDependencyLoader(true)]
private void load(OsuColour colours, LadderManager manager)
{
this.manager = manager;
colourWinner = colours.BlueDarker;
colourNormal = OsuColour.Gray(0.2f);
@ -117,7 +126,7 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
{
if (Team == null) return true;
if (Team == null) return false;
if (args.Button == MouseButton.Left)
{
@ -136,7 +145,7 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
pairing.CancelMatchStart();
}
return true;
return false;
}
private void updateWinStyle()
@ -147,5 +156,27 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
scoreText.Font = AcronymText.Font = winner ? "Exo2.0-Bold" : "Exo2.0-Regular";
}
public MenuItem[] ContextMenuItems => new[]
{
new MenuItem("Populate team", () => team.Value = manager.Teams.Random()),
};
}
internal static class Extensions
{
public static T Random<T>(this IEnumerable<T> enumerable)
{
if (enumerable == null)
{
throw new ArgumentNullException(nameof(enumerable));
}
// note: creating a Random instance each call may not be correct for you,
// consider a thread-safe static instance
var r = new Random();
var list = enumerable as IList<T> ?? enumerable.ToList();
return list.Count == 0 ? default(T) : list[r.Next(0, list.Count)];
}
}
}

View File

@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace osu.Game.Tournament.Screens.Ladder.Components
{
public class LadderInfo
{
public List<MatchPairing> Pairings = new List<MatchPairing>();
}
}

View File

@ -0,0 +1,51 @@
using System.Collections.Generic;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.States;
using osu.Game.Graphics.Cursor;
using osu.Game.Tournament.Components;
using SixLabors.Primitives;
namespace osu.Game.Tournament.Screens.Ladder.Components
{
public class LadderManager : CompositeDrawable
{
public readonly LadderInfo Info;
public readonly List<TournamentTeam> Teams;
private readonly OsuContextMenuContainer content;
public LadderManager(LadderInfo info, List<TournamentTeam> teams)
{
Info = info;
Teams = teams;
RelativeSizeAxes = Axes.Both;
InternalChild = content = new OsuContextMenuContainer
{
RelativeSizeAxes = Axes.Both
};
foreach (var pairing in info.Pairings)
addPairing(pairing);
}
protected void AddPairing(MatchPairing pairing)
{
Info.Pairings.Add(pairing);
addPairing(pairing);
}
private void addPairing(MatchPairing pairing) => content.Add(new DrawableMatchPairing(pairing));
protected override bool OnClick(InputState state)
{
AddPairing(new MatchPairing
{
Position = new Point((int)state.Mouse.Position.X, (int)state.Mouse.Position.Y)
});
return true;
}
}
}

View File

@ -1,8 +1,10 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using Newtonsoft.Json;
using osu.Framework.Configuration;
using osu.Game.Tournament.Components;
using SixLabors.Primitives;
namespace osu.Game.Tournament.Screens.Ladder.Components
{
@ -11,13 +13,22 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
/// </summary>
public class MatchPairing
{
public Bindable<TournamentTeam> Team1 = new Bindable<TournamentTeam>();
public Bindable<int?> Team1Score = new Bindable<int?>();
public readonly Bindable<TournamentTeam> Team1 = new Bindable<TournamentTeam>();
public Bindable<TournamentTeam> Team2 = new Bindable<TournamentTeam>();
public Bindable<int?> Team2Score = new Bindable<int?>();
public readonly Bindable<int?> Team1Score = new Bindable<int?>();
public Bindable<bool> Completed = new Bindable<bool>();
public readonly Bindable<TournamentTeam> Team2 = new Bindable<TournamentTeam>();
public readonly Bindable<int?> Team2Score = new Bindable<int?>();
public readonly Bindable<bool> Completed = new Bindable<bool>();
[JsonProperty]
public Point Position;
public MatchPairing()
{
}
public MatchPairing(TournamentTeam team1 = null, TournamentTeam team2 = null)
{