2019-01-24 16:43:03 +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.
2018-04-13 17:19:50 +08:00
using System ;
using System.Collections.Generic ;
2018-11-28 15:39:08 +08:00
using System.ComponentModel.DataAnnotations.Schema ;
using System.Linq ;
2018-09-28 17:29:49 +08:00
using Newtonsoft.Json ;
2018-12-26 21:42:17 +08:00
using Newtonsoft.Json.Converters ;
2020-09-25 19:22:59 +08:00
using osu.Framework.Extensions ;
2018-04-13 17:19:50 +08:00
using osu.Game.Beatmaps ;
2018-11-28 15:39:08 +08:00
using osu.Game.Database ;
2018-11-28 15:33:42 +08:00
using osu.Game.Rulesets ;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Mods ;
2018-11-28 15:33:42 +08:00
using osu.Game.Rulesets.Scoring ;
2019-09-06 14:24:00 +08:00
using osu.Game.Users ;
2020-02-04 12:17:23 +08:00
using osu.Game.Utils ;
2018-04-13 17:19:50 +08:00
2018-11-28 15:12:57 +08:00
namespace osu.Game.Scoring
2018-04-13 17:19:50 +08:00
{
2019-06-12 04:01:57 +08:00
public class ScoreInfo : IHasFiles < ScoreFileInfo > , IHasPrimaryKey , ISoftDelete , IEquatable < ScoreInfo >
2018-04-13 17:19:50 +08:00
{
2018-11-28 15:39:08 +08:00
public int ID { get ; set ; }
2018-12-14 20:09:17 +08:00
[JsonProperty("rank")]
2018-12-26 21:42:17 +08:00
[JsonConverter(typeof(StringEnumConverter))]
2018-04-13 17:19:50 +08:00
public ScoreRank Rank { get ; set ; }
2018-12-14 20:09:17 +08:00
[JsonProperty("total_score")]
2019-02-26 12:10:07 +08:00
public long TotalScore { get ; set ; }
2018-04-13 17:19:50 +08:00
2018-12-14 20:09:17 +08:00
[JsonProperty("accuracy")]
2021-03-25 16:50:21 +08:00
[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 17:19:50 +08:00
public double Accuracy { get ; set ; }
2020-02-03 23:09:07 +08:00
[JsonIgnore]
2020-02-04 12:17:23 +08:00
public string DisplayAccuracy = > Accuracy . FormatAccuracy ( ) ;
2020-02-03 23:09:07 +08:00
2019-01-04 15:33:35 +08:00
[JsonProperty(@"pp")]
2018-04-13 17:19:50 +08:00
public double? PP { get ; set ; }
2018-12-14 20:09:17 +08:00
[JsonProperty("max_combo")]
2018-04-13 17:19:50 +08:00
public int MaxCombo { get ; set ; }
2018-12-14 20:09:17 +08:00
[JsonIgnore]
public int Combo { get ; set ; } // Todo: Shouldn't exist in here
2018-04-13 17:19:50 +08:00
2018-12-14 20:09:17 +08:00
[JsonIgnore]
2018-11-28 16:26:46 +08:00
public int RulesetID { get ; set ; }
2018-12-14 20:09:17 +08:00
[JsonProperty("passed")]
[NotMapped]
public bool Passed { get ; set ; } = true ;
[JsonIgnore]
2018-11-30 15:11:09 +08:00
public virtual RulesetInfo Ruleset { get ; set ; }
2018-04-13 17:19:50 +08:00
2018-11-30 15:11:09 +08:00
private Mod [ ] mods ;
2018-12-14 20:09:17 +08:00
[JsonProperty("mods")]
2018-11-30 15:11:09 +08:00
[NotMapped]
2018-11-28 18:47:20 +08:00
public Mod [ ] Mods
2018-11-28 15:39:08 +08:00
{
2018-11-28 18:47:20 +08:00
get
2018-11-28 15:39:08 +08:00
{
2018-11-30 17:52:31 +08:00
if ( mods ! = null )
return mods ;
2018-11-30 15:11:09 +08:00
if ( modsJson = = null )
2018-11-29 17:29:25 +08:00
return Array . Empty < Mod > ( ) ;
2018-11-30 15:11:09 +08:00
return getModsFromRuleset ( JsonConvert . DeserializeObject < DeserializedMod [ ] > ( modsJson ) ) ;
}
set
{
2021-03-21 18:01:06 +08:00
modsJson = null ;
2018-11-30 15:11:09 +08:00
mods = value ;
2018-11-28 15:39:08 +08:00
}
}
2018-11-30 16:16:00 +08:00
private Mod [ ] getModsFromRuleset ( DeserializedMod [ ] mods ) = > Ruleset . CreateInstance ( ) . GetAllMods ( ) . Where ( mod = > mods . Any ( d = > d . Acronym = = mod . Acronym ) ) . ToArray ( ) ;
2018-11-30 15:11:09 +08:00
private string modsJson ;
2018-12-14 20:09:17 +08:00
[JsonIgnore]
2018-11-30 15:11:09 +08:00
[Column("Mods")]
public string ModsJson
{
2021-03-21 18:01:06 +08:00
get
{
if ( modsJson ! = null )
return modsJson ;
if ( mods = = null )
return null ;
return modsJson = JsonConvert . SerializeObject ( mods . Select ( m = > new DeserializedMod { Acronym = m . Acronym } ) ) ;
}
2018-11-30 15:11:09 +08:00
set
{
modsJson = value ;
// we potentially can't update this yet due to Ruleset being late-bound, so instead update on read as necessary.
mods = null ;
}
}
2018-11-28 18:47:20 +08:00
2018-12-22 14:17:35 +08:00
[NotMapped]
[JsonProperty("user")]
public User User { get ; set ; }
2018-04-13 17:19:50 +08:00
2018-12-14 20:09:17 +08:00
[JsonIgnore]
2018-11-30 15:11:09 +08:00
[Column("User")]
2018-11-28 15:39:08 +08:00
public string UserString
{
get = > User ? . Username ;
2019-02-25 12:58:19 +08:00
set
{
2020-06-03 15:48:44 +08:00
User ? ? = new User ( ) ;
2019-02-25 14:25:22 +08:00
User . Username = value ;
2019-02-25 12:58:19 +08:00
}
2019-02-23 16:04:02 +08:00
}
[JsonIgnore]
[Column("UserID")]
2020-11-06 11:40:54 +08:00
public int? UserID
2019-02-23 16:04:02 +08:00
{
2019-02-25 13:40:44 +08:00
get = > User ? . Id ? ? 1 ;
2019-02-25 12:58:19 +08:00
set
{
2020-06-03 15:48:44 +08:00
User ? ? = new User ( ) ;
2019-02-25 14:25:22 +08:00
User . Id = value ? ? 1 ;
2019-02-25 12:58:19 +08:00
}
2018-11-28 15:39:08 +08:00
}
2018-11-30 17:06:48 +08:00
[JsonIgnore]
2018-11-28 16:26:39 +08:00
public int BeatmapInfoID { get ; set ; }
2018-12-14 20:09:17 +08:00
[JsonIgnore]
2018-11-30 15:11:09 +08:00
public virtual BeatmapInfo Beatmap { get ; set ; }
2018-04-13 17:19:50 +08:00
2018-12-14 20:09:17 +08:00
[JsonIgnore]
2018-11-28 17:52:57 +08:00
public long? OnlineScoreID { get ; set ; }
2018-04-13 17:19:50 +08:00
2018-12-14 20:09:17 +08:00
[JsonIgnore]
2018-11-28 19:16:20 +08:00
public DateTimeOffset Date { get ; set ; }
2018-04-13 17:19:50 +08:00
2018-12-14 20:09:17 +08:00
[JsonProperty("statistics")]
2018-12-05 18:44:01 +08:00
public Dictionary < HitResult , int > Statistics = new Dictionary < HitResult , int > ( ) ;
2018-11-28 15:39:08 +08:00
2018-12-14 20:09:17 +08:00
[JsonIgnore]
2018-11-30 15:11:09 +08:00
[Column("Statistics")]
public string StatisticsJson
2018-11-30 13:27:08 +08:00
{
get = > JsonConvert . SerializeObject ( Statistics ) ;
set
{
if ( value = = null )
2018-11-30 15:11:09 +08:00
{
Statistics . Clear ( ) ;
2018-11-30 13:27:08 +08:00
return ;
2018-11-30 15:11:09 +08:00
}
2018-11-30 13:27:08 +08:00
2018-12-05 18:44:01 +08:00
Statistics = JsonConvert . DeserializeObject < Dictionary < HitResult , int > > ( value ) ;
2018-11-30 13:27:08 +08:00
}
}
2020-06-18 21:11:03 +08:00
[NotMapped]
[JsonIgnore]
2020-06-19 18:58:35 +08:00
public List < HitEvent > HitEvents { get ; set ; }
2020-06-15 21:44:55 +08:00
2018-11-30 17:06:48 +08:00
[JsonIgnore]
2018-11-28 15:39:08 +08:00
public List < ScoreFileInfo > Files { get ; set ; }
2018-12-14 20:09:17 +08:00
[JsonIgnore]
2018-11-30 16:36:06 +08:00
public string Hash { get ; set ; }
2018-12-14 20:09:17 +08:00
[JsonIgnore]
2018-11-28 15:39:08 +08:00
public bool DeletePending { get ; set ; }
2018-11-30 15:11:09 +08:00
2020-07-28 21:08:10 +08:00
/// <summary>
/// The position of this score, starting at 1.
/// </summary>
[NotMapped]
[JsonProperty("position")]
public int? Position { get ; set ; }
2020-09-29 17:55:06 +08:00
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 14:34:03 +08:00
public IEnumerable < HitResultDisplayStatistic > GetStatisticsForDisplay ( )
2020-09-25 19:22:59 +08:00
{
2020-10-07 14:34:03 +08:00
foreach ( var r in Ruleset . CreateInstance ( ) . GetHitResults ( ) )
2020-09-25 19:22:59 +08:00
{
2020-10-07 14:34:03 +08:00
int value = Statistics . GetOrDefault ( r . result ) ;
2020-09-25 19:22:59 +08:00
2020-10-07 14:34:03 +08:00
switch ( r . result )
2020-09-25 19:22:59 +08:00
{
case HitResult . SmallTickHit :
{
int total = value + Statistics . GetOrDefault ( HitResult . SmallTickMiss ) ;
if ( total > 0 )
2020-10-07 14:34:03 +08:00
yield return new HitResultDisplayStatistic ( r . result , value , total , r . displayName ) ;
2020-09-25 19:22:59 +08:00
break ;
}
case HitResult . LargeTickHit :
{
int total = value + Statistics . GetOrDefault ( HitResult . LargeTickMiss ) ;
if ( total > 0 )
2020-10-07 14:34:03 +08:00
yield return new HitResultDisplayStatistic ( r . result , value , total , r . displayName ) ;
2020-09-25 19:22:59 +08:00
break ;
}
case HitResult . SmallTickMiss :
case HitResult . LargeTickMiss :
break ;
default :
2020-10-07 14:34:03 +08:00
yield return new HitResultDisplayStatistic ( r . result , value , null , r . displayName ) ;
2020-09-25 19:22:59 +08:00
break ;
}
}
}
2018-11-30 15:11:09 +08:00
[Serializable]
2018-11-30 16:16:00 +08:00
protected class DeserializedMod : IMod
2018-11-30 15:11:09 +08:00
{
2018-11-30 16:16:00 +08:00
public string Acronym { get ; set ; }
2019-05-03 09:05:45 +08:00
public bool Equals ( IMod other ) = > Acronym = = other ? . Acronym ;
2018-11-30 15:11:09 +08:00
}
2019-02-25 17:42:08 +08:00
public override string ToString ( ) = > $"{User} playing {Beatmap}" ;
2019-06-12 04:01:57 +08:00
2019-12-03 12:33:42 +08: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 17:19:50 +08:00
}
}