summaryrefslogtreecommitdiff
path: root/server/lib/core/state/modifier_deck.dart
blob: b7c00f12bc827e7018ca5d8d4d5185899472c214 (plain)
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
part of 'game_state.dart';

// ignore_for_file: library_private_types_in_public_api

class ModifierDeck {
  static const int _kImbuementLevel1 = 1;
  static const int _kImbuementLevel2 = 2;
  static const int _kPlusMinus1Count = 5;
  static const int _kPlus0Count = 6;
  static const int _kCursePosition = 5;

  final String name;
  final CardStack<ModifierCard> _drawPile = CardStack<ModifierCard>();
  final CardStack<ModifierCard> _discardPile = CardStack<ModifierCard>();
  final CardStack<ModifierCard> _removedPile = CardStack<ModifierCard>();
  final Map<String, int> _removables = {
    "curse": 0,
    "bless": 0,
    "in-enfeeble": 0,
    "vi-enfeeble": 0,
    "vi-gr-enfeeble": 0,
    "li-enfeeble": 0,
    "in-empower": 0,
    "rm-empower": 0,
    "vi-empower": 0,
    "vi-gr-empower": 0,
  };

  /// Removable gfx keys that get physical draw-pile card sync. In the
  /// reference app this is exactly the set of counters that have a
  /// `_handleRemovableCards` listener attached in `_initListeners()`, i.e. the
  /// keys present at construction. Entries added later by `getRemovable()`
  /// (e.g. "enfeeble") never trigger physical card sync.
  final Set<String> _syncedRemovables = {};

  int _badOmen = 0;
  bool _corrosiveSpew = false;
  int _addedMinusOnes = 0;
  int _imbuement = 0;

  int _revealedCount = 0;
  bool _cassandraSpecial = false;

  bool _needsShuffle = false;
  bool get needsShuffle => _needsShuffle;

  BuiltList<ModifierCard> get drawPileContents =>
      BuiltList.of(_drawPile.getList());
  BuiltList<ModifierCard> get discardPileContents =>
      BuiltList.of(_discardPile.getList());
  BuiltList<ModifierCard> get removedPileContents =>
      BuiltList.of(_removedPile.getList());
  bool get drawPileIsEmpty => _drawPile.isEmpty;
  bool get drawPileIsNotEmpty => _drawPile.isNotEmpty;
  bool get discardPileIsEmpty => _discardPile.isEmpty;
  bool get discardPileIsNotEmpty => _discardPile.isNotEmpty;
  int get drawPileSize => _drawPile.size();
  int get discardPileSize => _discardPile.size();
  int get removedPileSize => _removedPile.size();
  ModifierCard get discardPileTop => _discardPile.peek;
  ModifierCard get drawPileTop => _drawPile.peek;

  CardStack<ModifierCard> get drawPileNotifier => _drawPile;
  int get badOmen => _badOmen;
  bool get corrosiveSpew => _corrosiveSpew;
  int get addedMinusOnes => _addedMinusOnes;
  int get imbuement => _imbuement;
  int get revealedCount => _revealedCount;
  bool get cassandraSpecial => _cassandraSpecial;

  /// The removable-card counter totals (bless / curse / empower / enfeeble
  /// variants) — the client's deck-header counters read these.
  Map<String, int> get removables => Map.unmodifiable(_removables);

  ModifierDeck(this.name) {
    //build deck
    _initDeck();
    _initListeners();
  }

  ModifierDeck.fromJson(this.name, Map<String, dynamic> modifierDeckData) {
    _initDeck();
    _initListeners();
    updateFromJson(modifierDeckData);
  }

  /// Resets this deck to the default 20-card state.
  void resetToDefault() {
    _initDeck();
    _cassandraSpecial = false;
  }

  /// Updates this deck in-place from [modifierDeckData].
  void updateFromJson(Map<String, dynamic> modifierDeckData) {
    // Reset removable counts first (the piles are fully replaced below
    // anyway, so no physical card sync is needed here).
    for (final key in _removables.keys) {
      _removables[key] = 0;
    }
    _needsShuffle = false;

    for (final item in modifierDeckData["drawPile"] as List) {
      String gfx = item["gfx"];
      if (gfx == "curse" ||
          gfx.contains("empower") ||
          gfx.contains("enfeeble") ||
          gfx == "bless") {
        addRemovableValue(_StateModifier(), gfx, 1);
      }
    }
    for (final item in modifierDeckData["discardPile"] as List) {
      String gfx = item["gfx"];
      if (_isMultiplyType(gfx)) {
        _needsShuffle = true;
      }
    }

    _drawPile.clear();
    _discardPile.clear();
    _removedPile.clear();
    _drawPile.setList(_getCardsFromJson(modifierDeckData, "drawPile"));
    _discardPile.setList(_getCardsFromJson(modifierDeckData, "discardPile"));
    if (modifierDeckData.containsKey('removedPile')) {
      _removedPile.setList(_getCardsFromJson(modifierDeckData, "removedPile"));
    }

    _imbuement = modifierDeckData.containsKey("imbuement")
        ? modifierDeckData['imbuement'] as int
        : 0;
    _badOmen = modifierDeckData.containsKey('badOmen')
        ? modifierDeckData["badOmen"] as int
        : 0;
    _corrosiveSpew = modifierDeckData.containsKey('corrosiveSpew')
        ? modifierDeckData["corrosiveSpew"] as bool
        : false;
    _revealedCount = min(
      modifierDeckData.containsKey('revealed')
          ? modifierDeckData["revealed"] as int
          : 0,
      drawPileSize,
    );
    _cassandraSpecial = modifierDeckData.containsKey('cassandra')
        ? modifierDeckData["cassandra"] as bool
        : false;
    _addedMinusOnes = modifierDeckData.containsKey('addedMinusOnes')
        ? modifierDeckData["addedMinusOnes"] as int
        : 0;

    // The counter totals are the source of truth; the physical-card rebuild
    // above already re-derived them for saves written before `removables`
    // existed on the wire.
    if (modifierDeckData.containsKey('removables')) {
      final saved = modifierDeckData['removables'] as Map<String, dynamic>;
      for (final key in _removables.keys) {
        _removables[key] = (saved[key] as int?) ?? _removables[key]!;
      }
    }
  }

  void setRemovableValue(_StateModifier _, String id, int value) {
    if (_removables.containsKey(id)) {
      _removables[id] = value;
      if (_syncedRemovables.contains(id)) {
        _handleRemovableCards(value, id);
      }
    }
  }

  void addRemovableValue(_StateModifier _, String id, int value) {
    final current = _removables[id];
    if (current != null) {
      final newValue = current + value;
      _removables[id] = newValue;
      if (_syncedRemovables.contains(id)) {
        _handleRemovableCards(newValue, id);
      }
    }
  }

  int getRemovable(String id) {
    _removables[id] ??= 0;
    return _removables[id] ?? 0;
  }

  void moveCardToRemovedPile(_StateModifier s, String gfx) {
    if (hasCard(gfx)) {
      removeCard(s, gfx);
      _removedPile.add(ModifierCard(CardType.add, gfx));
    }
  }

  void restoreCardFromRemovedPile(_StateModifier s, String gfx, CardType type) {
    addCard(s, gfx, type);
    _removedPile.removeFirstWhere((card) => card.gfx == gfx);
  }

  void setBadOmen(_StateModifier _, int value) {
    _badOmen = value;
  }

  void setCorrosiveSpew(_StateModifier _) {
    _corrosiveSpew = true;
  }

  void addCSSanctuary(_StateModifier s, {GameState? gameState}) {
    //adds one of each
    final gs = gameState ?? getIt<GameState>();
    _drawPile.add(gs._sanctuaryDeck.drawFlip(s));
    _drawPile.add(gs._sanctuaryDeck.drawMult(s));
    _drawPile.shuffle();
  }

  void removeCSSanctuary(_StateModifier _, {GameState? gameState}) {
    var list = _drawPile.getList();
    final gs = gameState ?? getIt<GameState>();
    for (int i = list.length - 1; i >= 0; i--) {
      if (list[i].gfx.startsWith("sanctuary")) {
        gs._sanctuaryDeck.returnCard(list[i].gfx);
        _drawPile.removeAt(i);
      }
    }
    list = _discardPile.getList();
    for (int i = list.length - 1; i >= 0; i--) {
      if (list[i].gfx.startsWith("sanctuary")) {
        gs._sanctuaryDeck.returnCard(list[i].gfx);
        _discardPile.removeAt(i);
      }
    }
  }

  bool hasCSSanctuary() {
    if (_drawPile.getList().firstWhereOrNull((test) {
              return test.gfx.startsWith("sanctuary");
            }) !=
            null ||
        _discardPile.getList().firstWhereOrNull((test) {
              return test.gfx.startsWith("sanctuary");
            }) !=
            null) {
      return true;
    }
    return false;
  }

  void addCSPartyCard(_StateModifier s, int type) {
    addCard(s, "party/$type", CardType.remove);
  }

  void removeCSPartyCard(_StateModifier _) {
    _drawPile.removeWhere((test) {
      return test.gfx.startsWith("party/");
    });
    _discardPile.removeWhere((test) {
      return test.gfx.startsWith("party/");
    });
  }

  bool hasPartyCard() {
    if (_drawPile.getList().firstWhereOrNull((test) {
              return test.gfx.startsWith("party/");
            }) !=
            null ||
        _discardPile.getList().firstWhereOrNull((test) {
              return test.gfx.startsWith("party/");
            }) !=
            null) {
      return true;
    }
    return false;
  }

  void addHailSpecial(_StateModifier s) {
    addCard(s, "special/hail", CardType.add);
  }

  void removeHailSpecial(_StateModifier s) {
    removeCard(s, "special/hail");
  }

  void revealCards(_StateModifier _, int amount) {
    _revealedCount = amount;
  }

  void setCassandraSpecial(_StateModifier _, bool on) {
    _cassandraSpecial = on;
  }

  bool hasHail() {
    if (_discardPile.getList().firstWhereOrNull(
          (item) => item.gfx == "special/hail",
        ) !=
        null) {
      return true;
    }
    if (_drawPile.getList().firstWhereOrNull(
          (item) => item.gfx == "special/hail",
        ) !=
        null) {
      return true;
    }
    return false;
  }

  void addMinusOne(_StateModifier _) {
    _addedMinusOnes++;
    _drawPile.insert(0, ModifierCard(CardType.add, "minus1"));
    _shuffleDrawPileRespectCassandra();
    _revealedCount = 0;
    if (_addedMinusOnes < 0) {
      //do not add/remove extra minus ones to removed pile
      _removedPile.removeFirstWhere((item) => item.gfx == "minus1");
    }
  }

  void removeMinusOne(_StateModifier _) {
    _shuffle();

    final card = _removeCardFromDrawPile("minus1");
    if (card != null) {
      _addedMinusOnes--;
      if (_addedMinusOnes < 0) {
        _removedPile.add(card);
      }
    }
  }

  bool hasMinus1() {
    return _drawPile.getList().firstWhereOrNull(
              (element) => element.gfx == "minus1",
            ) !=
            null ||
        _discardPile.getList().firstWhereOrNull(
              (element) => element.gfx == "minus1",
            ) !=
            null;
  }

  bool hasMinus2() {
    return !(_removedPile.getList().firstWhereOrNull(
          (element) => element.gfx == "minus2",
        ) !=
        null);
  }

  bool hasNull() {
    return _drawPile.getList().firstWhereOrNull(
              (element) => element.gfx == "nullAttack",
            ) !=
            null ||
        _discardPile.getList().firstWhereOrNull(
              (element) => element.gfx == "nullAttack",
            ) !=
            null;
  }

  void removeNull(_StateModifier _) {
    _shuffle();
    final card = _removeCardFromDrawPile("nullAttack");
    if (card != null) {
      _removedPile.add(card);
    }
  }

  void addNull(_StateModifier _) {
    _drawPile.add(ModifierCard(CardType.multiply, "nullAttack"));
    _removedPile.removeFirstWhere((item) => item.gfx == "nullAttack");
    _shuffle();
  }

  bool hasCard(String gfx) {
    final drawPileHas =
        _drawPile.getList().firstWhereOrNull((test) => test.gfx == gfx) != null;
    final discardPileHas =
        _discardPile.getList().firstWhereOrNull((test) => test.gfx == gfx) !=
        null;
    return (drawPileHas || discardPileHas);
  }

  void removeCard(_StateModifier _, String gfx) {
    _shuffle();
    _removeCardFromDrawPile(gfx);
    _shuffle();
  }

  void removeCardFromDiscard(_StateModifier _, int index) {
    final card = _discardPile.removeAt(index);
    _removedPile.add(card);
    if (card.gfx == "minus1") {
      _addedMinusOnes--;
    }
  }

  void removeCardFromDrawPile(_StateModifier _, int index) {
    final card = _drawPile.removeAt(index);
    _removedPile.add(card);
    if (card.gfx == "minus1") {
      _addedMinusOnes--;
    }
    if (_revealedCount > _drawPile.size() - 1 - index) {
      _revealedCount--;
    }
  }

  void returnCardToDiscard(_StateModifier _, int index) {
    final card = _removedPile.removeAt(index);
    _discardPile.add(card);
    if (card.gfx == "minus1") {
      _addedMinusOnes++;
    }
  }

  void returnCardToDrawPileFromRemoved(_StateModifier _, int index) {
    final card = _removedPile.removeAt(index);
    _drawPile.add(card);
    _revealedCount++;
    if (card.gfx == "minus1") {
      _addedMinusOnes++;
    }
  }

  void returnCardToDrawPile(_StateModifier _) {
    final card = _discardPile.pop();
    _drawPile.push(card);
    //todo: how to tell if revealed should change? if it is over 0?
  }

  void addCard(_StateModifier _, String id, CardType type) {
    _drawPile.add(ModifierCard(type, id));
    _shuffle();
  }

  void removeMinusTwo(_StateModifier _) {
    _shuffle();
    final card = _removeCardFromDrawPile("minus2");
    if (card != null) {
      _removedPile.add(card);
    }
  }

  void addMinusTwo(_StateModifier _) {
    _drawPile.add(ModifierCard(CardType.add, "minus2"));
    _removedPile.removeFirstWhere((item) => item.gfx == "minus2");
    _shuffle();
  }

  void setImbue1(_StateModifier _) {
    assert(name.isEmpty); //only basic deck for this feature

    _shuffle();
    _removeCardFromDrawPile("minus1");
    _removeCardFromDrawPile("minus1");
    _removeCardFromDrawPile("minus1");
    _drawPile.add(ModifierCard(CardType.add, "imbue-plus1"));
    _drawPile.add(ModifierCard(CardType.add, "imbue-plus1"));
    _drawPile.add(ModifierCard(CardType.add, "imbue-plus1"));
    _drawPile.add(ModifierCard(CardType.add, "imbue-plus2muddle"));
    _drawPile.add(ModifierCard(CardType.add, "imbue-plus0poison"));
    _shuffle();
    _imbuement = _kImbuementLevel1;
  }

  void setImbue2(_StateModifier m) {
    assert(name.isEmpty); //only basic deck for this feature

    if (_imbuement == 0) {
      setImbue1(m);
    }

    _shuffle();
    _removeCardFromDrawPile("minus2");
    _removeCardFromDrawPile("plus0");
    _removeCardFromDrawPile("plus0");

    _drawPile.add(ModifierCard(CardType.add, "imbue2-plus3"));
    _drawPile.add(ModifierCard(CardType.add, "imbue2-plus1heal1"));
    _drawPile.add(ModifierCard(CardType.add, "imbue2-plus1heal1"));
    _drawPile.add(ModifierCard(CardType.add, "imbue2-plus1curse"));
    _drawPile.add(ModifierCard(CardType.add, "imbue2-plus0wound"));
    _shuffle();
    _imbuement = _kImbuementLevel2;
  }

  void resetImbue(_StateModifier _) {
    assert(name.isEmpty); //only basic deck for this feature

    if (_imbuement != 0) {
      _shuffle();
      _drawPile.removeWhere((element) => element.gfx.startsWith("imbue"));

      _drawPile.add(ModifierCard(CardType.add, "minus1"));
      _drawPile.add(ModifierCard(CardType.add, "minus1"));
      _drawPile.add(ModifierCard(CardType.add, "minus1"));
      if (_imbuement == _kImbuementLevel2) {
        if (hasMinus2()) {
          //if minus2 has not been separately removed
          _drawPile.add(ModifierCard(CardType.add, "minus2"));
        }
        _drawPile.add(ModifierCard(CardType.add, "plus0"));
        _drawPile.add(ModifierCard(CardType.add, "plus0"));
      }
      _imbuement = 0;
      _shuffle();
    }
  }

  void shuffle(_StateModifier _) {
    if (_cassandraSpecial) {
      //dont shuffle the revealed cards
      _shuffleOnlyBelowRevealed();
    } else {
      _shuffle();
    }
  }

  void shuffleUnDrawn(_StateModifier _) {
    _drawPile.shuffle();
    _revealedCount = 0;
  }

  void draw(_StateModifier _) {
    if (_revealedCount > 0) {
      _revealedCount--;
    }
    //shuffle deck, for the case the deck ends during play
    if (_drawPile.isEmpty) {
      _shuffle();
    }
    //put top of draw pile on discard pile
    if (_drawPile.isNotEmpty) {
      ModifierCard card = _drawPile.pop();
      if (card.type == CardType.multiply) {
        _needsShuffle = true;
      }

      if (_removables[card.gfx] != null) {
        addRemovableValue(_StateModifier(), card.gfx, -1);
      }
      _discardPile.push(card);
    }
  }

  void reorderCards(_StateModifier s, int newIndex, int oldIndex) {
    List<ModifierCard> list = List.of(_drawPile.getList());
    final item = list.removeAt(oldIndex);
    list.insert(newIndex, item);
    _drawPile.setList(list);

    //if rearranging between revealed and unrevealed then un-reveal everything below the unknown moved card.
    final revertOldIndex = _drawPile.size() - oldIndex;
    final revertNewIndex = _drawPile.size() - newIndex;
    if (revertOldIndex <= revealedCount && revertNewIndex > revealedCount) {
      //moving one revealed card to unrevealed area - hide one
      revealCards(s, revealedCount - 1);
    }
    if (revertNewIndex <= revealedCount && revertOldIndex > revealedCount) {
      //hide new index card and those below
      revealCards(s, revertNewIndex - 1);
    }
  }

  Map<String, dynamic> toJson() => {
    'addedMinusOnes': _addedMinusOnes,
    'imbuement': _imbuement,
    'badOmen': _badOmen,
    'corrosiveSpew': _corrosiveSpew,
    'revealed': _revealedCount,
    'cassandra': _cassandraSpecial,
    'removables': _removables,
    'drawPile': _drawPile.getList().map((c) => c.toJson()).toList(),
    'removedPile': _removedPile.getList().map((c) => c.toJson()).toList(),
    'discardPile': _discardPile.getList().map((c) => c.toJson()).toList(),
  };

  @override
  String toString() => json.encode(toJson());

  void _initListeners() {
    // UI-only in the reference app. In the reference this attached the
    // _handleRemovableCards listener to each counter notifier, so physical
    // card sync only ever happened for the keys present here (always the ten
    // initial ones); record that set for set/addRemovableValue.
    _syncedRemovables
      ..clear()
      ..addAll(_removables.keys);
  }

  List<ModifierCard> _getCardsFromJson(
    Map<String, dynamic> modifierDeckData,
    String deckId,
  ) {
    List<ModifierCard> newList = [];
    for (final item in modifierDeckData[deckId] as List) {
      String gfx = item["gfx"];
      gfx = gfx.replaceAll("-allies", "");
      if (gfx == "curse") {
        newList.add(ModifierCard(CardType.remove, gfx));
      } else if (gfx.contains("enfeeble")) {
        if (gfx == "enfeeble") {
          //if updating from old version
          gfx = "in-enfeeble";
        }
        newList.add(ModifierCard(CardType.remove, gfx));
      } else if (gfx.contains("empower")) {
        newList.add(ModifierCard(CardType.remove, gfx));
      } else if (gfx == "bless") {
        newList.add(ModifierCard(CardType.remove, gfx));
      } else if (_isMultiplyType(gfx)) {
        newList.add(ModifierCard(CardType.multiply, gfx));
      } else {
        newList.add(ModifierCard(CardType.add, gfx));
      }
    }
    return newList;
  }

  void _initDeck() {
    List<ModifierCard> cards = [];
    cards.add(ModifierCard(CardType.add, "minus2"));
    cards.add(ModifierCard(CardType.add, "plus2"));
    cards.add(ModifierCard(CardType.multiply, "doubleAttack"));
    cards.add(ModifierCard(CardType.multiply, "nullAttack"));
    for (int i = 0; i < _kPlusMinus1Count; i++) {
      cards.add(ModifierCard(CardType.add, "minus1"));
      cards.add(ModifierCard(CardType.add, "plus1"));
    }
    for (int i = 0; i < _kPlus0Count; i++) {
      cards.add(ModifierCard(CardType.add, "plus0"));
    }
    _drawPile.setList(cards);
    _discardPile.setList([]);
    _removedPile.setList([]);
    _shuffle();
    _badOmen = 0;
    _corrosiveSpew = false;
    _addedMinusOnes = 0;
    _imbuement = 0;
    _needsShuffle = false;
    for (final item in _removables.keys) {
      _removables[item] = 0;
    }
  }

  ModifierCard? _removeCardFromDrawPile(String gfx) {
    final card = _drawPile.getList().lastWhereOrNull(
      (element) => element.gfx == gfx,
    );
    if (card != null) {
      _drawPile.remove(card);
      _drawPile.shuffle();
    }
    return card;
  }

  /// Syncs the physical removable cards in the draw pile with the
  /// [notifierValue] stored in [_removables] for [gfx]. In the reference app
  /// this ran as a listener on the removable counter notifiers; it is now
  /// called directly from [setRemovableValue] and [addRemovableValue].
  void _handleRemovableCards(int notifierValue, String gfx) {
    //count and add or remove, then shuffle
    int count = 0;
    bool shuffle = true;
    for (final item in _drawPile.getList()) {
      if (item.gfx == gfx) {
        count++;
      }
    }
    if (count == notifierValue) {
      shuffle = false;
    } else if (count < notifierValue) {
      for (int i = count; i < notifierValue; i++) {
        if (gfx == "rm-empower" && corrosiveSpew) {
          shuffle = false;
          //put on top
          //_drawPile.insert(0, ModifierCard(CardType.remove, gfx));
          _drawPile.push(ModifierCard(CardType.remove, gfx));
        } else if (gfx == "curse" && _badOmen > 0) {
          _badOmen--;
          shuffle = false;
          //put in sixth or as far down as it goes.
          int position = _kCursePosition;
          final size = _drawPile.getList().length;
          if (size < _kPlus0Count) {
            position = size;
          }
          _drawPile.insert(size - position, ModifierCard(CardType.remove, gfx));
        } else {
          _drawPile.insert(0, ModifierCard(CardType.remove, gfx));
        }
      }
    } else {
      int toRemove = count - notifierValue;
      for (int i = 0; i < toRemove; i++) {
        _drawPile.removeFirstWhere((card) => card.gfx == gfx);
      }
    }
    if (shuffle) {
      _shuffleDrawPileRespectCassandra();
    }
  }

  void _shuffle() {
    while (_discardPile.isNotEmpty) {
      ModifierCard card = _discardPile.pop();
      //remove curse and bless
      if (card.type != CardType.remove) {
        _drawPile.push(card);
      }
    }
    _drawPile.shuffle();

    _needsShuffle = false;
    _revealedCount = 0;
  }

  void _shuffleOnlyBelowRevealed() {
    final revealCount = _revealedCount;
    List<ModifierCard> revealed = [];
    for (int i = 0; i < _revealedCount; i++) {
      revealed.insert(0, _drawPile.pop());
    }
    _shuffle();
    _revealedCount = revealCount;
    for (int i = 0; i < _revealedCount; i++) {
      _drawPile.add(revealed[i]);
    }
  }

  void _shuffleDrawPileRespectCassandra() {
    if (_cassandraSpecial) {
      //dont shuffle the revealed cards
      List<ModifierCard> revealed = [];
      for (int i = 0; i < _revealedCount; i++) {
        revealed.insert(0, _drawPile.pop());
      }
      _drawPile.shuffle();
      for (int i = 0; i < _revealedCount; i++) {
        _drawPile.add(revealed[i]);
      }
    } else {
      _revealedCount = 0;
      _drawPile.shuffle();
    }
  }

  bool _isMultiplyType(String gfx) {
    if (gfx.contains("nullAttack") || gfx.contains("doubleAttack")) {
      return true;
    }
    if (gfx == "P4" && name == "Nightshroud") {
      if (GameMethods.getCharacterByName(
            "Nightshroud",
          )?.characterClass.edition ==
          "Gloomhaven 2nd Edition") {
        return true;
      }
    }
    return false;
  }
}

enum CardType { add, multiply, remove }

class ModifierCard {
  final CardType type;
  final String gfx;

  ModifierCard(this.type, this.gfx);

  Map<String, dynamic> toJson() => {'gfx': gfx};

  @override
  String toString() => '{"gfx": "$gfx" }';
}