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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

364 lines
13 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 System;
using System.Collections.Generic;
2023-07-25 19:50:55 +08:00
using System.Diagnostics;
using System.Drawing;
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;
2019-06-18 13:51:48 +08:00
using osu.Game.Tournament.Models;
using osu.Game.Tournament.Screens.Editors;
using osuTK;
using osuTK.Graphics;
using osuTK.Input;
namespace osu.Game.Tournament.Screens.Ladder.Components
{
2019-06-18 13:57:05 +08:00
public partial class DrawableTournamentMatch : CompositeDrawable
{
2019-06-18 13:57:05 +08:00
public readonly TournamentMatch Match;
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;
private readonly Drawable currentMatchSelectionBox;
2023-07-25 19:50:55 +08:00
private Bindable<TournamentMatch>? globalSelection;
2018-09-22 04:52:25 +08:00
2023-07-30 00:57:44 +08:00
[Resolved]
2023-07-25 19:50:55 +08:00
private LadderEditorInfo? editorInfo { get; set; }
2023-07-30 00:57:44 +08:00
[Resolved]
2023-07-25 19:50:55 +08:00
private LadderInfo? ladderInfo { get; set; }
2019-06-18 13:57:05 +08:00
public DrawableTournamentMatch(TournamentMatch match, bool editor = false)
{
2019-06-18 13:57:05 +08:00
Match = match;
this.editor = editor;
AutoSizeAxes = Axes.Both;
const float border_thickness = 5;
const float spacing = 2;
Margin = new MarginPadding(10);
InternalChildren = new Drawable[]
{
Flow = new FillFlowContainer<DrawableMatchTeam>
2018-09-22 04:52:25 +08:00
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Spacing = new Vector2(spacing)
2018-09-22 04:52:25 +08:00
},
new Container
2018-11-07 00:20:32 +08:00
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding(-10),
2018-11-07 00:20:32 +08:00
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Child = selectionBox = new Container
{
RelativeSizeAxes = Axes.Both,
Alpha = 0,
Masking = true,
BorderColour = Color4.YellowGreen,
BorderThickness = border_thickness,
Child = new Box
{
RelativeSizeAxes = Axes.Both,
AlwaysPresent = true,
Alpha = 0,
}
},
},
new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding(-(spacing + border_thickness)),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Child = currentMatchSelectionBox = new Container
{
RelativeSizeAxes = Axes.Both,
Alpha = 0,
BorderColour = Color4.White,
BorderThickness = border_thickness,
Masking = true,
Child = new Box
{
RelativeSizeAxes = Axes.Both,
AlwaysPresent = true,
Alpha = 0,
}
},
}
};
2019-06-18 13:57:05 +08: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(_ =>
{
updateWinConditions();
Changed?.Invoke();
});
2019-06-18 13:57:05 +08:00
boundReference(match.Completed).BindValueChanged(_ => updateProgression());
boundReference(match.Progression).BindValueChanged(_ => updateProgression());
boundReference(match.LosersProgression).BindValueChanged(_ => updateProgression());
boundReference(match.Losers).BindValueChanged(_ =>
{
updateTeams();
Changed?.Invoke();
});
2019-06-18 13:57:05 +08:00
boundReference(match.Current).BindValueChanged(_ => updateCurrentMatch(), true);
boundReference(match.Position).BindValueChanged(pos =>
{
if (!IsDragged)
Position = new Vector2(pos.NewValue.X, pos.NewValue.Y);
Changed?.Invoke();
}, true);
updateTeams();
}
/// <summary>
2023-07-25 13:51:51 +08:00
/// Fired when something changed that requires a ladder redraw.
/// </summary>
2023-07-25 19:50:55 +08:00
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-07 00:20:32 +08:00
private void updateCurrentMatch()
{
2019-06-18 13:57:05 +08:00
if (Match.Current.Value)
currentMatchSelectionBox.Show();
2018-11-07 00:20:32 +08:00
else
currentMatchSelectionBox.Hide();
2018-11-07 00:20:32 +08:00
}
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 != null)
2019-06-18 13:57:05 +08:00
editorInfo.Selected.Value = Match;
else if (ladderInfo != null)
2019-06-18 13:57:05 +08:00
ladderInfo.CurrentMatch.Value = Match;
2018-09-22 04:52:25 +08:00
}
else
selectionBox.Hide();
}
}
2018-08-26 17:37:46 +08:00
private void updateProgression()
{
2019-06-18 13:57:05 +08:00
if (!Match.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.
2019-06-18 13:57:05 +08:00
if (Match.Progression.Value != null && Match.Progression.Value.Team1.Value == Match.Team1.Value)
Match.Progression.Value.Team1.Value = null;
2018-08-26 17:37:46 +08:00
2019-06-18 13:57:05 +08:00
if (Match.Progression.Value != null && Match.Progression.Value.Team2.Value == Match.Team2.Value)
Match.Progression.Value.Team2.Value = null;
2019-06-18 13:57:05 +08:00
if (Match.LosersProgression.Value != null && Match.LosersProgression.Value.Team1.Value == Match.Team1.Value)
Match.LosersProgression.Value.Team1.Value = null;
2018-08-26 17:37:46 +08:00
2019-06-18 13:57:05 +08:00
if (Match.LosersProgression.Value != null && Match.LosersProgression.Value.Team2.Value == Match.Team2.Value)
Match.LosersProgression.Value.Team2.Value = null;
}
else
2018-09-25 01:31:48 +08:00
{
2023-07-25 19:50:55 +08:00
Debug.Assert(Match.Winner != null);
transferProgression(Match.Progression.Value, Match.Winner);
Debug.Assert(Match.Loser != null);
transferProgression(Match.LosersProgression.Value, Match.Loser);
}
Changed?.Invoke();
}
2018-09-25 01:31:48 +08:00
2023-07-25 19:50:55 +08:00
private void transferProgression(TournamentMatch? destination, TournamentTeam team)
{
if (destination == null) return;
2019-06-18 13:57:05 +08:00
bool progressionAbove = destination.ID < Match.ID;
2023-07-25 19:50:55 +08: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-25 01:31:48 +08:00
}
destinationTeam.Value = team;
2018-08-26 17:37:46 +08:00
}
private void updateWinConditions()
{
2019-06-18 13:57:05 +08:00
if (Match.Round.Value == null) return;
int instantWinAmount = Match.Round.Value.BestOf.Value / 2;
2018-09-24 01:16:59 +08:00
2019-06-18 13:57:05 +08:00
Match.Completed.Value = Match.Round.Value.BestOf.Value > 0
2023-07-20 18:51:58 +08:00
&& (Match.Team1Score.Value + Match.Team2Score.Value >= Match.Round.Value.BestOf.Value || Match.Team1Score.Value > instantWinAmount
|| Match.Team2Score.Value > instantWinAmount);
}
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 => Selected = s.NewValue == Match, true);
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.
2019-06-18 13:57:05 +08:00
if (Match.Team1.Value == null || Match.Team2.Value == null)
Match.CancelMatchStart();
2019-06-18 13:57:05 +08:00
if (Match.ConditionalMatches.Count > 0)
2018-12-01 14:32:11 +08:00
{
2019-06-18 13:57:05 +08:00
foreach (var conditional in Match.ConditionalMatches)
2018-12-01 14:32:11 +08:00
{
2023-07-25 19:50:55 +08:00
bool team1Match = Match.Team1Acronym != null && conditional.Acronyms.Contains(Match.Team1Acronym);
bool team2Match = Match.Team2Acronym != null && conditional.Acronyms.Contains(Match.Team2Acronym);
2018-12-01 14:32:11 +08:00
if (team1Match && team2Match)
2019-06-18 13:57:05 +08:00
Match.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-06-18 13:57:05 +08:00
new DrawableMatchTeam(Match.Team1.Value, Match, Match.Losers.Value),
new DrawableMatchTeam(Match.Team2.Value, Match, Match.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 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
{
if (editorInfo == null || Match is ConditionalTournamentMatch || e.Button != MouseButton.Left)
2018-09-22 04:52:25 +08:00
return false;
Selected = true;
return true;
}
2023-07-20 18:51:58 +08:00
private Vector2 positionAtStartOfDrag;
protected override bool OnDragStart(DragStartEvent e)
{
if (editorInfo != null)
{
positionAtStartOfDrag = Position;
return true;
}
return false;
}
protected override void OnDrag(DragEvent e)
{
base.OnDrag(e);
2018-09-22 04:52:25 +08:00
Selected = true;
2023-07-20 18:51:58 +08:00
this.MoveTo(snapToGrid(positionAtStartOfDrag + (e.MousePosition - e.MouseDownPosition)));
Match.Position.Value = new Point((int)Position.X, (int)Position.Y);
}
2018-09-21 18:58:47 +08:00
2023-07-20 18:51:58 +08:00
private Vector2 snapToGrid(Vector2 pos) =>
new Vector2(
(int)(pos.X / LadderEditorScreen.GRID_SPACING) * LadderEditorScreen.GRID_SPACING,
(int)(pos.Y / LadderEditorScreen.GRID_SPACING) * LadderEditorScreen.GRID_SPACING
2023-07-20 18:51:58 +08:00
);
2018-09-21 18:58:47 +08:00
public void Remove()
{
2018-09-22 04:52:25 +08:00
Selected = false;
2019-06-18 13:57:05 +08:00
Match.Progression.Value = null;
Match.LosersProgression.Value = null;
2018-09-22 04:52:25 +08:00
2023-07-25 19:50:55 +08:00
if (ladderInfo == null)
return;
2019-06-18 13:57:05 +08:00
ladderInfo.Matches.Remove(Match);
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 18:58:47 +08:00
}
}
}