1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-19 06:43:21 +08:00
osu-lazer/osu.Game.Tournament/Screens/Ladder/Components/DrawableMatchPairing.cs

280 lines
9.7 KiB
C#
Raw Normal View History

2019-03-04 12:24:19 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osu.Framework.Allocation;
2019-03-02 12:40:43 +08:00
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-09-22 04:52:25 +08:00
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
using osu.Game.Tournament.Components;
using osuTK;
using osuTK.Graphics;
using osuTK.Input;
using SixLabors.Primitives;
namespace osu.Game.Tournament.Screens.Ladder.Components
{
public class DrawableMatchPairing : CompositeDrawable
{
2018-08-26 17:37:46 +08:00
public readonly MatchPairing Pairing;
private readonly bool editor;
2018-11-11 09:13:17 +08:00
protected readonly FillFlowContainer<DrawableMatchTeam> Flow;
2018-09-22 04:52:25 +08:00
private readonly Drawable selectionBox;
2018-11-07 00:20:32 +08:00
private readonly Drawable currentMatchSelectionBox;
2018-09-22 04:52:25 +08:00
private Bindable<MatchPairing> globalSelection;
[Resolved(CanBeNull = true)]
2018-11-06 13:49:20 +08:00
private LadderEditorInfo editorInfo { get; set; }
[Resolved(CanBeNull = true)]
private LadderInfo ladderInfo { get; set; }
public DrawableMatchPairing(MatchPairing pairing, bool editor = false)
{
Pairing = pairing;
this.editor = editor;
AutoSizeAxes = Axes.Both;
Margin = new MarginPadding(5);
2018-09-22 04:52:25 +08:00
InternalChildren = new[]
{
2018-09-22 04:52:25 +08:00
selectionBox = new Container
{
CornerRadius = 5,
Masking = true,
Scale = new Vector2(1.05f),
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Alpha = 0,
Colour = Color4.YellowGreen,
Child = new Box { RelativeSizeAxes = Axes.Both }
},
2018-11-07 00:20:32 +08:00
currentMatchSelectionBox = new Container
{
CornerRadius = 5,
Masking = true,
Scale = new Vector2(1.05f),
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Alpha = 0,
Colour = Color4.OrangeRed,
Child = new Box { RelativeSizeAxes = Axes.Both }
},
2018-11-11 09:13:17 +08:00
Flow = new FillFlowContainer<DrawableMatchTeam>
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Spacing = new Vector2(2)
2018-09-16 04:35:51 +08:00
}
};
pairing.Team1.BindValueChanged(_ => updateTeams());
pairing.Team2.BindValueChanged(_ => updateTeams());
pairing.Team1Score.BindValueChanged(_ => updateWinConditions());
pairing.Team2Score.BindValueChanged(_ => updateWinConditions());
pairing.Grouping.BindValueChanged(_ => updateWinConditions());
pairing.Completed.BindValueChanged(_ => updateProgression());
2018-09-16 04:35:51 +08:00
pairing.Progression.BindValueChanged(_ => updateProgression());
2018-09-25 01:31:48 +08:00
pairing.LosersProgression.BindValueChanged(_ => updateProgression());
2018-09-25 02:55:24 +08:00
pairing.Losers.BindValueChanged(_ => updateTeams());
2018-11-07 00:20:32 +08:00
pairing.Current.BindValueChanged(_ => updateCurrentMatch(), true);
pairing.Position.BindValueChanged(pos =>
{
if (IsDragged) return;
2019-03-02 12:40:43 +08:00
Position = new Vector2(pos.NewValue.X, pos.NewValue.Y);
}, true);
updateTeams();
}
2018-11-07 00:20:32 +08:00
private void updateCurrentMatch()
{
if (Pairing.Current.Value)
currentMatchSelectionBox.Show();
else
currentMatchSelectionBox.Hide();
}
2018-09-22 04:52:25 +08:00
private bool selected;
public bool Selected
{
get => selected;
set
{
if (value == selected) return;
2019-03-02 12:52:56 +08:00
2018-09-22 04:52:25 +08:00
selected = value;
if (selected)
{
selectionBox.Show();
if (editor)
editorInfo.Selected.Value = Pairing;
else
ladderInfo.CurrentMatch.Value = Pairing;
2018-09-22 04:52:25 +08:00
}
else
selectionBox.Hide();
}
}
2018-08-26 17:37:46 +08:00
private void updateProgression()
{
2019-03-02 12:40:43 +08:00
if (!Pairing.Completed.Value)
2018-09-25 01:31:48 +08:00
{
// ensure we clear any of our teams from our progression.
// this is not pretty logic but should suffice for now.
if (Pairing.Progression.Value != null && Pairing.Progression.Value.Team1.Value == Pairing.Team1.Value)
Pairing.Progression.Value.Team1.Value = null;
2018-08-26 17:37:46 +08:00
if (Pairing.Progression.Value != null && Pairing.Progression.Value.Team2.Value == Pairing.Team2.Value)
Pairing.Progression.Value.Team2.Value = null;
if (Pairing.LosersProgression.Value != null && Pairing.LosersProgression.Value.Team1.Value == Pairing.Team1.Value)
Pairing.LosersProgression.Value.Team1.Value = null;
2018-08-26 17:37:46 +08:00
if (Pairing.LosersProgression.Value != null && Pairing.LosersProgression.Value.Team2.Value == Pairing.Team2.Value)
Pairing.LosersProgression.Value.Team2.Value = null;
}
else
2018-09-25 01:31:48 +08:00
{
transferProgression(Pairing.Progression?.Value, Pairing.Winner);
transferProgression(Pairing.LosersProgression?.Value, Pairing.Loser);
}
}
2018-09-25 01:31:48 +08:00
private void transferProgression(MatchPairing destination, TournamentTeam team)
{
if (destination == null) return;
bool progressionAbove = destination.ID < Pairing.ID;
Bindable<TournamentTeam> destinationTeam;
// check for the case where we have already transferred out value
if (destination.Team1.Value == team)
destinationTeam = destination.Team1;
else if (destination.Team2.Value == team)
destinationTeam = destination.Team2;
else
{
destinationTeam = progressionAbove ? destination.Team2 : destination.Team1;
if (destinationTeam.Value != null)
destinationTeam = progressionAbove ? destination.Team1 : destination.Team2;
2018-09-25 01:31:48 +08:00
}
destinationTeam.Value = team;
2018-08-26 17:37:46 +08:00
}
private void updateWinConditions()
{
2018-09-25 09:27:54 +08:00
if (Pairing.Grouping.Value == null) return;
2019-03-02 12:40:43 +08:00
var instaWinAmount = Pairing.Grouping.Value.BestOf.Value / 2;
2018-09-24 01:16:59 +08:00
2019-03-02 12:40:43 +08:00
Pairing.Completed.Value = Pairing.Grouping.Value.BestOf.Value > 0
&& (Pairing.Team1Score.Value + Pairing.Team2Score.Value >= Pairing.Grouping.Value.BestOf.Value || Pairing.Team1Score.Value > instaWinAmount || Pairing.Team2Score.Value > instaWinAmount);
}
2018-08-26 17:37:46 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
updateTeams();
2018-09-22 04:52:25 +08:00
if (editorInfo != null)
{
globalSelection = editorInfo.Selected.GetBoundCopy();
globalSelection.BindValueChanged(s =>
{
2019-03-02 12:40:43 +08:00
if (s.NewValue != Pairing) Selected = false;
2018-09-22 04:52:25 +08:00
});
}
2018-08-26 17:37:46 +08:00
}
private void updateTeams()
{
2018-08-26 17:37:46 +08:00
if (LoadState != LoadState.Loaded)
return;
// todo: teams may need to be bindable for transitions at a later point.
if (Pairing.Team1.Value == null || Pairing.Team2.Value == null)
Pairing.CancelMatchStart();
2018-12-01 14:32:11 +08:00
if (Pairing.ConditionalPairings.Count > 0)
{
foreach (var conditional in Pairing.ConditionalPairings)
{
var team1Match = conditional.Acronyms.Contains(Pairing.Team1Acronym);
var team2Match = conditional.Acronyms.Contains(Pairing.Team2Acronym);
if (team1Match && team2Match)
2019-03-02 12:40:43 +08:00
Pairing.Date.Value = conditional.Date.Value;
2018-12-01 14:32:11 +08:00
}
}
2018-11-11 09:13:17 +08:00
Flow.Children = new[]
{
2019-03-02 12:40:43 +08:00
new DrawableMatchTeam(Pairing.Team1.Value, Pairing, Pairing.Losers.Value),
new DrawableMatchTeam(Pairing.Team2.Value, Pairing, Pairing.Losers.Value)
};
2018-08-26 17:37:46 +08:00
2018-08-27 16:08:00 +08:00
SchedulerAfterChildren.Add(() => Scheduler.Add(updateProgression));
updateWinConditions();
}
protected override bool OnMouseDown(MouseDownEvent e) => e.Button == MouseButton.Left && editorInfo != null;
protected override bool OnDragStart(DragStartEvent e) => editorInfo != null;
protected override bool OnKeyDown(KeyDownEvent e)
2018-09-22 04:52:25 +08:00
{
if (Selected && editorInfo != null && e.Key == Key.Delete)
2018-09-22 04:52:25 +08:00
{
Remove();
return true;
}
return base.OnKeyDown(e);
2018-09-22 04:52:25 +08:00
}
protected override bool OnClick(ClickEvent e)
2018-09-22 04:52:25 +08:00
{
2018-12-01 14:32:11 +08:00
if (editorInfo == null || Pairing is ConditionalMatchPairing)
2018-09-22 04:52:25 +08:00
return false;
Selected = true;
return true;
}
protected override bool OnDrag(DragEvent e)
{
if (base.OnDrag(e)) return true;
2018-09-22 04:52:25 +08:00
Selected = true;
this.MoveToOffset(e.Delta);
var pos = Position;
Pairing.Position.Value = new Point((int)pos.X, (int)pos.Y);
return true;
}
2018-09-21 18:58:47 +08:00
public void Remove()
{
2018-09-22 04:52:25 +08:00
Selected = false;
2018-09-21 18:58:47 +08:00
Pairing.Progression.Value = null;
2018-09-25 01:31:48 +08:00
Pairing.LosersProgression.Value = null;
2018-09-22 04:52:25 +08:00
2018-09-21 18:58:47 +08:00
Expire();
}
}
}