osu/osu.Game/Scoring/ScoreInfo.cs

283 lines
8.2 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 09:19:50 +00:00
using System;
using System.Collections.Generic;
2018-11-28 07:39:08 +00:00
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
2018-09-28 09:29:49 +00:00
using Newtonsoft.Json;
2018-12-26 13:42:17 +00:00
using Newtonsoft.Json.Converters;
using osu.Framework.Extensions;
2018-04-13 09:19:50 +00:00
using osu.Game.Beatmaps;
2018-11-28 07:39:08 +00:00
using osu.Game.Database;
using osu.Game.Online.API;
2018-11-28 07:33:42 +00:00
using osu.Game.Rulesets;
2018-04-13 09:19:50 +00:00
using osu.Game.Rulesets.Mods;
2018-11-28 07:33:42 +00:00
using osu.Game.Rulesets.Scoring;
2019-09-06 06:24:00 +00:00
using osu.Game.Users;
using osu.Game.Utils;
2018-04-13 09:19:50 +00:00
2018-11-28 07:12:57 +00:00
namespace osu.Game.Scoring
2018-04-13 09:19:50 +00:00
{
public class ScoreInfo : IHasFiles<ScoreFileInfo>, IHasPrimaryKey, ISoftDelete, IEquatable<ScoreInfo>
2018-04-13 09:19:50 +00:00
{
2018-11-28 07:39:08 +00:00
public int ID { get; set; }
2018-12-14 12:09:17 +00:00
[JsonProperty("rank")]
2018-12-26 13:42:17 +00:00
[JsonConverter(typeof(StringEnumConverter))]
2018-04-13 09:19:50 +00:00
public ScoreRank Rank { get; set; }
2018-12-14 12:09:17 +00:00
[JsonProperty("total_score")]
2019-02-26 04:10:07 +00:00
public long TotalScore { get; set; }
2018-04-13 09:19:50 +00:00
2018-12-14 12:09:17 +00:00
[JsonProperty("accuracy")]
[Column(TypeName = "DECIMAL(1,4)")] // TODO: This data type is wrong (should contain more precision). But at the same time, we probably don't need to be storing this in the database.
2018-04-13 09:19:50 +00:00
public double Accuracy { get; set; }
[JsonIgnore]
public string DisplayAccuracy => Accuracy.FormatAccuracy();
2019-01-04 07:33:35 +00:00
[JsonProperty(@"pp")]
2018-04-13 09:19:50 +00:00
public double? PP { get; set; }
2018-12-14 12:09:17 +00:00
[JsonProperty("max_combo")]
2018-04-13 09:19:50 +00:00
public int MaxCombo { get; set; }
2018-12-14 12:09:17 +00:00
[JsonIgnore]
public int Combo { get; set; } // Todo: Shouldn't exist in here
2018-04-13 09:19:50 +00:00
2018-12-14 12:09:17 +00:00
[JsonIgnore]
2018-11-28 08:26:46 +00:00
public int RulesetID { get; set; }
2018-12-14 12:09:17 +00:00
[JsonProperty("passed")]
[NotMapped]
public bool Passed { get; set; } = true;
[JsonIgnore]
2018-11-30 07:11:09 +00:00
public virtual RulesetInfo Ruleset { get; set; }
2018-04-13 09:19:50 +00:00
private APIMod[] localAPIMods;
2018-11-30 07:11:09 +00:00
private Mod[] mods;
[JsonIgnore]
2018-11-30 07:11:09 +00:00
[NotMapped]
2018-11-28 10:47:20 +00:00
public Mod[] Mods
2018-11-28 07:39:08 +00:00
{
2018-11-28 10:47:20 +00:00
get
2018-11-28 07:39:08 +00:00
{
2018-11-30 09:52:31 +00:00
if (mods != null)
return mods;
2018-11-30 07:11:09 +00:00
2021-04-12 11:49:26 +00:00
if (localAPIMods == null)
return Array.Empty<Mod>();
var rulesetInstance = Ruleset.CreateInstance();
return apiMods.Select(m => m.ToMod(rulesetInstance)).ToArray();
2018-11-30 07:11:09 +00:00
}
set
{
localAPIMods = null;
2018-11-30 07:11:09 +00:00
mods = value;
2018-11-28 07:39:08 +00:00
}
}
// Used for API serialisation/deserialisation.
[JsonProperty("mods")]
2021-04-12 11:49:44 +00:00
[NotMapped]
private APIMod[] apiMods
2018-11-30 07:11:09 +00:00
{
get
{
if (localAPIMods != null)
return localAPIMods;
if (mods == null)
return Array.Empty<APIMod>();
return localAPIMods = mods.Select(m => new APIMod(m)).ToArray();
}
2018-11-30 07:11:09 +00:00
set
{
localAPIMods = value;
2018-11-30 07:11:09 +00:00
// We potentially can't update this yet due to Ruleset being late-bound, so instead update on read as necessary.
2018-11-30 07:11:09 +00:00
mods = null;
}
}
2018-11-28 10:47:20 +00:00
// Used for database serialisation/deserialisation.
2021-04-12 11:49:37 +00:00
[JsonIgnore]
[Column("Mods")]
2021-04-12 11:50:18 +00:00
public string ModsJson
{
get => JsonConvert.SerializeObject(apiMods);
set => apiMods = JsonConvert.DeserializeObject<APIMod[]>(value);
}
2018-12-22 06:17:35 +00:00
[NotMapped]
[JsonProperty("user")]
public User User { get; set; }
2018-04-13 09:19:50 +00:00
2018-12-14 12:09:17 +00:00
[JsonIgnore]
2018-11-30 07:11:09 +00:00
[Column("User")]
2018-11-28 07:39:08 +00:00
public string UserString
{
get => User?.Username;
set
{
User ??= new User();
2019-02-25 06:25:22 +00:00
User.Username = value;
}
}
[JsonIgnore]
[Column("UserID")]
public int? UserID
{
2019-02-25 05:40:44 +00:00
get => User?.Id ?? 1;
set
{
User ??= new User();
2019-02-25 06:25:22 +00:00
User.Id = value ?? 1;
}
2018-11-28 07:39:08 +00:00
}
2018-11-30 09:06:48 +00:00
[JsonIgnore]
public int BeatmapInfoID { get; set; }
2018-12-14 12:09:17 +00:00
[JsonIgnore]
2018-11-30 07:11:09 +00:00
public virtual BeatmapInfo Beatmap { get; set; }
2018-04-13 09:19:50 +00:00
2018-12-14 12:09:17 +00:00
[JsonIgnore]
2018-11-28 09:52:57 +00:00
public long? OnlineScoreID { get; set; }
2018-04-13 09:19:50 +00:00
2018-12-14 12:09:17 +00:00
[JsonIgnore]
2018-11-28 11:16:20 +00:00
public DateTimeOffset Date { get; set; }
2018-04-13 09:19:50 +00:00
2018-12-14 12:09:17 +00:00
[JsonProperty("statistics")]
2018-12-05 10:44:01 +00:00
public Dictionary<HitResult, int> Statistics = new Dictionary<HitResult, int>();
2018-11-28 07:39:08 +00:00
2018-12-14 12:09:17 +00:00
[JsonIgnore]
2018-11-30 07:11:09 +00:00
[Column("Statistics")]
public string StatisticsJson
{
get => JsonConvert.SerializeObject(Statistics);
set
{
if (value == null)
2018-11-30 07:11:09 +00:00
{
Statistics.Clear();
return;
2018-11-30 07:11:09 +00:00
}
2018-12-05 10:44:01 +00:00
Statistics = JsonConvert.DeserializeObject<Dictionary<HitResult, int>>(value);
}
}
[NotMapped]
[JsonIgnore]
2020-06-19 10:58:35 +00:00
public List<HitEvent> HitEvents { get; set; }
2018-11-30 09:06:48 +00:00
[JsonIgnore]
2018-11-28 07:39:08 +00:00
public List<ScoreFileInfo> Files { get; set; }
2018-12-14 12:09:17 +00:00
[JsonIgnore]
public string Hash { get; set; }
2018-12-14 12:09:17 +00:00
[JsonIgnore]
2018-11-28 07:39:08 +00:00
public bool DeletePending { get; set; }
2018-11-30 07:11:09 +00:00
/// <summary>
/// The position of this score, starting at 1.
/// </summary>
[NotMapped]
[JsonProperty("position")]
public int? Position { get; set; }
private bool isLegacyScore;
/// <summary>
/// Whether this <see cref="ScoreInfo"/> represents a legacy (osu!stable) score.
/// </summary>
[JsonIgnore]
[NotMapped]
public bool IsLegacyScore
{
get
{
if (isLegacyScore)
return true;
// The above check will catch legacy online scores that have an appropriate UserString + UserId.
// For non-online scores such as those imported in, a heuristic is used based on the following table:
//
// Mode | UserString | UserId
// --------------- | ---------- | ---------
// stable | <username> | 1
// lazer | <username> | <userid>
// lazer (offline) | Guest | 1
return ID > 0 && UserID == 1 && UserString != "Guest";
}
set => isLegacyScore = value;
}
2020-10-07 06:34:03 +00:00
public IEnumerable<HitResultDisplayStatistic> GetStatisticsForDisplay()
{
2020-10-07 06:34:03 +00:00
foreach (var r in Ruleset.CreateInstance().GetHitResults())
{
2020-10-07 06:34:03 +00:00
int value = Statistics.GetOrDefault(r.result);
2020-10-07 06:34:03 +00:00
switch (r.result)
{
case HitResult.SmallTickHit:
{
int total = value + Statistics.GetOrDefault(HitResult.SmallTickMiss);
if (total > 0)
2020-10-07 06:34:03 +00:00
yield return new HitResultDisplayStatistic(r.result, value, total, r.displayName);
break;
}
case HitResult.LargeTickHit:
{
int total = value + Statistics.GetOrDefault(HitResult.LargeTickMiss);
if (total > 0)
2020-10-07 06:34:03 +00:00
yield return new HitResultDisplayStatistic(r.result, value, total, r.displayName);
break;
}
case HitResult.SmallTickMiss:
case HitResult.LargeTickMiss:
break;
default:
2020-10-07 06:34:03 +00:00
yield return new HitResultDisplayStatistic(r.result, value, null, r.displayName);
break;
}
}
}
2019-02-25 09:42:08 +00:00
public override string ToString() => $"{User} playing {Beatmap}";
2019-12-03 04:33:42 +00:00
public bool Equals(ScoreInfo other)
{
if (other == null)
return false;
if (ID != 0 && other.ID != 0)
return ID == other.ID;
if (OnlineScoreID.HasValue && other.OnlineScoreID.HasValue)
return OnlineScoreID == other.OnlineScoreID;
if (!string.IsNullOrEmpty(Hash) && !string.IsNullOrEmpty(other.Hash))
return Hash == other.Hash;
return ReferenceEquals(this, other);
}
2018-04-13 09:19:50 +00:00
}
}