File size: 31,851 Bytes
96027bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { configState } from './js/core/ConfigState.js';
import { SettingsModal } from './js/components/SettingsModal.js';
import { AreaPopovers } from './js/components/AreaPopovers.js';
import { dashboardStats } from './js/core/DashboardStats.js';
import { simulationClient } from './js/core/SimulationClient.js';
import { analyticsModal } from './js/ui/AnalyticsModal.js';
import { tableRenderer } from './js/ui/TableRenderer.js';
import { staffRenderer } from './js/ui/StaffRenderer.js';
import { customerRenderer } from './js/ui/CustomerRenderer.js';

// Initialize UI Components
const settingsModal = new SettingsModal();
const areaPopovers = new AreaPopovers();

// Listen to configuration updates to save state
configState.addEventListener('updated', (e) => {
    localStorage.setItem('catsySimConfig', JSON.stringify(e.detail));
    
    const toast = document.getElementById('toast');
    if (toast) {
        toast.style.opacity = '1';
        clearTimeout(toast.timeoutId);
        toast.timeoutId = setTimeout(() => {
            toast.style.opacity = '0';
        }, 3000);
    }
    
    // We intentionally do NOT auto-restart startSimulation() here.
    // The user must manually click 'Restart' to apply new settings,
    // to avoid wiping the active simulation UI abruptly when tweaking settings.
});

document.addEventListener('DOMContentLoaded', () => {
    loadConfigFromStorage();
});

function loadConfigFromStorage() {
    const saved = localStorage.getItem('catsySimConfig');
    if (!saved) return;
    try {
        const config = JSON.parse(saved);
        
        const directMap = {
            'cashiers': 'cfg-cashiers', 'baristas': 'cfg-baristas', 'tables': 'cfg-tables',
            'resArrivalMin': 'cfg-res-arrival-min', 'resArrivalMax': 'cfg-res-arrival-max',
            'arrival': 'cfg-arrival', 'duration': 'cfg-duration', 'warmupTime': 'cfg-warmup',
            'balkThreshold': 'cfg-balk-threshold', 'maxStrikes': 'cfg-max-strikes',
            'decideMin': 'cfg-decide-min', 'decideMax': 'cfg-decide-max',
            'payMin': 'cfg-pay-min', 'payMax': 'cfg-pay-max',
            'prepMin': 'cfg-prep-min', 'prepMax': 'cfg-prep-max',
            'dwellMin': 'cfg-dwell-min', 'dwellMax': 'cfg-dwell-max',
            'replications': 'cfg-replications', 'shiftHours': 'cfg-shift-hours'
        };
        
        const probMap = {
            'takeoutProb': 'cfg-takeout-prob', 'resProb': 'cfg-res-prob',
            'balkProb': 'cfg-balk-prob', 'renegeProb': 'cfg-renege-prob'
        };
        
        for (const [key, domId] of Object.entries(directMap)) {
            if (config[key] !== undefined) {
                const el = document.getElementById(domId);
                if (el) el.value = config[key];
            }
        }
        
        for (const [key, domId] of Object.entries(probMap)) {
            if (config[key] !== undefined) {
                const el = document.getElementById(domId);
                if (el) el.value = config[key] * 100; // Convert back to percentage
            }
        }
        
        // Also update the scenario select box if it matches an arrival value
        const scenarioSelect = document.getElementById('cfg-scenario');
        if (scenarioSelect && config.arrival) {
            let match = false;
            for (let option of scenarioSelect.options) {
                if (option.value == config.arrival && option.value !== 'custom') {
                    scenarioSelect.value = option.value;
                    match = true;
                    break;
                }
            }
            if (!match) scenarioSelect.value = 'custom';
        }
        
        // Update the span displays for quick controls
        const dispCashiers = document.getElementById('disp-cashiers');
        if (dispCashiers && config.cashiers) dispCashiers.innerText = config.cashiers;
        const dispBaristas = document.getElementById('disp-baristas');
        if (dispBaristas && config.baristas) dispBaristas.innerText = config.baristas;
        const dispTables = document.getElementById('disp-tables');
        if (dispTables && config.tables) dispTables.innerText = config.tables;
        
    } catch (e) {
        console.error('Failed to parse saved config', e);
    }
}
configState.refreshFromDOM();

const btnStart = document.getElementById('btn-start');
const btnStop = document.getElementById('btn-stop');
const btnRestart = document.getElementById('btn-restart');
const statusDot = document.getElementById('connection-status');
const statusText = document.getElementById('status-text');
const floatingLayer = document.getElementById('floating-layer');

let customerTableMap = new Map(); // Maps customer ID to table element ID
let customerFrustrationMap = new Map(); // Maps customer ID to frustration level (0-3)

let cashierQueueLens = []; // Array of lengths per cashier
let waitingAreaLen = 0;
let pickupAreaLen = 0;
let totalCustomers = 0;
let servedCustomers = 0;
let lostCustomers = 0;
let totalDineIn = 0;
let totalTakeout = 0;
let totalReservations = 0;
let missedReservations = 0;
const cashierOffsets = new Map();
const customerCashierMap = new Map();
const waitingAreaOffsets = new Map();
const pickupAreaOffsets = new Map();
const customerBaristaMap = new Map();
let availableTables = [];
let availableResTables = [];
let cashierTimers = {};
let baristaTimers = {};

function enforceBounds() {
    const cashiersEl = document.getElementById('cfg-cashiers');
    if (cashiersEl) {
        let cVal = parseInt(cashiersEl.value);
        if (isNaN(cVal) || cVal < 1) cashiersEl.value = 1;
        else if (cVal > 10) cashiersEl.value = 10;
    }
    
    const baristasEl = document.getElementById('cfg-baristas');
    if (baristasEl) {
        let bVal = parseInt(baristasEl.value);
        if (isNaN(bVal) || bVal < 1) baristasEl.value = 1;
        else if (bVal > 15) baristasEl.value = 15;
    }
}

function startSimulation() {
    enforceBounds();
    
    // Ensure the backend isn't stuck in a paused state (e.g. if the user hit restart while the settings modal was open)
    fetch('/api/resume', { method: 'POST' }).catch(e => console.error(e));
    simulationClient.resume(); // also ensure frontend is unpaused
    
    simulationClient.connect();
}

window.addEventListener('sim:connected', () => {
    if (statusDot) statusDot.className = 'dot green';
    if (statusText) statusText.innerText = 'Connected';
    if (btnStart) {
        btnStart.disabled = false;
        btnStart.innerText = '⏸ Pause';
        btnStart.classList.add('playing');
    }
    if (btnStop) btnStop.disabled = false;
    if (btnRestart) btnRestart.disabled = false;
    
    const warmupTime = configState.getConfig().warmupTime || 0;
    const warmupIndicator = document.getElementById('warmup-indicator');
    if (warmupIndicator) {
        if (warmupTime > 0) {
            warmupIndicator.style.display = 'inline-block';
        } else {
            warmupIndicator.style.display = 'none';
        }
    }
    resetSimulation();
});

window.addEventListener('sim:disconnected', () => {
    if (statusDot) statusDot.className = 'status-indicator__dot status-indicator__dot--disconnected';
    if (statusText) statusText.innerText = 'Disconnected';
    if (btnStart) {
        btnStart.disabled = false;
        btnStart.innerText = '▶ Play';
        btnStart.classList.remove('playing');
    }
    if (btnStop) btnStop.disabled = true;
    if (btnRestart) btnRestart.disabled = true;
    
    // Stop all visual timers immediately
    for (const key in cashierTimers) {
        if (cashierTimers[key] && cashierTimers[key].interval) {
            clearInterval(cashierTimers[key].interval);
        }
    }
    for (const key in baristaTimers) {
        if (baristaTimers[key] && baristaTimers[key].interval) {
            clearInterval(baristaTimers[key].interval);
        }
    }
    staffRenderer.clearAllTimers();
});

window.addEventListener('sim:event', (e) => {
    handleEvent(e.detail);
});

btnStart.addEventListener('click', () => {
    // If not connected, start the simulation
    if (!simulationClient.socket || simulationClient.socket.readyState !== WebSocket.OPEN) {
        startSimulation();
        return;
    }
    
    // If already connected, toggle pause/resume
    if (simulationClient.isPaused) {
        // Resume
        simulationClient.resume();
        fetch('/api/resume', { method: 'POST' }).catch(e => console.error(e));
        btnStart.innerText = '⏸ Pause';
        btnStart.classList.add('playing');
    } else {
        // Pause
        simulationClient.pause();
        fetch('/api/pause', { method: 'POST' }).catch(e => console.error(e));
        btnStart.innerText = '▶ Resume';
        btnStart.classList.remove('playing');
    }
});

btnStop.addEventListener('click', () => {
    simulationClient.disconnect();
    btnStart.disabled = false;
    btnStop.disabled = true;
    if (btnRestart) btnRestart.disabled = true;
});

if (btnRestart) {
    btnRestart.addEventListener('click', () => {
        simulationClient.disconnect();
        // Slight delay to ensure the disconnect processes completely before reconnecting
        setTimeout(() => {
            startSimulation();
        }, 100);
    });
}



function resetSimulation() {
    floatingLayer.innerHTML = '';
    cashierOffsets.clear();
    customerCashierMap.clear();
    waitingAreaOffsets.clear();
    pickupAreaOffsets.clear();
    customerBaristaMap.clear();
    // Clear all manual intervals
    for (const key in cashierTimers) {
        if (cashierTimers[key] && cashierTimers[key].interval) {
            clearInterval(cashierTimers[key].interval);
        }
    }
    cashierTimers = {};
    
    for (const key in baristaTimers) {
        if (baristaTimers[key] && baristaTimers[key].interval) {
            clearInterval(baristaTimers[key].interval);
        }
    }
    baristaTimers = {};

    staffRenderer.clearAllTimers();
    customerRenderer.clearAll();
    customerTableMap.clear();
    customerFrustrationMap.clear();
    totalCustomers = 0;
    servedCustomers = 0;
    lostCustomers = 0;
    totalDineIn = 0;
    totalTakeout = 0;
    cashierQueueLens = [];
    waitingAreaLen = 0;
    pickupAreaLen = 0;
    
    // Reset Dashboard UI values
    const paceEl = document.getElementById('current-pace');
    if (paceEl) paceEl.innerText = 'Quiet 💤';
    
    document.getElementById('sim-time').innerText = '0.0s';
    document.getElementById('total-customers').innerText = '0';
    document.getElementById('served-customers').innerText = '0';
    document.getElementById('lost-customers').innerText = '0';
    document.getElementById('cashier-queue-len').innerText = '0';
    document.getElementById('waiting-area-len').innerText = '0';
    
    // Dynamically draw cashiers
    const cashiersCount = parseInt(document.getElementById('cfg-cashiers').value);
    staffRenderer.drawCashiers(cashiersCount);
    
    // Initialize queue lengths array
    cashierQueueLens = Array(cashiersCount).fill(0);

    // Dynamically draw baristas
    const baristasCount = parseInt(document.getElementById('cfg-baristas').value);
    staffRenderer.drawBaristas(baristasCount);
    
    // Dynamically draw tables
    const tablesCount = parseInt(document.getElementById('cfg-tables').value);
    tableRenderer.draw(tablesCount);
}




function updateStats(time) {
    document.getElementById('sim-time').innerText = time.toFixed(1) + 's';
    document.getElementById('total-customers').innerText = totalCustomers;
    document.getElementById('served-customers').innerText = servedCustomers;
    document.getElementById('lost-customers').innerText = lostCustomers;
    document.getElementById('total-dine-in').innerText = totalDineIn;
    document.getElementById('total-takeout').innerText = totalTakeout;
    
    // Sum all individual cashier queues
    const totalCashierQueue = cashierQueueLens.reduce((a, b) => a + b, 0);
    document.getElementById('cashier-queue-len').innerText = totalCashierQueue;
    
    document.getElementById('waiting-area-len').innerText = waitingAreaLen + pickupAreaLen;
}


function handleEvent(data) {
    if (tableRenderer && data.time) {
        tableRenderer.tickTimers(data.time);
    }
    if (data.event === 'warmup_complete') {
        document.getElementById('warmup-indicator').style.display = 'none';
        totalCustomers = 0;
        servedCustomers = 0;
        lostCustomers = 0;
        totalDineIn = 0;
        totalTakeout = 0;
        totalReservations = 0;
        missedReservations = 0;
        document.getElementById('total-customers').innerText = totalCustomers;
        document.getElementById('served-customers').innerText = servedCustomers;
        document.getElementById('lost-customers').innerText = lostCustomers;
        document.getElementById('total-dine-in').innerText = totalDineIn;
        document.getElementById('total-takeout').innerText = totalTakeout;
        const totalResEl = document.getElementById('total-reservations');
        if (totalResEl) totalResEl.innerText = totalReservations;
        const missedResEl = document.getElementById('missed-reservations');
        if (missedResEl) missedResEl.innerText = missedReservations;
        return;
    }
    if (data.event === 'pace_change') {
        const paceEl = document.getElementById('current-pace');
        if (paceEl) {
            const pace = data.pace;
            let color = '#3498db'; // Steady
            let text = 'Steady ☕';
            if (pace === 'Peak') {
                color = '#e74c3c';
                text = 'Peak 🔥';
            } else if (pace === 'Quiet') {
                color = '#9b59b6';
                text = 'Quiet 💤';
            }
            paceEl.style.color = color;
            paceEl.innerText = text;
        }
        return;
    }
    
    const id = data.customer_id;
    
    switch(data.event) {
        case 'arrive':
            totalCustomers++;
            document.getElementById('total-customers').innerText = totalCustomers;
            const numId = parseInt(id.replace('Cust-', '')) || 0;
            customerRenderer.moveCustomer(id, 'entrance', numId);
            break;
            
        case 'balking_start':
            const balkStartEl = customerRenderer.customers.get(id);
            if (balkStartEl) balkStartEl.classList.add('customer--walkout');
            customerRenderer.incrementFrustration(id, 4);
            break;
            
        case 'balk_leave':
            lostCustomers++;
            const balkEl = customerRenderer.customers.get(id);
            if (balkEl) {
                const badge = balkEl.querySelector('.customer__badge');
                if (badge) {
                    badge.innerText = '🙅';
                    badge.style.display = 'flex';
                }
                // Storm out from entrance
                balkEl.classList.add('customer--walkout');
                balkEl.style.transition = 'transform 3.5s ease-in, opacity 3.5s ease-in';
                balkEl.style.transform += ' translate(-80px, 200px)';
                balkEl.style.opacity = '0';
                setTimeout(() => {
                    customerRenderer.removeCustomer(id);
                }, 3500);
            } else {
                customerRenderer.removeCustomer(id);
            }
            break;
            
        case 'frustrated_waiting':
            customerRenderer.incrementFrustration(id, 1);
            break;
            
        case 'patience_warning':
            customerRenderer.incrementFrustration(id, 2);
            break;
            
        case 'prep_waiting_frustration':
            customerRenderer.incrementFrustration(id, 3);
            break;
            
        case 'renege_leave':
            lostCustomers++; // Count them as a lost walkout
            
            // Remove them from the queue tracking
            const cIdxToLeave = data.cashier_index;
            if (isNaN(cashierQueueLens[cIdxToLeave]) || cashierQueueLens[cIdxToLeave] === undefined) { cashierQueueLens[cIdxToLeave] = 0; }
            cashierQueueLens[cIdxToLeave] = Math.max(0, cashierQueueLens[cIdxToLeave] - 1);
            
            const leavingOffset = cashierOffsets.get(id);
            
            // Advance everyone behind them forward
            for (let [custId, cIdx] of customerCashierMap.entries()) {
                if (cIdx === cIdxToLeave && custId !== id) {
                    let currentOffset = cashierOffsets.get(custId);
                    if (currentOffset !== undefined && currentOffset > leavingOffset) {
                        let newOffset = currentOffset - 1;
                        cashierOffsets.set(custId, newOffset);
                        customerRenderer.moveCustomer(custId, `cashier-queue-${cIdxToLeave}`, newOffset);
                    }
                }
            }
            
            // Remove from maps
            cashierOffsets.delete(id);
            customerCashierMap.delete(id);
            
            // Animate them storming out in frustration
            const renegeEl = customerRenderer.customers.get(id);
            if (renegeEl) {
                renegeEl.classList.add('customer--walkout');
                const badge = renegeEl.querySelector('.customer__badge');
                if (badge) {
                    badge.innerText = '🏃';
                    badge.animate([
                        { transform: 'scale(1)' },
                        { transform: 'scale(1.5)' },
                        { transform: 'scale(1)' }
                    ], {
                        duration: 600,
                        iterations: Infinity,
                        easing: 'ease-in-out'
                    });
                }
                
                // Instantly step OUT of the queue so the person behind them doesn't walk through them
                renegeEl.style.transform += ' translate(0px, 40px)';
                
                // Wait for 2 seconds so the user can clearly see them get frustrated
                setTimeout(() => {
                    // Then they storm out completely
                    renegeEl.style.transition = 'transform 3.5s ease-in, opacity 3.5s ease-in';
                    renegeEl.style.transform += ' translate(-150px, 200px)';
                    renegeEl.style.opacity = '0';
                    setTimeout(() => {
                        if (renegeEl.parentNode) renegeEl.parentNode.removeChild(renegeEl);
                        customerRenderer.customers.delete(id);
                    }, 3500);
                }, 2000);
            }
            break;
            
        case 'vip_checkin':
            const vipCashierIdx = data.cashier_index !== undefined ? data.cashier_index : 0;
            customerRenderer.moveCustomer(id, `vip-checkin-${vipCashierIdx}`);
            customerRenderer.markAsVIP(id);
            // Small timeout to let the movement start before triggering the animation
            setTimeout(() => {
                customerRenderer.triggerVIPAnimation(id);
            }, 300);
            break;

        case 'queue_cashier':
            const cIdx = data.cashier_index;
            customerCashierMap.set(id, cIdx);
            
            if (isNaN(cashierQueueLens[cIdx]) || cashierQueueLens[cIdx] === undefined) { cashierQueueLens[cIdx] = 0; }
            cashierQueueLens[cIdx]++;
            cashierOffsets.set(id, cashierQueueLens[cIdx] - 1);
            customerRenderer.moveCustomer(id, `cashier-queue-${cIdx}`, cashierQueueLens[cIdx] - 1);
            break;
            
        case 'start_deciding':
            const decIdx = data.cashier_index;
            if (isNaN(cashierQueueLens[decIdx]) || cashierQueueLens[decIdx] === undefined) { cashierQueueLens[decIdx] = 0; }
            cashierQueueLens[decIdx] = Math.max(0, cashierQueueLens[decIdx] - 1);
            
            customerRenderer.moveCustomer(id, `cashier-queue-${decIdx}`, -1);
            
            const decEl = customerRenderer.customers.get(id);
            if (decEl) {
                const badge = decEl.querySelector('.customer__badge');
                if (badge) {
                    badge.innerText = '💭';
                    badge.style.display = 'flex';
                }
            }
            
            // Start the cashier countdown timer!
            if (data.duration) {
                if (cashierTimers[decIdx]) clearInterval(cashierTimers[decIdx].interval);
                
                let elapsed = 0;
                const totalDuration = data.duration;
                const speedFactor = 0.5; // Backend RealtimeEnvironment factor
                const realTickMs = 100;
                const simTickSec = (realTickMs / 1000) / speedFactor;
                
                const timerEl = document.getElementById(`timer-cashier-${decIdx}`);
                if (timerEl) {
                    timerEl.innerText = '0.0s';
                    timerEl.classList.remove('idle');
                }
                
                const interval = setInterval(() => {
                    if (simulationClient && simulationClient.isPaused) return;
                    
                    elapsed += simTickSec;
                    if (elapsed >= totalDuration) {
                        elapsed = totalDuration;
                        clearInterval(interval);
                        // Freeze on final time until customer walks away
                        if (timerEl) timerEl.innerText = elapsed.toFixed(1) + 's';
                    } else {
                        if (timerEl) timerEl.innerText = elapsed.toFixed(1) + 's';
                    }
                }, realTickMs);
                cashierTimers[decIdx] = { interval };
            }
            
            // Advance everyone else in this specific queue forward by 1 spot
            for (let [custId, cIdx] of customerCashierMap.entries()) {
                if (cIdx === decIdx && custId !== id) {
                    let currentOffset = cashierOffsets.get(custId);
                    if (currentOffset !== undefined && currentOffset > 0) {
                        let newOffset = currentOffset - 1;
                        cashierOffsets.set(custId, newOffset);
                        customerRenderer.moveCustomer(custId, `cashier-queue-${decIdx}`, newOffset);
                    }
                }
            }
            break;
            
        case 'start_paying':
            // Customer is paying, no physical move needed, just logical state
            const payingCIdx = customerCashierMap.get(id);
            if (payingCIdx !== undefined) {
                const cAnim = document.getElementById(`working-anim-cashier-${payingCIdx}`);
                if (cAnim) cAnim.style.display = 'block';
            }
            
            const payEl = customerRenderer.customers.get(id);
            if (payEl) {
                const badge = payEl.querySelector('.customer__badge');
                if (badge) {
                    badge.innerText = '💳';
                    badge.style.display = 'flex';
                }
            }
            break;
            
        case 'waiting_pickup':
            totalTakeout++;
            document.getElementById('total-takeout').innerText = totalTakeout;
            // Go to waiting/pickup area if tables are full, or if waiting for takeout
            pickupAreaLen++;
            pickupAreaOffsets.set(id, pickupAreaLen - 1);
            customerRenderer.moveCustomer(id, 'pickup-area', pickupAreaLen - 1);
            customerRenderer.updateFrustrationVisuals(id);
            
            // Customer left the cashier, reset the cashier timer
            const leftCIdxTakeout = customerCashierMap.get(id);
            if (leftCIdxTakeout !== undefined && cashierTimers[leftCIdxTakeout]) {
                clearInterval(cashierTimers[leftCIdxTakeout].interval);
                const timerEl = document.getElementById(`timer-cashier-${leftCIdxTakeout}`);
                if (timerEl) {
                    timerEl.innerText = 'Idle';
                    timerEl.classList.add('idle');
                }
                const cAnim = document.getElementById(`working-anim-cashier-${leftCIdxTakeout}`);
                if (cAnim) cAnim.style.display = 'none';
                showPopUpIcon(`cashier-staff-${leftCIdxTakeout}`, '💰');
            }
            break;
            
        case 'waiting_table':
            totalDineIn++;
            document.getElementById('total-dine-in').innerText = totalDineIn;
            // Go to waiting/pickup area if tables are full, or if waiting for takeout
            waitingAreaLen++;
            waitingAreaOffsets.set(id, waitingAreaLen - 1);
            customerRenderer.moveCustomer(id, 'waiting-area', waitingAreaLen - 1);
            customerRenderer.updateFrustrationVisuals(id);
            
            // Customer left the cashier, reset the cashier timer
            const leftCIdx = customerCashierMap.get(id);
            if (leftCIdx !== undefined && cashierTimers[leftCIdx]) {
                clearInterval(cashierTimers[leftCIdx].interval);
                const timerEl = document.getElementById(`timer-cashier-${leftCIdx}`);
                if (timerEl) {
                    timerEl.innerText = 'Idle';
                    timerEl.classList.add('idle');
                }
                const cAnim = document.getElementById(`working-anim-cashier-${leftCIdx}`);
                if (cAnim) cAnim.style.display = 'none';
                showPopUpIcon(`cashier-staff-${leftCIdx}`, '💰');
            }
            break;
            
        case 'table_reserved':
            totalReservations++;
            const totalResEl = document.getElementById('total-reservations');
            if (totalResEl) totalResEl.innerText = totalReservations;
            tableRenderer.setReserved(data.table_id, data.time, data.limit);
            break;
            
        case 'table_unreserved':
            tableRenderer.setUnreserved(data.table_id);
            break;
            
        case 'missed_reservation':
            missedReservations++;
            const missedResEl = document.getElementById('missed-reservations');
            if (missedResEl) missedResEl.innerText = missedReservations;
            break;
            
        case 'seated_waiting_order':
            waitingAreaLen = Math.max(0, waitingAreaLen - 1);
            
            const tableId = data.table_id;
            customerTableMap.set(id, tableId);
            customerRenderer.moveCustomer(id, `table-${tableId}`);
            
            tableRenderer.setOccupied(tableId, data.time, data.is_reservation, 'waiting');
            break;
            
        case 'start_prep':
            // Barista started making it. We don't move the customer, they are still at the table.
            const bIdx = data.barista_index;
            customerBaristaMap.set(id, bIdx);
            
            const bStatus = document.getElementById(`status-barista-${bIdx}`);
            if (bStatus) {
                bStatus.innerText = `Prep for #${id.replace('Cust-', '')}`;
                bStatus.style.color = '#e94f37';
            }
            
            const bAnim = document.getElementById(`working-anim-${bIdx}`);
            if (bAnim) bAnim.style.display = 'block';
            
            // Start the barista countdown timer
            if (data.duration) {
                if (baristaTimers[bIdx]) clearInterval(baristaTimers[bIdx].interval);
                
                let elapsed = 0;
                const totalDuration = data.duration;
                const speedFactor = 0.5; // Backend RealtimeEnvironment factor
                const realTickMs = 100;
                const simTickSec = (realTickMs / 1000) / speedFactor;
                
                const timerEl = document.getElementById(`timer-barista-${bIdx}`);
                if (timerEl) {
                    timerEl.innerText = '0.0s';
                    timerEl.classList.remove('idle');
                }
                
                const interval = setInterval(() => {
                    if (simulationClient && simulationClient.isPaused) return;
                    
                    elapsed += simTickSec;
                    if (elapsed >= totalDuration) {
                        elapsed = totalDuration;
                        clearInterval(interval);
                        if (timerEl) timerEl.innerText = elapsed.toFixed(1) + 's';
                    } else {
                        if (timerEl) timerEl.innerText = elapsed.toFixed(1) + 's';
                    }
                }, realTickMs);
                baristaTimers[bIdx] = { interval };
            }
            break;
            
        case 'served':
            customerRenderer.frustrationMap.set(id, 0);
            customerRenderer.updateFrustrationVisuals(id);
            // Order arrived! Time to eat.
            const tId = customerTableMap.get(id);
            if (tId) {
                // If it's a reserved customer, badge has already been hidden during setOccupied, but pass false to be safe
                tableRenderer.setOccupied(tId, data.time, false, 'eating');
            }
            
            const finishedBIdx = customerBaristaMap.get(id);
            if (finishedBIdx !== undefined) {
                if (baristaTimers[finishedBIdx]) clearInterval(baristaTimers[finishedBIdx].interval);
                const timerEl = document.getElementById(`timer-barista-${finishedBIdx}`);
                if (timerEl) {
                    timerEl.innerText = 'Idle';
                    timerEl.classList.add('idle');
                }
                const finishedBStatus = document.getElementById(`status-barista-${finishedBIdx}`);
                if (finishedBStatus) {
                    finishedBStatus.innerText = 'Idle';
                    finishedBStatus.style.color = '#aaa';
                }
                const bAnim = document.getElementById(`working-anim-${finishedBIdx}`);
                if (bAnim) bAnim.style.display = 'none';
                
                customerBaristaMap.delete(id);
                showPopUpIcon(`barista-staff-${finishedBIdx}`, '☕');
            }
            break;
            
        case 'leave':
            servedCustomers++;
            const el = customerRenderer.customers.get(id);
            if (el) {
                el.style.transform += ' translate(500px, 0)';
                el.style.opacity = '0';
                setTimeout(() => {
                    if (el.parentNode) el.parentNode.removeChild(el);
                    customerRenderer.customers.delete(id);
                }, 800);
            }
            
            // Check if they were in the waiting/pickup area (Takeout customers)
            if (waitingAreaOffsets.has(id)) {
                waitingAreaLen = Math.max(0, waitingAreaLen - 1);
                waitingAreaOffsets.delete(id);
            } else if (pickupAreaOffsets.has(id)) {
                pickupAreaLen = Math.max(0, pickupAreaLen - 1);
                pickupAreaOffsets.delete(id);
            }
            
            // Clear table status and mark table as available again (Dine-in customers)
            const leftTableId = customerTableMap.get(id);
            if (leftTableId) {
                tableRenderer.setEmpty(leftTableId);
                customerTableMap.delete(id);
            }
            break;
            
        case 'sim_end':
            statusDot.className = 'dot';
            statusDot.style.backgroundColor = '#4a90e2';
            statusText.innerText = 'Completed';
            break;
    }
    
    updateStats(data.time);
}