From 800bda7e815caa2ff0739642444e431acf49c1f3 Mon Sep 17 00:00:00 2001 From: Andrei Zavatski Date: Wed, 27 Nov 2019 23:35:02 +0300 Subject: [PATCH] Refactor tables to avoid code duplication --- .../Visual/Online/TestSceneRankingsTables.cs | 1 + .../Rankings/Tables/CountriesTable.cs | 39 +++------ .../Rankings/Tables/PerformanceTable.cs | 77 ++--------------- .../Overlays/Rankings/Tables/RankingsTable.cs | 35 +++++++- .../Overlays/Rankings/Tables/ScoresTable.cs | 85 +++---------------- .../Rankings/Tables/UserBasedTable.cs | 70 +++++++++++++++ 6 files changed, 133 insertions(+), 174 deletions(-) create mode 100644 osu.Game/Overlays/Rankings/Tables/UserBasedTable.cs diff --git a/osu.Game.Tests/Visual/Online/TestSceneRankingsTables.cs b/osu.Game.Tests/Visual/Online/TestSceneRankingsTables.cs index 2c76b1937b..ff238803a5 100644 --- a/osu.Game.Tests/Visual/Online/TestSceneRankingsTables.cs +++ b/osu.Game.Tests/Visual/Online/TestSceneRankingsTables.cs @@ -29,6 +29,7 @@ namespace osu.Game.Tests.Visual.Online typeof(ScoresTable), typeof(CountriesTable), typeof(TableRowBackground), + typeof(UserBasedTable), typeof(RankingsTable<>) }; diff --git a/osu.Game/Overlays/Rankings/Tables/CountriesTable.cs b/osu.Game/Overlays/Rankings/Tables/CountriesTable.cs index 878f85b346..208a4dbfe4 100644 --- a/osu.Game/Overlays/Rankings/Tables/CountriesTable.cs +++ b/osu.Game/Overlays/Rankings/Tables/CountriesTable.cs @@ -3,12 +3,10 @@ using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; -using osu.Game.Graphics; -using osu.Game.Graphics.Sprites; -using osu.Game.Users.Drawables; -using osuTK; using System; using osu.Game.Users; +using osu.Game.Graphics.Sprites; +using osu.Game.Graphics; namespace osu.Game.Overlays.Rankings.Tables { @@ -29,31 +27,16 @@ namespace osu.Game.Overlays.Rankings.Tables new TableColumn("Avg. Perf.", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)), }; - protected override Drawable[] CreateContent(int index, CountryStatistics item) => new Drawable[] + protected override Country GetCountry(CountryStatistics item) => item.Country; + + protected override Drawable CreateFlagContent(CountryStatistics item) => new OsuSpriteText + { + Font = OsuFont.GetFont(size: TEXT_SIZE), + Text = $@"{item.Country.FullName}", + }; + + protected override Drawable[] CreateAdditionalContent(CountryStatistics item) => new[] { - new OsuSpriteText - { - Text = $"#{index + 1}", - Font = OsuFont.GetFont(size: TEXT_SIZE, weight: FontWeight.Bold) - }, - new FillFlowContainer - { - AutoSizeAxes = Axes.Both, - Direction = FillDirection.Horizontal, - Spacing = new Vector2(7, 0), - Children = new Drawable[] - { - new UpdateableFlag(item.Country) - { - Size = new Vector2(20, 13), - ShowPlaceholderOnNull = false, - }, - new RowText - { - Text = $@"{item.Country.FullName}", - } - } - }, new ColoredRowText { Text = $@"{item.ActiveUsers:N0}", diff --git a/osu.Game/Overlays/Rankings/Tables/PerformanceTable.cs b/osu.Game/Overlays/Rankings/Tables/PerformanceTable.cs index 010da07858..263f6052cc 100644 --- a/osu.Game/Overlays/Rankings/Tables/PerformanceTable.cs +++ b/osu.Game/Overlays/Rankings/Tables/PerformanceTable.cs @@ -3,91 +3,28 @@ using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; -using System.Collections.Generic; -using osu.Game.Graphics; -using osu.Game.Graphics.Containers; -using osu.Game.Graphics.Sprites; -using osu.Game.Users.Drawables; -using osuTK; using osu.Game.Users; namespace osu.Game.Overlays.Rankings.Tables { - public class PerformanceTable : RankingsTable + public class PerformanceTable : UserBasedTable { public PerformanceTable(int page = 1) : base(page) { } - protected override TableColumn[] CreateAdditionalHeaders() => new[] + protected override TableColumn[] CreateUniqueHeaders() => new[] { - new TableColumn("Accuracy", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)), - new TableColumn("Play Count", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)), new TableColumn("Performance", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)), - new TableColumn("SS", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)), - new TableColumn("S", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)), - new TableColumn("A", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)), }; - protected override Drawable[] CreateContent(int index, UserStatistics item) + protected override Drawable[] CreateUniqueContent(UserStatistics item) => new[] { - var content = new List + new RowText { - new OsuSpriteText - { - Text = $"#{index + 1}", - Font = OsuFont.GetFont(size: TEXT_SIZE, weight: FontWeight.Bold) - }, - }; - - var username = new LinkFlowContainer(t => t.Font = OsuFont.GetFont(size: TEXT_SIZE)) { AutoSizeAxes = Axes.Both }; - username.AddUserLink(item.User); - - content.AddRange(new Drawable[] - { - new FillFlowContainer - { - AutoSizeAxes = Axes.Both, - Direction = FillDirection.Horizontal, - Spacing = new Vector2(7, 0), - Children = new Drawable[] - { - new UpdateableFlag(item.User.Country) - { - Size = new Vector2(20, 13), - ShowPlaceholderOnNull = false, - }, - username - } - }, - new ColoredRowText - { - Text = $@"{item.Accuracy:F2}%", - }, - new ColoredRowText - { - Text = $@"{item.PlayCount:N0}", - }, - new RowText - { - Text = $@"{item.PP:N0}", - }, - new ColoredRowText - { - Text = $@"{item.GradesCount.SS + item.GradesCount.SSPlus:N0}", - }, - new ColoredRowText - { - Text = $@"{item.GradesCount.S + item.GradesCount.SPlus:N0}", - }, - new ColoredRowText - { - Text = $@"{item.GradesCount.A:N0}", - }, - }); - - return content.ToArray(); - } + Text = $@"{item.PP:N0}", + } + }; } } diff --git a/osu.Game/Overlays/Rankings/Tables/RankingsTable.cs b/osu.Game/Overlays/Rankings/Tables/RankingsTable.cs index 530295b039..d085b3a9a8 100644 --- a/osu.Game/Overlays/Rankings/Tables/RankingsTable.cs +++ b/osu.Game/Overlays/Rankings/Tables/RankingsTable.cs @@ -10,6 +10,9 @@ using osu.Framework.Extensions; using osu.Game.Graphics; using osu.Game.Graphics.Sprites; using osu.Framework.Extensions.IEnumerableExtensions; +using osu.Game.Users; +using osu.Game.Users.Drawables; +using osuTK; namespace osu.Game.Overlays.Rankings.Tables { @@ -54,10 +57,12 @@ namespace osu.Game.Overlays.Rankings.Tables value.ForEach(_ => backgroundFlow.Add(new TableRowBackground())); Columns = mainHeaders.Concat(CreateAdditionalHeaders()).ToArray(); - Content = value.Select((s, i) => CreateContent((page - 1) * items_per_page + i, s)).ToArray().ToRectangular(); + Content = value.Select((s, i) => createContent((page - 1) * items_per_page + i, s)).ToArray().ToRectangular(); } } + private Drawable[] createContent(int index, TModel item) => new Drawable[] { createIndexDrawable(index), createMainContent(item) }.Concat(CreateAdditionalContent(item)).ToArray(); + private static TableColumn[] mainHeaders => new[] { new TableColumn(string.Empty, Anchor.Centre, new Dimension(GridSizeMode.Absolute, 50)), // place @@ -66,10 +71,36 @@ namespace osu.Game.Overlays.Rankings.Tables protected abstract TableColumn[] CreateAdditionalHeaders(); - protected abstract Drawable[] CreateContent(int index, TModel item); + protected abstract Drawable[] CreateAdditionalContent(TModel item); protected override Drawable CreateHeader(int index, TableColumn column) => new HeaderText(column?.Header ?? string.Empty, HighlightedColumn()); + protected abstract Country GetCountry(TModel item); + + protected abstract Drawable CreateFlagContent(TModel item); + + private OsuSpriteText createIndexDrawable(int index) => new OsuSpriteText + { + Text = $"#{index + 1}", + Font = OsuFont.GetFont(size: TEXT_SIZE, weight: FontWeight.Bold) + }; + + private FillFlowContainer createMainContent(TModel item) => new FillFlowContainer + { + AutoSizeAxes = Axes.Both, + Direction = FillDirection.Horizontal, + Spacing = new Vector2(7, 0), + Children = new Drawable[] + { + new UpdateableFlag(GetCountry(item)) + { + Size = new Vector2(20, 13), + ShowPlaceholderOnNull = false, + }, + CreateFlagContent(item) + } + }; + protected virtual string HighlightedColumn() => @"Performance"; private class HeaderText : OsuSpriteText diff --git a/osu.Game/Overlays/Rankings/Tables/ScoresTable.cs b/osu.Game/Overlays/Rankings/Tables/ScoresTable.cs index d40da63f7f..0d4999ee8f 100644 --- a/osu.Game/Overlays/Rankings/Tables/ScoresTable.cs +++ b/osu.Game/Overlays/Rankings/Tables/ScoresTable.cs @@ -3,97 +3,34 @@ using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; -using osu.Game.Graphics; -using osu.Game.Graphics.Sprites; -using osu.Game.Users.Drawables; -using osuTK; -using System.Collections.Generic; -using osu.Game.Graphics.Containers; using osu.Game.Users; namespace osu.Game.Overlays.Rankings.Tables { - public class ScoresTable : RankingsTable + public class ScoresTable : UserBasedTable { public ScoresTable(int page = 1) : base(page) { } - protected override TableColumn[] CreateAdditionalHeaders() => new[] + protected override TableColumn[] CreateUniqueHeaders() => new[] { - new TableColumn("Accuracy", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)), - new TableColumn("Play Count", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)), new TableColumn("Total Score", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)), - new TableColumn("Ranked Score", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)), - new TableColumn("SS", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)), - new TableColumn("S", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)), - new TableColumn("A", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)), + new TableColumn("Ranked Score", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)) }; - protected override Drawable[] CreateContent(int index, UserStatistics item) + protected override Drawable[] CreateUniqueContent(UserStatistics item) => new[] { - var content = new List + new ColoredRowText { - new OsuSpriteText - { - Text = $"#{index + 1}", - Font = OsuFont.GetFont(size: TEXT_SIZE, weight: FontWeight.Bold) - }, - }; - - var username = new LinkFlowContainer(t => t.Font = OsuFont.GetFont(size: TEXT_SIZE)) { AutoSizeAxes = Axes.Both }; - username.AddUserLink(item.User); - - content.AddRange(new Drawable[] + Text = $@"{item.TotalScore:N0}", + }, + new RowText { - new FillFlowContainer - { - AutoSizeAxes = Axes.Both, - Direction = FillDirection.Horizontal, - Spacing = new Vector2(7, 0), - Children = new Drawable[] - { - new UpdateableFlag(item.User.Country) - { - Size = new Vector2(20, 13), - ShowPlaceholderOnNull = false, - }, - username - } - }, - new ColoredRowText - { - Text = $@"{item.Accuracy:F2}%", - }, - new ColoredRowText - { - Text = $@"{item.PlayCount:N0}", - }, - new ColoredRowText - { - Text = $@"{item.TotalScore:N0}", - }, - new RowText - { - Text = $@"{item.RankedScore:N0}", - }, - new ColoredRowText - { - Text = $@"{item.GradesCount.SS + item.GradesCount.SSPlus:N0}", - }, - new ColoredRowText - { - Text = $@"{item.GradesCount.S + item.GradesCount.SPlus:N0}", - }, - new ColoredRowText - { - Text = $@"{item.GradesCount.A:N0}", - }, - }); - - return content.ToArray(); - } + Text = $@"{item.RankedScore:N0}", + } + }; protected override string HighlightedColumn() => @"Ranked Score"; } diff --git a/osu.Game/Overlays/Rankings/Tables/UserBasedTable.cs b/osu.Game/Overlays/Rankings/Tables/UserBasedTable.cs new file mode 100644 index 0000000000..1283db9e32 --- /dev/null +++ b/osu.Game/Overlays/Rankings/Tables/UserBasedTable.cs @@ -0,0 +1,70 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System.Linq; +using osu.Framework.Graphics; +using osu.Framework.Graphics.Containers; +using osu.Game.Graphics; +using osu.Game.Graphics.Containers; +using osu.Game.Users; + +namespace osu.Game.Overlays.Rankings.Tables +{ + public abstract class UserBasedTable : RankingsTable + { + protected UserBasedTable(int page) + : base(page) + { + } + + protected override TableColumn[] CreateAdditionalHeaders() => new[] + { + new TableColumn("Accuracy", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)), + new TableColumn("Play Count", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)), + }.Concat(CreateUniqueHeaders()).Concat(new[] + { + new TableColumn("SS", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)), + new TableColumn("S", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)), + new TableColumn("A", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)), + }).ToArray(); + + protected override Country GetCountry(UserStatistics item) => item.User.Country; + + protected override Drawable CreateFlagContent(UserStatistics item) + { + var username = new LinkFlowContainer(t => t.Font = OsuFont.GetFont(size: TEXT_SIZE)) { AutoSizeAxes = Axes.Both }; + username.AddUserLink(item.User); + return username; + } + + protected override Drawable[] CreateAdditionalContent(UserStatistics item) => new[] + { + new ColoredRowText + { + Text = $@"{item.Accuracy:F2}%", + }, + new ColoredRowText + { + Text = $@"{item.PlayCount:N0}", + }, + }.Concat(CreateUniqueContent(item)).Concat(new[] + { + new ColoredRowText + { + Text = $@"{item.GradesCount.SS + item.GradesCount.SSPlus:N0}", + }, + new ColoredRowText + { + Text = $@"{item.GradesCount.S + item.GradesCount.SPlus:N0}", + }, + new ColoredRowText + { + Text = $@"{item.GradesCount.A:N0}", + } + }).ToArray(); + + protected abstract TableColumn[] CreateUniqueHeaders(); + + protected abstract Drawable[] CreateUniqueContent(UserStatistics item); + } +}