diff --git a/osu.Game.Tournament/Models/TournamentTeam.cs b/osu.Game.Tournament/Models/TournamentTeam.cs index ab353d771a..4368c2fda8 100644 --- a/osu.Game.Tournament/Models/TournamentTeam.cs +++ b/osu.Game.Tournament/Models/TournamentTeam.cs @@ -2,6 +2,7 @@ // See the LICENCE file in the repository root for full licence text. using System; +using System.Diagnostics; using System.Linq; using Newtonsoft.Json; using osu.Framework.Bindables; @@ -49,25 +50,9 @@ namespace osu.Game.Tournament.Models public Bindable Seed = new Bindable(string.Empty); - [JsonIgnore] - public Bindable LastYearPlacing = new Bindable("N/A"); - - /// - /// Previously, a value of 0 was meant to indicate "no placement last year". - /// This will convert the number 0 from an old bracket.json file back to the "N/A" string (new default). - /// - [JsonProperty("LastYearPlacing")] - private object lastYearPlacing - { - get => LastYearPlacing.Value; - set - { - if (value is long oldValue && oldValue == 0) - LastYearPlacing.Value = LastYearPlacing.Default; - else - LastYearPlacing.Value = value.ToString() ?? LastYearPlacing.Default; - } - } + [JsonProperty] + [JsonConverter(typeof(LastYearPlacingConverter))] + public Bindable LastYearPlacing = new Bindable(@"N/A"); [JsonProperty] public BindableList Players { get; } = new BindableList(); @@ -90,5 +75,37 @@ namespace osu.Game.Tournament.Models } public override string ToString() => FullName.Value ?? Acronym.Value; + + public class LastYearPlacingConverter : JsonConverter + { + public override bool CanConvert(Type objectType) => objectType == typeof(Bindable); + + public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) + => serializer.Serialize(writer, ((Bindable)value!).Value); + + public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) + { + var lastYearPlacing = existingValue as Bindable; + Debug.Assert(lastYearPlacing != null); + + switch (reader.TokenType) + { + case JsonToken.String: + lastYearPlacing.Value = (string?)reader.Value ?? lastYearPlacing.Default; + break; + + case JsonToken.Integer: + long value = (long)reader.Value!; + lastYearPlacing.Value = value > 0 ? $@"#{value}" : lastYearPlacing.Default; + break; + + default: + reader.Read(); + break; + } + + return lastYearPlacing; + } + } } }