Hussien Haider commited on
Commit
7f68ba0
·
1 Parent(s): 73b272a
app/api/onevone.py CHANGED
@@ -76,7 +76,7 @@ MAIN_DURATION_LABELS = {
76
  1500: "25 دقيقة",
77
  1800: "30 دقيقة",
78
  }
79
- READY_TIMEOUT_S = 90 # max wait for both clients to load the challenge
80
 
81
  # per-match ready-handshake state. The timer task waits on the Event and polls
82
  # the set; the /ready endpoint mutates the set and sets the Event to wake the
@@ -557,16 +557,32 @@ async def post_match_ready(match_id: str, req: ReadyRequest):
557
  ps = await _sb_select("onevone_players", f"room_id=eq.{match['room_id']}&select=user_id")
558
  if not any(str(p["user_id"]) == str(req.userId) for p in ps):
559
  raise HTTPException(status_code=403, detail="لست لاعباً في هذه المباراة")
 
 
 
 
 
 
 
 
 
 
 
560
  s = _match_ready.setdefault(match_id, set())
561
- s.add(str(req.userId))
 
 
562
  evt = _match_ready_signal.get(match_id)
563
  if evt:
564
  evt.set()
565
- await _broadcast_room_event(match["room_id"], {
566
- "type": "ready",
567
- "userId": str(req.userId),
568
- })
569
- return {"ok": True, "ready_count": len(s)}
 
 
 
570
 
571
 
572
  # --------------------------------------------------------------------------- #
@@ -754,6 +770,12 @@ async def get_match_challenge(match_id: str, userId: str):
754
  training = _map_vuln_hunter_row_to_training(row, team_role)
755
  else:
756
  training = _map_encryption_row_to_training(row, team_role)
 
 
 
 
 
 
757
  return {"training": training, "match": _serialize_match(match)}
758
 
759
 
 
76
  1500: "25 دقيقة",
77
  1800: "30 دقيقة",
78
  }
79
+ READY_TIMEOUT_S = 45 # max wait for both clients to load the challenge
80
 
81
  # per-match ready-handshake state. The timer task waits on the Event and polls
82
  # the set; the /ready endpoint mutates the set and sets the Event to wake the
 
557
  ps = await _sb_select("onevone_players", f"room_id=eq.{match['room_id']}&select=user_id")
558
  if not any(str(p["user_id"]) == str(req.userId) for p in ps):
559
  raise HTTPException(status_code=403, detail="لست لاعباً في هذه المباراة")
560
+ await _mark_player_ready(match_id, str(req.userId), match["room_id"])
561
+ return {"ok": True, "ready_count": len(_match_ready.get(match_id, set()))}
562
+
563
+
564
+ async def _mark_player_ready(match_id: str, user_id: str, room_id: str) -> None:
565
+ """Add a player to the per-match ready set and wake the timer task.
566
+
567
+ Used by both the explicit ``/ready`` endpoint and the implicit
568
+ ``/challenge`` fetch (a client that successfully loaded the challenge
569
+ is, by definition, ready). Idempotent: re-marking a player is a no-op.
570
+ """
571
  s = _match_ready.setdefault(match_id, set())
572
+ if str(user_id) in s:
573
+ return # already ready
574
+ s.add(str(user_id))
575
  evt = _match_ready_signal.get(match_id)
576
  if evt:
577
  evt.set()
578
+ try:
579
+ await _broadcast_room_event(room_id, {
580
+ "type": "ready",
581
+ "userId": str(user_id),
582
+ })
583
+ except Exception:
584
+ # broadcast failure must not break the match flow
585
+ pass
586
 
587
 
588
  # --------------------------------------------------------------------------- #
 
770
  training = _map_vuln_hunter_row_to_training(row, team_role)
771
  else:
772
  training = _map_encryption_row_to_training(row, team_role)
773
+ # Auto-mark this player as ready: a client that successfully fetched the
774
+ # challenge is, by definition, ready. This means clients don't have to
775
+ # also POST /ready — the implicit signal is enough. /ready still works
776
+ # as a backup for clients whose challenge load failed and recovered.
777
+ if match["state"] in ("countdown", "ready"):
778
+ await _mark_player_ready(match_id, str(userId), match["room_id"])
779
  return {"training": training, "match": _serialize_match(match)}
780
 
781
 
db/schema/012_onevone_vuln_hunter.sql ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -- Migration 012: extend onevone_matches.challenge_type CHECK to include
2
+ -- `vulnerability-hunter` (added to the verifier dispatch in
3
+ -- app/api/onevone.py after the table was created).
4
+ --
5
+ -- Idempotent: re-runnable; drops any existing constraint whose name starts
6
+ -- with `onevone_matches_challenge_type_check` and recreates it with the
7
+ -- extended whitelist.
8
+
9
+ BEGIN;
10
+
11
+ -- Drop the old constraint (if it exists under any of the names we've used).
12
+ ALTER TABLE public.onevone_matches
13
+ DROP CONSTRAINT IF EXISTS onevone_matches_challenge_type_check;
14
+
15
+ -- Add the new one with `vulnerability-hunter` allowed.
16
+ ALTER TABLE public.onevone_matches
17
+ ADD CONSTRAINT onevone_matches_challenge_type_check
18
+ CHECK (challenge_type IN (
19
+ 'crypto',
20
+ 'web',
21
+ 'code-fixing',
22
+ 'log-analysis',
23
+ 'vulnerability-hunter'
24
+ ));
25
+
26
+ COMMIT;
27
+
28
+ -- ---------------------------------------------------------------------------
29
+ -- Verification:
30
+ -- SELECT conname, pg_get_constraintdef(oid)
31
+ -- FROM pg_constraint
32
+ -- WHERE conrelid = 'public.onevone_matches'::regclass
33
+ -- AND conname = 'onevone_matches_challenge_type_check';
34
+ -- Expected: a single row whose definition includes all 5 values.
35
+ -- ---------------------------------------------------------------------------