-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizer.html
More file actions
1409 lines (1222 loc) · 56.3 KB
/
Copy pathvisualizer.html
File metadata and controls
1409 lines (1222 loc) · 56.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AlphaLudo - Game Visualizer</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
color: #fff;
}
h1 {
margin-bottom: 20px;
font-size: 2rem;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
/* Explicit 4 columns */
gap: 20px;
justify-content: center;
align-items: start;
max-width: 1800px;
/* Increase width to fit 4 cols */
}
.board-container {
position: relative;
/* Anchor for absolute labels */
background: #2a2a4a;
padding: 20px;
border-radius: 15px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}
.board {
display: grid;
grid-template-columns: repeat(15, 35px);
grid-template-rows: repeat(15, 35px);
gap: 2px;
background: #1a1a2e;
padding: 5px;
border-radius: 10px;
}
.cell {
width: 35px;
height: 35px;
background: #3a3a5a;
border-radius: 5px;
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
font-weight: bold;
transition: all 0.3s ease;
}
.cell.path {
background: #4a4a6a;
}
.cell.safe {
background: #2d5a3d;
border: 2px solid #4a9a5a;
}
.cell.home-p0 {
background: #4a1a1a;
}
/* P0 = Red home run */
.cell.home-p1 {
background: #1a4a1a;
}
/* P1 = Green home run */
.cell.home-p2 {
background: #4a4a1a;
}
/* P2 = Yellow home run */
.cell.home-p3 {
background: #1a1a4a;
}
/* P3 = Blue home run */
.id-label {
position: absolute;
color: #ffff00;
border: 1px solid #ffff00;
}
#id-p0 {
top: 40px;
left: 40px;
color: #ff4444;
border: 2px solid #ff4444;
background: #2a1a1a;
}
#id-p1 {
top: 40px;
right: 40px;
color: #00ff00;
border: 2px solid #00ff00;
background: #1a2a1a;
}
#id-p2 {
bottom: 40px;
right: 40px;
color: #ffff00;
border: 2px solid #ffff00;
background: #2a2a1a;
}
#id-p3 {
bottom: 40px;
left: 40px;
color: #4444ff;
border: 2px solid #4444ff;
background: #1a1a2a;
}
.cell.base-p0 {
background: #8a3d3d;
}
/* Red base */
.cell.base-p1 {
background: #2d8a3d;
}
/* Green base */
.cell.base-p2 {
background: #8a8a3d;
}
/* Yellow base */
.cell.base-p3 {
background: #3d3d8a;
}
/* Blue base */
.token {
width: 25px;
height: 25px;
border-radius: 50%;
border: 3px solid #fff;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
}
.token-p0 {
background: #ff4444;
}
/* Red */
.token-p1 {
background: #00ff00;
}
/* Green */
.token-p2 {
background: #ffff00;
}
/* Yellow */
.token-p3 {
background: #4444ff;
}
/* Blue */
.info-panel {
background: #2a2a4a;
padding: 20px;
border-radius: 15px;
min-width: 250px;
}
.info-panel h2 {
margin-bottom: 15px;
color: #aaa;
}
.player-info {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 10px;
padding: 10px;
background: #3a3a5a;
border-radius: 8px;
}
.player-info.active {
border: 2px solid #00ff88;
background: #3a4a5a;
}
.player-dot {
width: 20px;
height: 20px;
border-radius: 50%;
}
.dice {
font-size: 3rem;
text-align: center;
margin: 20px 0;
padding: 20px;
background: #3a3a5a;
border-radius: 10px;
}
.controls {
margin-top: 20px;
}
button {
background: linear-gradient(135deg, #00ff88 0%, #00cc66 100%);
color: #1a1a2e;
border: none;
padding: 12px 25px;
border-radius: 8px;
font-size: 1rem;
font-weight: bold;
cursor: pointer;
margin: 5px;
transition: transform 0.2s;
}
button:hover {
transform: scale(1.05);
}
#gameLog {
margin-top: 20px;
max-height: 200px;
overflow-y: auto;
background: #1a1a2e;
padding: 10px;
border-radius: 8px;
font-family: monospace;
font-size: 12px;
}
</style>
</head>
<body>
<h1>🎲 AlphaLudo - Game Visualizer</h1>
<div class="container">
<!-- Column 1: Board -->
<div class="board-container">
<div id="id-p0" class="id-label">P0: Main</div>
<div id="id-p1" class="id-label">P1: Main</div>
<div id="id-p2" class="id-label">P2: Main</div>
<div id="id-p3" class="id-label">P3: Main</div>
<div class="board" id="board">
<!-- Cells generated by JS -->
</div>
</div>
<!-- Column 2: Game State & Controls -->
<div class="info-panel" style="min-width: 250px;">
<h2>Game State</h2>
<div id="players">
<div class="player-info" id="player0">
<div class="player-dot token-p0"></div>
<span>Player 0 (Red)</span>
<span id="score0">0/4</span>
</div>
<div class="player-info" id="player1">
<div class="player-dot token-p1"></div>
<span>Player 1 (Grn)</span>
<span id="score1">0/4</span>
</div>
<div class="player-info" id="player2">
<div class="player-dot token-p2"></div>
<span>Player 2 (Yel)</span>
<span id="score2">0/4</span>
</div>
<div class="player-info" id="player3">
<div class="player-dot token-p3"></div>
<span>Player 3 (Blu)</span>
<span id="score3">0/4</span>
</div>
</div>
<div class="dice">
🎲 <span id="diceValue">-</span>
</div>
<div class="controls">
<select id="gameSelector" onchange="changeGame()"
style="padding: 8px; border-radius: 8px; background: #2a2a4a; color: white; border: 1px solid #00ff88; margin-right: 5px; cursor: pointer;">
<!-- Populated by JS -->
</select>
<button onclick="connectWebSocket()">Connect</button>
<button onclick="requestState()">Refresh</button>
<button id="pauseBtn" onclick="togglePause()"
style="background: linear-gradient(135deg, #ff8800 0%, #ff6600 100%);">Pause</button>
<button onclick="toggleDebugPath()"
style="background: linear-gradient(135deg, #ff4444 0%, #cc3333 100%);">Debug Path</button>
<div style="margin-top: 10px; font-size: 13px; color: #ccc;">
Batch Progress: <span id="batchStats" style="color: #00ff88; font-weight: bold;">0 / 32</span>
</div>
</div>
<div id="strategyPanel" style="margin-top: 15px; padding: 10px; background: #1a1a2e; border-radius: 8px;">
<h3 style="margin: 0 0 10px 0; color: #aaa; font-size: 14px;">Strategy Metrics (Progress %)</h3>
<div id="progressBars" style="font-size: 11px;">
<!-- Populated by JS -->
</div>
</div>
</div>
<!-- Column 3: Stats & Logs -->
<div class="info-panel" style="min-width: 300px;">
<h2>Statistics</h2>
<div id="trainingStats" style="margin-top: 15px; padding: 10px; background: #1a1a2e; border-radius: 8px;">
<h3 style="margin: 0 0 10px 0; color: #aaa; font-size: 14px;">Training Progress</h3>
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 8px; font-size: 12px;">
<div>Iter: <span id="statIter" style="color: #00ff88;">-</span></div>
<div>Batch G: <span id="statGame" style="color: #00ff88;">-</span></div>
<div>Total G: <span id="statTotalGames" style="color: #ffff00;">-</span></div>
<div>Eff. G: <span id="statEffGames" style="color: #ffff00;">-</span></div>
<div>Loss: <span id="statLoss" style="color: #ff8800;">-</span></div>
<div>Win%: <span id="statWinRate" style="color: #00aaff;">-</span></div>
<div>Buff: <span id="statBuffer" style="color: #aaa;">-</span></div>
<div>Time: <span id="statTime" style="color: #ccc;">-</span></div>
<div>Avg: <span id="statAvgGame" style="color: #ccc;">-</span></div>
<div>ETA: <span id="statETA" style="color: #ff4444;">-</span></div>
<div>Last Iter: <span id="statLastIterTime" style="color: #66ccff;">-</span></div>
<div>Avg Time: <span id="statAvgGameTime" style="color: #66ccff;">-</span></div>
<div>Cur Time: <span id="statCurGameTime" style="color: #cc66ff;">-</span></div>
</div>
</div>
<div id="chartPanel" style="margin-top: 15px; padding: 10px; background: #1a1a2e; border-radius: 8px;">
<h3 style="margin: 0 0 10px 0; color: #aaa; font-size: 14px;">Training Trends</h3>
<canvas id="trainingChart" width="280" height="150" style="width: 100%;"></canvas>
</div>
<!-- Elo Ratings Panel -->
<div id="eloPanel" style="margin-top: 15px; padding: 10px; background: #1a1a2e; border-radius: 8px;">
<h3 style="margin: 0 0 10px 0; color: #aaa; font-size: 14px;">📊 Elo Ratings</h3>
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 8px; font-size: 12px;">
<div style="background: #2a2a4e; padding: 8px; border-radius: 4px;">
<div style="color: #888; font-size: 10px;">Main Model</div>
<div id="eloMain" style="color: #00ff88; font-size: 18px; font-weight: bold;">1500</div>
</div>
</div>
<div style="background: #2a2a4e; padding: 8px; border-radius: 4px;">
<div style="color: #888; font-size: 10px;">Current Ghost</div>
<div style="display: flex; justify-content: space-between;">
<div id="eloGhost" style="color: #ff66ff; font-size: 14px;">-</div>
<div id="ghostWinrate" style="color: #00aaff; font-size: 12px;">WR: -</div>
</div>
<div id="eloGhostName" style="color: #666; font-size: 9px;">-</div>
</div>
</div>
<canvas id="eloChart" width="280" height="150"
style="width: 100%; margin-top: 10px; background: #1a1a2e;"></canvas>
<div id="eloRankings" style="margin-top: 8px; font-size: 10px; color: #888;">
<!-- Rankings will be populated here -->
</div>
</div>
<div id="gameLog" style="height: 100px;"></div>
</div>
<!-- Column 4: League Stats -->
<div class="info-panel" style="min-width: 300px;">
<h2>League Stats</h2>
<div id="leagueStatsPanel" style="margin-top: 15px; padding: 10px; background: #1a1a2e; border-radius: 8px;">
<table style="width: 100%; font-size: 11px; border-collapse: collapse; text-align: center; color: #fff;">
<thead>
<tr style="border-bottom: 1px solid #444;">
<th>Model</th>
<th>Wins</th>
<th>Played</th>
<th>Win%</th>
</tr>
</thead>
<tbody id="leagueStatsBody">
</tbody>
</table>
</div>
<div id="diceStatsPanel" style="margin-top: 15px; padding: 10px; background: #1a1a2e; border-radius: 8px;">
<h3 style="margin: 0 0 10px 0; color: #aaa; font-size: 14px;">Dice Distribution</h3>
<table style="width: 100%; font-size: 11px; border-collapse: collapse; text-align: center; color: #fff;">
<thead>
<tr style="border-bottom: 1px solid #444;">
<th style="padding: 2px;">P</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
</tr>
</thead>
<tbody id="diceStatsBody">
</tbody>
</table>
</div>
<div id="sessionStatsPanel" style="margin-top: 15px; padding: 10px; background: #1a1a2e; border-radius: 8px;">
<h3 style="margin: 0 0 10px 0; color: #aaa; font-size: 14px;">Session Wins (Client-side)</h3>
<table style="width: 100%; font-size: 11px; border-collapse: collapse; text-align: center; color: #fff;">
<thead>
<tr style="border-bottom: 1px solid #444;">
<th>P0 (Red)</th>
<th>P1 (Grn)</th>
<th>P2 (Yel)</th>
<th>P3 (Blu)</th>
</tr>
</thead>
<tbody>
<tr>
<td id="win0" style="color: #ff4444; font-weight: bold;">0</td>
<td id="win1" style="color: #00ff00; font-weight: bold;">0</td>
<td id="win2" style="color: #ffff00; font-weight: bold;">0</td>
<td id="win3" style="color: #4444ff; font-weight: bold;">0</td>
</tr>
</tbody>
</table>
</div>
<!-- Reward Config Panel -->
<div id="rewardConfigPanel"
style="margin-top: 15px; padding: 10px; background: #1a1a2e; border-radius: 8px; display: none;">
<h3 style="margin: 0 0 10px 0; color: #aaa; font-size: 14px;">🎯 Reward Config (Live)</h3>
<div style="font-size: 11px; color: #888; margin-bottom: 8px;">Changes apply on next iteration</div>
<!-- Win/Lose (fixed, not editable) -->
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 10px; font-size: 11px;">
<div style="background: #2a4a2a; padding: 6px; border-radius: 4px; text-align: center;">
🏆 Win: <span style="color: #00ff00; font-weight: bold;">+1.0</span>
</div>
<div style="background: #4a2a2a; padding: 6px; border-radius: 4px; text-align: center;">
💀 Lose: <span style="color: #ff4444; font-weight: bold;">-1.0</span>
</div>
</div>
<div style="display: grid; gap: 10px;">
<div>
<label style="color: #ff6666; font-size: 12px;">✂️ Cut Bonus: <span
id="cutValue">0.10</span></label>
<input type="range" id="cutSlider" min="0" max="0.5" step="0.01" value="0.10"
style="width: 100%; cursor: pointer;" oninput="updateReward('cut', this.value)">
</div>
<div>
<label style="color: #66ff66; font-size: 12px;">🏠 Home Bonus: <span
id="homeValue">0.25</span></label>
<input type="range" id="homeSlider" min="0" max="0.5" step="0.01" value="0.25"
style="width: 100%; cursor: pointer;" oninput="updateReward('home', this.value)">
</div>
<div>
<label style="color: #6666ff; font-size: 12px;">🛡️ Safe Bonus: <span
id="safeValue">0.05</span></label>
<input type="range" id="safeSlider" min="0" max="0.25" step="0.01" value="0.05"
style="width: 100%; cursor: pointer;" oninput="updateReward('safe', this.value)">
</div>
<div style="margin-top: 5px;">
<label style="color: #ffaa00; font-size: 12px;">📚 Stack: <span id="stackValue">0.05</span></label>
<input type="range" id="stackSlider" min="0" max="0.1" step="0.001" value="0.05"
style="width: 100%; cursor: pointer;" oninput="updateReward('stack', this.value)">
<div style="font-size: 10px; color: #666;">Reward for tokens sharing a square</div>
</div>
</div>
</div>
<!-- MCTS Settings Panel -->
<div id="mctsSettingsPanel" style="margin-top: 15px; padding: 10px; background: #1a1a2e; border-radius: 8px;">
<h3 style="margin: 0 0 10px 0; color: #aaa; font-size: 14px;">⚡ MCTS Optimization</h3>
<div style="font-size: 11px; color: #888; margin-bottom: 8px;">Speeds up training without quality loss
</div>
<div style="display: grid; gap: 10px;">
<div style="margin-top: 5px;">
<label style="color: #00ccff; font-size: 12px;">⚡ Early Term: <span
id="earlyTermValue">90%</span></label>
<input type="range" id="earlyTermSlider" min="0.5" max="1.0" step="0.05" value="0.9"
style="width: 100%; cursor: pointer;"
oninput="updateMCTS('early_termination_threshold', this.value)">
<div style="font-size: 10px; color: #666;">Stop MCTS if one move has this % of visits</div>
</div>
</div>
</div>
</div>
</div>
<script>
// Board layout constants
const BOARD_SIZE = 15;
const BASE_POS = -1;
const HOME_POS = 99;
// Path coordinates for Player 0
const PATH_COORDS_P0 = [
[6, 1], [6, 2], [6, 3], [6, 4], [6, 5],
[5, 6], [4, 6], [3, 6], [2, 6], [1, 6], [0, 6],
[0, 7], [0, 8],
[1, 8], [2, 8], [3, 8], [4, 8], [5, 8],
[6, 9], [6, 10], [6, 11], [6, 12], [6, 13], [6, 14],
[7, 14], [8, 14],
[8, 13], [8, 12], [8, 11], [8, 10], [8, 9],
[9, 8], [10, 8], [11, 8], [12, 8], [13, 8], [14, 8],
[14, 7], [14, 6],
[13, 6], [12, 6], [11, 6], [10, 6], [9, 6],
[8, 5], [8, 4], [8, 3], [8, 2], [8, 1], [8, 0],
[7, 0] // 50 (End)
];
const HOME_RUN_P0 = [[7, 1], [7, 2], [7, 3], [7, 4], [7, 5]];
const HOME_COORD_P0 = [7, 6];
const BASE_COORDS = [
[[2, 2], [2, 3], [3, 2], [3, 3]], // P0 (Red - TL)
[[2, 11], [2, 12], [3, 11], [3, 12]], // P1 (Green - TR)
[[11, 11], [11, 12], [12, 11], [12, 12]], // P2 (Yellow - BR)
[[11, 2], [11, 3], [12, 2], [12, 3]] // P3 (Blue - BL)
];
const SAFE_INDICES = new Set([0, 8, 13, 21, 26, 34, 39, 47]);
let ws = null;
let lastState = null;
let debugPathMode = false;
let selectedGameId = 0;
let batchSize = 32; // Default, updated by server
// Init Selector (Placeholder, populated by server event)
function initSelector(size) {
batchSize = size;
const sel = document.getElementById('gameSelector');
sel.innerHTML = '';
for (let i = 0; i < size; i++) {
const opt = document.createElement('option');
opt.value = i;
opt.textContent = `Game ${i}`;
opt.id = `opt-game-${i}`;
sel.appendChild(opt);
}
// Restore selection if valid
if (selectedGameId < size) {
sel.value = selectedGameId;
} else {
sel.value = 0;
selectedGameId = 0;
}
}
// Initial default population
(function () {
initSelector(32);
})();
function changeGame() {
const sel = document.getElementById('gameSelector');
selectedGameId = parseInt(sel.value);
gameFinished = false; // Reset pause flag
// Remove any finish overlays
const overlay = document.getElementById('finishOverlay');
if (overlay) overlay.remove();
log(`Switched to Game ${selectedGameId}`);
updatePauseButton(); // Update button state for new game
requestState();
}
let pausedGames = new Set();
function togglePause() {
const btn = document.getElementById('pauseBtn');
const isPaused = pausedGames.has(selectedGameId);
if (isPaused) {
// Resume
send({ type: 'resume_game', game_id: selectedGameId });
pausedGames.delete(selectedGameId);
} else {
// Pause
send({ type: 'pause_game', game_id: selectedGameId });
pausedGames.add(selectedGameId);
}
updatePauseButton();
}
function updatePauseButton() {
const btn = document.getElementById('pauseBtn');
if (pausedGames.has(selectedGameId)) {
btn.textContent = "Resume";
btn.style.background = "linear-gradient(135deg, #00ff88 0%, #00cc66 100%)";
} else {
btn.textContent = "Pause";
btn.style.background = "linear-gradient(135deg, #ff8800 0%, #ff6600 100%)";
}
}
// Auto-resume all when tab hidden/visible? No, explicitly on dashboard switch.
// Actually user said: "as soon as we go to dashboard, all the games will resume"
// Since this is a SPA, we don't have separate "dashboard page".
// But we are in the "Game UI". Where is the "Dashboard"?
// Ah, the user might mean switching away from game specific view?
// Wait, the UI shows Board + Stats side-by-side. It is ONE dashboard.
// Maybe "go to dashboard" means "unselect specific game"?
// Or user implies there's a different view?
// Re-reading: "in each game UI ... add a pause game button ... as soon as we go to dashboard, all the games will resume"
// This visualizer IS the dashboard.
// Maybe the user thinks of "Game UI" as maximizing one game?
// But currently it's all in one view.
// Let's assume "Dashboard" means selecting "Game 0" (or similar) vs "Dashboard mode"?
// No, maybe I should add a "Resume All" button or "Back to Overview" which triggers it.
// BUT, the requirements say "since I can analyse it if needed... note that iteration will not finish... even if I set one paused, if I go to dashboard, then it should resume."
// I will interpret "Go to Dashboard" as maybe refreshing the page or a specific action?
// Actually, let's just ensure if the user CLOSES the browser or reloads, it resumes.
// Or add a "Resume All" button explicitly.
// Wait, if I Pause Game 5, and then switch dropdown to Game 6, should Game 5 resume?
// "if I go to dashboard, then it should resume" -> This implies leaving the "Game UI".
// I'll add a "Resume All" button next to pause for safety.
// AND, whenever 'initSelector' is called (re-connect), maybe resume?
// Let's just implement the button logic correctly first.
function updateGameStatus(gameId, winner) {
const opt = document.getElementById(`opt-game-${gameId}`);
if (opt) {
// Add Checkmark if not already present
if (!opt.textContent.includes('✅')) {
opt.textContent = `Game ${gameId} ✅ (P${winner})`;
}
}
// Update stats
updateBatchProgress();
}
let completedGamesCount = 0;
let ghostGameIds = new Set(); // Track which games have ghost players
function updateBatchProgress() {
const statEl = document.getElementById('batchStats');
if (statEl) {
statEl.textContent = `${completedGamesCount} / ${batchSize}`;
}
}
let batchStartTime = Date.now();
let gameStartTimes = {};
let lastIterTime = 0;
function updateTimeStats() {
// Update Current Game Time
const curTimeEl = document.getElementById('statCurGameTime');
if (curTimeEl && selectedGameId !== -1 && !pausedGames.has(selectedGameId)) {
const start = gameStartTimes[selectedGameId] || Date.now();
// Ensure start is in ms. If server sent seconds (float), convert.
const startMs = (start < 10000000000) ? start * 1000 : start;
const elapsed = (Date.now() - startMs) / 1000;
curTimeEl.textContent = elapsed.toFixed(1) + 's';
}
// Periodically request update? No, just run on interval
}
setInterval(updateTimeStats, 100); // Update 100ms for smooth timer
function markGhostGames(gameIds) {
ghostGameIds = new Set(gameIds);
// Update dropdown items with ghost icon
for (const gid of gameIds) {
const opt = document.getElementById(`opt-game-${gid}`);
if (opt && !opt.textContent.includes('👻')) {
opt.textContent = `Game ${gid} 👻`;
}
}
}
function markHeuristicGames(gameIds) {
// Update dropdown items with Heuristic icon
for (const gid of gameIds) {
const opt = document.getElementById(`opt-game-${gid}`);
if (opt && !opt.textContent.includes('🤖')) {
opt.textContent = `Game ${gid} 🤖`;
}
}
}
function toggleDebugPath() {
debugPathMode = !debugPathMode;
initBoard(); // Re-draw board text
if (lastState) renderState(lastState); // Re-render tokens
}
// Session tracking
let sessionWins = [0, 0, 0, 0];
let currentGameId = -1;
let lastRecordedGameId = -2;
function updateProgress(state) {
const container = document.getElementById('progressBars');
if (!container) return;
container.innerHTML = '';
const colors = ['#ff4444', '#00ff00', '#ffff00', '#4444ff'];
const labels = ['P0', 'P1', 'P2', 'P3'];
for (let p = 0; p < 4; p++) {
let totalSteps = 0;
let tokensHome = state.scores[p];
totalSteps += tokensHome * 57;
for (let t = 0; t < 4; t++) {
const pos = state.player_positions[p][t];
if (pos !== -1 && pos !== 99) {
totalSteps += (pos + 1);
}
}
const maxSteps = 4 * 57;
const progress = Math.min(100, Math.round((totalSteps / maxSteps) * 100));
const row = document.createElement('div');
row.style.marginBottom = '4px';
row.innerHTML = `
<div style="display: flex; justify-content: space-between; margin-bottom: 2px;">
<span style="color: ${colors[p]}">${labels[p]}</span>
<span>${progress}%</span>
</div>
<div style="width: 100%; height: 4px; background: #333; border-radius: 2px;">
<div style="width: ${progress}%; background: ${colors[p]}; height: 100%; border-radius: 2px; transition: width 0.3s;"></div>
</div>
`;
container.appendChild(row);
}
}
function checkWinner(state) {
if (currentGameId === -1) return;
if (currentGameId === lastRecordedGameId) return;
for (let p = 0; p < 4; p++) {
if (state.scores[p] >= 4) {
sessionWins[p]++;
document.getElementById(`win${p}`).textContent = sessionWins[p];
lastRecordedGameId = currentGameId;
log(`Game ${currentGameId} Winner: P${p}`);
break;
}
}
}
function formatTime(seconds) {
if (!seconds) return '0s';
const h = Math.floor(seconds / 3600);
const m = Math.floor((seconds % 3600) / 60);
const s = Math.floor(seconds % 60);
if (h > 0) return `${h}h ${m}m`;
if (m > 0) return `${m}m ${s}s`;
return `${s}s`;
}
function rotateCoord(r, c, times) {
for (let i = 0; i < times; i++) {
[r, c] = [c, 14 - r];
}
return [r, c];
}
function getBoardCoords(player, pos, tokenIdx) {
let localR, localC;
if (pos === -1 || pos === BASE_POS) {
// Base
[localR, localC] = BASE_COORDS[player][tokenIdx];
return [localR, localC]; // Base coords are already absolute in BASE_COORDS?
// Wait, BASE_COORDS are defined as global absolute coords?
// Let's check BASE_COORDS definition.
}
if (pos === HOME_POS) {
[localR, localC] = HOME_COORD_P0;
} else if (pos > 50) {
const idx = pos - 51;
if (idx < 5) {
[localR, localC] = HOME_RUN_P0[idx];
} else {
[localR, localC] = HOME_COORD_P0;
}
} else {
[localR, localC] = PATH_COORDS_P0[pos];
}
return rotateCoord(localR, localC, player);
}
function initBoard() {
const board = document.getElementById('board');
board.innerHTML = '';
for (let r = 0; r < BOARD_SIZE; r++) {
for (let c = 0; c < BOARD_SIZE; c++) {
const cell = document.createElement('div');
cell.className = 'cell';
cell.id = `cell-${r}-${c}`;
cell.dataset.row = r;
cell.dataset.col = c;
board.appendChild(cell);
}
}
// Mark path cells
for (let p = 0; p < 4; p++) {
// Main Path
for (let i = 0; i < PATH_COORDS_P0.length; i++) {
const [r, c] = rotateCoord(PATH_COORDS_P0[i][0], PATH_COORDS_P0[i][1], p);
const cell = document.getElementById(`cell-${r}-${c}`);
if (cell) {
cell.classList.add('path');
if (SAFE_INDICES.has(i)) {
cell.classList.add('safe');
}
// DEBUG: Write numbers for P0 only (Red)
if (debugPathMode && p === 0) {
cell.textContent = i;
cell.style.color = '#fff';
cell.style.fontSize = '10px';
}
}
}
// Home run
for (let i = 0; i < 5; i++) {
const [r, c] = rotateCoord(HOME_RUN_P0[i][0], HOME_RUN_P0[i][1], p);
const cell = document.getElementById(`cell-${r}-${c}`);
if (cell) {
cell.classList.add(`home-p${p}`);
// DEBUG: Write numbers for P0 Home Run
if (debugPathMode && p === 0) {
cell.textContent = 51 + i;
cell.style.color = '#fff';
cell.style.fontSize = '10px';
}
}
}
// Base
for (let t = 0; t < 4; t++) {
const [r, c] = BASE_COORDS[p][t];
const cell = document.getElementById(`cell-${r}-${c}`);
if (cell) cell.classList.add(`base-p${p}`);
}
}
}
function renderState(state) {
lastState = state;
updateProgress(state);
checkWinner(state);
// Clear tokens
document.querySelectorAll('.token').forEach(t => t.remove());
// Place tokens
for (let p = 0; p < 4; p++) {
for (let t = 0; t < 4; t++) {
try {
const pos = state.player_positions[p][t];
if (pos === HOME_POS) continue;
const coords = getBoardCoords(p, pos, t);
if (!coords) {
log(`Error: No coords for P${p} T${t} Pos${pos}`);
continue;
}
const [r, c] = coords;
const cell = document.getElementById(`cell-${r}-${c}`);
if (cell) {
const token = document.createElement('div');
token.className = `token token-p${p}`;
cell.appendChild(token);
} else {
// Log missing cell
// log(`Missing cell: ${r}-${c} for P${p} T${t}`);
}
} catch (e) {
log(`Render Error P${p} T${t}: ${e.message}`);
console.error(e);
}
}
}
// Update scores
for (let p = 0; p < 4; p++) {
document.getElementById(`score${p}`).textContent = `${state.scores[p]}/4`;
}
// Update current player
document.querySelectorAll('.player-info').forEach(el => el.classList.remove('active'));
document.getElementById(`player${state.current_player}`).classList.add('active');
// Update dice
if (state.current_dice_roll > 0) {
document.getElementById('diceValue').textContent = state.current_dice_roll;
} else if (state.current_dice_roll === 0 && document.getElementById('diceValue').textContent === '-') {
// Only set to 0/- if currently empty
}
}
function log(msg) {
const logEl = document.getElementById('gameLog');
logEl.innerHTML = `${new Date().toLocaleTimeString()}: ${msg}<br>` + logEl.innerHTML;
}
function renderDiceStats(stats) {
const tbody = document.getElementById('diceStatsBody');
tbody.innerHTML = '';
const playerNames = ['P0 (Red)', 'P1 (Grn)', 'P2 (Yel)', 'P3 (Blu)'];
const playerColors = ['#ff4444', '#00ff00', '#ffff00', '#4444ff'];
for (let p = 0; p < 4; p++) {
const tr = document.createElement('tr');
tr.style.borderBottom = '1px solid #333';
// Player name cell
const tdName = document.createElement('td');
tdName.textContent = playerNames[p];
tdName.style.padding = '4px';
tdName.style.color = playerColors[p];
tdName.style.fontWeight = 'bold';
tr.appendChild(tdName);
// Dice counts (indices 1-6)
for (let d = 1; d <= 6; d++) {
const td = document.createElement('td');
td.textContent = stats[p][d];
tr.appendChild(td);
}
tbody.appendChild(tr);
}
}
function renderChart(history) {
// ... (existing chart logic) ...
const canvas = document.getElementById('trainingChart');
if (!canvas) return;
const ctx = canvas.getContext('2d');
const w = canvas.width;
const h = canvas.height;
// Clear
ctx.fillStyle = '#1a1a2e';
ctx.fillRect(0, 0, w, h);
if (history.length < 2) return;
// Padding
const p = 20;
const graphW = w - 2 * p;
const graphH = h - 2 * p;
// Scaling
const maxIter = history[history.length - 1].iteration;
const minIter = history[0].iteration;
const iterRange = maxIter - minIter || 1;
// Loss Y-Axis (Left) - Orange
// Find max loss for scaling
let maxLoss = 0;
history.forEach(h => maxLoss = Math.max(maxLoss, h.loss));
maxLoss = Math.ceil(maxLoss * 1.2 * 10) / 10; // add headroom