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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
|
/// Command registry: wire name → command construction.
///
/// The Phase 2 network layer receives `{name, args}` messages from clients
/// and calls [buildCommand] to turn them into a [Command] for the
/// `ActionHandler`. [commandName] is the reverse lookup (runtime type →
/// wire name), used by describe/tests.
///
/// Conventions (PORTING.md §9):
///
/// * Wire names are the class name minus the `Command` suffix in
/// lower-camel case (acronym runs lower-cased in full): `DrawCommand` →
/// `draw`, `AddMonsterCommand` → `addMonster`, `ChangeHealthCommand` →
/// `changeHealth`, `AMDCassandraSpecialCommand` → `amdCassandraSpecial`.
/// The wire name comes from the class name, never the file name (the file
/// `remove__special_loot_card_command.dart` holds `RemoveSpecialLootCardCommand`
/// → `removeSpecialLootCard`).
/// * Args are a JSON `Map<String, dynamic>`. The wire keys are the
/// constructor parameter names — positional parameters in positional
/// order, leading underscores stripped — e.g. `AddPerkCommand(characterId,
/// index)` takes `{'characterId': ..., 'index': ...}`.
/// * Optional parameters may be omitted or given as JSON `null`.
/// * `GameState`, `Settings` and `GameData` are never wire args — they are
/// injected from the service locator (see [buildCommand]).
/// * Enums travel as name strings (`'chill'`, `'fire'`, `'elite'`, ...).
/// Complex object args are resolved against the live game state (see the
/// per-entry doc comments in [_registry]).
///
/// Any failure — unknown command, missing required arg, wrong JSON type,
/// unknown enum value, unresolvable object reference — is reported as
/// [CommandRegistryError] with a human-readable message naming the command
/// and the arg. Failures thrown by a command's own constructor (e.g.
/// `AddCharacterCommand` for an unknown character id) are wrapped the same
/// way so the network layer only ever sees [CommandRegistryError].
library;
import 'package:x_haven_server/core/commands/activate_monster_type_command.dart';
import 'package:x_haven_server/core/commands/add_character_command.dart';
import 'package:x_haven_server/core/commands/add_condition_command.dart';
import 'package:x_haven_server/core/commands/add_cs_party_card_command.dart';
import 'package:x_haven_server/core/commands/add_faction_card_command.dart';
import 'package:x_haven_server/core/commands/add_loot_card_command.dart';
import 'package:x_haven_server/core/commands/add_monster_command.dart';
import 'package:x_haven_server/core/commands/add_note_row_command.dart';
import 'package:x_haven_server/core/commands/add_perk_command.dart';
import 'package:x_haven_server/core/commands/add_special_loot_card_command.dart';
import 'package:x_haven_server/core/commands/add_standee_command.dart';
import 'package:x_haven_server/core/commands/amd_add_minus_one_command.dart';
import 'package:x_haven_server/core/commands/amd_cassandra_special_command.dart';
import 'package:x_haven_server/core/commands/amd_imbue1_command.dart';
import 'package:x_haven_server/core/commands/amd_imbue2_command.dart';
import 'package:x_haven_server/core/commands/amd_remove_imbue_command.dart';
import 'package:x_haven_server/core/commands/amd_remove_minus_1_command.dart';
import 'package:x_haven_server/core/commands/amd_remove_minus_2_command.dart';
import 'package:x_haven_server/core/commands/amd_remove_plus_0_command.dart';
import 'package:x_haven_server/core/commands/amd_reveal_command.dart';
import 'package:x_haven_server/core/commands/bad_omen_command.dart';
import 'package:x_haven_server/core/commands/change_name_command.dart';
import 'package:x_haven_server/core/commands/change_stat_commands/change_bless_command.dart';
import 'package:x_haven_server/core/commands/change_stat_commands/change_curse_command.dart';
import 'package:x_haven_server/core/commands/change_stat_commands/change_empower_command.dart';
import 'package:x_haven_server/core/commands/change_stat_commands/change_enfeeble_command.dart';
import 'package:x_haven_server/core/commands/change_stat_commands/change_health_command.dart';
import 'package:x_haven_server/core/commands/change_stat_commands/change_max_health_command.dart';
import 'package:x_haven_server/core/commands/change_stat_commands/change_xp_command.dart';
import 'package:x_haven_server/core/commands/clear_unlocked_classes_command.dart';
import 'package:x_haven_server/core/commands/corrosive_spew_command.dart';
import 'package:x_haven_server/core/commands/donate_cs_sanctuary_command.dart';
import 'package:x_haven_server/core/commands/draw_ability_card_command.dart';
import 'package:x_haven_server/core/commands/draw_command.dart';
import 'package:x_haven_server/core/commands/draw_loot_card_command.dart';
import 'package:x_haven_server/core/commands/draw_modifier_card_command.dart';
import 'package:x_haven_server/core/commands/enhance_loot_card_command.dart';
import 'package:x_haven_server/core/commands/hide_ally_deck_command.dart';
import 'package:x_haven_server/core/commands/ice_wraith_change_form_command.dart';
import 'package:x_haven_server/core/commands/imbue_element_command.dart';
import 'package:x_haven_server/core/commands/load_character_save_command.dart';
import 'package:x_haven_server/core/commands/load_save_command.dart';
import 'package:x_haven_server/core/commands/next_round_command.dart';
import 'package:x_haven_server/core/commands/remove__special_loot_card_command.dart';
import 'package:x_haven_server/core/commands/remove_amd_card_command.dart';
import 'package:x_haven_server/core/commands/remove_card_command.dart';
import 'package:x_haven_server/core/commands/remove_character_command.dart';
import 'package:x_haven_server/core/commands/remove_condition_command.dart';
import 'package:x_haven_server/core/commands/remove_cs_party_card_command.dart';
import 'package:x_haven_server/core/commands/remove_cs_sanctuary_donation_command.dart';
import 'package:x_haven_server/core/commands/remove_monster_command.dart';
import 'package:x_haven_server/core/commands/remove_note_row_command.dart';
import 'package:x_haven_server/core/commands/reorder_ability_list_command.dart';
import 'package:x_haven_server/core/commands/reorder_list_command.dart';
import 'package:x_haven_server/core/commands/reorder_modifier_list_command.dart';
import 'package:x_haven_server/core/commands/return_loot_card_command.dart';
import 'package:x_haven_server/core/commands/return_modifier_card_command.dart';
import 'package:x_haven_server/core/commands/return_removed_amd_card_command.dart';
import 'package:x_haven_server/core/commands/set_ally_deck_in_og_gloom_command.dart';
import 'package:x_haven_server/core/commands/set_as_summon_command.dart';
import 'package:x_haven_server/core/commands/set_auto_level_adjust_command.dart';
import 'package:x_haven_server/core/commands/set_campaign_command.dart';
import 'package:x_haven_server/core/commands/set_character_level_command.dart';
import 'package:x_haven_server/core/commands/set_difficulty_command.dart';
import 'package:x_haven_server/core/commands/set_figure_note_command.dart';
import 'package:x_haven_server/core/commands/set_init_command.dart';
import 'package:x_haven_server/core/commands/set_level_command.dart';
import 'package:x_haven_server/core/commands/set_loot_owner_command.dart';
import 'package:x_haven_server/core/commands/set_note_row_color_command.dart';
import 'package:x_haven_server/core/commands/set_note_row_link_command.dart';
import 'package:x_haven_server/core/commands/set_note_row_text_command.dart';
import 'package:x_haven_server/core/commands/set_scenario_command.dart';
import 'package:x_haven_server/core/commands/set_solo_command.dart';
import 'package:x_haven_server/core/commands/show_ally_deck_command.dart';
import 'package:x_haven_server/core/commands/shuffle_ability_card_command.dart';
import 'package:x_haven_server/core/commands/shuffle_amd_card_command.dart';
import 'package:x_haven_server/core/commands/shuffle_drawn_ability_card_command.dart';
import 'package:x_haven_server/core/commands/track_standees_command.dart';
import 'package:x_haven_server/core/commands/turn_done_command.dart';
import 'package:x_haven_server/core/commands/unlock_special_command.dart';
import 'package:x_haven_server/core/commands/use_element_command.dart';
import 'package:x_haven_server/core/commands/use_fh_perks_command.dart';
import 'package:x_haven_server/core/enums.dart';
import 'package:x_haven_server/core/game_data.dart';
import 'package:x_haven_server/core/models/monster_ability.dart';
import 'package:x_haven_server/core/service_locator.dart';
import 'package:x_haven_server/core/settings.dart';
import 'package:x_haven_server/core/state/game_state.dart';
/// One entry: wire name, arg parsing/validation, and command construction.
class CommandDescriptor {
const CommandDescriptor(this.name, this.construct);
final String name; // e.g. 'draw', 'addMonster', 'changeHealth'
final Command Function(GameState gameState, Map<String, dynamic> args)
construct;
}
/// Thrown for unknown command names or invalid/missing args.
class CommandRegistryError implements Exception {
CommandRegistryError(this.message);
final String message;
@override
String toString() => 'CommandRegistryError: $message';
}
/// Builds a [Command] from a wire name + JSON arg map.
///
/// Throws [CommandRegistryError] with a human-readable message on unknown
/// names, invalid/missing args, or command-construction failure.
Command buildCommand(
String name,
Map<String, dynamic> args,
GameState gameState,
) {
final descriptor = _registry[name];
if (descriptor == null) {
throw CommandRegistryError(
'Unknown command "$name". '
'Known commands: ${_registry.keys.join(', ')}',
);
}
try {
return descriptor.construct(gameState, args);
} on StateError catch (error, stackTrace) {
Error.throwWithStackTrace(
CommandRegistryError('Command "$name": ${error.message}'),
stackTrace,
);
} on ArgumentError catch (error, stackTrace) {
Error.throwWithStackTrace(
CommandRegistryError(
'Command "$name": ${error.message ?? 'invalid arguments'}',
),
stackTrace,
);
}
}
/// Reverse lookup (for describe/tests): wire name of [command]'s runtime
/// type.
///
/// Throws [CommandRegistryError] if [command]'s type is not registered.
String commandName(Command command) {
final name = _nameByType[command.runtimeType];
if (name == null) {
throw CommandRegistryError(
'No wire name registered for ${command.runtimeType}',
);
}
return name;
}
/// All registered commands, wire name → [CommandDescriptor].
final Map<String, CommandDescriptor> _registry = {
'activateMonsterType': CommandDescriptor(
'activateMonsterType',
(gs, args) => ActivateMonsterTypeCommand(
_str('activateMonsterType', 'name', args),
_bool('activateMonsterType', 'activate', args),
gameState: gs,
),
),
'addCharacter': CommandDescriptor(
'addCharacter',
(gs, args) => AddCharacterCommand(
_str('addCharacter', 'id', args),
_str('addCharacter', 'edition', args),
_strOr('addCharacter', 'display', args),
_int('addCharacter', 'level', args),
),
),
'addCondition': CommandDescriptor(
'addCondition',
(gs, args) => AddConditionCommand(
_condition('addCondition', 'condition', args),
_str('addCondition', 'figureId', args),
_strOr('addCondition', 'ownerId', args),
gameState: gs,
),
),
'addCSPartyCard': CommandDescriptor(
'addCSPartyCard',
(gs, args) => AddCSPartyCardCommand(
_str('addCSPartyCard', 'characterId', args),
_int('addCSPartyCard', 'type', args),
gameState: gs,
),
),
'addFactionCard': CommandDescriptor(
'addFactionCard',
(gs, args) => AddFactionCardCommand(
_str('addFactionCard', 'characterId', args),
_str('addFactionCard', 'cardId', args),
_bool('addFactionCard', 'add', args),
gameState: gs,
),
),
'addLootCard': CommandDescriptor(
'addLootCard',
(gs, args) => AddLootCardCommand(
_str('addLootCard', 'resourceType', args),
gameState: gs,
),
),
'addMonster': CommandDescriptor(
'addMonster',
(gs, args) => AddMonsterCommand(
_str('addMonster', 'name', args),
_intOr('addMonster', 'level', args),
_bool('addMonster', 'isAlly', args),
gameState: gs,
),
),
// `noteRow` is a JSON object `{id, text, color, linkedId, standeeNr}`;
// `id` is a client-generated unique string.
'addNoteRow': CommandDescriptor(
'addNoteRow',
(gs, args) =>
AddNoteRowCommand(_noteRow('addNoteRow', args), gameState: gs),
),
'addPerk': CommandDescriptor(
'addPerk',
(gs, args) => AddPerkCommand(
_str('addPerk', 'characterId', args),
_int('addPerk', 'index', args),
),
),
'addSpecialLootCard': CommandDescriptor(
'addSpecialLootCard',
(gs, args) => AddSpecialLootCardCommand(
_int('addSpecialLootCard', 'nr', args),
gameState: gs,
),
),
// `summon` is an optional JSON object
// `{standeeNr, name, health, move, attack, range, gfx}`; absent/null → null.
'addStandee': CommandDescriptor(
'addStandee',
(gs, args) => AddStandeeCommand(
_int('addStandee', 'nr', args),
_summonData('addStandee', args),
_str('addStandee', 'ownerId', args),
_monsterType('addStandee', 'type', args),
_bool('addStandee', 'addAsSummon', args),
gameState: gs,
),
),
'amdAddMinusOne': CommandDescriptor(
'amdAddMinusOne',
(gs, args) => AmdAddMinusOneCommand(
_str('amdAddMinusOne', 'name', args),
gameState: gs,
),
),
'amdCassandraSpecial': CommandDescriptor(
'amdCassandraSpecial',
(gs, args) => AMDCassandraSpecialCommand(
_str('amdCassandraSpecial', 'deckId', args),
_bool('amdCassandraSpecial', 'on', args),
gameState: gs,
),
),
'amdImbue1': CommandDescriptor(
'amdImbue1',
(gs, args) => AMDImbue1Command(gameState: gs),
),
'amdImbue2': CommandDescriptor(
'amdImbue2',
(gs, args) => AMDImbue2Command(gameState: gs),
),
'amdRemoveImbue': CommandDescriptor(
'amdRemoveImbue',
(gs, args) => AMDRemoveImbueCommand(gameState: gs),
),
'amdRemoveMinus1': CommandDescriptor(
'amdRemoveMinus1',
(gs, args) => AMDRemoveMinus1Command(
_str('amdRemoveMinus1', 'name', args),
gameState: gs,
),
),
'amdRemoveMinus2': CommandDescriptor(
'amdRemoveMinus2',
(gs, args) => AMDRemoveMinus2Command(
_bool('amdRemoveMinus2', 'allies', args),
gameState: gs,
),
),
'amdRemovePlus0': CommandDescriptor(
'amdRemovePlus0',
(gs, args) => AmdRemovePlus0Command(
_str('amdRemovePlus0', 'name', args),
_bool('amdRemovePlus0', 'remove', args),
gameState: gs,
),
),
'amdReveal': CommandDescriptor(
'amdReveal',
(gs, args) => AMDRevealCommand(
amount: _int('amdReveal', 'amount', args),
name: _str('amdReveal', 'name', args),
gameState: gs,
),
),
'badOmen': CommandDescriptor(
'badOmen',
(gs, args) =>
BadOmenCommand(_bool('badOmen', 'allies', args), gameState: gs),
),
'changeBless': CommandDescriptor(
'changeBless',
(gs, args) => ChangeBlessCommand(
_int('changeBless', 'change', args),
_str('changeBless', 'figureId', args),
_strOr('changeBless', 'ownerId', args),
gameState: gs,
),
),
'changeCurse': CommandDescriptor(
'changeCurse',
(gs, args) => ChangeCurseCommand(
_int('changeCurse', 'change', args),
_str('changeCurse', 'figureId', args),
_strOr('changeCurse', 'ownerId', args),
gameState: gs,
),
),
'changeEmpower': CommandDescriptor(
'changeEmpower',
(gs, args) => ChangeEmpowerCommand(
_int('changeEmpower', 'change', args),
_str('changeEmpower', 'gfx', args),
_str('changeEmpower', 'figureId', args),
_strOr('changeEmpower', 'ownerId', args),
gameState: gs,
),
),
'changeEnfeeble': CommandDescriptor(
'changeEnfeeble',
(gs, args) => ChangeEnfeebleCommand(
_int('changeEnfeeble', 'change', args),
_str('changeEnfeeble', 'gfx', args),
_str('changeEnfeeble', 'figureId', args),
_strOr('changeEnfeeble', 'ownerId', args),
gameState: gs,
),
),
'changeHealth': CommandDescriptor(
'changeHealth',
(gs, args) => ChangeHealthCommand(
_int('changeHealth', 'change', args),
_str('changeHealth', 'figureId', args),
_strOr('changeHealth', 'ownerId', args),
gameState: gs,
),
),
'changeMaxHealth': CommandDescriptor(
'changeMaxHealth',
(gs, args) => ChangeMaxHealthCommand(
_int('changeMaxHealth', 'change', args),
_str('changeMaxHealth', 'figureId', args),
_strOr('changeMaxHealth', 'ownerId', args),
gameState: gs,
),
),
'changeName': CommandDescriptor(
'changeName',
(gs, args) => ChangeNameCommand(
_str('changeName', 'name', args),
_str('changeName', 'characterId', args),
gameState: gs,
),
),
'changeXP': CommandDescriptor(
'changeXP',
(gs, args) => ChangeXPCommand(
_int('changeXP', 'change', args),
_str('changeXP', 'figureId', args),
_strOr('changeXP', 'ownerId', args),
gameState: gs,
),
),
'clearUnlockedClasses': CommandDescriptor(
'clearUnlockedClasses',
(gs, args) => ClearUnlockedClassesCommand(),
),
'corrosiveSpew': CommandDescriptor(
'corrosiveSpew',
(gs, args) => CorrosiveSpewCommand(gameState: gs),
),
'donateCSSanctuary': CommandDescriptor(
'donateCSSanctuary',
(gs, args) => DonateCSSanctuaryCommand(
_str('donateCSSanctuary', 'characterId', args),
gameState: gs,
),
),
'draw': CommandDescriptor('draw', (gs, args) => DrawCommand(gameState: gs)),
'drawAbilityCard': CommandDescriptor(
'drawAbilityCard',
(gs, args) =>
DrawAbilityCardCommand(_str('drawAbilityCard', 'ownerId', args)),
),
'drawLootCard': CommandDescriptor(
'drawLootCard',
(gs, args) => DrawLootCardCommand(gameState: gs),
),
'drawModifierCard': CommandDescriptor(
'drawModifierCard',
(gs, args) => DrawModifierCardCommand(
_str('drawModifierCard', 'name', args),
gameState: gs,
),
),
'enhanceLootCard': CommandDescriptor(
'enhanceLootCard',
(gs, args) => EnhanceLootCardCommand(
_int('enhanceLootCard', 'id', args),
_int('enhanceLootCard', 'value', args),
_str('enhanceLootCard', 'resourceType', args),
gameState: gs,
),
),
'hideAllyDeck': CommandDescriptor(
'hideAllyDeck',
(gs, args) => HideAllyDeckCommand(),
),
'iceWraithChangeForm': CommandDescriptor(
'iceWraithChangeForm',
(gs, args) => IceWraithChangeFormCommand(
_bool('iceWraithChangeForm', 'isElite', args),
_strOr('iceWraithChangeForm', 'ownerId', args),
_str('iceWraithChangeForm', 'figureId', args),
gameState: gs,
),
),
'imbueElement': CommandDescriptor(
'imbueElement',
(gs, args) => ImbueElementCommand(
_elements('imbueElement', 'element', args),
_bool('imbueElement', 'half', args),
gameState: gs,
),
),
'loadCharacterSave': CommandDescriptor(
'loadCharacterSave',
(gs, args) => LoadCharacterSaveCommand(
_str('loadCharacterSave', 'saveName', args),
_str('loadCharacterSave', 'saveData', args),
gameState: gs,
),
),
'loadSave': CommandDescriptor(
'loadSave',
(gs, args) => LoadSaveCommand(
_str('loadSave', 'saveName', args),
_str('loadSave', 'saveData', args),
gameState: gs,
),
),
// GameData and Settings are injected, never wire args.
'nextRound': CommandDescriptor(
'nextRound',
(gs, args) => NextRoundCommand(
gameState: gs,
gameData: getIt<GameData>(),
settings: getIt<Settings>(),
),
),
'removeAMDCard': CommandDescriptor(
'removeAMDCard',
(gs, args) => RemoveAMDCardCommand(
index: _int('removeAMDCard', 'index', args),
name: _str('removeAMDCard', 'name', args),
fromDrawPile: _boolOr('removeAMDCard', 'fromDrawPile', args, false),
gameState: gs,
),
),
// `deck` + `nr` resolve the live card model from the current ability decks
// (draw pile, then discard pile), mirroring the command's own execute().
'removeCard': CommandDescriptor(
'removeCard',
(gs, args) => RemoveCardCommand(
_abilityCard(
gs,
'removeCard',
_str('removeCard', 'deck', args),
_int('removeCard', 'nr', args),
),
gameState: gs,
),
),
// The reference constructor takes `List<Character> names`; the wire takes
// `ids` (List<String>) and resolves the live Character objects by id. An
// empty result is not an error (the reference tolerates no-ops).
'removeCharacter': CommandDescriptor(
'removeCharacter',
(gs, args) => RemoveCharacterCommand(
_characters(gs, _strList('removeCharacter', 'ids', args)),
gameState: gs,
),
),
'removeCondition': CommandDescriptor(
'removeCondition',
(gs, args) => RemoveConditionCommand(
_condition('removeCondition', 'condition', args),
_str('removeCondition', 'figureId', args),
_strOr('removeCondition', 'ownerId', args),
gameState: gs,
),
),
'removeCSPartyCard': CommandDescriptor(
'removeCSPartyCard',
(gs, args) => RemoveCSPartyCardCommand(
_str('removeCSPartyCard', 'characterId', args),
gameState: gs,
),
),
'removeCSSanctuaryDonation': CommandDescriptor(
'removeCSSanctuaryDonation',
(gs, args) => RemoveCSSanctuaryDonationCommand(
_str('removeCSSanctuaryDonation', 'characterId', args),
gameState: gs,
),
),
// The reference constructor takes `List<Monster> names`; the wire takes
// `ids` (List<String>) and resolves the live Monster objects by id. An
// empty result is not an error (the reference tolerates no-ops).
'removeMonster': CommandDescriptor(
'removeMonster',
(gs, args) => RemoveMonsterCommand(
_monsters(gs, _strList('removeMonster', 'ids', args)),
gameState: gs,
),
),
'removeNoteRow': CommandDescriptor(
'removeNoteRow',
(gs, args) => RemoveNoteRowCommand(
_str('removeNoteRow', 'noteId', args),
gameState: gs,
),
),
'removeSpecialLootCard': CommandDescriptor(
'removeSpecialLootCard',
(gs, args) => RemoveSpecialLootCardCommand(
_int('removeSpecialLootCard', 'nr', args),
gameState: gs,
),
),
'reorderAbilityList': CommandDescriptor(
'reorderAbilityList',
(gs, args) => ReorderAbilityListCommand(
_str('reorderAbilityList', 'deck', args),
_int('reorderAbilityList', 'newIndex', args),
_int('reorderAbilityList', 'oldIndex', args),
gameState: gs,
),
),
'reorderList': CommandDescriptor(
'reorderList',
(gs, args) => ReorderListCommand(
_int('reorderList', 'newIndex', args),
_int('reorderList', 'oldIndex', args),
gameState: gs,
),
),
'reorderModifierList': CommandDescriptor(
'reorderModifierList',
(gs, args) => ReorderModifierListCommand(
_int('reorderModifierList', 'newIndex', args),
_int('reorderModifierList', 'oldIndex', args),
_str('reorderModifierList', 'name', args),
gameState: gs,
),
),
'returnLootCard': CommandDescriptor(
'returnLootCard',
(gs, args) => ReturnLootCardCommand(
_bool('returnLootCard', 'top', args),
gameState: gs,
),
),
'returnModifierCard': CommandDescriptor(
'returnModifierCard',
(gs, args) =>
ReturnModifierCardCommand(_str('returnModifierCard', 'name', args)),
),
'returnRemovedAMDCard': CommandDescriptor(
'returnRemovedAMDCard',
(gs, args) => ReturnRemovedAMDCardCommand(
index: _int('returnRemovedAMDCard', 'index', args),
name: _str('returnRemovedAMDCard', 'name', args),
toDrawPile: _boolOr('returnRemovedAMDCard', 'toDrawPile', args, false),
gameState: gs,
),
),
'setAllyDeckInOgGloom': CommandDescriptor(
'setAllyDeckInOgGloom',
(gs, args) => SetAllyDeckInOgGloomCommand(
_bool('setAllyDeckInOgGloom', 'set', args),
gameState: gs,
),
),
'setAsSummon': CommandDescriptor(
'setAsSummon',
(gs, args) => SetAsSummonCommand(
_bool('setAsSummon', 'summoned', args),
_str('setAsSummon', 'figureId', args),
_strOr('setAsSummon', 'ownerId', args),
gameState: gs,
),
),
'setAutoLevelAdjust': CommandDescriptor(
'setAutoLevelAdjust',
(gs, args) => SetAutoLevelAdjustCommand(
_bool('setAutoLevelAdjust', 'on', args),
gameState: gs,
),
),
'setCampaign': CommandDescriptor(
'setCampaign',
(gs, args) => SetCampaignCommand(_str('setCampaign', 'campaign', args)),
),
'setCharacterLevel': CommandDescriptor(
'setCharacterLevel',
(gs, args) => SetCharacterLevelCommand(
_int('setCharacterLevel', 'level', args),
_str('setCharacterLevel', 'characterId', args),
),
),
'setDifficulty': CommandDescriptor(
'setDifficulty',
(gs, args) => SetDifficultyCommand(
_int('setDifficulty', 'difficulty', args),
gameState: gs,
),
),
'setFigureNote': CommandDescriptor(
'setFigureNote',
(gs, args) => SetFigureNoteCommand(
_str('setFigureNote', 'note', args),
_str('setFigureNote', 'figureId', args),
gameState: gs,
),
),
'setInit': CommandDescriptor(
'setInit',
(gs, args) => SetInitCommand(
_str('setInit', 'characterId', args),
_int('setInit', 'init', args),
gameState: gs,
),
),
'setLevel': CommandDescriptor(
'setLevel',
(gs, args) => SetLevelCommand(
_int('setLevel', 'level', args),
_strOr('setLevel', 'monsterId', args),
),
),
// `cardId` resolves the live [LootCard] from the loot deck (draw pile,
// discard pile, and the material/herb pools).
'setLootOwner': CommandDescriptor(
'setLootOwner',
(gs, args) => SetLootOwnerCommand(
_str('setLootOwner', 'ownerId', args),
_lootCard(gs, 'setLootOwner', _int('setLootOwner', 'cardId', args)),
),
),
'setNoteRowColor': CommandDescriptor(
'setNoteRowColor',
(gs, args) => SetNoteRowColorCommand(
_int('setNoteRowColor', 'color', args),
_str('setNoteRowColor', 'noteId', args),
gameState: gs,
),
),
'setNoteRowLink': CommandDescriptor(
'setNoteRowLink',
(gs, args) => SetNoteRowLinkCommand(
_str('setNoteRowLink', 'noteId', args),
_str('setNoteRowLink', 'linkedId', args),
_int('setNoteRowLink', 'standeeNr', args),
gameState: gs,
),
),
'setNoteRowText': CommandDescriptor(
'setNoteRowText',
(gs, args) => SetNoteRowTextCommand(
_str('setNoteRowText', 'text', args),
_str('setNoteRowText', 'noteId', args),
gameState: gs,
),
),
'setScenario': CommandDescriptor(
'setScenario',
(gs, args) => SetScenarioCommand(
_str('setScenario', 'scenario', args),
_bool('setScenario', 'section', args),
gameState: gs,
),
),
'setSolo': CommandDescriptor(
'setSolo',
(gs, args) => SetSoloCommand(_bool('setSolo', 'solo', args)),
),
'showAllyDeck': CommandDescriptor(
'showAllyDeck',
(gs, args) => ShowAllyDeckCommand(),
),
'shuffleAbilityCard': CommandDescriptor(
'shuffleAbilityCard',
(gs, args) =>
ShuffleAbilityCardCommand(_str('shuffleAbilityCard', 'ownerId', args)),
),
'shuffleAMDCard': CommandDescriptor(
'shuffleAMDCard',
(gs, args) => ShuffleAMDCardCommand(
_str('shuffleAMDCard', 'name', args),
gameState: gs,
),
),
'shuffleDrawnAbilityCard': CommandDescriptor(
'shuffleDrawnAbilityCard',
(gs, args) => ShuffleDrawnAbilityCardCommand(
_str('shuffleDrawnAbilityCard', 'deck', args),
),
),
// Settings is injected, never a wire arg.
'trackStandees': CommandDescriptor(
'trackStandees',
(gs, args) => TrackStandeesCommand(
_bool('trackStandees', 'track', args),
gameState: gs,
settings: getIt<Settings>(),
),
),
'turnDone': CommandDescriptor(
'turnDone',
(gs, args) => TurnDoneCommand(_str('turnDone', 'id', args), gameState: gs),
),
'unlockSpecial': CommandDescriptor(
'unlockSpecial',
(gs, args) =>
UnlockSpecialCommand(_str('unlockSpecial', 'id', args), gameState: gs),
),
'useElement': CommandDescriptor(
'useElement',
(gs, args) => UseElementCommand(
_elements('useElement', 'element', args),
gameState: gs,
),
),
'useFHPerks': CommandDescriptor(
'useFHPerks',
(gs, args) => UseFHPerksCommand(_str('useFHPerks', 'characterId', args)),
),
};
/// Wire name of each registered command's runtime type, for [commandName].
final Map<Type, String> _nameByType = {
ActivateMonsterTypeCommand: 'activateMonsterType',
AddCharacterCommand: 'addCharacter',
AddConditionCommand: 'addCondition',
AddCSPartyCardCommand: 'addCSPartyCard',
AddFactionCardCommand: 'addFactionCard',
AddLootCardCommand: 'addLootCard',
AddMonsterCommand: 'addMonster',
AddNoteRowCommand: 'addNoteRow',
AddPerkCommand: 'addPerk',
AddSpecialLootCardCommand: 'addSpecialLootCard',
AddStandeeCommand: 'addStandee',
AmdAddMinusOneCommand: 'amdAddMinusOne',
AMDCassandraSpecialCommand: 'amdCassandraSpecial',
AMDImbue1Command: 'amdImbue1',
AMDImbue2Command: 'amdImbue2',
AMDRemoveImbueCommand: 'amdRemoveImbue',
AMDRemoveMinus1Command: 'amdRemoveMinus1',
AMDRemoveMinus2Command: 'amdRemoveMinus2',
AmdRemovePlus0Command: 'amdRemovePlus0',
AMDRevealCommand: 'amdReveal',
BadOmenCommand: 'badOmen',
ChangeBlessCommand: 'changeBless',
ChangeCurseCommand: 'changeCurse',
ChangeEmpowerCommand: 'changeEmpower',
ChangeEnfeebleCommand: 'changeEnfeeble',
ChangeHealthCommand: 'changeHealth',
ChangeMaxHealthCommand: 'changeMaxHealth',
ChangeNameCommand: 'changeName',
ChangeXPCommand: 'changeXP',
ClearUnlockedClassesCommand: 'clearUnlockedClasses',
CorrosiveSpewCommand: 'corrosiveSpew',
DonateCSSanctuaryCommand: 'donateCSSanctuary',
DrawAbilityCardCommand: 'drawAbilityCard',
DrawCommand: 'draw',
DrawLootCardCommand: 'drawLootCard',
DrawModifierCardCommand: 'drawModifierCard',
EnhanceLootCardCommand: 'enhanceLootCard',
HideAllyDeckCommand: 'hideAllyDeck',
IceWraithChangeFormCommand: 'iceWraithChangeForm',
ImbueElementCommand: 'imbueElement',
LoadCharacterSaveCommand: 'loadCharacterSave',
LoadSaveCommand: 'loadSave',
NextRoundCommand: 'nextRound',
RemoveAMDCardCommand: 'removeAMDCard',
RemoveCardCommand: 'removeCard',
RemoveCharacterCommand: 'removeCharacter',
RemoveConditionCommand: 'removeCondition',
RemoveCSPartyCardCommand: 'removeCSPartyCard',
RemoveCSSanctuaryDonationCommand: 'removeCSSanctuaryDonation',
RemoveMonsterCommand: 'removeMonster',
RemoveNoteRowCommand: 'removeNoteRow',
RemoveSpecialLootCardCommand: 'removeSpecialLootCard',
ReorderAbilityListCommand: 'reorderAbilityList',
ReorderListCommand: 'reorderList',
ReorderModifierListCommand: 'reorderModifierList',
ReturnLootCardCommand: 'returnLootCard',
ReturnModifierCardCommand: 'returnModifierCard',
ReturnRemovedAMDCardCommand: 'returnRemovedAMDCard',
SetAllyDeckInOgGloomCommand: 'setAllyDeckInOgGloom',
SetAsSummonCommand: 'setAsSummon',
SetAutoLevelAdjustCommand: 'setAutoLevelAdjust',
SetCampaignCommand: 'setCampaign',
SetCharacterLevelCommand: 'setCharacterLevel',
SetDifficultyCommand: 'setDifficulty',
SetFigureNoteCommand: 'setFigureNote',
SetInitCommand: 'setInit',
SetLevelCommand: 'setLevel',
SetLootOwnerCommand: 'setLootOwner',
SetNoteRowColorCommand: 'setNoteRowColor',
SetNoteRowLinkCommand: 'setNoteRowLink',
SetNoteRowTextCommand: 'setNoteRowText',
SetScenarioCommand: 'setScenario',
SetSoloCommand: 'setSolo',
ShowAllyDeckCommand: 'showAllyDeck',
ShuffleAbilityCardCommand: 'shuffleAbilityCard',
ShuffleAMDCardCommand: 'shuffleAMDCard',
ShuffleDrawnAbilityCardCommand: 'shuffleDrawnAbilityCard',
TrackStandeesCommand: 'trackStandees',
TurnDoneCommand: 'turnDone',
UnlockSpecialCommand: 'unlockSpecial',
UseElementCommand: 'useElement',
UseFHPerksCommand: 'useFHPerks',
};
// --- arg helpers -----------------------------------------------------------
//
// Every helper throws [CommandRegistryError] with a message naming the
// command and the offending arg. An arg that is missing or JSON `null` is
// treated as "not provided" for optional parameters.
Object _requiredArg(String command, String key, Map<String, dynamic> args) {
if (!args.containsKey(key)) {
throw CommandRegistryError(
'Command "$command" is missing required argument "$key"',
);
}
return args[key]!;
}
Object? _optionalArg(String command, String key, Map<String, dynamic> args) {
if (!args.containsKey(key)) return null;
return args[key];
}
Never _typeError(String command, String key, String expected, Object actual) {
throw CommandRegistryError(
'Command "$command" argument "$key" must be $expected, '
'got ${actual.runtimeType}',
);
}
String _str(String command, String key, Map<String, dynamic> args) {
final value = _requiredArg(command, key, args);
if (value is! String) _typeError(command, key, 'a String', value);
return value;
}
String? _strOr(String command, String key, Map<String, dynamic> args) {
final value = _optionalArg(command, key, args);
if (value == null) return null;
if (value is! String) _typeError(command, key, 'a String', value);
return value;
}
int _int(String command, String key, Map<String, dynamic> args) {
final value = _requiredArg(command, key, args);
if (value is! int) _typeError(command, key, 'an int', value);
return value;
}
int? _intOr(String command, String key, Map<String, dynamic> args) {
final value = _optionalArg(command, key, args);
if (value == null) return null;
if (value is! int) _typeError(command, key, 'an int', value);
return value;
}
bool _bool(String command, String key, Map<String, dynamic> args) {
final value = _requiredArg(command, key, args);
if (value is! bool) _typeError(command, key, 'a bool', value);
return value;
}
bool _boolOr(
String command,
String key,
Map<String, dynamic> args,
bool defaultValue,
) {
final value = _optionalArg(command, key, args);
if (value == null) return defaultValue;
if (value is! bool) _typeError(command, key, 'a bool', value);
return value;
}
List<String> _strList(String command, String key, Map<String, dynamic> args) {
final value = _requiredArg(command, key, args);
if (value is! List) _typeError(command, key, 'a List', value);
final result = <String>[];
int i = 0;
for (final item in value) {
if (item is! String) _typeError(command, '$key[$i]', 'a String', item);
result.add(item);
i++;
}
return result;
}
Map<String, dynamic> _map(
String command,
String key,
Map<String, dynamic> args,
) {
final value = _requiredArg(command, key, args);
if (value is! Map) _typeError(command, key, 'a JSON object', value);
return Map<String, dynamic>.from(value);
}
Map<String, dynamic>? _mapOr(
String command,
String key,
Map<String, dynamic> args,
) {
final value = _optionalArg(command, key, args);
if (value == null) return null;
if (value is! Map) _typeError(command, key, 'a JSON object', value);
return Map<String, dynamic>.from(value);
}
/// Resolves an enum wire arg (a name string) to a value of [values].
T _enumValue<T extends Enum>(
String command,
String key,
Map<String, dynamic> args,
List<T> values,
) {
final name = _str(command, key, args);
for (final value in values) {
if (value.name == name) return value;
}
throw CommandRegistryError(
'Command "$command" argument "$key" has invalid value "$name"; '
'expected one of: ${values.map((e) => e.name).join(', ')}',
);
}
Condition _condition(String command, String key, Map<String, dynamic> args) =>
_enumValue(command, key, args, Condition.values);
Elements _elements(String command, String key, Map<String, dynamic> args) =>
_enumValue(command, key, args, Elements.values);
MonsterType _monsterType(
String command,
String key,
Map<String, dynamic> args,
) => _enumValue(command, key, args, MonsterType.values);
/// Resolves the optional `summon` arg into a [SummonData].
SummonData? _summonData(String command, Map<String, dynamic> args) {
final value = _mapOr(command, 'summon', args);
if (value == null) return null;
return SummonData(
_int(command, 'summon.standeeNr', value),
_str(command, 'summon.name', value),
_int(command, 'summon.health', value),
_int(command, 'summon.move', value),
_int(command, 'summon.attack', value),
_int(command, 'summon.range', value),
_str(command, 'summon.gfx', value),
);
}
/// Resolves the `noteRow` arg into a [NoteRow]. All five keys
/// `{id, text, color, linkedId, standeeNr}` are required; `id` is a
/// client-generated unique string.
NoteRow _noteRow(String command, Map<String, dynamic> args) {
final value = _map(command, 'noteRow', args);
_str(command, 'noteRow.id', value);
_str(command, 'noteRow.text', value);
_int(command, 'noteRow.color', value);
_str(command, 'noteRow.linkedId', value);
_int(command, 'noteRow.standeeNr', value);
return NoteRow.fromJson(value);
}
/// Finds the live [LootCard] with [cardId] in the loot deck: the draw pile,
/// the discard pile, and the material/herb pools (pool cards are the same
/// instances that enter the deck, so this covers every live card).
LootCard _lootCard(GameState gameState, String command, int cardId) {
final deck = gameState.lootDeck;
final all = <LootCard>[
...deck.drawPileContents,
...deck.discardPileContents,
...deck.coinPool,
...deck.lumberPool,
...deck.hidePool,
...deck.metalPool,
...deck.corpsecapPool,
...deck.arrowvinePool,
...deck.flamefruitPool,
...deck.axenutPool,
...deck.rockrootPool,
...deck.snowthistlePool,
];
for (final card in all) {
if (card.id == cardId) return card;
}
throw CommandRegistryError(
'Command "$command": no loot card with id $cardId in the loot deck',
);
}
/// Finds the live ability card with [nr] in the ability deck named
/// [deckName] (draw pile, then discard pile) — the same lookup the command's
/// own execute() does.
MonsterAbilityCardModel _abilityCard(
GameState gameState,
String command,
String deckName,
int nr,
) {
for (final deck in gameState.currentAbilityDecks) {
if (deck.name != deckName) continue;
for (final card in deck.drawPileContents) {
if (card.nr == nr) return card;
}
for (final card in deck.discardPileContents) {
if (card.nr == nr) return card;
}
}
throw CommandRegistryError(
'Command "$command": no ability card with nr $nr in deck "$deckName"',
);
}
/// Resolves wire `ids` (character ids) to the live [Character] objects in the
/// current list. An empty result is not an error.
List<Character> _characters(GameState gameState, List<String> ids) {
return gameState.currentList
.where((item) => item is Character && ids.contains(item.id))
.cast<Character>()
.toList();
}
/// Resolves wire `ids` (monster ids) to the live [Monster] objects in the
/// current list. An empty result is not an error.
List<Monster> _monsters(GameState gameState, List<String> ids) {
return gameState.currentList
.where((item) => item is Monster && ids.contains(item.id))
.cast<Monster>()
.toList();
}
|