File size: 36,066 Bytes
50e1d54
 
 
 
 
 
 
 
 
9d97138
50e1d54
 
 
 
 
9d97138
50e1d54
 
 
 
9d97138
50e1d54
9d97138
 
21c192a
ed832a7
9d97138
50e1d54
 
 
9d97138
 
 
50e1d54
 
 
 
 
8807cbe
 
 
 
 
 
 
50e1d54
 
 
 
9d97138
50e1d54
 
 
 
 
 
 
 
 
 
 
9d97138
21c192a
 
ed832a7
 
 
 
 
 
 
 
 
 
9d97138
50e1d54
 
 
 
 
 
 
 
8807cbe
 
50e1d54
 
 
 
 
 
 
8807cbe
 
 
 
 
 
 
 
 
 
 
 
 
50e1d54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d97138
50e1d54
9d97138
50e1d54
9d97138
50e1d54
9d97138
 
50e1d54
9d97138
 
50e1d54
9d97138
50e1d54
9d97138
 
 
50e1d54
9d97138
50e1d54
9d97138
 
 
50e1d54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d97138
 
50e1d54
9d97138
 
50e1d54
9d97138
 
 
50e1d54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d97138
8f48bbc
 
50e1d54
 
8f48bbc
50e1d54
 
 
 
 
8f48bbc
50e1d54
eb58522
 
 
 
 
50e1d54
eb58522
 
 
 
 
50e1d54
eb58522
 
 
 
 
50e1d54
 
eb58522
 
8f48bbc
 
 
 
 
50e1d54
 
 
8f48bbc
 
 
 
 
 
50e1d54
 
 
8f48bbc
 
50e1d54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb58522
 
 
50e1d54
eb58522
50e1d54
eb58522
 
50e1d54
eb58522
 
50e1d54
 
 
 
eb58522
 
50e1d54
 
 
eb58522
 
 
 
50e1d54
 
 
 
 
 
 
 
 
 
8807cbe
 
 
50e1d54
 
 
 
 
 
 
 
 
 
 
 
 
9d97138
 
50e1d54
 
 
 
 
9d97138
 
 
8f48bbc
 
 
50e1d54
 
 
 
 
9d97138
eb58522
 
 
8f48bbc
 
50e1d54
 
 
 
 
 
 
 
 
9d97138
eb58522
 
 
 
 
 
 
 
50e1d54
9d97138
 
 
8f48bbc
50e1d54
 
 
 
9d97138
8f48bbc
 
 
 
50e1d54
9d97138
50e1d54
9d97138
50e1d54
9d97138
50e1d54
8f48bbc
50e1d54
 
 
 
9d97138
eb58522
 
 
8f48bbc
 
50e1d54
8f48bbc
9d97138
 
5dd93eb
 
 
50e1d54
 
5dd93eb
 
 
50e1d54
 
 
 
5dd93eb
50e1d54
9d97138
 
 
 
5dd93eb
 
 
 
 
 
 
 
 
50e1d54
5dd93eb
 
 
 
 
 
50e1d54
5dd93eb
9d97138
 
 
 
 
50e1d54
9d97138
 
50e1d54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8807cbe
 
 
50e1d54
 
 
 
9d97138
eb58522
50e1d54
 
 
8f48bbc
eb58522
 
 
50e1d54
 
 
 
 
eb58522
50e1d54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f48bbc
 
50e1d54
 
 
8f48bbc
 
 
50e1d54
 
 
9d97138
50e1d54
 
 
 
9d97138
 
 
50e1d54
 
9d97138
50e1d54
 
9d97138
50e1d54
 
 
 
 
 
 
 
 
 
 
 
 
9d97138
 
50e1d54
 
 
8807cbe
 
 
9d97138
50e1d54
 
 
 
 
 
8807cbe
50e1d54
 
 
 
 
 
9d97138
 
50e1d54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e7cff3
50e1d54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e7cff3
50e1d54
 
 
8807cbe
9d97138
50e1d54
 
8807cbe
 
50e1d54
 
 
 
8807cbe
 
 
 
 
 
 
 
 
50e1d54
 
 
 
 
 
8807cbe
 
50e1d54
 
 
 
 
 
8807cbe
 
50e1d54
 
 
 
 
 
 
 
8807cbe
 
50e1d54
 
 
 
 
 
 
 
 
8807cbe
50e1d54
 
 
 
 
 
 
 
 
 
 
 
 
 
8807cbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50e1d54
 
 
 
 
8807cbe
 
50e1d54
 
8807cbe
 
 
 
 
 
 
50e1d54
8807cbe
 
 
 
 
 
50e1d54
 
 
 
 
 
 
9d97138
 
 
50e1d54
 
 
9d97138
 
 
 
50e1d54
 
 
 
 
 
 
 
 
 
9d97138
eb58522
 
 
 
 
50e1d54
eb58522
50e1d54
 
 
eb58522
 
 
50e1d54
 
 
 
9d97138
 
50e1d54
 
9d97138
 
50e1d54
 
 
 
 
 
9d97138
50e1d54
 
 
 
 
 
 
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
"""
Telearn Arcade Hub — FastAPI server.

Serves the project catalog, static assets, and WebSocket bridge that streams
headless Pygame/OpenCV student projects to the browser.
"""
from __future__ import annotations

import argparse
import asyncio
import hashlib
import json
import logging
import os
import signal
import subprocess
import sys
import time
from typing import Any, Optional

from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from classroom_service import router as classroom_router
from admin_service import router as admin_router

# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
PROJECTS_DIR = os.path.join(BASE_DIR, "projects")
TEMPLATES_DIR = os.path.join(BASE_DIR, "templates")
STATIC_DIR = os.path.join(BASE_DIR, "static")
MODELS_DIR = os.path.join(BASE_DIR, "models")

MAX_CONCURRENT_RUNNERS = int(os.environ.get("MAX_CONCURRENT_RUNNERS", "3"))
SESSION_TIMEOUT_SEC = int(os.environ.get("SESSION_TIMEOUT_SEC", "900"))  # 15 min
IDLE_TIMEOUT_SEC = max(1, int(os.environ.get("IDLE_TIMEOUT_SEC", "720")))  # 12 min
IDLE_WARNING_SEC = min(
    IDLE_TIMEOUT_SEC, max(0, int(os.environ.get("IDLE_WARNING_SEC", "600")))
)  # warn after 10 min idle
HEARTBEAT_TIMEOUT_SEC = max(
    30, int(os.environ.get("HEARTBEAT_TIMEOUT_SEC", "75"))
)
PROJECT_CACHE_TTL_SEC = float(os.environ.get("PROJECT_CACHE_TTL_SEC", "10"))
RUNNER_CONNECT_TIMEOUT_SEC = float(os.environ.get("RUNNER_CONNECT_TIMEOUT_SEC", "8"))

os.makedirs(PROJECTS_DIR, exist_ok=True)
os.makedirs(TEMPLATES_DIR, exist_ok=True)
os.makedirs(STATIC_DIR, exist_ok=True)
os.makedirs(MODELS_DIR, exist_ok=True)

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s [server] %(message)s",
    datefmt="%Y-%m-%dT%H:%M:%S",
)
log = logging.getLogger("telearn.server")

app = FastAPI(title="Student Pygame/OpenCV Arcade Hub")
templates = Jinja2Templates(directory=TEMPLATES_DIR)
app.mount("/classroom-assets", StaticFiles(directory=os.path.join(BASE_DIR, "classroom_static")), name="classroom-assets")
app.include_router(classroom_router)
app.include_router(admin_router)


@app.middleware("http")
async def browser_runtime_headers(request: Request, call_next):
    """Enable SharedArrayBuffer for interactive input inside the Python worker."""
    response = await call_next(request)
    response.headers["Cross-Origin-Opener-Policy"] = "same-origin"
    response.headers["Cross-Origin-Embedder-Policy"] = "credentialless"
    return response

# Concurrency + live session metrics
runner_semaphore = asyncio.Semaphore(MAX_CONCURRENT_RUNNERS)
active_sessions: dict[str, dict[str, Any]] = {}
_sessions_lock = asyncio.Lock()
_metrics = {
    "sessions_started": 0,
    "sessions_rejected": 0,
    "sessions_timed_out": 0,
    "sessions_idle_timed_out": 0,
    "sessions_heartbeat_timed_out": 0,
    "sessions_failed_connect": 0,
}

# Project tree cache
_project_cache: dict[str, Any] = {"tree": None, "built_at": 0.0, "signature": None}


def _session_idle_state(
    idle_for_sec: float,
    warning_sec: float = IDLE_WARNING_SEC,
    timeout_sec: float = IDLE_TIMEOUT_SEC,
) -> str:
    """Return active, warning, or timeout for a session's idle duration."""
    if idle_for_sec >= timeout_sec:
        return "timeout"
    if warning_sec < timeout_sec and idle_for_sec >= warning_sec:
        return "warning"
    return "active"


# ---------------------------------------------------------------------------
# Project scanning (cached)
# ---------------------------------------------------------------------------
def _dir_signature(root: str) -> str:
    """Cheap signature: path + mtime + name for dirs up to depth 3."""
    parts: list[str] = []
    if not os.path.isdir(root):
        return ""
    try:
        for class_name in sorted(os.listdir(root)):
            class_path = os.path.join(root, class_name)
            if not os.path.isdir(class_path) or class_name.startswith("."):
                continue
            try:
                parts.append(f"{class_name}:{os.path.getmtime(class_path)}")
            except OSError:
                parts.append(class_name)
            for student_name in sorted(os.listdir(class_path)):
                student_path = os.path.join(class_path, student_name)
                if not os.path.isdir(student_path) or student_name.startswith("."):
                    continue
                try:
                    parts.append(f"{class_name}/{student_name}:{os.path.getmtime(student_path)}")
                except OSError:
                    parts.append(f"{class_name}/{student_name}")
                for project_name in sorted(os.listdir(student_path)):
                    project_path = os.path.join(student_path, project_name)
                    if not os.path.isdir(project_path) or project_name.startswith("."):
                        continue
                    try:
                        parts.append(
                            f"{class_name}/{student_name}/{project_name}:{os.path.getmtime(project_path)}"
                        )
                    except OSError:
                        parts.append(f"{class_name}/{student_name}/{project_name}")
    except OSError:
        return ""
    return "|".join(parts)


def _scan_projects_uncached() -> dict:
    """
    Scan projects/ into:
    { class_name: { student_name: [ {name, type, url}, ... ] } }
    Priority: python > html > apk > external
    """
    tree: dict = {}
    if not os.path.exists(PROJECTS_DIR):
        return tree

    for class_name in sorted(os.listdir(PROJECTS_DIR)):
        class_path = os.path.join(PROJECTS_DIR, class_name)
        if not os.path.isdir(class_path) or class_name.startswith("."):
            continue

        tree[class_name] = {}
        for student_name in sorted(os.listdir(class_path)):
            student_path = os.path.join(class_path, student_name)
            if not os.path.isdir(student_path) or student_name.startswith("."):
                continue

            projects = []
            for project_name in sorted(os.listdir(student_path)):
                project_dir = os.path.join(student_path, project_name)
                if not os.path.isdir(project_dir) or project_name.startswith("."):
                    continue

                main_py = os.path.join(project_dir, "main.py")
                index_html = os.path.join(project_dir, "index.html")
                link_txt = os.path.join(project_dir, "link.txt")

                apk_files = []
                try:
                    apk_files = [
                        f
                        for f in os.listdir(project_dir)
                        if f.endswith(".apk") and not f.startswith(".")
                    ]
                except OSError:
                    pass

                if os.path.exists(main_py):
                    projects.append({"name": project_name, "type": "python", "url": ""})
                elif os.path.exists(index_html):
                    projects.append({"name": project_name, "type": "html", "url": ""})
                elif apk_files:
                    projects.append({"name": project_name, "type": "apk", "url": apk_files[0]})
                elif os.path.exists(link_txt):
                    try:
                        with open(link_txt, "r", encoding="utf-8") as f:
                            url = f.read().strip()
                        if url:
                            projects.append({"name": project_name, "type": "external", "url": url})
                    except OSError:
                        pass

            if projects:
                tree[class_name][student_name] = projects

        if not tree[class_name]:
            del tree[class_name]

    return tree


def scan_projects(force: bool = False) -> dict:
    """Return cached project tree; refresh when TTL expires or directory signature changes."""
    now = time.time()
    sig = _dir_signature(PROJECTS_DIR)
    cache_age = now - _project_cache["built_at"]
    if (
        not force
        and _project_cache["tree"] is not None
        and cache_age < PROJECT_CACHE_TTL_SEC
        and _project_cache["signature"] == sig
    ):
        return _project_cache["tree"]

    tree = _scan_projects_uncached()
    _project_cache["tree"] = tree
    _project_cache["built_at"] = now
    _project_cache["signature"] = sig
    return tree


def get_id(name: str) -> str:
    """Stable 10-char hex hash for URL tokens."""
    return hashlib.sha256(name.encode("utf-8")).hexdigest()[:10]


def decode_class_student(
    class_name_or_id: str, student_name_or_id: Optional[str] = None
) -> tuple[Optional[str], Optional[str]]:
    """Resolve hashed or raw class/student identifiers to folder names."""
    all_projects = scan_projects()

    if not student_name_or_id:
        for c in all_projects:
            for s in all_projects[c]:
                if get_id(f"{c}/{s}") == class_name_or_id:
                    return c, s

        if "/" in class_name_or_id:
            parts = class_name_or_id.split("/", 1)
            c, s = parts[0], parts[1]
            if c in all_projects and s in all_projects[c]:
                return c, s

        real_class = None
        for c in all_projects:
            if get_id(c) == class_name_or_id:
                real_class = c
                break
        if not real_class and class_name_or_id in all_projects:
            real_class = class_name_or_id
        return real_class, None

    real_class = None
    for c in all_projects:
        if get_id(c) == class_name_or_id:
            real_class = c
            break
    if not real_class and class_name_or_id in all_projects:
        real_class = class_name_or_id

    real_student = None
    if real_class and real_class in all_projects:
        for s in all_projects[real_class]:
            if get_id(f"{real_class}/{s}") == student_name_or_id:
                real_student = s
                break
        if not real_student and student_name_or_id in all_projects[real_class]:
            real_student = student_name_or_id

    return real_class, real_student


def _safe_project_path(*parts: str) -> Optional[str]:
    """Join under PROJECTS_DIR; return None if path escapes the root."""
    file_path = os.path.join(PROJECTS_DIR, *parts)
    abs_path = os.path.abspath(file_path)
    root = os.path.abspath(PROJECTS_DIR)
    if not abs_path.startswith(root + os.sep) and abs_path != root:
        return None
    return abs_path


# ---------------------------------------------------------------------------
# Static / logo
# ---------------------------------------------------------------------------
if os.path.isdir(STATIC_DIR):
    app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")


@app.get("/images/logo-light.png")
@app.get("/logo.png")
async def get_logo():
    return FileResponse(os.path.join(TEMPLATES_DIR, "logo-light.png"))


# Controlled project file serving only (no raw StaticFiles mount on projects/)
@app.get("/static_projects/{student_token}/{project_name}/{path:path}")
async def serve_static_project_file(student_token: str, project_name: str, path: str):
    real_class, real_student = decode_class_student(student_token)

    if not real_class or not real_student:
        # Legacy: /static_projects/{class_id}/{student_id}/{project}/{path}
        parts = path.split("/", 1)
        if len(parts) == 2:
            actual_project_name, actual_path = parts[0], parts[1]
            real_class, real_student = decode_class_student(student_token, project_name)
            if real_class and real_student:
                file_path = _safe_project_path(
                    real_class, real_student, actual_project_name, actual_path
                )
                if file_path and os.path.isfile(file_path):
                    return FileResponse(file_path)
        return HTMLResponse("Not Found", status_code=404)

    file_path = _safe_project_path(real_class, real_student, project_name, path)
    if not file_path or not os.path.isfile(file_path):
        return HTMLResponse("File Not Found", status_code=404)
    return FileResponse(file_path)


# ---------------------------------------------------------------------------
# HTTP routes
# ---------------------------------------------------------------------------
@app.get("/health")
async def health():
    return {
        "status": "ok",
        "active_sessions": len(active_sessions),
        "max_concurrent_runners": MAX_CONCURRENT_RUNNERS,
        "session_timeout_sec": SESSION_TIMEOUT_SEC,
        "idle_warning_sec": IDLE_WARNING_SEC,
        "idle_timeout_sec": IDLE_TIMEOUT_SEC,
        "heartbeat_timeout_sec": HEARTBEAT_TIMEOUT_SEC,
    }


@app.get("/api/metrics")
async def metrics():
    return {
        "active_sessions": len(active_sessions),
        "max_concurrent_runners": MAX_CONCURRENT_RUNNERS,
        "sessions": list(active_sessions.values()),
        **_metrics,
    }


@app.get("/", response_class=HTMLResponse)
async def read_index(request: Request):
    return templates.TemplateResponse(
        "index.html",
        {"request": request, "restrict_class": "", "restrict_student": ""},
    )


@app.get("/class/{class_name}", response_class=HTMLResponse)
async def read_class(request: Request, class_name: str):
    real_class, _ = decode_class_student(class_name)
    if not real_class:
        return HTMLResponse("Class not found", status_code=404)
    return templates.TemplateResponse(
        "index.html",
        {"request": request, "restrict_class": real_class, "restrict_student": ""},
    )


@app.get("/student/{student_token}", response_class=HTMLResponse)
async def read_student(request: Request, student_token: str):
    real_class, real_student = decode_class_student(student_token)
    if not real_class or not real_student:
        return HTMLResponse("Student not found", status_code=404)
    return templates.TemplateResponse(
        "index.html",
        {
            "request": request,
            "restrict_class": real_class,
            "restrict_student": real_student,
        },
    )


@app.get("/student/{class_name}/{student_name}", response_class=HTMLResponse)
async def read_student_old(request: Request, class_name: str, student_name: str):
    real_class, real_student = decode_class_student(class_name, student_name)
    if not real_class or not real_student:
        return HTMLResponse("Student not found", status_code=404)
    student_token = get_id(f"{real_class}/{real_student}")
    return RedirectResponse(url=f"/student/{student_token}", status_code=307)


@app.get("/api/projects", response_class=JSONResponse)
async def get_projects(class_name: str = None, student_name: str = None):
    all_projects = scan_projects()
    class_ids = {c: get_id(c) for c in all_projects}
    student_ids = {
        f"{c}/{s}": get_id(f"{c}/{s}") for c in all_projects for s in all_projects[c]
    }

    if class_name:
        real_class, real_student = decode_class_student(class_name, student_name)
        if real_class in all_projects:
            if real_student:
                if real_student in all_projects[real_class]:
                    filtered = {real_class: {real_student: all_projects[real_class][real_student]}}
                else:
                    filtered = {real_class: {}}
            else:
                filtered = {real_class: all_projects[real_class]}
        else:
            filtered = {}
    else:
        filtered = all_projects

    return {"projects": filtered, "class_ids": class_ids, "student_ids": student_ids}


@app.get("/run/{student_token}/{project_name}", response_class=HTMLResponse)
async def run_project_page(request: Request, student_token: str, project_name: str):
    real_class, real_student = decode_class_student(student_token)
    if not real_class or not real_student:
        return HTMLResponse("Project not found", status_code=404)

    proj_dir = os.path.join(PROJECTS_DIR, real_class, real_student, project_name)
    proj_type = "unknown"
    external_url = ""
    apk_name = ""
    apk_size = ""
    appetize_key = ""
    view_only = request.query_params.get("view") == "1"

    apk_files = []
    if os.path.isdir(proj_dir):
        try:
            apk_files = [
                f for f in os.listdir(proj_dir) if f.endswith(".apk") and not f.startswith(".")
            ]
        except OSError:
            pass

    if os.path.exists(os.path.join(proj_dir, "main.py")):
        proj_type = "python"
    elif os.path.exists(os.path.join(proj_dir, "index.html")):
        proj_type = "html"
    elif apk_files:
        proj_type = "apk"
        apk_name = apk_files[0]
        try:
            size_bytes = os.path.getsize(os.path.join(proj_dir, apk_name))
            if size_bytes < 1024 * 1024:
                apk_size = f"{size_bytes / 1024:.1f} KB"
            else:
                apk_size = f"{size_bytes / (1024 * 1024):.1f} MB"
        except OSError:
            pass
        appetize_path = os.path.join(proj_dir, "appetize.txt")
        if os.path.exists(appetize_path):
            try:
                with open(appetize_path, "r", encoding="utf-8") as f:
                    appetize_key = f.read().strip()
            except OSError:
                pass
    elif os.path.exists(os.path.join(proj_dir, "link.txt")):
        proj_type = "external"
        try:
            with open(os.path.join(proj_dir, "link.txt"), "r", encoding="utf-8") as f:
                external_url = f.read().strip()
        except OSError:
            pass

    return templates.TemplateResponse(
        "player.html",
        {
            "request": request,
            "class_name": real_class,
            "student_name": real_student,
            "student_token": student_token,
            "project_name": project_name,
            "project_type": proj_type,
            "external_url": external_url,
            "apk_name": apk_name,
            "apk_size": apk_size,
            "appetize_key": appetize_key,
            "view_only": view_only,
            "session_timeout_sec": SESSION_TIMEOUT_SEC,
            "idle_warning_sec": IDLE_WARNING_SEC,
            "idle_timeout_sec": IDLE_TIMEOUT_SEC,
            "heartbeat_interval_sec": min(20, max(10, HEARTBEAT_TIMEOUT_SEC // 3)),
            "max_concurrent_runners": MAX_CONCURRENT_RUNNERS,
        },
    )


@app.get("/run/{class_name}/{student_name}/{project_name}", response_class=HTMLResponse)
async def run_project_page_old(
    request: Request, class_name: str, student_name: str, project_name: str
):
    real_class, real_student = decode_class_student(class_name, student_name)
    if not real_class or not real_student:
        return HTMLResponse("Project not found", status_code=404)
    student_token = get_id(f"{real_class}/{real_student}")
    qs = f"?{request.url.query}" if request.url.query else ""
    return RedirectResponse(
        url=f"/run/{student_token}/{project_name}{qs}", status_code=307
    )


# ---------------------------------------------------------------------------
# Process helpers
# ---------------------------------------------------------------------------
def _kill_process_group(process: asyncio.subprocess.Process) -> None:
    """Terminate the entire process group (runner + children)."""
    if process.returncode is not None:
        return
    pid = process.pid
    try:
        if sys.platform == "win32":
            process.terminate()
        else:
            try:
                os.killpg(pid, signal.SIGTERM)
            except (ProcessLookupError, PermissionError, OSError):
                process.terminate()
    except ProcessLookupError:
        pass


async def _cleanup_process(process: asyncio.subprocess.Process) -> None:
    if process is None or process.returncode is not None:
        return
    log.info("Cleaning up process group pid=%s", process.pid)
    _kill_process_group(process)
    try:
        await asyncio.wait_for(process.wait(), timeout=2.0)
    except asyncio.TimeoutError:
        try:
            if sys.platform == "win32":
                process.kill()
            else:
                try:
                    os.killpg(process.pid, signal.SIGKILL)
                except (ProcessLookupError, PermissionError, OSError):
                    process.kill()
        except ProcessLookupError:
            pass
        try:
            await process.wait()
        except Exception:
            pass


# ---------------------------------------------------------------------------
# WebSocket play session
# ---------------------------------------------------------------------------
async def play_project_ws_internal(
    websocket: WebSocket, real_class: str, real_student: str, project_name: str
):
    if not real_class or not real_student:
        await websocket.accept()
        await websocket.send_json(
            {"type": "log", "data": "錯誤: 找不到該專案的執行路徑!\n"}
        )
        await websocket.close()
        return

    project_path = os.path.join(
        PROJECTS_DIR, real_class, real_student, project_name, "main.py"
    )
    if not os.path.exists(project_path):
        await websocket.accept()
        await websocket.send_json(
            {"type": "log", "data": "錯誤: 找不到該專案的 main.py 檔案!\n"}
        )
        await websocket.close()
        return

    # Concurrency gate: reject immediately if all runner slots are busy
    acquired = False
    try:
        await asyncio.wait_for(runner_semaphore.acquire(), timeout=0.05)
        acquired = True
    except asyncio.TimeoutError:
        _metrics["sessions_rejected"] += 1
        await websocket.accept()
        await websocket.send_json(
            {
                "type": "log",
                "data": (
                    f"目前展示名額已滿(最多同時 {MAX_CONCURRENT_RUNNERS} 個專案)。"
                    "請稍後再試。\n"
                ),
            }
        )
        await websocket.send_json({"type": "status", "data": "busy"})
        await websocket.close(code=1013)
        return

    await websocket.accept()
    session_id = f"{real_class}/{real_student}/{project_name}/{time.time():.0f}"
    started_at = time.time()
    started_monotonic = time.monotonic()
    last_activity_at = started_monotonic
    last_client_seen_at = started_monotonic

    async with _sessions_lock:
        active_sessions[session_id] = {
            "class": real_class,
            "student": real_student,
            "project": project_name,
            "started_at": started_at,
            "last_activity_at": started_at,
        }
        _metrics["sessions_started"] += 1

    process = None
    tcp_server = None
    runner_writer = None

    try:
        connected_event = asyncio.Event()
        runner_reader = None

        async def handle_runner_conn(reader, writer):
            nonlocal runner_reader, runner_writer
            runner_reader = reader
            runner_writer = writer
            connected_event.set()

        tcp_server = await asyncio.start_server(handle_runner_conn, "127.0.0.1", 0)
        tcp_port = tcp_server.sockets[0].getsockname()[1]

        runner_script = os.path.join(BASE_DIR, "runner.py")
        cmd = [
            sys.executable,
            runner_script,
            "--project_path",
            project_path,
            "--port",
            str(tcp_port),
            "--models_dir",
            MODELS_DIR,
        ]

        popen_kwargs: dict[str, Any] = {
            "stdout": subprocess.DEVNULL,
            "stderr": subprocess.DEVNULL,
        }
        if sys.platform != "win32":
            popen_kwargs["start_new_session"] = True

        process = await asyncio.create_subprocess_exec(*cmd, **popen_kwargs)
        log.info(
            "Started runner pid=%s session=%s port=%s",
            process.pid,
            session_id,
            tcp_port,
        )

        try:
            await asyncio.wait_for(
                connected_event.wait(), timeout=RUNNER_CONNECT_TIMEOUT_SEC
            )
            log.info("Runner pid=%s connected via TCP", process.pid)
            await websocket.send_json(
                {"type": "status", "data": "running"}
            )
            await websocket.send_json(
                {
                    "type": "log",
                    "data": f"[系統] 專案執行器已就緒(最長 {SESSION_TIMEOUT_SEC // 60} 分鐘)。\n",
                }
            )
        except asyncio.TimeoutError:
            _metrics["sessions_failed_connect"] += 1
            log.error("Runner pid=%s connect timeout", process.pid)
            await websocket.send_json(
                {"type": "log", "data": "伺服器錯誤: 執行模擬器連線超時!\n"}
            )
            await websocket.send_json({"type": "status", "data": "error"})
            return

        async def tcp_to_ws():
            try:
                while True:
                    type_bytes = await runner_reader.readexactly(4)
                    msg_type = type_bytes.decode("utf-8")
                    len_bytes = await runner_reader.readexactly(4)
                    length = int.from_bytes(len_bytes, byteorder="big")
                    payload = await runner_reader.readexactly(length)

                    if msg_type == "FRME":
                        await websocket.send_bytes(payload)
                    elif msg_type == "LOGS":
                        log_text = payload.decode("utf-8", errors="ignore")
                        await websocket.send_json({"type": "log", "data": log_text})
                    elif msg_type == "EXIT":
                        log.info("Runner pid=%s sent EXIT", process.pid)
                        await websocket.send_json({"type": "status", "data": "stopped"})
                        break
            except asyncio.IncompleteReadError:
                log.info("Runner pid=%s TCP incomplete read", process.pid)
            except WebSocketDisconnect:
                pass
            except Exception as e:
                log.exception("tcp_to_ws error: %s", e)

        async def ws_to_tcp():
            nonlocal last_activity_at, last_client_seen_at
            try:
                while True:
                    data = await websocket.receive()
                    now = time.monotonic()
                    last_client_seen_at = now
                    if "text" in data:
                        message = json.loads(data["text"])
                        mtype = message.get("type")

                        if mtype == "heartbeat":
                            continue

                        if mtype == "activity":
                            last_activity_at = now
                            active_sessions[session_id]["last_activity_at"] = time.time()
                        elif mtype in ("keydown", "keyup"):
                            last_activity_at = now
                            active_sessions[session_id]["last_activity_at"] = time.time()
                            payload = json.dumps(message).encode("utf-8")
                            runner_writer.write(
                                b"KEYS" + len(payload).to_bytes(4, "big") + payload
                            )
                            await runner_writer.drain()
                        elif mtype in ("mousemove", "mousedown", "mouseup"):
                            last_activity_at = now
                            active_sessions[session_id]["last_activity_at"] = time.time()
                            payload = json.dumps(message).encode("utf-8")
                            runner_writer.write(
                                b"MOUS" + len(payload).to_bytes(4, "big") + payload
                            )
                            await runner_writer.drain()
                        elif mtype == "cv2key":
                            last_activity_at = now
                            active_sessions[session_id]["last_activity_at"] = time.time()
                            payload = json.dumps(
                                {"key_code": message.get("keyCode")}
                            ).encode("utf-8")
                            runner_writer.write(
                                b"CV2K" + len(payload).to_bytes(4, "big") + payload
                            )
                            await runner_writer.drain()
                        elif mtype == "terminal_input":
                            last_activity_at = now
                            active_sessions[session_id]["last_activity_at"] = time.time()
                            text = message.get("data", "")
                            if not text.endswith("\n"):
                                text += "\n"
                            payload = text.encode("utf-8")
                            runner_writer.write(
                                b"INPT" + len(payload).to_bytes(4, "big") + payload
                            )
                            await runner_writer.drain()
                        elif mtype == "pygame_quit":
                            last_activity_at = now
                            runner_writer.write(b"QUIT" + (0).to_bytes(4, "big") + b"")
                            await runner_writer.drain()
                    elif "bytes" in data:
                        payload = data["bytes"]
                        runner_writer.write(
                            b"CAMF" + len(payload).to_bytes(4, "big") + payload
                        )
                        await runner_writer.drain()
            except WebSocketDisconnect:
                log.info("Browser WebSocket disconnected session=%s", session_id)
            except Exception as e:
                log.exception("ws_to_tcp error: %s", e)

        async def session_watchdog():
            warning_sent = False
            while True:
                await asyncio.sleep(1)
                now = time.monotonic()
                idle_for = now - last_activity_at
                session_age = now - started_monotonic
                heartbeat_age = now - last_client_seen_at
                idle_state = _session_idle_state(idle_for)

                if session_age >= SESSION_TIMEOUT_SEC:
                    _metrics["sessions_timed_out"] += 1
                    log.warning("Session hard timeout session=%s", session_id)
                    await websocket.send_json(
                        {
                            "type": "log",
                            "data": f"\n[系統] 已達最長執行時間({SESSION_TIMEOUT_SEC // 60} 分鐘),自動結束。\n",
                        }
                    )
                    await websocket.send_json({"type": "status", "data": "timeout"})
                    break

                if heartbeat_age >= HEARTBEAT_TIMEOUT_SEC:
                    _metrics["sessions_heartbeat_timed_out"] += 1
                    log.warning("Session heartbeat timeout session=%s", session_id)
                    await websocket.send_json(
                        {
                            "type": "log",
                            "data": "\n[系統] 瀏覽器已離線,自動釋放展示名額。\n",
                        }
                    )
                    await websocket.send_json(
                        {"type": "status", "data": "connection_timeout"}
                    )
                    break

                if idle_state == "timeout":
                    _metrics["sessions_idle_timed_out"] += 1
                    log.info("Session idle timeout session=%s", session_id)
                    await websocket.send_json(
                        {
                            "type": "log",
                            "data": f"\n[系統] 已閒置 {IDLE_TIMEOUT_SEC // 60} 分鐘,自動結束並釋放展示名額。\n",
                        }
                    )
                    await websocket.send_json(
                        {"type": "status", "data": "idle_timeout"}
                    )
                    break

                if idle_state == "warning" and not warning_sent:
                    warning_sent = True
                    await websocket.send_json(
                        {
                            "type": "idle_warning",
                            "remaining_sec": max(
                                0, int(IDLE_TIMEOUT_SEC - idle_for)
                            ),
                        }
                    )
                elif idle_state == "active" and warning_sent:
                    warning_sent = False
                    await websocket.send_json({"type": "idle_resumed"})

            try:
                await websocket.close(code=1000)
            except Exception:
                pass

        tcp_task = asyncio.create_task(tcp_to_ws())
        ws_task = asyncio.create_task(ws_to_tcp())
        watchdog = asyncio.create_task(session_watchdog())
        try:
            _, pending = await asyncio.wait(
                {tcp_task, ws_task, watchdog},
                return_when=asyncio.FIRST_COMPLETED,
            )
            for task in pending:
                task.cancel()
            await asyncio.gather(*pending, return_exceptions=True)
        finally:
            for task in (tcp_task, ws_task, watchdog):
                if not task.done():
                    task.cancel()
            await asyncio.gather(
                tcp_task, ws_task, watchdog, return_exceptions=True
            )

    finally:
        if process is not None:
            await _cleanup_process(process)
        if runner_writer is not None:
            try:
                runner_writer.close()
                await runner_writer.wait_closed()
            except Exception:
                pass
        if tcp_server is not None:
            tcp_server.close()
            await tcp_server.wait_closed()
        try:
            await websocket.close()
        except Exception:
            pass
        async with _sessions_lock:
            active_sessions.pop(session_id, None)
        if acquired:
            runner_semaphore.release()
        log.info(
            "Session ended session=%s duration=%.1fs",
            session_id,
            time.time() - started_at,
        )


@app.websocket("/ws/play/{student_token}/{project_name}")
async def play_project_ws(websocket: WebSocket, student_token: str, project_name: str):
    real_class, real_student = decode_class_student(student_token)
    await play_project_ws_internal(websocket, real_class, real_student, project_name)


@app.websocket("/ws/play/{class_name}/{student_name}/{project_name}")
async def play_project_ws_old(
    websocket: WebSocket, class_name: str, student_name: str, project_name: str
):
    real_class, real_student = decode_class_student(class_name, student_name)
    await play_project_ws_internal(websocket, real_class, real_student, project_name)


# ---------------------------------------------------------------------------
# Entrypoint
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    import uvicorn

    parser = argparse.ArgumentParser(description="Telearn Arcade Hub")
    parser.add_argument("--host", default="127.0.0.1", help="Server host")
    parser.add_argument("--port", type=int, default=8000, help="Server port")
    parser.add_argument(
        "--reload",
        action="store_true",
        default=os.environ.get("DEV_RELOAD", "").lower() in ("1", "true", "yes"),
        help="Enable auto-reload (dev only)",
    )
    args = parser.parse_args()

    uvicorn.run(
        "server:app",
        host=args.host,
        port=args.port,
        reload=args.reload,
    )