2019-03-04 04:24:19 +00: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.
|
2018-08-25 16:14:18 +00:00
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2019-06-14 10:16:20 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2020-12-03 08:43:09 +00:00
|
|
|
using System.Drawing;
|
2018-08-26 10:11:46 +00:00
|
|
|
using osu.Framework.Allocation;
|
2019-03-02 04:40:43 +00:00
|
|
|
using osu.Framework.Bindables;
|
2018-08-25 16:14:18 +00:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2018-09-21 20:52:25 +00:00
|
|
|
using osu.Framework.Graphics.Shapes;
|
2018-10-12 09:49:47 +00:00
|
|
|
using osu.Framework.Input.Events;
|
2019-06-18 05:51:48 +00:00
|
|
|
using osu.Game.Tournament.Models;
|
2018-11-22 01:25:30 +00:00
|
|
|
using osuTK;
|
|
|
|
using osuTK.Graphics;
|
|
|
|
using osuTK.Input;
|
2018-08-25 16:14:18 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Tournament.Screens.Ladder.Components
|
|
|
|
{
|
2019-06-18 05:57:05 +00:00
|
|
|
public class DrawableTournamentMatch : CompositeDrawable
|
2018-08-25 16:14:18 +00:00
|
|
|
{
|
2019-06-18 05:57:05 +00:00
|
|
|
public readonly TournamentMatch Match;
|
2019-02-17 10:48:54 +00:00
|
|
|
private readonly bool editor;
|
2018-11-11 01:13:17 +00:00
|
|
|
protected readonly FillFlowContainer<DrawableMatchTeam> Flow;
|
2018-09-21 20:52:25 +00:00
|
|
|
private readonly Drawable selectionBox;
|
2020-03-07 04:26:54 +00:00
|
|
|
protected readonly Drawable CurrentMatchSelectionBox;
|
2019-06-18 05:57:05 +00:00
|
|
|
private Bindable<TournamentMatch> globalSelection;
|
2018-09-21 20:52:25 +00:00
|
|
|
|
|
|
|
[Resolved(CanBeNull = true)]
|
2018-11-06 05:49:20 +00:00
|
|
|
private LadderEditorInfo editorInfo { get; set; }
|
2018-08-26 10:11:46 +00:00
|
|
|
|
2019-02-17 10:48:54 +00:00
|
|
|
[Resolved(CanBeNull = true)]
|
|
|
|
private LadderInfo ladderInfo { get; set; }
|
|
|
|
|
2019-06-18 05:57:05 +00:00
|
|
|
public DrawableTournamentMatch(TournamentMatch match, bool editor = false)
|
2018-08-26 10:11:46 +00:00
|
|
|
{
|
2019-06-18 05:57:05 +00:00
|
|
|
Match = match;
|
2019-02-17 10:48:54 +00:00
|
|
|
this.editor = editor;
|
2018-08-26 10:11:46 +00:00
|
|
|
|
|
|
|
AutoSizeAxes = Axes.Both;
|
|
|
|
|
|
|
|
Margin = new MarginPadding(5);
|
|
|
|
|
2018-09-21 20:52:25 +00:00
|
|
|
InternalChildren = new[]
|
2018-08-26 10:11:46 +00:00
|
|
|
{
|
2018-09-21 20:52:25 +00:00
|
|
|
selectionBox = new Container
|
|
|
|
{
|
2020-03-06 16:37:15 +00:00
|
|
|
Scale = new Vector2(1.1f),
|
2018-09-21 20:52:25 +00:00
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Alpha = 0,
|
|
|
|
Colour = Color4.YellowGreen,
|
|
|
|
Child = new Box { RelativeSizeAxes = Axes.Both }
|
|
|
|
},
|
2020-03-07 04:26:54 +00:00
|
|
|
CurrentMatchSelectionBox = new Container
|
2018-11-06 16:20:32 +00:00
|
|
|
{
|
2020-03-07 04:26:54 +00:00
|
|
|
Scale = new Vector2(1.05f, 1.1f),
|
2018-11-06 16:20:32 +00:00
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Alpha = 0,
|
2020-03-07 07:38:50 +00:00
|
|
|
Colour = Color4.White,
|
2018-11-06 16:20:32 +00:00
|
|
|
Child = new Box { RelativeSizeAxes = Axes.Both }
|
|
|
|
},
|
2018-11-11 01:13:17 +00:00
|
|
|
Flow = new FillFlowContainer<DrawableMatchTeam>
|
2018-08-26 10:11:46 +00:00
|
|
|
{
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
Spacing = new Vector2(2)
|
2018-09-15 20:35:51 +00:00
|
|
|
}
|
2018-08-26 10:11:46 +00:00
|
|
|
};
|
2018-11-16 11:14:34 +00:00
|
|
|
|
2019-06-18 05:57:05 +00:00
|
|
|
boundReference(match.Team1).BindValueChanged(_ => updateTeams());
|
|
|
|
boundReference(match.Team2).BindValueChanged(_ => updateTeams());
|
|
|
|
boundReference(match.Team1Score).BindValueChanged(_ => updateWinConditions());
|
|
|
|
boundReference(match.Team2Score).BindValueChanged(_ => updateWinConditions());
|
|
|
|
boundReference(match.Round).BindValueChanged(_ =>
|
2018-11-16 11:14:34 +00:00
|
|
|
{
|
2019-06-14 10:16:20 +00:00
|
|
|
updateWinConditions();
|
|
|
|
Changed?.Invoke();
|
|
|
|
});
|
2019-06-18 05:57:05 +00:00
|
|
|
boundReference(match.Completed).BindValueChanged(_ => updateProgression());
|
|
|
|
boundReference(match.Progression).BindValueChanged(_ => updateProgression());
|
|
|
|
boundReference(match.LosersProgression).BindValueChanged(_ => updateProgression());
|
|
|
|
boundReference(match.Losers).BindValueChanged(_ =>
|
2019-06-14 10:16:20 +00:00
|
|
|
{
|
|
|
|
updateTeams();
|
|
|
|
Changed?.Invoke();
|
|
|
|
});
|
2019-06-18 05:57:05 +00:00
|
|
|
boundReference(match.Current).BindValueChanged(_ => updateCurrentMatch(), true);
|
|
|
|
boundReference(match.Position).BindValueChanged(pos =>
|
2019-06-14 10:16:20 +00:00
|
|
|
{
|
|
|
|
if (!IsDragged)
|
|
|
|
Position = new Vector2(pos.NewValue.X, pos.NewValue.Y);
|
|
|
|
Changed?.Invoke();
|
2018-11-16 11:14:34 +00:00
|
|
|
}, true);
|
2018-08-26 10:11:46 +00:00
|
|
|
|
|
|
|
updateTeams();
|
|
|
|
}
|
|
|
|
|
2019-06-14 10:16:20 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Fired when somethign changed that requires a ladder redraw.
|
|
|
|
/// </summary>
|
|
|
|
public Action Changed;
|
|
|
|
|
|
|
|
private readonly List<IUnbindable> refBindables = new List<IUnbindable>();
|
|
|
|
|
|
|
|
private T boundReference<T>(T obj)
|
|
|
|
where T : IBindable
|
|
|
|
{
|
|
|
|
obj = (T)obj.GetBoundCopy();
|
|
|
|
refBindables.Add(obj);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
foreach (var b in refBindables)
|
|
|
|
b.UnbindAll();
|
|
|
|
}
|
|
|
|
|
2018-11-06 16:20:32 +00:00
|
|
|
private void updateCurrentMatch()
|
|
|
|
{
|
2019-06-18 05:57:05 +00:00
|
|
|
if (Match.Current.Value)
|
2020-03-07 04:26:54 +00:00
|
|
|
CurrentMatchSelectionBox.Show();
|
2018-11-06 16:20:32 +00:00
|
|
|
else
|
2020-03-07 04:26:54 +00:00
|
|
|
CurrentMatchSelectionBox.Hide();
|
2018-11-06 16:20:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 20:52:25 +00:00
|
|
|
private bool selected;
|
|
|
|
|
|
|
|
public bool Selected
|
|
|
|
{
|
|
|
|
get => selected;
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == selected) return;
|
2019-03-02 04:52:56 +00:00
|
|
|
|
2018-09-21 20:52:25 +00:00
|
|
|
selected = value;
|
|
|
|
|
|
|
|
if (selected)
|
|
|
|
{
|
|
|
|
selectionBox.Show();
|
2020-11-14 13:48:48 +00:00
|
|
|
if (editor && editorInfo != null)
|
2019-06-18 05:57:05 +00:00
|
|
|
editorInfo.Selected.Value = Match;
|
2020-11-14 13:48:48 +00:00
|
|
|
else if (ladderInfo != null)
|
2019-06-18 05:57:05 +00:00
|
|
|
ladderInfo.CurrentMatch.Value = Match;
|
2018-09-21 20:52:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
selectionBox.Hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-26 09:37:46 +00:00
|
|
|
private void updateProgression()
|
|
|
|
{
|
2019-06-18 05:57:05 +00:00
|
|
|
if (!Match.Completed.Value)
|
2018-09-24 17:31:48 +00:00
|
|
|
{
|
2018-10-13 18:04:06 +00:00
|
|
|
// ensure we clear any of our teams from our progression.
|
|
|
|
// this is not pretty logic but should suffice for now.
|
2019-06-18 05:57:05 +00:00
|
|
|
if (Match.Progression.Value != null && Match.Progression.Value.Team1.Value == Match.Team1.Value)
|
|
|
|
Match.Progression.Value.Team1.Value = null;
|
2018-08-26 09:37:46 +00:00
|
|
|
|
2019-06-18 05:57:05 +00:00
|
|
|
if (Match.Progression.Value != null && Match.Progression.Value.Team2.Value == Match.Team2.Value)
|
|
|
|
Match.Progression.Value.Team2.Value = null;
|
2018-10-13 18:04:06 +00:00
|
|
|
|
2019-06-18 05:57:05 +00:00
|
|
|
if (Match.LosersProgression.Value != null && Match.LosersProgression.Value.Team1.Value == Match.Team1.Value)
|
|
|
|
Match.LosersProgression.Value.Team1.Value = null;
|
2018-08-26 09:37:46 +00:00
|
|
|
|
2019-06-18 05:57:05 +00:00
|
|
|
if (Match.LosersProgression.Value != null && Match.LosersProgression.Value.Team2.Value == Match.Team2.Value)
|
|
|
|
Match.LosersProgression.Value.Team2.Value = null;
|
2018-10-13 18:04:06 +00:00
|
|
|
}
|
|
|
|
else
|
2018-09-24 17:31:48 +00:00
|
|
|
{
|
2019-06-18 05:57:05 +00:00
|
|
|
transferProgression(Match.Progression?.Value, Match.Winner);
|
|
|
|
transferProgression(Match.LosersProgression?.Value, Match.Loser);
|
2018-10-13 18:04:06 +00:00
|
|
|
}
|
2019-06-14 10:16:20 +00:00
|
|
|
|
|
|
|
Changed?.Invoke();
|
2018-10-13 18:04:06 +00:00
|
|
|
}
|
2018-09-24 17:31:48 +00:00
|
|
|
|
2019-06-18 05:57:05 +00:00
|
|
|
private void transferProgression(TournamentMatch destination, TournamentTeam team)
|
2018-10-13 18:04:06 +00:00
|
|
|
{
|
|
|
|
if (destination == null) return;
|
|
|
|
|
2019-06-18 05:57:05 +00:00
|
|
|
bool progressionAbove = destination.ID < Match.ID;
|
2018-10-13 18:04:06 +00:00
|
|
|
|
|
|
|
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-24 17:31:48 +00:00
|
|
|
}
|
2018-10-13 18:04:06 +00:00
|
|
|
|
|
|
|
destinationTeam.Value = team;
|
2018-08-26 09:37:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-26 10:11:46 +00:00
|
|
|
private void updateWinConditions()
|
2018-08-25 16:14:18 +00:00
|
|
|
{
|
2019-06-18 05:57:05 +00:00
|
|
|
if (Match.Round.Value == null) return;
|
2018-08-25 16:14:18 +00:00
|
|
|
|
2021-12-10 05:15:38 +00:00
|
|
|
int instantWinAmount = Match.Round.Value.BestOf.Value / 2;
|
2018-09-23 17:16:59 +00:00
|
|
|
|
2019-06-18 05:57:05 +00:00
|
|
|
Match.Completed.Value = Match.Round.Value.BestOf.Value > 0
|
2021-12-10 05:15:38 +00:00
|
|
|
&& (Match.Team1Score.Value + Match.Team2Score.Value >= Match.Round.Value.BestOf.Value || Match.Team1Score.Value > instantWinAmount || Match.Team2Score.Value > instantWinAmount);
|
2018-08-25 16:14:18 +00:00
|
|
|
}
|
|
|
|
|
2018-08-26 09:37:46 +00:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
updateTeams();
|
2018-09-21 20:52:25 +00:00
|
|
|
|
|
|
|
if (editorInfo != null)
|
|
|
|
{
|
|
|
|
globalSelection = editorInfo.Selected.GetBoundCopy();
|
|
|
|
globalSelection.BindValueChanged(s =>
|
|
|
|
{
|
2019-06-18 05:57:05 +00:00
|
|
|
if (s.NewValue != Match) Selected = false;
|
2018-09-21 20:52:25 +00:00
|
|
|
});
|
|
|
|
}
|
2018-08-26 09:37:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 16:14:18 +00:00
|
|
|
private void updateTeams()
|
|
|
|
{
|
2018-08-26 09:37:46 +00:00
|
|
|
if (LoadState != LoadState.Loaded)
|
|
|
|
return;
|
|
|
|
|
2018-08-25 16:14:18 +00:00
|
|
|
// todo: teams may need to be bindable for transitions at a later point.
|
|
|
|
|
2019-06-18 05:57:05 +00:00
|
|
|
if (Match.Team1.Value == null || Match.Team2.Value == null)
|
|
|
|
Match.CancelMatchStart();
|
2018-08-26 10:11:46 +00:00
|
|
|
|
2019-06-18 05:57:05 +00:00
|
|
|
if (Match.ConditionalMatches.Count > 0)
|
2018-12-01 06:32:11 +00:00
|
|
|
{
|
2019-06-18 05:57:05 +00:00
|
|
|
foreach (var conditional in Match.ConditionalMatches)
|
2018-12-01 06:32:11 +00:00
|
|
|
{
|
2021-10-27 04:04:41 +00:00
|
|
|
bool team1Match = conditional.Acronyms.Contains(Match.Team1Acronym);
|
|
|
|
bool team2Match = conditional.Acronyms.Contains(Match.Team2Acronym);
|
2018-12-01 06:32:11 +00:00
|
|
|
|
|
|
|
if (team1Match && team2Match)
|
2019-06-18 05:57:05 +00:00
|
|
|
Match.Date.Value = conditional.Date.Value;
|
2018-12-01 06:32:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-11 01:13:17 +00:00
|
|
|
Flow.Children = new[]
|
2018-08-25 16:14:18 +00:00
|
|
|
{
|
2019-06-18 05:57:05 +00:00
|
|
|
new DrawableMatchTeam(Match.Team1.Value, Match, Match.Losers.Value),
|
|
|
|
new DrawableMatchTeam(Match.Team2.Value, Match, Match.Losers.Value)
|
2018-08-25 16:14:18 +00:00
|
|
|
};
|
2018-08-26 09:37:46 +00:00
|
|
|
|
2018-08-27 08:08:00 +00:00
|
|
|
SchedulerAfterChildren.Add(() => Scheduler.Add(updateProgression));
|
2018-08-26 10:11:46 +00:00
|
|
|
updateWinConditions();
|
2018-08-25 16:14:18 +00:00
|
|
|
}
|
2018-09-09 19:51:38 +00:00
|
|
|
|
2018-11-16 11:14:34 +00:00
|
|
|
protected override bool OnMouseDown(MouseDownEvent e) => e.Button == MouseButton.Left && editorInfo != null;
|
2018-09-09 19:51:38 +00:00
|
|
|
|
2018-11-16 11:14:34 +00:00
|
|
|
protected override bool OnDragStart(DragStartEvent e) => editorInfo != null;
|
2018-09-09 19:51:38 +00:00
|
|
|
|
2018-10-12 09:49:47 +00:00
|
|
|
protected override bool OnKeyDown(KeyDownEvent e)
|
2018-09-21 20:52:25 +00:00
|
|
|
{
|
2018-11-16 11:14:34 +00:00
|
|
|
if (Selected && editorInfo != null && e.Key == Key.Delete)
|
2018-09-21 20:52:25 +00:00
|
|
|
{
|
|
|
|
Remove();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-12 09:49:47 +00:00
|
|
|
return base.OnKeyDown(e);
|
2018-09-21 20:52:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 09:49:47 +00:00
|
|
|
protected override bool OnClick(ClickEvent e)
|
2018-09-21 20:52:25 +00:00
|
|
|
{
|
2022-09-02 09:14:47 +00:00
|
|
|
if (editorInfo == null || Match is ConditionalTournamentMatch || e.Button != MouseButton.Left)
|
2018-09-21 20:52:25 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
Selected = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-20 09:17:21 +00:00
|
|
|
protected override void OnDrag(DragEvent e)
|
2018-09-09 19:51:38 +00:00
|
|
|
{
|
2020-01-20 09:17:21 +00:00
|
|
|
base.OnDrag(e);
|
2018-09-09 19:51:38 +00:00
|
|
|
|
2018-09-21 20:52:25 +00:00
|
|
|
Selected = true;
|
2018-10-12 09:49:47 +00:00
|
|
|
this.MoveToOffset(e.Delta);
|
2018-09-09 19:51:38 +00:00
|
|
|
|
|
|
|
var pos = Position;
|
2019-06-18 05:57:05 +00:00
|
|
|
Match.Position.Value = new Point((int)pos.X, (int)pos.Y);
|
2018-09-09 19:51:38 +00:00
|
|
|
}
|
2018-09-21 10:58:47 +00:00
|
|
|
|
|
|
|
public void Remove()
|
|
|
|
{
|
2018-09-21 20:52:25 +00:00
|
|
|
Selected = false;
|
2019-06-18 05:57:05 +00:00
|
|
|
Match.Progression.Value = null;
|
|
|
|
Match.LosersProgression.Value = null;
|
2018-09-21 20:52:25 +00:00
|
|
|
|
2019-06-18 05:57:05 +00:00
|
|
|
ladderInfo.Matches.Remove(Match);
|
2021-07-16 14:51:31 +00:00
|
|
|
|
|
|
|
foreach (var m in ladderInfo.Matches)
|
|
|
|
{
|
|
|
|
if (m.Progression.Value == Match)
|
|
|
|
m.Progression.Value = null;
|
|
|
|
|
|
|
|
if (m.LosersProgression.Value == Match)
|
|
|
|
m.LosersProgression.Value = null;
|
|
|
|
}
|
2018-09-21 10:58:47 +00:00
|
|
|
}
|
2018-08-25 16:14:18 +00:00
|
|
|
}
|
|
|
|
}
|