1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-26 18:32:56 +08:00
osu-lazer/osu.Game/Scoring/ScoreInfo.cs

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

291 lines
8.5 KiB
C#
Raw Normal View History

// 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-04-13 17:19:50 +08:00
2017-03-15 13:06:05 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Newtonsoft.Json;
using osu.Framework.Localisation;
using osu.Framework.Testing;
2017-07-26 12:22:46 +08:00
using osu.Game.Beatmaps;
2018-11-28 15:39:08 +08:00
using osu.Game.Database;
using osu.Game.Models;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
2018-11-28 15:33:42 +08:00
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Scoring;
using osu.Game.Users;
using osu.Game.Utils;
using Realms;
2018-11-28 15:12:57 +08:00
namespace osu.Game.Scoring
2016-11-29 14:41:48 +08:00
{
[ExcludeFromDynamicCompile]
[MapTo("Score")]
public class ScoreInfo : RealmObject, IHasGuidPrimaryKey, IHasRealmFiles, ISoftDelete, IEquatable<ScoreInfo>, IScoreInfo
2016-11-29 14:41:48 +08:00
{
[PrimaryKey]
public Guid ID { get; set; }
2018-04-13 17:19:50 +08:00
public BeatmapInfo BeatmapInfo { get; set; } = null!;
public RulesetInfo Ruleset { get; set; } = null!;
public IList<RealmNamedFileUsage> Files { get; } = null!;
2021-04-21 14:16:28 +08:00
public string Hash { get; set; } = string.Empty;
2018-11-28 15:39:08 +08:00
public bool DeletePending { get; set; }
2018-11-30 15:11:09 +08:00
public long TotalScore { get; set; }
public int MaxCombo { get; set; }
public double Accuracy { get; set; }
public bool HasReplay => !string.IsNullOrEmpty(Hash);
public DateTimeOffset Date { get; set; }
public double? PP { get; set; }
[Indexed]
public long OnlineID { get; set; } = -1;
[MapTo("User")]
public RealmUser RealmUser { get; set; } = null!;
[MapTo("Mods")]
public string ModsJson { get; set; } = string.Empty;
[MapTo("Statistics")]
public string StatisticsJson { get; set; } = string.Empty;
public ScoreInfo(BeatmapInfo? beatmap = null, RulesetInfo? ruleset = null, RealmUser? realmUser = null)
{
Ruleset = ruleset ?? new RulesetInfo();
BeatmapInfo = beatmap ?? new BeatmapInfo();
RealmUser = realmUser ?? new RealmUser();
ID = Guid.NewGuid();
}
[UsedImplicitly] // Realm
private ScoreInfo()
{
}
// TODO: this is a bit temporary to account for the fact that this class is used to ferry API user data to certain UI components.
// Eventually we should either persist enough information to realm to not require the API lookups, or perform the API lookups locally.
private APIUser? user;
[Ignored]
public APIUser User
{
get => user ??= new APIUser
{
Id = RealmUser.OnlineID,
2022-07-16 11:30:25 +08:00
Username = RealmUser.Username,
2022-07-18 15:16:59 +08:00
CountryCode = RealmUser.CountryCode,
};
set
{
user = value;
RealmUser = new RealmUser
{
OnlineID = user.OnlineID,
2022-07-16 11:30:25 +08:00
Username = user.Username,
2022-07-18 15:16:59 +08:00
CountryCode = user.CountryCode,
};
}
}
[Ignored]
public ScoreRank Rank
2019-12-03 12:33:42 +08:00
{
get => (ScoreRank)RankInt;
set => RankInt = (int)value;
2019-12-03 12:33:42 +08:00
}
[MapTo(nameof(Rank))]
public int RankInt { get; set; }
2021-10-28 17:23:52 +08:00
IRulesetInfo IScoreInfo.Ruleset => Ruleset;
IBeatmapInfo IScoreInfo.Beatmap => BeatmapInfo;
IUser IScoreInfo.User => User;
IEnumerable<INamedFileUsage> IHasNamedFiles.Files => Files;
#region Properties required to make things work with existing usages
public Guid BeatmapInfoID => BeatmapInfo.ID;
public int UserID => RealmUser.OnlineID;
public int RulesetID => Ruleset.OnlineID;
[Ignored]
public List<HitEvent> HitEvents { get; set; } = new List<HitEvent>();
public ScoreInfo DeepClone()
{
var clone = (ScoreInfo)this.Detach().MemberwiseClone();
clone.Statistics = new Dictionary<HitResult, int>(clone.Statistics);
clone.RealmUser = new RealmUser
{
OnlineID = RealmUser.OnlineID,
Username = RealmUser.Username,
2022-07-18 15:16:59 +08:00
CountryCode = RealmUser.CountryCode,
};
return clone;
}
[Ignored]
public bool Passed { get; set; } = true;
public int Combo { get; set; }
/// <summary>
/// The position of this score, starting at 1.
/// </summary>
[Ignored]
public int? Position { get; set; } // TODO: remove after all calls to `CreateScoreInfo` are gone.
[Ignored]
public LocalisableString DisplayAccuracy => Accuracy.FormatAccuracy();
/// <summary>
/// Whether this <see cref="ScoreInfo"/> represents a legacy (osu!stable) score.
/// </summary>
[Ignored]
public bool IsLegacyScore => Mods.OfType<ModClassic>().Any();
private Dictionary<HitResult, int>? statistics;
[Ignored]
public Dictionary<HitResult, int> Statistics
{
get
{
if (statistics != null)
return statistics;
if (!string.IsNullOrEmpty(StatisticsJson))
statistics = JsonConvert.DeserializeObject<Dictionary<HitResult, int>>(StatisticsJson);
return statistics ??= new Dictionary<HitResult, int>();
}
set => statistics = value;
}
private Mod[]? mods;
[Ignored]
public Mod[] Mods
{
get
{
if (mods != null)
return mods;
return APIMods.Select(m => m.ToMod(Ruleset.CreateInstance())).ToArray();
}
set
{
clearAllMods();
mods = value;
updateModsJson();
}
}
private APIMod[]? apiMods;
// Used for API serialisation/deserialisation.
[Ignored]
public APIMod[] APIMods
{
get
{
if (apiMods != null) return apiMods;
// prioritise reading from realm backing
if (!string.IsNullOrEmpty(ModsJson))
apiMods = JsonConvert.DeserializeObject<APIMod[]>(ModsJson);
// then check mods set via Mods property.
if (mods != null)
apiMods ??= mods.Select(m => new APIMod(m)).ToArray();
return apiMods ?? Array.Empty<APIMod>();
}
set
{
clearAllMods();
apiMods = value;
updateModsJson();
}
}
private void clearAllMods()
{
ModsJson = string.Empty;
mods = null;
apiMods = null;
}
private void updateModsJson()
{
ModsJson = APIMods.Length > 0
? JsonConvert.SerializeObject(APIMods)
: string.Empty;
}
public IEnumerable<HitResultDisplayStatistic> GetStatisticsForDisplay()
{
foreach (var r in Ruleset.CreateInstance().GetHitResults())
{
int value = Statistics.GetValueOrDefault(r.result);
switch (r.result)
{
case HitResult.SmallTickHit:
{
int total = value + Statistics.GetValueOrDefault(HitResult.SmallTickMiss);
if (total > 0)
yield return new HitResultDisplayStatistic(r.result, value, total, r.displayName);
break;
}
case HitResult.LargeTickHit:
{
int total = value + Statistics.GetValueOrDefault(HitResult.LargeTickMiss);
if (total > 0)
yield return new HitResultDisplayStatistic(r.result, value, total, r.displayName);
break;
}
case HitResult.SmallTickMiss:
case HitResult.LargeTickMiss:
break;
default:
yield return new HitResultDisplayStatistic(r.result, value, null, r.displayName);
break;
}
}
}
#endregion
public bool Equals(ScoreInfo other) => other.ID == ID;
public override string ToString() => this.GetDisplayTitle();
2016-11-29 14:41:48 +08:00
}
}