-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathWorldInformation.cs
611 lines (546 loc) · 19.4 KB
/
WorldInformation.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
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using Bencodex;
using Bencodex.Types;
using Nekoyume.Model.State;
using Nekoyume.TableData;
namespace Nekoyume.Model
{
[Serializable]
public class WorldInformation : IState, ISerializable, ICloneable
{
[Serializable]
public struct World : IState
{
public readonly int Id;
public readonly string Name;
public readonly int StageBegin;
public readonly int StageEnd;
public readonly long UnlockedBlockIndex;
public readonly long StageClearedBlockIndex;
public readonly int StageClearedId;
public bool IsUnlocked => UnlockedBlockIndex != -1;
public bool IsStageCleared => StageClearedBlockIndex != -1;
public World(
WorldSheet.Row worldRow,
long unlockedBlockIndex = -1,
long stageClearedBlockIndex = -1,
int stageClearedId = -1)
{
Id = worldRow.Id;
Name = worldRow.Name;
StageBegin = worldRow.StageBegin;
StageEnd = worldRow.StageEnd;
UnlockedBlockIndex = unlockedBlockIndex;
StageClearedBlockIndex = stageClearedBlockIndex;
StageClearedId = stageClearedId;
}
public World(World world, long unlockedBlockIndex = -1)
{
Id = world.Id;
Name = world.Name;
StageBegin = world.StageBegin;
StageEnd = world.StageEnd;
UnlockedBlockIndex = unlockedBlockIndex;
StageClearedBlockIndex = world.StageClearedBlockIndex;
StageClearedId = world.StageClearedId;
}
public World(World world, long stageClearedBlockIndex, int stageClearedId)
{
Id = world.Id;
Name = world.Name;
StageBegin = world.StageBegin;
StageEnd = world.StageEnd;
UnlockedBlockIndex = world.UnlockedBlockIndex;
StageClearedBlockIndex = stageClearedBlockIndex;
StageClearedId = stageClearedId;
}
public World(Bencodex.Types.Dictionary serialized)
{
Id = serialized.GetInteger("Id");
Name = serialized.GetString("Name");
StageBegin = serialized.GetInteger("StageBegin");
StageEnd = serialized.GetInteger("StageEnd");
UnlockedBlockIndex = serialized.GetLong("UnlockedBlockIndex");
StageClearedBlockIndex = serialized.GetLong("StageClearedBlockIndex");
StageClearedId = serialized.GetInteger("StageClearedId");
}
public IValue Serialize()
{
return new Bencodex.Types.Dictionary(new Dictionary<IKey, IValue>
{
[(Bencodex.Types.Text) "Id"] = Id.Serialize(),
[(Bencodex.Types.Text) "Name"] = Name.Serialize(),
[(Bencodex.Types.Text) "StageBegin"] = StageBegin.Serialize(),
[(Bencodex.Types.Text) "StageEnd"] = StageEnd.Serialize(),
[(Bencodex.Types.Text) "UnlockedBlockIndex"] = UnlockedBlockIndex.Serialize(),
[(Bencodex.Types.Text) "StageClearedBlockIndex"] =
StageClearedBlockIndex.Serialize(),
[(Bencodex.Types.Text) "StageClearedId"] = StageClearedId.Serialize(),
});
}
public bool ContainsStageId(int stageId)
{
return stageId >= StageBegin &&
stageId <= StageEnd;
}
public bool IsPlayable(int stageId)
{
return stageId <= GetNextStageIdForPlay();
}
public int GetNextStageIdForPlay()
{
if (!IsUnlocked)
return -1;
return GetNextStageId();
}
public int GetNextStageId()
{
return IsStageCleared ? Math.Min(StageEnd, StageClearedId + 1) : StageBegin;
}
}
private static readonly Codec _codec = new Codec();
private Dictionary _serialized;
private Dictionary<int, World> _worlds;
/// <summary>
/// key: worldId
/// </summary>
private IDictionary<int, World> worlds
{
get
{
if (_worlds is null)
{
_worlds = _serialized.ToDictionary(
kv => kv.Key.ToInteger(),
kv => new World((Bencodex.Types.Dictionary)kv.Value)
);
_serialized = null;
}
return _worlds;
}
}
public WorldInformation(
long blockIndex,
WorldSheet worldSheet,
bool openAllOfWorldsAndStages = false, string avatarName = "")
{
if (worldSheet is null)
{
return;
}
var orderedSheet = worldSheet.OrderedList;
_worlds = new Dictionary<int, World>();
if (openAllOfWorldsAndStages)
{
if(avatarName == "8World")
{
foreach (var row in orderedSheet)
{
//for test world 8
if(row.Id == 7)
{
_worlds.Add(row.Id, new World(row, blockIndex, blockIndex, row.StageEnd - 1));
}
else if(row.Id == 8)
{
_worlds.Add(row.Id, new World(row));
}
else
{
_worlds.Add(row.Id, new World(row, blockIndex, blockIndex, row.StageEnd));
}
}
}
else
{
foreach (var row in orderedSheet)
{
_worlds.Add(row.Id, new World(row, blockIndex, blockIndex, row.StageEnd));
}
}
}
else
{
var isFirst = true;
foreach (var row in orderedSheet)
{
var worldId = row.Id;
if (isFirst)
{
isFirst = false;
_worlds.Add(worldId, new World(row, blockIndex));
}
else
{
_worlds.Add(worldId, new World(row));
}
}
}
}
public WorldInformation(long blockIndex, WorldSheet worldSheet, int clearStageId = 0)
{
if (worldSheet is null)
{
return;
}
var orderedSheet = worldSheet.OrderedList;
_worlds = new Dictionary<int, World>();
if (clearStageId > 0)
{
foreach (var row in orderedSheet)
{
if (row.StageBegin > clearStageId)
{
_worlds.Add(row.Id, new World(row));
}
else if (row.StageEnd > clearStageId)
{
_worlds.Add(row.Id, new World(row, blockIndex, blockIndex, clearStageId));
}
else
{
_worlds.Add(row.Id, new World(row, blockIndex, blockIndex, row.StageEnd));
}
}
}
else
{
var isFirst = true;
foreach (var row in orderedSheet)
{
var worldId = row.Id;
if (isFirst)
{
isFirst = false;
_worlds.Add(worldId, new World(row, blockIndex));
}
else
{
_worlds.Add(worldId, new World(row));
}
}
}
}
public WorldInformation(Bencodex.Types.Dictionary serialized)
{
_serialized = serialized;
}
private WorldInformation(SerializationInfo info, StreamingContext context)
: this((Dictionary)_codec.Decode((byte[])info.GetValue("serialized", typeof(byte[]))))
{
}
public IValue Serialize()
{
if (_serialized is Dictionary d)
{
return d;
}
#pragma warning disable LAA1002
return new Bencodex.Types.Dictionary(_worlds.Select(kv =>
#pragma warning restore LAA1002
new KeyValuePair<IKey, IValue>(
(Bencodex.Types.Text) kv.Key.Serialize(),
(Bencodex.Types.Dictionary) kv.Value.Serialize())));
}
public bool IsWorldUnlocked(int worldId) =>
TryGetWorld(worldId, out var world)
&& world.IsUnlocked;
public bool IsStageCleared(int stageId)
{
int clearedStageId;
if (stageId >= GameConfig.MimisbrunnrStartStageId
? TryGetLastClearedMimisbrunnrStageId(out clearedStageId)
: TryGetLastClearedStageId(out clearedStageId))
{
return stageId <= clearedStageId;
}
return false;
}
public bool TryAddWorld(WorldSheet.Row worldRow, out World world)
{
if (worldRow is null || (_serialized is Dictionary d
? d.ContainsKey((IKey)worldRow.Id.Serialize())
: _worlds.ContainsKey(worldRow.Id)))
{
world = default;
return false;
}
world = new World(worldRow);
if (_serialized is Dictionary s)
{
var key = (IKey)worldRow.Id.Serialize();
_serialized = (Dictionary)s.Add(key, world.Serialize());
}
else
{
worlds.Add(worldRow.Id, world);
}
return true;
}
public void UpdateWorld(WorldSheet.Row worldRow)
{
var key = (IKey)worldRow.Id.Serialize();
var originWorld = _serialized is Dictionary d
? new World((Dictionary)d[key])
: _worlds[worldRow.Id];
var world = new World(
worldRow,
originWorld.UnlockedBlockIndex,
originWorld.StageClearedBlockIndex,
originWorld.StageClearedId
);
if (_serialized is Dictionary s)
{
_serialized = (Dictionary)s.SetItem(key, world.Serialize());
}
else
{
_worlds[worldRow.Id] = world;
}
}
/// <summary>
/// Get `World` object that equals to `worldId` argument.
/// </summary>
/// <param name="worldId"></param>
/// <param name="world"></param>
/// <returns></returns>
/// <exception cref="KeyNotFoundException"></exception>
public bool TryGetWorld(int worldId, out World world)
{
if (!worlds.ContainsKey(worldId))
{
world = default;
return false;
}
world = worlds[worldId];
return true;
}
public bool TryGetFirstWorld(out World world)
{
if (worlds.Count == 0)
{
world = default;
return false;
}
world = worlds.OrderBy(w => w.Key).First().Value;
return true;
}
/// <summary>
/// Get `World` object that contains `stageId` argument.
/// </summary>
/// <param name="stageId"></param>
/// <param name="world"></param>
/// <returns></returns>
public bool TryGetWorldByStageId(int stageId, out World world)
{
foreach (World w in worlds.Values)
{
if (w.ContainsStageId(stageId))
{
world = w;
return true;
}
}
world = default;
return false;
}
/// <summary>
/// Get `World` object that contains the most recent stage clear.
/// </summary>
/// <param name="world"></param>
/// <returns></returns>
public bool TryGetUnlockedWorldByStageClearedBlockIndex(out World world)
{
try
{
world = worlds.Values
.Where(e => e.IsStageCleared)
.OrderByDescending(e => e.StageClearedBlockIndex)
.First();
return true;
}
catch
{
world = default;
return false;
}
}
/// <summary>
/// Get stage id of the most recent cleared.(ignore mimisbrunnr)
/// </summary>
/// <param name="stageId"></param>
/// <returns></returns>
public bool TryGetLastClearedStageId(out int stageId)
{
var clearedStages = worlds.Values
.Where(world => world.Id < GameConfig.MimisbrunnrWorldId &&
world.IsStageCleared)
.ToList();
if (clearedStages.Any())
{
stageId = clearedStages.Max(world => world.StageClearedId);
return true;
}
stageId = default;
return false;
}
public bool TryGetLastClearedMimisbrunnrStageId(out int stageId)
{
var clearedStages = worlds.Values
.Where(world => world.Id == GameConfig.MimisbrunnrWorldId &&
world.IsStageCleared)
.ToList();
if (clearedStages.Any())
{
stageId = clearedStages.Max(world => world.StageClearedId);
return true;
}
stageId = default;
return false;
}
/// <summary>
/// Clear a specific stage. And consider world unlock.
/// </summary>
/// <param name="worldId"></param>
/// <param name="stageId"></param>
/// <param name="clearedAt"></param>
/// <param name="worldSheet"></param>
/// <param name="worldUnlockSheet"></param>
/// <exception cref="FailedToUnlockWorldException"></exception>
public void ClearStage(
int worldId,
int stageId,
long clearedAt,
WorldSheet worldSheet,
WorldUnlockSheet worldUnlockSheet)
{
if (!worlds.ContainsKey(worldId))
{
return;
}
var world = worlds[worldId];
if (stageId < world.StageBegin ||
stageId > world.StageEnd)
{
return;
}
// NOTE: Always consider world unlock.
// Because even a stage that has already been cleared can be a trigger for world unlock due to the table patch.
if (worldUnlockSheet.TryGetUnlockedInformation(worldId, stageId, out var worldIdsToUnlock))
{
foreach (var worldIdToUnlock in worldIdsToUnlock)
{
UnlockWorld(worldIdToUnlock, clearedAt, worldSheet);
}
}
if (stageId <= world.StageClearedId)
{
return;
}
worlds[worldId] = new World(world, clearedAt, stageId);
}
public void AddAndUnlockNewWorld(WorldSheet.Row worldRow, long unlockedAt, WorldSheet worldSheet)
{
var worldId = worldRow.Id;
if (IsStageCleared(worldRow.StageBegin - 1))
{
var world = new World(worldRow);
worlds.Add(worldId, world);
UnlockWorld(worldId, unlockedAt, worldSheet);
}
else
{
throw new FailedAddWorldException($"Failed to add {worldId} world to WorldInformation.");
}
}
public void AddAndUnlockMimisbrunnrWorld(
WorldSheet.Row worldRow,
long unlockedAt,
WorldSheet worldSheet,
WorldUnlockSheet worldUnlockSheet)
{
var succeed = false;
var worldId = worldRow.Id;
if (worldId == GameConfig.MimisbrunnrWorldId)
{
var unlockRow = worldUnlockSheet.OrderedList.FirstOrDefault(row => row.WorldIdToUnlock == worldId);
if (!(unlockRow is null) &&
IsStageCleared(unlockRow.StageId))
{
succeed = true;
}
}
else if (IsStageCleared(worldRow.StageBegin - 1))
{
succeed = true;
}
if (succeed)
{
var world = new World(worldRow);
worlds.Add(worldId, world);
UnlockWorld(worldId, unlockedAt, worldSheet);
}
else
{
throw new FailedAddWorldException($"Failed to add {worldId} world to WorldInformation.");
}
}
/// <summary>
/// Unlock a specific world.
/// </summary>
/// <param name="worldId"></param>
/// <param name="unlockedAt"></param>
/// <param name="worldSheet"></param>
/// <exception cref="FailedToUnlockWorldException"></exception>
public void UnlockWorld(int worldId, long unlockedAt, WorldSheet worldSheet)
{
World world;
if (worlds.ContainsKey(worldId))
{
world = worlds[worldId];
}
else if (!worldSheet.TryGetValue(worldId, out var worldRow) ||
!TryAddWorld(worldRow, out world))
{
throw new FailedToUnlockWorldException($"{nameof(worldId)}: {worldId}");
}
if (world.IsUnlocked)
{
return;
}
worlds[worldId] = new World(world, unlockedAt);
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("serialized", _codec.Encode(Serialize()));
}
public object Clone()
{
return new WorldInformation((Dictionary)Serialize());
}
}
[Serializable]
public class FailedToUnlockWorldException : Exception
{
public FailedToUnlockWorldException(string message) : base(message)
{
}
protected FailedToUnlockWorldException(SerializationInfo info, StreamingContext context) :
base(info, context)
{
}
}
[Serializable]
public class FailedAddWorldException : Exception
{
public FailedAddWorldException(string message) : base(message)
{
}
protected FailedAddWorldException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}