Repository navigation
Expand file tree
/
Copy pathMcpMod.Formatting.cs
More file actions
1116 lines (987 loc) · 49.9 KB
/
Copy pathMcpMod.Formatting.cs
File metadata and controls
1116 lines (987 loc) · 49.9 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace STS2_MCP;
public static partial class McpMod
{
private static string FormatAsMarkdown(Dictionary<string, object?> state)
{
var sb = new StringBuilder();
string stateType = state.TryGetValue("state_type", out var st) ? st?.ToString() ?? "unknown" : "unknown";
bool isMultiplayer = state.TryGetValue("game_mode", out var gm) && gm?.ToString() == "multiplayer";
if (isMultiplayer)
sb.AppendLine($"# Multiplayer Game State: {stateType}");
else
sb.AppendLine($"# Game State: {stateType}");
sb.AppendLine();
if (state.TryGetValue("run", out var runObj) && runObj is Dictionary<string, object?> run)
{
sb.AppendLine($"**Act {run["act"]}** | Floor {run["floor"]} | Ascension {run["ascension"]}");
sb.AppendLine();
}
if (stateType == "menu")
{
FormatMenuMarkdown(sb, state);
return sb.ToString();
}
if (stateType == "game_over")
{
FormatGameOverMarkdown(sb, state);
return sb.ToString();
}
if (state.TryGetValue("message", out var msg) && msg != null)
{
sb.AppendLine(msg.ToString());
return sb.ToString();
}
// Multiplayer players summary (top-level)
if (isMultiplayer && state.TryGetValue("players", out var playersListObj)
&& playersListObj is List<Dictionary<string, object?>> playersList && playersList.Count > 0)
{
sb.AppendLine("## Party");
foreach (var p in playersList)
{
string youTag = p["is_local"] is true ? " **(YOU)**" : "";
string aliveTag = p["is_alive"] is false ? " [DEAD]" : "";
string readyTag = p.TryGetValue("is_ready_to_end_turn", out var rdy) && rdy is true ? " [READY]" : "";
sb.AppendLine($"- **{p["character"]}**{youTag}{aliveTag}{readyTag} - HP: {p["hp"]}/{p["max_hp"]} | Gold: {p["gold"]}");
// Show teammate pets inline
if (p.TryGetValue("pets", out var tPetsObj) && tPetsObj is List<Dictionary<string, object?>> tPets)
{
foreach (var pet in tPets)
{
bool petAlive = pet.TryGetValue("alive", out var pa) && pa is true;
string petStatus = petAlive
? $"HP: {pet["hp"]}/{pet["max_hp"]} | Block: {pet["block"]}"
: "DEAD";
sb.AppendLine($" - Pet: **{pet["name"]}** - {petStatus}");
}
}
}
sb.AppendLine();
}
// Top-level player summary (non-combat singleplayer only; combat renders its own detailed view)
bool hasBattle = state.ContainsKey("battle");
if (!isMultiplayer && !hasBattle
&& state.TryGetValue("player", out var topPlayerObj) && topPlayerObj is Dictionary<string, object?> topPlayer)
{
sb.AppendLine("## Player (You)");
string stars = topPlayer.TryGetValue("stars", out var s) && s != null ? $" | Stars: {s}" : "";
sb.AppendLine($"**{topPlayer["character"]}** - HP: {topPlayer["hp"]}/{topPlayer["max_hp"]} | Gold: {topPlayer["gold"]}{stars}");
sb.AppendLine();
FormatListSection(sb, "Relics", topPlayer, "relics", r =>
{
string counter = r.TryGetValue("counter", out var c) && c != null ? $" [{c}]" : "";
return $"- **{r["name"]}**{counter}: {r["description"]}";
});
FormatPotionsSection(sb, topPlayer);
}
if (state.TryGetValue("battle", out var battleObj) && battleObj is Dictionary<string, object?> battle)
{
var battlePlayer = state.TryGetValue("player", out var bp) && bp is Dictionary<string, object?> bpd ? bpd : null;
FormatBattleMarkdown(sb, battle, battlePlayer);
}
if (state.TryGetValue("event", out var eventObj) && eventObj is Dictionary<string, object?> eventData)
{
FormatEventMarkdown(sb, eventData);
if (isMultiplayer)
FormatEventVotesMarkdown(sb, eventData);
}
if (state.TryGetValue("rest_site", out var restObj) && restObj is Dictionary<string, object?> restData)
{
FormatRestSiteMarkdown(sb, restData);
}
if (state.TryGetValue("shop", out var shopObj) && shopObj is Dictionary<string, object?> shopData)
{
FormatShopMarkdown(sb, shopData);
}
if (state.TryGetValue("fake_merchant", out var fmObj) && fmObj is Dictionary<string, object?> fmData)
{
FormatFakeMerchantMarkdown(sb, fmData);
}
if (state.TryGetValue("map", out var mapObj) && mapObj is Dictionary<string, object?> mapData)
{
FormatMapMarkdown(sb, mapData);
if (isMultiplayer)
FormatMapVotesMarkdown(sb, mapData);
}
if (state.TryGetValue("rewards", out var rewardsObj) && rewardsObj is Dictionary<string, object?> rewards)
{
FormatRewardsMarkdown(sb, rewards);
}
if (state.TryGetValue("card_reward", out var cardRewardObj) && cardRewardObj is Dictionary<string, object?> cardReward)
{
FormatCardRewardMarkdown(sb, cardReward);
}
if (state.TryGetValue("hand_select", out var handSelectObj) && handSelectObj is Dictionary<string, object?> handSelect)
{
FormatHandSelectMarkdown(sb, handSelect);
}
if (state.TryGetValue("card_select", out var cardSelectObj) && cardSelectObj is Dictionary<string, object?> cardSelect)
{
FormatCardSelectMarkdown(sb, cardSelect);
}
if (state.TryGetValue("bundle_select", out var bundleSelectObj) && bundleSelectObj is Dictionary<string, object?> bundleSelect)
{
FormatBundleSelectMarkdown(sb, bundleSelect);
}
if (state.TryGetValue("relic_select", out var relicSelectObj) && relicSelectObj is Dictionary<string, object?> relicSelect)
{
FormatRelicSelectMarkdown(sb, relicSelect);
}
if (state.TryGetValue("crystal_sphere", out var crystalSphereObj) && crystalSphereObj is Dictionary<string, object?> crystalSphere)
{
FormatCrystalSphereMarkdown(sb, crystalSphere);
}
if (state.TryGetValue("treasure", out var treasureObj) && treasureObj is Dictionary<string, object?> treasureData)
{
FormatTreasureMarkdown(sb, treasureData);
if (isMultiplayer)
FormatTreasureBidsMarkdown(sb, treasureData);
}
if (state.TryGetValue("overlay", out var overlayObj) && overlayObj is Dictionary<string, object?> overlayData)
{
sb.AppendLine($"## Overlay: {overlayData.GetValueOrDefault("screen_type")}");
sb.AppendLine(overlayData.GetValueOrDefault("message")?.ToString());
sb.AppendLine();
}
// Keyword glossary - collect all unique keyword definitions
var glossary = new Dictionary<string, string>();
CollectKeywordsFromState(state, glossary);
if (glossary.Count > 0)
{
sb.AppendLine("## Keyword Glossary");
foreach (var (name, description) in glossary.OrderBy(kv => kv.Key))
sb.AppendLine($"- **{name}**: {description}");
sb.AppendLine();
}
return sb.ToString();
}
private static void FormatMenuMarkdown(StringBuilder sb, Dictionary<string, object?> state)
{
var screen = state.TryGetValue("menu_screen", out var ms) ? ms?.ToString() ?? "main" : "main";
sb.AppendLine($"## Menu: {screen}");
if (state.TryGetValue("message", out var msg) && msg != null)
sb.AppendLine(msg.ToString());
sb.AppendLine();
// MP lobby block — ready/ascension/roster — same shape on character_select and load_lobby
if (state.TryGetValue("lobby", out var lobbyObj) &&
lobbyObj is Dictionary<string, object?> lobby)
{
FormatLobbyMarkdown(sb, lobby);
}
// Multiplayer Join — friends list
if (state.TryGetValue("friends", out var friendsObj) &&
friendsObj is List<Dictionary<string, object?>> friends)
{
bool fastMp = state.TryGetValue("fast_mp", out var fastObj) && fastObj is true;
bool loading = state.TryGetValue("loading", out var loadingObj) && loadingObj is true;
bool noFriends = state.TryGetValue("no_friends", out var noObj) && noObj is true;
sb.AppendLine($"### Friends ({friends.Count})"
+ (fastMp ? " _[FastMP — auto-joins localhost:33771]_" : "")
+ (loading ? " _[loading...]_" : ""));
if (friends.Count == 0)
{
sb.AppendLine(noFriends
? "_No friends with open lobbies. Use `refresh` to retry._"
: "_(empty)_");
}
else
{
foreach (var friend in friends)
{
var idx = friend.GetValueOrDefault("index")?.ToString() ?? "?";
var name = friend.GetValueOrDefault("name")?.ToString() ?? "(unknown)";
var pid = friend.GetValueOrDefault("player_id")?.ToString() ?? "?";
var enabled = !friend.TryGetValue("enabled", out var en) || en is not false;
sb.AppendLine($"- `join_{idx}` **{name}** ({pid}){(enabled ? "" : " (disabled)")}");
}
}
sb.AppendLine();
}
if (state.TryGetValue("options", out var optionsObj) && optionsObj != null)
FormatMenuOptionsMarkdown(sb, optionsObj);
if (state.TryGetValue("characters", out var charactersObj) &&
charactersObj is List<Dictionary<string, object?>> characters &&
characters.Count > 0)
{
sb.AppendLine("### Characters");
foreach (var character in characters)
{
var id = character.GetValueOrDefault("id")?.ToString() ?? "?";
var name = character.GetValueOrDefault("name")?.ToString() ?? id;
var locked = character.TryGetValue("locked", out var lockedObj) && lockedObj is true ? " (LOCKED)" : "";
var hp = character.GetValueOrDefault("hp")?.ToString() ?? "?";
var gold = character.GetValueOrDefault("gold")?.ToString() ?? "?";
var energy = character.GetValueOrDefault("energy")?.ToString() ?? "?";
sb.AppendLine($"- `{id}` **{name}**{locked} - HP: {hp} | Gold: {gold} | Energy: {energy}");
}
sb.AppendLine();
sb.AppendLine("Use `menu_select` with an unlocked character ID or name, then `confirm`/`embark`.");
sb.AppendLine();
}
}
private static void FormatLobbyMarkdown(StringBuilder sb, Dictionary<string, object?> lobby)
{
var type = lobby.GetValueOrDefault("type")?.ToString() ?? "?";
var mode = lobby.GetValueOrDefault("game_mode")?.ToString() ?? "standard";
var asc = lobby.GetValueOrDefault("ascension")?.ToString() ?? "0";
var maxAsc = lobby.GetValueOrDefault("max_ascension")?.ToString();
var allReady = lobby.TryGetValue("all_ready", out var ar) && ar is true;
var aboutToBegin = lobby.TryGetValue("is_about_to_begin", out var ab) && ab is true;
sb.Append($"### Lobby ({type}, {mode}) — Ascension: {asc}");
if (!string.IsNullOrEmpty(maxAsc) && maxAsc != "0")
sb.Append($" / max {maxAsc}");
sb.AppendLine();
sb.AppendLine($"- All ready: **{allReady}**" + (aboutToBegin ? " — about to begin" : ""));
// MP load lobby uses act/floor/connected_player_count/expected_player_count
if (lobby.TryGetValue("act", out var actObj))
{
var act = actObj?.ToString();
var floor = lobby.GetValueOrDefault("floor")?.ToString();
sb.AppendLine($"- Saved progress: act {act}, floor {floor}");
}
if (lobby.TryGetValue("connected_player_count", out var connObj))
{
var conn = connObj?.ToString();
var expected = lobby.GetValueOrDefault("expected_player_count")?.ToString();
sb.AppendLine($"- Connected: {conn}/{expected}");
}
if (lobby.TryGetValue("players", out var pObj) &&
pObj is List<Dictionary<string, object?>> pl &&
pl.Count > 0)
{
sb.AppendLine("- Players:");
foreach (var p in pl)
{
var local = p.TryGetValue("is_local", out var l) && l is true ? " (you)" : "";
var host = p.TryGetValue("is_host", out var h) && h is true ? " [host]" : "";
var ready = p.TryGetValue("is_ready", out var r) && r is true ? " ✓" : "";
var connected = p.TryGetValue("is_connected", out var c) ? (c is true ? "" : " (disconnected)") : "";
var name = p.GetValueOrDefault("platform_name")?.ToString();
var charName = p.GetValueOrDefault("character")?.ToString()
?? p.GetValueOrDefault("character_id")?.ToString() ?? "?";
var label = string.IsNullOrEmpty(name) ? p.GetValueOrDefault("id")?.ToString() ?? "?" : name;
sb.AppendLine($" - {label}{local}{host}: {charName}{ready}{connected}");
}
}
sb.AppendLine();
}
private static void FormatGameOverMarkdown(StringBuilder sb, Dictionary<string, object?> state)
{
if (state.TryGetValue("game_over", out var gameOverObj) &&
gameOverObj is Dictionary<string, object?> gameOver)
{
if (gameOver.TryGetValue("message", out var msg) && msg != null)
sb.AppendLine(msg.ToString());
if (gameOver.TryGetValue("options", out var optionsObj) && optionsObj != null)
FormatMenuOptionsMarkdown(sb, optionsObj);
}
}
private static void FormatMenuOptionsMarkdown(StringBuilder sb, object optionsObj)
{
if (optionsObj is List<string> names && names.Count > 0)
{
sb.AppendLine("### Options");
foreach (var name in names)
sb.AppendLine($"- `{name}`");
sb.AppendLine();
return;
}
if (optionsObj is List<Dictionary<string, object?>> options && options.Count > 0)
{
sb.AppendLine("### Options");
foreach (var opt in options)
{
var name = opt.GetValueOrDefault("name")?.ToString() ?? "?";
var enabled = !opt.TryGetValue("enabled", out var enabledObj) || enabledObj is not false;
sb.AppendLine($"- `{name}`{(enabled ? "" : " (disabled)")}");
}
sb.AppendLine();
}
}
private static void FormatBattleMarkdown(StringBuilder sb, Dictionary<string, object?> battle, Dictionary<string, object?>? player)
{
string allReady = battle.TryGetValue("all_players_ready", out var ar) ? $" | All Ready: {ar}" : "";
sb.AppendLine($"**Round {battle["round"]}** | Turn: {battle["turn"]} | Play Phase: {battle["is_play_phase"]}{allReady}");
sb.AppendLine();
if (player != null)
{
sb.AppendLine("## Player (You)");
string stars = player.TryGetValue("stars", out var s) && s != null ? $" | Stars: {s}" : "";
sb.AppendLine($"**{player["character"]}** - HP: {player["hp"]}/{player["max_hp"]} | Block: {player["block"]} | Energy: {player["energy"]}/{player["max_energy"]}{stars} | Gold: {player["gold"]}");
sb.AppendLine();
FormatListSection(sb, "Status", player, "status", p => $"- **{p["name"]}** ({FormatStatusAmount(p["amount"])}): {p["description"]}");
FormatListSection(sb, "Relics", player, "relics", r =>
{
string counter = r.TryGetValue("counter", out var c) && c != null ? $" [{c}]" : "";
return $"- **{r["name"]}**{counter}: {r["description"]}";
});
FormatPotionsSection(sb, player);
if (player.TryGetValue("hand", out var handObj) && handObj is List<Dictionary<string, object?>> hand && hand.Count > 0)
{
sb.AppendLine("### Hand");
foreach (var card in hand)
{
string playable = card["can_play"] is true ? "✓" : "✗";
string keywords = card.TryGetValue("keywords", out var kw) && kw is List<string> kwList && kwList.Count > 0
? $" [{string.Join(", ", kwList)}]" : "";
string starCost = card.TryGetValue("star_cost", out var sc) && sc != null ? $" + {sc} star" : "";
sb.AppendLine($"- [{card["index"]}] **{card["name"]}** ({card["cost"]} energy{starCost}) [{card["type"]}] {playable}{keywords} - {card["description"]} (target: {card["target_type"]})");
}
sb.AppendLine();
}
FormatDeckPilesMarkdown(sb, player);
if (player.TryGetValue("orbs", out var orbsObj) && orbsObj is List<Dictionary<string, object?>> orbs && orbs.Count > 0)
{
int slots = player.TryGetValue("orb_slots", out var osVal) && osVal is int sv ? sv : orbs.Count;
int empty = player.TryGetValue("orb_empty_slots", out var esVal) && esVal is int ev ? ev : 0;
sb.AppendLine($"### Orbs ({orbs.Count}/{slots} slots)");
foreach (var orb in orbs)
{
string desc = orb.TryGetValue("description", out var d) && d != null ? $" - {d}" : "";
sb.AppendLine($"- **{orb["name"]}** (passive: {orb["passive_val"]}, evoke: {orb["evoke_val"]}){desc}");
}
if (empty > 0)
sb.AppendLine($"- *{empty} empty slot(s)*");
sb.AppendLine();
}
FormatPetsMarkdown(sb, player);
}
if (battle.TryGetValue("enemies", out var enemiesObj) && enemiesObj is List<Dictionary<string, object?>> enemies && enemies.Count > 0)
{
sb.AppendLine("## Enemies");
foreach (var enemy in enemies)
{
sb.AppendLine($"### {enemy["name"]} (`{enemy["entity_id"]}`)");
sb.AppendLine($"HP: {enemy["hp"]}/{enemy["max_hp"]} | Block: {enemy["block"]}");
if (enemy.TryGetValue("intents", out var intentsObj) && intentsObj is List<Dictionary<string, object?>> intents && intents.Count > 0)
{
sb.Append("**Intent:** ");
sb.AppendLine(string.Join(", ", intents.Select(i =>
{
string title = i.TryGetValue("title", out var t) && t != null ? t.ToString()! : i["type"]!.ToString()!;
string typeTag = $" ({i["type"]})";
string label = i.TryGetValue("label", out var l) && l is string ls && ls.Length > 0 ? $" {ls}" : "";
string desc = i.TryGetValue("description", out var d) && d is string ds && ds.Length > 0 ? $" - {ds}" : "";
return $"{title}{typeTag}{label}{desc}";
})));
}
FormatListSection(sb, "Status", enemy, "status", p => $" - **{p["name"]}** ({FormatStatusAmount(p["amount"])}): {p["description"]}");
sb.AppendLine();
}
}
}
private static void FormatDeckPilesMarkdown(StringBuilder sb, Dictionary<string, object?> player)
{
sb.AppendLine("### Deck Information");
sb.AppendLine();
FormatPileMarkdown(sb, player, "draw_pile", "draw_pile_count", "Draw Pile", " sorted by rarity");
FormatPileMarkdown(sb, player, "discard_pile", "discard_pile_count", "Discard Pile");
FormatPileMarkdown(sb, player, "exhaust_pile", "exhaust_pile_count", "Exhaust Pile");
}
private static void FormatPetsMarkdown(StringBuilder sb, Dictionary<string, object?> player)
{
if (!player.TryGetValue("pets", out var petsObj)
|| petsObj is not List<Dictionary<string, object?>> pets
|| pets.Count == 0)
return;
sb.AppendLine("### Pets");
foreach (var pet in pets)
{
bool alive = pet.TryGetValue("alive", out var a) && a is true;
string status = alive
? $"HP: {pet["hp"]}/{pet["max_hp"]} | Block: {pet["block"]}"
: "DEAD";
sb.AppendLine($"- **{pet["name"]}** (`{pet["id"]}`) - {status}");
if (alive && pet.TryGetValue("status", out var statusObj)
&& statusObj is List<Dictionary<string, object?>> statusList && statusList.Count > 0)
{
sb.AppendLine(" **Status**");
foreach (var p in statusList)
sb.AppendLine($" - **{p["name"]}** ({FormatStatusAmount(p["amount"])}): {p["description"]}");
}
}
sb.AppendLine();
}
private static void FormatPileMarkdown(StringBuilder sb, Dictionary<string, object?> player,
string pileKey, string countKey, string label, string suffix = "")
{
sb.AppendLine($"#### {label} ({player[countKey]} cards{(suffix.Length > 0 ? "," : "")}{suffix})");
if (player.TryGetValue(pileKey, out var pileObj) && pileObj is List<Dictionary<string, object?>> pile && pile.Count > 0)
{
foreach (var card in pile)
{
string starCost = card.TryGetValue("star_cost", out var sc) && sc != null ? $" + {sc} star" : "";
sb.AppendLine($"- {card["name"]} ({card["cost"]}{starCost}): {card["description"]}");
}
}
else
sb.AppendLine("- *(empty)*");
sb.AppendLine();
}
private static void FormatEventMarkdown(StringBuilder sb, Dictionary<string, object?> evt)
{
string name = evt.TryGetValue("event_name", out var n) && n != null ? n.ToString()! : "Unknown Event";
bool isAncient = evt.TryGetValue("is_ancient", out var a) && a is true;
sb.AppendLine($"## {(isAncient ? "Ancient" : "Event")}: {name}");
sb.AppendLine();
bool inDialogue = evt.TryGetValue("in_dialogue", out var d) && d is true;
if (inDialogue)
{
sb.AppendLine("*Ancient dialogue in progress - use `advance_dialogue` to continue.*");
sb.AppendLine();
return;
}
if (evt.TryGetValue("options", out var optObj) && optObj is List<Dictionary<string, object?>> options && options.Count > 0)
{
sb.AppendLine("### Options");
foreach (var opt in options)
{
bool locked = opt["is_locked"] is true;
bool proceed = opt["is_proceed"] is true;
bool chosen = opt["was_chosen"] is true;
string tag = locked ? " (LOCKED)" : chosen ? " (CHOSEN)" : proceed ? " (PROCEED)" : "";
string relic = opt.TryGetValue("relic_name", out var rn) && rn != null ? $" [Relic: {rn}]" : "";
sb.AppendLine($"- [{opt["index"]}] **{opt["title"]}**{tag}{relic} - {opt["description"]}");
}
sb.AppendLine();
}
else
{
sb.AppendLine("No options available.");
sb.AppendLine();
}
}
private static void FormatRestSiteMarkdown(StringBuilder sb, Dictionary<string, object?> restSite)
{
if (restSite.TryGetValue("options", out var optObj) && optObj is List<Dictionary<string, object?>> options && options.Count > 0)
{
sb.AppendLine("## Rest Site Options");
foreach (var opt in options)
{
string enabled = opt["is_enabled"] is true ? "" : " (DISABLED)";
sb.AppendLine($"- [{opt["index"]}] **{opt["name"]}**{enabled} - {opt["description"]}");
}
sb.AppendLine();
}
bool canProceed = restSite.TryGetValue("can_proceed", out var cp) && cp is true;
sb.AppendLine($"**Can proceed:** {(canProceed ? "Yes" : "No")}");
sb.AppendLine();
}
private static void FormatShopMarkdown(StringBuilder sb, Dictionary<string, object?> shop)
{
if (shop.TryGetValue("error", out var err) && err != null)
{
sb.AppendLine("## Shop");
sb.AppendLine($"**Note:** {err}");
sb.AppendLine();
}
if (shop.TryGetValue("items", out var itemsObj) && itemsObj is List<Dictionary<string, object?>> items)
{
sb.AppendLine("## Shop Inventory");
string? lastCategory = null;
foreach (var item in items)
{
string category = item["category"]?.ToString() ?? "";
if (category != lastCategory)
{
string header = category switch { "card" => "Cards", "relic" => "Relics", "potion" => "Potions", "card_removal" => "Services", _ => category };
sb.AppendLine($"### {header}");
lastCategory = category;
}
bool stocked = item["is_stocked"] is true;
bool afford = item["can_afford"] is true;
string priceTag = stocked ? $"{item["price"]}g" : "SOLD";
string affordTag = stocked && !afford ? " (can't afford)" : "";
string saleTag = item.TryGetValue("on_sale", out var os) && os is true ? " **SALE**" : "";
string cardCost = item.TryGetValue("card_cost", out var cc) && cc != null ? cc.ToString()! : "";
string cardStarCost = item.TryGetValue("card_star_cost", out var csc) && csc != null ? $" + {csc} star" : "";
string cardEnergy = cardCost != "" ? $" ({cardCost} energy{cardStarCost})" : "";
string desc = category switch
{
"card" => $"**{item.GetValueOrDefault("card_name")}**{cardEnergy} [{item.GetValueOrDefault("card_type")}] {item.GetValueOrDefault("card_rarity")} - {item.GetValueOrDefault("card_description")}",
"relic" => $"**{item.GetValueOrDefault("relic_name")}** - {item.GetValueOrDefault("relic_description")}",
"potion" => $"**{item.GetValueOrDefault("potion_name")}** - {item.GetValueOrDefault("potion_description")}",
"card_removal" => "**Remove a card** from your deck",
_ => "Unknown item"
};
sb.AppendLine($"- [{item["index"]}] {desc} - {priceTag}{saleTag}{affordTag}");
}
sb.AppendLine();
}
bool canProceed = shop.TryGetValue("can_proceed", out var cp) && cp is true;
sb.AppendLine($"**Can proceed:** {(canProceed ? "Yes" : "No")}");
sb.AppendLine();
}
private static void FormatFakeMerchantMarkdown(StringBuilder sb, Dictionary<string, object?> fm)
{
string name = fm.TryGetValue("event_name", out var n) && n != null ? n.ToString()! : "Fake Merchant";
bool startedFight = fm.TryGetValue("started_fight", out var sf) && sf is true;
sb.AppendLine($"## Event: {name}");
sb.AppendLine();
if (startedFight)
{
sb.AppendLine("*The fake merchant has been defeated. Use `proceed` to open the map.*");
sb.AppendLine();
return;
}
sb.AppendLine("*This is a fake merchant selling dubious relics. You can browse and buy, or throw a Foul Potion (use_potion) to start a fight.*");
sb.AppendLine();
// Reuse shop formatting for the nested shop object
if (fm.TryGetValue("shop", out var shopObj) && shopObj is Dictionary<string, object?> shopData)
FormatShopMarkdown(sb, shopData);
}
private static void FormatMapMarkdown(StringBuilder sb, Dictionary<string, object?> map)
{
// Path taken
if (map.TryGetValue("visited", out var visitedObj) && visitedObj is List<Dictionary<string, object?>> visited && visited.Count > 0)
{
sb.AppendLine("## Path Taken");
var parts = visited.Select((v, i) => $"{i + 1}. {v["type"]} ({v["col"]},{v["row"]})");
sb.AppendLine(string.Join(" → ", parts) + " ← current");
sb.AppendLine();
}
// Build node lookup for path traversal
var nodeLookup = new Dictionary<string, Dictionary<string, object?>>();
if (map.TryGetValue("nodes", out var nodesObj) && nodesObj is List<Dictionary<string, object?>> nodes)
{
foreach (var node in nodes)
nodeLookup[$"{node["col"]},{node["row"]}"] = node;
}
// Next options with future path trees
if (map.TryGetValue("next_options", out var optObj) && optObj is List<Dictionary<string, object?>> options && options.Count > 0)
{
sb.AppendLine("## Choose Next Node");
foreach (var opt in options)
{
sb.AppendLine($"- [{opt["index"]}] **{opt["type"]}** ({opt["col"]},{opt["row"]})");
string tree = BuildFuturePathTree(opt, nodeLookup);
if (tree.Length > 0)
sb.AppendLine($" Future paths: {tree}");
}
sb.AppendLine();
}
else
{
sb.AppendLine("## Map");
sb.AppendLine("No travelable nodes available.");
sb.AppendLine();
}
}
/// <summary>
/// BFS from a node through its children to build a future path tree string.
/// Format: -> Type (col,row) or Type (col,row) -> Type (col,row) or ...
/// </summary>
private static string BuildFuturePathTree(Dictionary<string, object?> startNode, Dictionary<string, Dictionary<string, object?>> nodeLookup)
{
// Look up the canonical node (with children) from the full node list
string startKey = $"{startNode["col"]},{startNode["row"]}";
var canonicalStart = nodeLookup.TryGetValue(startKey, out var cs) ? cs : startNode;
var currentKeys = GetChildKeys(canonicalStart);
var sb = new StringBuilder();
while (currentKeys.Count > 0)
{
// Collect node info for this level
var levelNodes = new List<(string type, int col, int row)>();
var nextKeys = new HashSet<string>();
foreach (var key in currentKeys.OrderBy(k => k))
{
if (nodeLookup.TryGetValue(key, out var node))
{
string type = node["type"]?.ToString() ?? "Unknown";
int col = node["col"] is int c ? c : Convert.ToInt32(node["col"]);
int row = node["row"] is int r ? r : Convert.ToInt32(node["row"]);
levelNodes.Add((type, col, row));
foreach (var childKey in GetChildKeys(node))
nextKeys.Add(childKey);
}
}
if (levelNodes.Count == 0) break;
sb.Append("-> ");
sb.Append(string.Join(" or ", levelNodes.Select(n => $"{n.type} ({n.col},{n.row})")));
sb.Append(' ');
currentKeys = nextKeys;
}
return sb.ToString().TrimEnd();
}
private static HashSet<string> GetChildKeys(Dictionary<string, object?> node)
{
var keys = new HashSet<string>();
if (node.TryGetValue("children", out var childrenObj) && childrenObj is System.Collections.IList childList)
{
foreach (var child in childList)
{
if (child is System.Collections.IList coords && coords.Count >= 2)
keys.Add($"{coords[0]},{coords[1]}");
}
}
return keys;
}
private static void FormatRewardsMarkdown(StringBuilder sb, Dictionary<string, object?> rewards)
{
if (rewards.TryGetValue("items", out var itemsObj) && itemsObj is List<Dictionary<string, object?>> items && items.Count > 0)
{
sb.AppendLine("## Rewards");
foreach (var item in items)
{
string extra = "";
if (item.TryGetValue("gold_amount", out var gold) && gold != null)
extra = $" ({gold} gold)";
else if (item.TryGetValue("potion_description", out var pDesc) && pDesc != null)
extra = $" - {pDesc}";
else if (item.TryGetValue("potion_name", out var pName) && pName != null)
extra = $" ({pName})";
sb.AppendLine($"- [{item["index"]}] **{item["type"]}**: {item["description"]}{extra}");
}
sb.AppendLine();
}
else
{
sb.AppendLine("## Rewards");
sb.AppendLine("No rewards available.");
sb.AppendLine();
}
bool canProceed = rewards.TryGetValue("can_proceed", out var cp) && cp is true;
sb.AppendLine($"**Can proceed:** {(canProceed ? "Yes" : "No")}");
sb.AppendLine();
}
private static void FormatCardRewardMarkdown(StringBuilder sb, Dictionary<string, object?> cardReward)
{
sb.AppendLine("## Card Reward Selection");
sb.AppendLine("Choose a card to add to your deck:");
sb.AppendLine();
if (cardReward.TryGetValue("cards", out var cardsObj) && cardsObj is List<Dictionary<string, object?>> cards)
{
foreach (var card in cards)
{
string starCost = card.TryGetValue("star_cost", out var sc) && sc != null ? $" + {sc} star" : "";
string keywords = card.TryGetValue("keywords", out var kw) && kw is List<string> kwList && kwList.Count > 0
? $" [{string.Join(", ", kwList)}]" : "";
sb.AppendLine($"- [{card["index"]}] **{card["name"]}** ({card["cost"]} energy{starCost}) [{card["type"]}] {card["rarity"]}{keywords} - {card["description"]}");
}
sb.AppendLine();
}
bool canSkip = cardReward.TryGetValue("can_skip", out var cs) && cs is true;
sb.AppendLine($"**Can skip:** {(canSkip ? "Yes" : "No")}");
sb.AppendLine();
}
private static void FormatRelicSelectMarkdown(StringBuilder sb, Dictionary<string, object?> relicSelect)
{
sb.AppendLine("## Relic Selection");
if (relicSelect.TryGetValue("prompt", out var p) && p != null)
sb.AppendLine($"*{p}*");
sb.AppendLine();
if (relicSelect.TryGetValue("relics", out var relicsObj) && relicsObj is List<Dictionary<string, object?>> relics)
{
foreach (var relic in relics)
sb.AppendLine($"- [{relic["index"]}] **{relic["name"]}** - {relic["description"]}");
sb.AppendLine();
}
bool canSkip = relicSelect.TryGetValue("can_skip", out var cs) && cs is true;
sb.AppendLine($"Use `select_relic(index)` to choose. Can skip: {(canSkip ? "Yes" : "No")}");
sb.AppendLine();
}
private static void FormatHandSelectMarkdown(StringBuilder sb, Dictionary<string, object?> handSelect)
{
sb.AppendLine("## In-Combat Card Selection");
if (handSelect.TryGetValue("prompt", out var promptObj) && promptObj != null)
sb.AppendLine($"*{promptObj}*");
sb.AppendLine();
string mode = handSelect.TryGetValue("mode", out var m) ? m?.ToString() ?? "simple_select" : "simple_select";
if (mode == "upgrade_select")
sb.AppendLine("**Mode:** Upgrade selection");
sb.AppendLine();
if (handSelect.TryGetValue("cards", out var cardsObj) && cardsObj is List<Dictionary<string, object?>> cards && cards.Count > 0)
{
sb.AppendLine("### Selectable Cards");
foreach (var card in cards)
{
string starCost = card.TryGetValue("star_cost", out var sc) && sc != null ? $" + {sc} star" : "";
sb.AppendLine($"- [{card["index"]}] **{card["name"]}** ({card["cost"]} energy{starCost}) [{card["type"]}] - {card["description"]}");
}
sb.AppendLine();
}
if (handSelect.TryGetValue("selected_cards", out var selObj) && selObj is List<Dictionary<string, object?>> selected && selected.Count > 0)
{
sb.AppendLine("### Already Selected");
foreach (var card in selected)
sb.AppendLine($"- {card["name"]}");
sb.AppendLine();
}
bool canConfirm = handSelect.TryGetValue("can_confirm", out var cc) && cc is true;
sb.AppendLine($"Use `combat_select_card(card_index)` to select. Can confirm: {(canConfirm ? "Yes - use `combat_confirm_selection`" : "No - select more cards")}");
sb.AppendLine();
}
private static void FormatCardSelectMarkdown(StringBuilder sb, Dictionary<string, object?> cardSelect)
{
string screenType = cardSelect.TryGetValue("screen_type", out var st) ? st?.ToString() ?? "select" : "select";
string screenLabel = screenType switch
{
"transform" => "Transform",
"upgrade" => "Upgrade",
"select" => "Select",
"simple_select" => "Select",
_ => screenType
};
sb.AppendLine($"## Card Selection: {screenLabel}");
if (cardSelect.TryGetValue("prompt", out var promptObj) && promptObj != null)
{
sb.AppendLine($"*{promptObj}*");
}
sb.AppendLine();
if (cardSelect.TryGetValue("cards", out var cardsObj) && cardsObj is List<Dictionary<string, object?>> cards)
{
sb.AppendLine("### Cards");
foreach (var card in cards)
{
string starCost = card.TryGetValue("star_cost", out var sc) && sc != null ? $" + {sc} star" : "";
sb.AppendLine($"- [{card["index"]}] **{card["name"]}** ({card["cost"]} energy{starCost}) [{card["type"]}] {card["rarity"]} - {card["description"]}");
}
sb.AppendLine();
}
bool preview = cardSelect.TryGetValue("preview_showing", out var pv) && pv is true;
bool canConfirm = cardSelect.TryGetValue("can_confirm", out var cc) && cc is true;
bool canCancel = cardSelect.TryGetValue("can_cancel", out var cn) && cn is true;
if (preview)
{
sb.AppendLine("**Preview is showing** - use `confirm_selection` to confirm or `cancel_selection` to go back.");
if (cardSelect.TryGetValue("preview_cards", out var previewCardsObj) && previewCardsObj is List<Dictionary<string, object?>> previewCards && previewCards.Count > 0)
{
sb.AppendLine("### Preview Cards");
foreach (var card in previewCards)
{
string starCost = card.TryGetValue("star_cost", out var sc) && sc != null ? $" + {sc} star" : "";
sb.AppendLine($"- **{card["name"]}** ({card["cost"]} energy{starCost}) [{card["type"]}] {card["rarity"]} - {card["description"]}");
}
sb.AppendLine();
}
}
else
{
sb.AppendLine($"**Select cards** using `select_card(index)`. Can confirm: {(canConfirm ? "Yes" : "No")} | Can cancel: {(canCancel ? "Yes" : "No")}");
}
sb.AppendLine();
}
private static void FormatBundleSelectMarkdown(StringBuilder sb, Dictionary<string, object?> bundleSelect)
{
sb.AppendLine("## Bundle Selection");
if (bundleSelect.TryGetValue("prompt", out var promptObj) && promptObj != null)
sb.AppendLine($"*{promptObj}*");
sb.AppendLine();
if (bundleSelect.TryGetValue("bundles", out var bundlesObj) && bundlesObj is List<Dictionary<string, object?>> bundles && bundles.Count > 0)
{
sb.AppendLine("### Bundles");
foreach (var bundle in bundles)
{
sb.AppendLine($"- [{bundle["index"]}] Bundle with {bundle["card_count"]} card(s)");
if (bundle.TryGetValue("cards", out var cardsObj) && cardsObj is List<Dictionary<string, object?>> cards)
{
foreach (var card in cards)
{
string starCost = card.TryGetValue("star_cost", out var sc) && sc != null ? $" + {sc} star" : "";
sb.AppendLine($" **{card["name"]}** ({card["cost"]} energy{starCost}) [{card["type"]}] {card["rarity"]} - {card["description"]}");
}
}
}
sb.AppendLine();
}
bool preview = bundleSelect.TryGetValue("preview_showing", out var pv) && pv is true;
bool canConfirm = bundleSelect.TryGetValue("can_confirm", out var cc) && cc is true;
bool canCancel = bundleSelect.TryGetValue("can_cancel", out var cn) && cn is true;
if (preview)
{
sb.AppendLine("**Preview is showing** - use `confirm_bundle_selection()` to confirm or `cancel_bundle_selection()` to go back.");
if (bundleSelect.TryGetValue("preview_cards", out var previewCardsObj) && previewCardsObj is List<Dictionary<string, object?>> previewCards && previewCards.Count > 0)
{
sb.AppendLine("### Preview Cards");
foreach (var card in previewCards)
{
string starCost = card.TryGetValue("star_cost", out var sc) && sc != null ? $" + {sc} star" : "";
sb.AppendLine($"- **{card["name"]}** ({card["cost"]} energy{starCost}) [{card["type"]}] {card["rarity"]} - {card["description"]}");
}
sb.AppendLine();
}
}
else
{
sb.AppendLine($"Use `select_bundle(index)` to open a bundle preview. Can confirm: {(canConfirm ? "Yes" : "No")} | Can cancel: {(canCancel ? "Yes" : "No")}");
sb.AppendLine();
}
}
private static void FormatCrystalSphereMarkdown(StringBuilder sb, Dictionary<string, object?> crystalSphere)
{
sb.AppendLine("## Crystal Sphere");
if (crystalSphere.TryGetValue("instructions_title", out var titleObj) && titleObj != null)
sb.AppendLine($"**{titleObj}**");
if (crystalSphere.TryGetValue("instructions_description", out var descObj) && descObj != null)
sb.AppendLine(descObj.ToString());
sb.AppendLine();
string tool = crystalSphere.TryGetValue("tool", out var toolObj) ? toolObj?.ToString() ?? "none" : "none";
string divinationsLeft = crystalSphere.TryGetValue("divinations_left_text", out var dlObj) && dlObj != null
? dlObj.ToString()!
: "Unknown";
sb.AppendLine($"**Tool:** {tool} | **Divinations:** {divinationsLeft}");
sb.AppendLine();
if (crystalSphere.TryGetValue("clickable_cells", out var cellsObj) && cellsObj is List<Dictionary<string, object?>> clickableCells && clickableCells.Count > 0)
{
sb.AppendLine("### Clickable Cells");
foreach (var cell in clickableCells)
sb.AppendLine($"- ({cell["x"]}, {cell["y"]})");
sb.AppendLine();
}
if (crystalSphere.TryGetValue("revealed_items", out var itemsObj) && itemsObj is List<Dictionary<string, object?>> revealedItems && revealedItems.Count > 0)
{
sb.AppendLine("### Revealed Items");
foreach (var item in revealedItems)
sb.AppendLine($"- **{item["item_type"]}** at ({item["x"]}, {item["y"]}) size {item["width"]}x{item["height"]}");
sb.AppendLine();
}
bool canProceed = crystalSphere.TryGetValue("can_proceed", out var cp) && cp is true;
if (canProceed)
{
sb.AppendLine("Use `crystal_sphere_proceed()` to continue.");
}
else
{
sb.AppendLine("Use `crystal_sphere_set_tool(tool)` with `big` or `small`, then `crystal_sphere_click_cell(x, y)`.");
}
sb.AppendLine();
}
private static void FormatTreasureMarkdown(StringBuilder sb, Dictionary<string, object?> treasure)
{
if (treasure.TryGetValue("relics", out var relicsObj) && relicsObj is List<Dictionary<string, object?>> relics && relics.Count > 0)
{
sb.AppendLine("## Treasure Relics");
foreach (var relic in relics)
{
string rarity = relic.TryGetValue("rarity", out var r) && r != null ? $" ({r})" : "";
sb.AppendLine($"- [{relic["index"]}] **{relic["name"]}**{rarity} - {relic["description"]}");
}
sb.AppendLine();
sb.AppendLine("Use `treasure_claim_relic(relic_index)` to claim a relic.");
}
else
{
sb.AppendLine("Chest is opening...");
}
sb.AppendLine();
bool canProceed = treasure.TryGetValue("can_proceed", out var cp) && cp is true;
if (canProceed)
sb.AppendLine("**Can proceed:** Yes");
sb.AppendLine();
}
private static string FormatStatusAmount(object? amount)
{
if (amount is int i && i == -1) return "indefinite";
return amount?.ToString() ?? "0";
}
private static void FormatListSection(StringBuilder sb, string title, Dictionary<string, object?> parent, string key,
Func<Dictionary<string, object?>, string> formatter)