forked from JavidPack/BossChecklist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataManager.cs
More file actions
503 lines (429 loc) · 18.3 KB
/
DataManager.cs
File metadata and controls
503 lines (429 loc) · 18.3 KB
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Terraria;
using Terraria.Localization;
using Terraria.ModLoader.IO;
namespace BossChecklist
{
[Flags]
internal enum NetRecordID : int {
None = 0,
PreviousAttemptOnly = 1,
FirstVictory = 2,
PersonalBest_Duration = 4,
PersonalBest_HitsTaken = 8,
NewPersonalBest = PersonalBest_Duration | PersonalBest_HitsTaken,
WorldRecord_Duration = 16,
WorldRecord_HitsTaken = 32,
WorldRecord = WorldRecord_Duration | WorldRecord_HitsTaken,
PersonalBest_Reset = 64, // Resetting personal best records will also remove record from World Records
FirstVictory_Reset = 128,
ResettingRecord = PersonalBest_Reset | FirstVictory_Reset
}
/// <summary>
/// Record container for player-based records. All personal records should be stored here and saved to a ModPlayer.
/// </summary>
public class BossRecord : TagSerializable
{
internal string bossKey;
internal PersonalStats stats = new PersonalStats();
public static Func<TagCompound, BossRecord> DESERIALIZER = tag => new BossRecord(tag);
private BossRecord(TagCompound tag) {
bossKey = tag.Get<string>(nameof(bossKey));
stats = tag.Get<PersonalStats>(nameof(stats));
}
public BossRecord(string bossKey) {
this.bossKey = bossKey;
}
public override string ToString() => $"Personal Records for: '{bossKey}'";
public TagCompound SerializeData() {
return new TagCompound {
{ nameof(bossKey), bossKey },
{ nameof(stats), stats }
};
}
}
/// <summary>
/// Record container for world-based records. All world records should be stored within this class and saved to a ModSystem.
/// </summary>
public class WorldRecord : TagSerializable
{
internal string bossKey;
internal WorldStats stats = new WorldStats();
public static Func<TagCompound, WorldRecord> DESERIALIZER = tag => new WorldRecord(tag);
private WorldRecord(TagCompound tag) {
bossKey = tag.Get<string>(nameof(bossKey));
stats = tag.Get<WorldStats>(nameof(stats));
}
public WorldRecord(string bossKey) {
this.bossKey = bossKey;
}
public TagCompound SerializeData() {
return new TagCompound {
{ nameof(bossKey), bossKey },
{ nameof(stats), stats }
};
}
public override string ToString() => $"World Records for: '{bossKey}'";
}
/// <summary>
/// Players are able to set personal records for boss fights.
/// This will hold the statistics and records of those fights, including the player's previous fight, first victory, and personal best.
/// <para>[Statistics]</para>
/// <list type="bullet">
/// <item> <term>Kills</term> <description>The total amount of fights that the player has won against the boss.</description> </item>
/// <item> <term>Deaths</term> <description>The total amount of deaths a player has experienced while fighting the boss.</description> </item>
/// <item> <term>Attempts</term> <description>The amount of fights a player has started against the boss, win or loss.</description> </item>
/// <item> <term>Play Time First</term> <description>The amount of play time that has passed up until the first kill of the boss.</description> </item>
/// </list>
/// <para>[Records]</para>
/// <list type="bullet">
/// <item> <term>Duration</term> <description>The amount of time it took to defeat the boss.</description> </item>
/// <item> <term>HitsTaken</term> <description>The amount of times a player has taken damage while fighting the boss.</description> </item>
/// </list>
/// </summary>
public class PersonalStats : TagSerializable
{
/// Statistics
public int kills;
public int deaths;
public int attempts;
public long playTimeFirst = -1;
/// Records
public int durationPrev = -1;
public int durationBest = -1;
public int durationPrevBest = -1;
public int durationFirst = -1;
public int hitsTakenPrev = -1;
public int hitsTakenBest = -1;
public int hitsTakenPrevBest = -1;
public int hitsTakenFirst = -1;
public static Func<TagCompound, PersonalStats> DESERIALIZER = tag => new PersonalStats(tag);
public PersonalStats() { }
private PersonalStats(TagCompound tag) {
kills = tag.Get<int>(nameof(kills));
deaths = tag.Get<int>(nameof(deaths));
attempts = tag.Get<int>(nameof(attempts));
playTimeFirst = tag.Get<long>(nameof(playTimeFirst));
durationPrev = tag.Get<int>(nameof(durationPrev));
durationBest = tag.Get<int>(nameof(durationBest));
durationPrevBest = tag.Get<int>(nameof(durationPrevBest));
durationFirst = tag.Get<int>(nameof(durationFirst));
hitsTakenPrev = tag.Get<int>(nameof(hitsTakenPrev));
hitsTakenBest = tag.Get<int>(nameof(hitsTakenBest));
hitsTakenPrevBest = tag.Get<int>(nameof(hitsTakenPrevBest));
hitsTakenFirst = tag.Get<int>(nameof(hitsTakenFirst));
}
public TagCompound SerializeData() {
return new TagCompound {
{ nameof(kills), kills },
{ nameof(deaths), deaths },
{ nameof(attempts), attempts },
{ nameof(playTimeFirst), playTimeFirst },
{ nameof(durationPrev), durationPrev },
{ nameof(durationBest), durationBest },
{ nameof(durationPrevBest), durationPrevBest },
{ nameof(durationFirst), durationFirst },
{ nameof(hitsTakenPrev), hitsTakenPrev },
{ nameof(hitsTakenBest), hitsTakenBest },
{ nameof(hitsTakenPrevBest), hitsTakenPrevBest },
{ nameof(hitsTakenFirst), hitsTakenFirst },
};
}
internal void NetSend(BinaryWriter writer, NetRecordID recordType) {
writer.Write((int)recordType); // Write the record type(s) we are changing as NetRecieve will need to read this value.
// If the record type is a reset, nothing else needs to be done, as the records will be wiped. Otherwise...
if (!recordType.HasFlag(NetRecordID.ResettingRecord)) {
// ...previous records are always overwritten for the player to view...
writer.Write(durationPrev);
writer.Write(hitsTakenPrev);
// ... and any first or new records we set will be flagged for sending
if (recordType.HasFlag(NetRecordID.PersonalBest_Duration)) {
writer.Write(durationBest);
writer.Write(durationPrevBest);
}
if (recordType.HasFlag(NetRecordID.PersonalBest_HitsTaken)) {
writer.Write(hitsTakenBest);
writer.Write(hitsTakenPrevBest);
}
if (recordType.HasFlag(NetRecordID.FirstVictory)) {
writer.Write(durationFirst);
writer.Write(hitsTakenFirst);
}
}
}
internal void NetRecieve(BinaryReader reader) {
NetRecordID recordType = (NetRecordID)reader.ReadInt32();
if (recordType.HasFlag(NetRecordID.ResettingRecord)) {
if (recordType.HasFlag(NetRecordID.FirstVictory_Reset)) {
playTimeFirst = -1;
durationFirst = hitsTakenFirst = -1;
}
if (recordType.HasFlag(NetRecordID.PersonalBest_Reset)) {
durationBest = durationPrevBest = hitsTakenBest = hitsTakenPrevBest = -1;
}
}
else {
kills++; // Kills always increase by 1, since records will only be updated when a boss is defeated
durationPrev = reader.ReadInt32();
hitsTakenPrev = reader.ReadInt32();
if (recordType.HasFlag(NetRecordID.PersonalBest_Duration)) {
durationBest = reader.ReadInt32();
durationPrevBest = reader.ReadInt32();
}
if (recordType.HasFlag(NetRecordID.PersonalBest_HitsTaken)) {
hitsTakenBest = reader.ReadInt32();
hitsTakenPrevBest = reader.ReadInt32();
}
if (recordType.HasFlag(NetRecordID.FirstVictory)) {
durationFirst = reader.ReadInt32();
hitsTakenFirst = reader.ReadInt32();
}
// This should always be read by Multiplayer clients, so create combat texts on new records
if (recordType.HasFlag(NetRecordID.WorldRecord)) {
CombatText.NewText(Main.LocalPlayer.getRect(), Color.LightYellow, "New Record!", true);
}
else if (recordType.HasFlag(NetRecordID.NewPersonalBest)) {
CombatText.NewText(Main.LocalPlayer.getRect(), Color.LightYellow, "New World Record!", true);
}
}
}
/// <summary>
/// Gets the personal kills and deaths of an entry record in a string format.
/// If the designated boss has not been killed nor has killed a player, 'Unchallenged' will be returned instead.
/// </summary>
public string GetKDR() {
if (kills == 0 && deaths == 0)
return Language.GetTextValue($"{BossLogUI.LangLog}.Records.Unchallenged");
return $"{kills} {Language.GetTextValue($"{BossLogUI.LangLog}.Records.Kills")} / {deaths} {Language.GetTextValue($"{BossLogUI.LangLog}.Records.Deaths")}";
}
/// <summary>
/// Gets the duration time of an entry record in a string format.
/// If the designated boss has not been defeated yet, 'No Record' will be returned instead.
/// </summary>
/// <param name="ticks">The about of ticks a fight took.</param>
/// <param name="sign">Only used when find a time difference using <see cref="TimeConversionDiff"/>.</param>
public static string TimeConversion(int ticks, string sign = "") {
if (ticks == -1)
return Language.GetTextValue($"{BossLogUI.LangLog}.Records.NoRecord");
const int TicksPerSecond = 60;
const int TicksPerMinute = TicksPerSecond * 60;
int minutes = ticks / TicksPerMinute; // Minutes will still show if 0
float seconds = (float)(ticks - (float)(minutes * TicksPerMinute)) / TicksPerSecond;
return $"{sign}{minutes}:{seconds.ToString("00.00")}";
}
/// <summary>
/// Takes a duration record and compares it against another.
/// The result will be in a string format along with a symbol to represent the difference direction.
/// </summary>
/// <param name="recordTicks">The recorded amount of ticks that is being compared against.</param>
/// <param name="compareTicks">The amount of ticks from the compare value.</param>
/// <param name="diff">The color value that represents the record time difference.
/// <list type="bullet">
/// <item><term>Red</term> <description>The time recorded is slower than the compare value (+)</description></item>
/// <item><term>Yellow</term> <description>No difference between the record's time (±)</description></item>
/// <item><term>Green</term> <description>The time recorded is faster than the compare value (-)</description></item></list>
/// </param>
public static string TimeConversionDiff(int recordTicks, int compareTicks, out Color diff) {
if (recordTicks == -1 || compareTicks == -1) {
diff = default;
return ""; // records cannot be compared
}
// A color and sign should be picked
int tickDiff = recordTicks - compareTicks;
string sign;
if (tickDiff > 0) {
sign = "+";
diff = Color.Red;
}
else if (tickDiff == 0) {
sign = "±";
diff = Color.Yellow;
}
else {
tickDiff *= -1;
sign = "-";
diff = Color.Green;
}
return TimeConversion(tickDiff, sign);
}
/// <summary>
/// Gets the hits taken entry record in a string format.
/// If the user's record is zero, 'No Hit!' will be returned instead.
/// Otherwise, if the entry has not been defeated yet, 'No Record' will be returned instead.
/// </summary>
/// <param name="count">The record being checked.</param>
public static string HitCount(int count) {
if (count == -1)
return Language.GetTextValue($"{BossLogUI.LangLog}.Records.NoRecord");
if (count == 0)
return Language.GetTextValue($"{BossLogUI.LangLog}.Records.NoHit");
return count.ToString();
}
/// <summary>
/// Takes a Hits Taken record and compares it against another.
/// The result will be in a string format along with a symbol to represent the difference direction.
/// </summary>
/// <param name="count">The recorded amount of hits that is being compared against.</param>
/// <param name="compareCount">The amount of hits from the compare value.</param>
/// <param name="diff">The color value that represents the record time difference.
/// <list type="bullet">
/// <item><term>Red</term> <description>The time recorded is slower than the compare value (+)</description></item>
/// <item><term>Yellow</term> <description>No difference between the record's time (±)</description></item>
/// <item><term>Green</term> <description>The time recorded is faster than the compare value (-)</description></item></list>
/// </param>
public static string HitCountDiff(int count, int compareCount, out Color diff) {
if (count == -1 || compareCount == -1) {
diff = default;
return ""; // records cannot be compared
}
// A color and sign should be picked
int countDiff = count - compareCount;
string sign;
if (countDiff > 0) {
sign = "+";
diff = Color.Red;
}
else if (countDiff == 0) {
sign = "±";
diff = Color.Yellow;
}
else {
countDiff *= -1;
sign = "-";
diff = Color.Green;
}
return $"{sign}{countDiff}";
}
/// <summary>
/// Gets the recorded play time snapshot for the entry's first defeation in a string format.
/// If the entry has not yet been defeated, 'Unchallenged' will be returned instead.
/// </summary>
public string PlayTimeToString() {
if (kills == 0)
return Language.GetTextValue($"{BossLogUI.LangLog}.Records.Unchallenged");
int hours = (int)(playTimeFirst / TimeSpan.TicksPerHour);
int minutes = (int)((playTimeFirst - (hours * TimeSpan.TicksPerHour)) / TimeSpan.TicksPerMinute);
float seconds = (float)((playTimeFirst - (float)(hours * TimeSpan.TicksPerHour) - (float)(minutes * TimeSpan.TicksPerMinute)) / TimeSpan.TicksPerSecond);
return $"{(hours > 0 ? hours + ":" : "")}{(hours > 0 ? minutes.ToString("00") : minutes)}:{seconds.ToString("00.00")}";
}
}
/* Plans for World Records
* All players that join a "world" are recorded to a list
* Server Host can remove anyone from this list (ex. Troll, wrong character join)
* Server grabs BEST Records from the list of players and determines which one is the best
*/
/// <summary>
/// In multiplayer, players are able to set world records against other players.
/// This will contain global kills and deaths as well as the best record's value and holder.
/// </summary>
public class WorldStats : TagSerializable
{
public int totalKills;
public int totalDeaths;
public List<string> durationHolder = new List<string> { };
public int durationWorld = -1;
public List<string> hitsTakenHolder = new List<string> { };
public int hitsTakenWorld = -1;
public bool DurationEmpty => durationHolder.Count == 0 || durationWorld == -1;
public bool HitsTakenEmpty => hitsTakenHolder.Count == 0 || hitsTakenWorld == -1;
public static Func<TagCompound, WorldStats> DESERIALIZER = tag => new WorldStats(tag);
public WorldStats() { }
private WorldStats(TagCompound tag) {
totalKills = tag.Get<int>(nameof(totalKills));
totalDeaths = tag.Get<int>(nameof(totalDeaths));
durationHolder = tag.GetList<string>(nameof(durationHolder)).ToList();
durationWorld = tag.Get<int>(nameof(durationWorld));
hitsTakenHolder = tag.GetList<string>(nameof(hitsTakenHolder)).ToList();
hitsTakenWorld = tag.Get<int>(nameof(hitsTakenWorld));
}
public TagCompound SerializeData() {
return new TagCompound {
{ nameof(totalKills), totalKills },
{ nameof(totalDeaths), totalDeaths },
{ nameof(durationHolder), durationHolder },
{ nameof(durationWorld), durationWorld },
{ nameof(hitsTakenHolder), hitsTakenHolder },
{ nameof(hitsTakenWorld), hitsTakenWorld },
};
}
internal void NetSend(BinaryWriter writer, NetRecordID netRecords) {
writer.Write((int)netRecords); // Write the record type(s) we are changing. NetRecieve will need to read this value.
// Packet should have any beaten record values and holders written on it
if (netRecords.HasFlag(NetRecordID.WorldRecord_Duration)) {
writer.Write(durationWorld);
writer.Write(durationHolder.Count);
foreach (string name in durationHolder) {
writer.Write(name);
}
}
if (netRecords.HasFlag(NetRecordID.WorldRecord_HitsTaken)) {
writer.Write(hitsTakenWorld);
writer.Write(hitsTakenHolder.Count);
foreach (string name in hitsTakenHolder) {
writer.Write(name);
}
}
}
internal void NetRecieve(BinaryReader reader) {
NetRecordID netRecords = (NetRecordID)reader.ReadInt32(); // Read the type of record being updated
totalKills++; // Kills always increase by 1, since records will only be updated when a boss is defeated
//TODO: figure out death counts
// Set the world record values and holders
if (netRecords.HasFlag(NetRecordID.WorldRecord_Duration)) {
durationWorld = reader.ReadInt32();
int durationHolderTotal = reader.ReadInt32();
durationHolder.Clear();
for (int i = 0; i < durationHolderTotal; i++) {
durationHolder.Add(reader.ReadString());
}
}
if (netRecords.HasFlag(NetRecordID.WorldRecord_HitsTaken)) {
hitsTakenWorld = reader.ReadInt32();
int hitsTakenHolderTotal = reader.ReadInt32();
hitsTakenHolder.Clear();
for (int i = 0; i < hitsTakenHolderTotal; i++) {
hitsTakenHolder.Add(reader.ReadString());
}
}
}
/// <summary>
/// Gets the total kills and deaths of an entry in a string format.
/// If the entry has not yet been defeated, 'Unchallenged' will be returned instead.
/// </summary>
public string GetGlobalKDR() {
if (totalKills == 0 && totalDeaths == 0)
return Language.GetTextValue($"{BossLogUI.LangLog}.Records.Unchallenged");
return $"{totalKills} {Language.GetTextValue($"{BossLogUI.LangLog}.Records.Kills")} / {totalDeaths} {Language.GetTextValue($"{BossLogUI.LangLog}.Records.Deaths")}";
}
/// <summary>
/// Lists the current holders of the duration world record.
/// If the entry has not yet been defated, 'Be the first to claim the world record!' will be returned instead.
/// </summary>
public string ListDurationRecordHolders() {
if (DurationEmpty)
return Language.GetTextValue($"{BossLogUI.LangLog}.Records.ClaimRecord");
string list = Language.GetTextValue($"{BossLogUI.LangLog}.Records.RecordHolder");
foreach (string name in durationHolder) {
list += $"\n •{name}";
}
return list;
}
/// <summary>
/// Lists the current holders of the hits taken world record.
/// If the entry has not yet been defated, 'Be the first to claim the world record!' will be returned instead.
/// </summary>
public string ListHitsTakenRecordHolders() {
if (HitsTakenEmpty)
return Language.GetTextValue($"{BossLogUI.LangLog}.Records.ClaimRecord");
string list = Language.GetTextValue($"{BossLogUI.LangLog}.Records.RecordHolder");
foreach (string name in hitsTakenHolder) {
list += $"\n •{name}";
}
return list;
}
}
}