-
Notifications
You must be signed in to change notification settings - Fork 7
/
ScoreInfo.cs
324 lines (252 loc) · 9.78 KB
/
ScoreInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// 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.
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Newtonsoft.Json;
using osu.Framework.Localisation;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Models;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Scoring;
using osu.Game.Users;
using osu.Game.Utils;
using Realms;
namespace osu.Game.Scoring
{
[ExcludeFromDynamicCompile]
[MapTo("Score")]
public partial class ScoreInfo : IRealmObject, IHasGuidPrimaryKey, IHasRealmFiles, ISoftDelete, IEquatable<ScoreInfo>, IScoreInfo
{
[PrimaryKey]
public Guid ID { get; set; }
public BeatmapInfo BeatmapInfo { get; set; } = null!;
public RulesetInfo Ruleset { get; set; } = null!;
public IList<RealmNamedFileUsage> Files { get; } = null!;
public string Hash { get; set; } = string.Empty;
public bool DeletePending { get; set; }
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;
[MapTo("MaximumStatistics")]
public string MaximumStatisticsJson { 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,
Username = RealmUser.Username,
CountryCode = RealmUser.CountryCode,
};
set
{
user = value;
RealmUser = new RealmUser
{
OnlineID = user.OnlineID,
Username = user.Username,
CountryCode = user.CountryCode,
};
}
}
[Ignored]
public ScoreRank Rank
{
get => (ScoreRank)RankInt;
set => RankInt = (int)value;
}
[MapTo(nameof(Rank))]
public int RankInt { get; set; }
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.MaximumStatistics = new Dictionary<HitResult, int>(clone.MaximumStatistics);
// Ensure we have fresh mods to avoid any references (ie. after gameplay).
clone.clearAllMods();
clone.ModsJson = ModsJson;
clone.RealmUser = new RealmUser
{
OnlineID = RealmUser.OnlineID,
Username = RealmUser.Username,
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 Dictionary<HitResult, int>? maximumStatistics;
[Ignored]
public Dictionary<HitResult, int> MaximumStatistics
{
get
{
if (maximumStatistics != null)
return maximumStatistics;
if (!string.IsNullOrEmpty(MaximumStatisticsJson))
maximumStatistics = JsonConvert.DeserializeObject<Dictionary<HitResult, int>>(MaximumStatisticsJson);
return maximumStatistics ??= new Dictionary<HitResult, int>();
}
set => maximumStatistics = 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.LargeBonus:
case HitResult.SmallBonus:
if (MaximumStatistics.TryGetValue(r.result, out int count) && count > 0)
yield return new HitResultDisplayStatistic(r.result, value, null, 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();
}
}