Repository navigation
Expand file tree
/
Copy pathgame.py
More file actions
165 lines (131 loc) · 6.01 KB
/
Copy pathgame.py
File metadata and controls
165 lines (131 loc) · 6.01 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
from utils.db.round import Round
from pymongo.database import Database
from utils.db.teams import get_all_teams, rotate_all_team_tokens, get_team_by_id, get_team_token_mapping, get_team_points_mapping, Team
from utils.db.boxes import get_all_boxes, get_all_claims, get_all_services, Box
from utils.checks.claim import Claim
from utils.task_queue import task_queue
import requests
import json
DEBUG = 1
class FrontendError(Exception):
pass
class Game:
def __init__(self, db: Database, round: Round, timeout: int = 2, interval: int = 600, jitter: int = 120, endpoint: str = "", phase_config: str = ""):
self.db: Database = db
self.round: Round = round
self.teams: list[Team] = get_all_teams(self.db)
self.boxes: dict[str, Box] = get_all_boxes(self.db)
self.timeout = timeout
self.interval = interval
self.jitter = jitter
self.frontend = endpoint
self.headers = {'Content-type': 'application/json'}
self.phase_config = phase_config
# Load box-specific tasks from phase config
if phase_config:
task_queue.load_phase_config(phase_config)
def get_box_tasks(self, hostname: str) -> list[dict]:
"""Get pending tasks for a specific box."""
return task_queue.get_tasks(hostname)
def save_round(self) -> None:
self.round.teams_to_save(self.teams)
self.round.boxes_to_save(list(self.boxes.values()))
self.round.update_db(self.db)
def sync(self) -> None:
self.teams = get_all_teams(self.db)
self.boxes = get_all_boxes(self.db)
def rotate_team_tokens(self):
rotate_all_team_tokens(self.teams)
def update_db(self):
for team in self.teams:
team.update_db(self.db)
for box in self.boxes.values():
box.update_db(self.db)
def send_claims(self):
res = requests.post(f"{self.frontend}/update/claims", data=json.dumps(get_all_claims(self.boxes)), headers=self.headers)
if not res.ok:
raise FrontendError("Could not send claim data to the frontend")
def send_services(self):
res = requests.post(f"{self.frontend}/update/services", data=json.dumps(get_all_services(self.boxes)), headers=self.headers)
if not res.ok:
raise FrontendError("Could not send service data to the frontend")
def send_teams(self):
res = requests.post(f"{self.frontend}/update/teams", data=json.dumps([team.to_dict() for team in self.teams]), headers=self.headers)
if not res.ok:
raise FrontendError("Could not send team data to frontend")
def verify_token(self, token: str) -> int | None:
"""
Verify a token and return the team ID if valid.
Checks both current token and cached token.
Args:
token: The token to verify
Returns:
Team ID if token is valid, None otherwise
"""
for team in self.teams:
if token == team.token or token == team.cached_token:
return team._id
return None
def assign_box_team_id(self, hostname: str, token: str, file_path: str) -> int | None:
"""
Verify token and assign team_id to the corresponding Claim.
Points are awarded later in check_all_and_update.
Args:
hostname: The box hostname
token: The team's token to verify
file_path: The file path of the Claim to update
Returns:
team_id if token is valid and check found, None otherwise
"""
# Verify token first
team_id = self.verify_token(token)
if team_id is None:
if DEBUG:
print(f"Invalid token: {token}")
return None
# Find the box by hostname - O(1) lookup
box = self.boxes.get(hostname)
if not box:
if DEBUG:
print(f"Box {hostname} not found")
return None
# Update claim team_id using the fast path lookup
if box.update_claim_by_path(file_path, team_id):
if DEBUG:
claim = box.get_claim_by_path(file_path)
print(f"Assigned team {team_id} to Claim {claim.name} on {hostname}")
return team_id
if DEBUG:
print(f"No Claim found with path {file_path} on {hostname}")
return None
def reset_all_claims(self) -> None:
"""Reset all claims' team_id back to -1."""
for box in self.boxes.values():
box.reset_all_claims()
if DEBUG:
print("Reset all claims team_id to -1")
def check_all_and_update(self):
if DEBUG:
print("Running all checks")
for box in self.boxes.values():
# Run service checks
services = 1
for service in box.services.values():
services &= service.run(box.ip)
# Process claims if services are up
if services:
team_update = {}
owner = tuple()
for claim in box.claims.values():
if claim.team_id != -1:
if claim.owner:
owner = (claim.team_id, claim.value)
else:
team_update[claim.team_id] = claim.value if claim.team_id not in team_update else team_update[claim.team_id] + claim.value
if owner:
team = get_team_by_id(self.teams, owner[0])
team.modify_team_points(owner[1])
else:
for team_id, value in team_update.items():
team = get_team_by_id(self.teams, team_id)
team.modify_team_points(value)