Repository navigation
Expand file tree
/
Copy pathsub_store.py
More file actions
340 lines (279 loc) · 12.2 KB
/
Copy pathsub_store.py
File metadata and controls
340 lines (279 loc) · 12.2 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
"""
Sub-request storage and domain logic — pure, no Discord / network deps so it's
unit-testable (mirrors practice_ice.py).
Two small lists, persisted to one JSON file:
requests — "I need a sub" posts. Each has a game date/time, how many
spots are needed, and who has filled them. A request auto-
expires once its game time has passed (plus a grace window).
availability — "I can sub" sign-ups: members offering to fill in. These age
out after a fixed number of days.
The store is deliberately generic: every request carries a `kind` field (default
"sub") so the same board machinery can later back pickup games, team-building,
etc. without a schema change.
State shape:
{
"board": {"channel_id": int, "message_id": int} | null,
"requests": [
{
"id": "a1b2c3d4",
"kind": "sub",
"requester_id": int,
"requester_name": str,
"game_ts": "2026-06-20T19:30:00", # ISO, club-local naive
"position": str, # e.g. "Lead", "Vice" (optional)
"notes": str, # free text (optional)
"spots_needed": int,
"filled": [{"user_id": int, "name": str, "ts": "..."}],
"created_ts": "..."
}, ...
],
"availability": [
{"user_id": int, "name": str, "note": str, "created_ts": "..."}, ...
]
}
"""
from __future__ import annotations
import json
import os
import uuid
from datetime import datetime, timedelta
from typing import Any, Optional
# How long after game start a request lingers before it's pruned. Covers a
# typical draw so a still-running game's card doesn't vanish mid-game.
DEFAULT_GRACE_HOURS = 3
# How long an "I can sub" sign-up stays on the board before aging out.
DEFAULT_AVAIL_DAYS = 14
# ── Persistence ─────────────────────────────────────────────────────────────
def empty_state() -> dict:
return {"board": None, "requests": [], "availability": []}
def load(path: str) -> dict:
if not os.path.exists(path):
return empty_state()
try:
with open(path, "r", encoding="utf-8") as f:
state = json.load(f)
except (json.JSONDecodeError, OSError):
return empty_state()
# Tolerate older/partial files.
state.setdefault("board", None)
state.setdefault("requests", [])
state.setdefault("availability", [])
for r in state["requests"]: # tolerate stores written before these fields existed
r.setdefault("filled", [])
r.setdefault("pending", [])
return state
def save(path: str, state: dict) -> None:
"""Atomic write so a crash mid-save can't corrupt the store."""
tmp = f"{path}.tmp"
with open(tmp, "w", encoding="utf-8") as f:
json.dump(state, f, indent=2)
os.replace(tmp, path)
# ── Helpers ─────────────────────────────────────────────────────────────────
def _now_iso(now: datetime) -> str:
return now.replace(microsecond=0).isoformat()
def find_request(state: dict, rid: str) -> Optional[dict]:
return next((r for r in state["requests"] if r["id"] == rid), None)
def open_spots(req: dict) -> int:
"""Spots not yet filled. Pending (invited, unconfirmed) subs reserve a spot."""
return max(0, int(req["spots_needed"]) - len(req.get("filled", [])) - len(req.get("pending", [])))
def is_filled_by(req: dict, user_id: int) -> bool:
return any(f["user_id"] == user_id for f in req.get("filled", []))
def is_pending_by(req: dict, user_id: int) -> bool:
return any(p["user_id"] == user_id for p in req.get("pending", []))
def is_involved(req: dict, user_id: int) -> bool:
return is_filled_by(req, user_id) or is_pending_by(req, user_id)
# ── Requests ────────────────────────────────────────────────────────────────
def new_request(
state: dict,
*,
requester_id: int,
requester_name: str,
game_ts: str,
spots_needed: int,
league_id="",
league: str = "",
team: str = "",
position: str = "",
notes: str = "",
kind: str = "sub",
now: Optional[datetime] = None,
) -> dict:
now = now or datetime.now()
req = {
"id": uuid.uuid4().hex[:8],
"kind": kind,
"requester_id": requester_id,
"requester_name": requester_name,
"league_id": str(league_id) if league_id not in (None, "") else "",
"league": league.strip(),
"team": team.strip(),
"game_ts": game_ts,
"position": position.strip(),
"notes": notes.strip(),
"spots_needed": max(1, int(spots_needed)),
"filled": [],
"pending": [],
"created_ts": _now_iso(now),
}
state["requests"].append(req)
return req
def toggle_spot(req: dict, user_id: int, name: str, now: Optional[datetime] = None) -> str:
"""
Self-service take/leave. Returns one of:
"removed" — caller was filling a spot and is now out
"added" — caller took an open spot
"full" — no open spots and caller wasn't already in
"""
if is_filled_by(req, user_id):
req["filled"] = [f for f in req["filled"] if f["user_id"] != user_id]
return "removed"
if open_spots(req) <= 0:
return "full"
req["filled"].append({"user_id": user_id, "name": name, "ts": _now_iso(now or datetime.now())})
return "added"
def add_sub(req: dict, user_id: int, name: str, now: Optional[datetime] = None) -> str:
"""Requester adds a named person. Returns "added" | "already" | "full"."""
if is_filled_by(req, user_id):
return "already"
if open_spots(req) <= 0:
return "full"
req["filled"].append({"user_id": user_id, "name": name, "ts": _now_iso(now or datetime.now())})
return "added"
def remove_sub(req: dict, user_id: int) -> str:
"""Requester removes a named person (filled or pending). Returns "removed" | "absent"."""
if is_filled_by(req, user_id):
req["filled"] = [f for f in req["filled"] if f["user_id"] != user_id]
return "removed"
if is_pending_by(req, user_id):
req["pending"] = [p for p in req["pending"] if p["user_id"] != user_id]
return "removed"
return "absent"
# ── Invitations (requester invites an available sub; they confirm via DM) ────
def invite_sub(req: dict, user_id: int, name: str, now: Optional[datetime] = None) -> str:
"""Reserve a spot for an invited sub, pending their confirmation.
Returns "invited" | "already" (filled or already pending) | "full"."""
if is_involved(req, user_id):
return "already"
if open_spots(req) <= 0:
return "full"
req.setdefault("pending", []).append(
{"user_id": user_id, "name": name, "ts": _now_iso(now or datetime.now())})
return "invited"
def confirm_sub(req: dict, user_id: int, name: str, now: Optional[datetime] = None) -> str:
"""Invited sub accepts: move them from pending to filled. Returns "confirmed" | "absent"."""
if not is_pending_by(req, user_id):
return "absent"
req["pending"] = [p for p in req["pending"] if p["user_id"] != user_id]
req.setdefault("filled", []).append(
{"user_id": user_id, "name": name, "ts": _now_iso(now or datetime.now())})
return "confirmed"
def decline_sub(req: dict, user_id: int) -> str:
"""Invited sub declines: free the reserved spot. Returns "declined" | "absent"."""
if not is_pending_by(req, user_id):
return "absent"
req["pending"] = [p for p in req["pending"] if p["user_id"] != user_id]
return "declined"
def close_request(state: dict, rid: str) -> bool:
before = len(state["requests"])
state["requests"] = [r for r in state["requests"] if r["id"] != rid]
return len(state["requests"]) < before
def requests_sorted(state: dict) -> list[dict]:
"""Open requests, soonest game first; unparseable game_ts sinks to the end."""
def key(r):
try:
return (0, datetime.fromisoformat(r["game_ts"]))
except (ValueError, KeyError):
return (1, datetime.max)
return sorted(state["requests"], key=key)
# ── Availability ────────────────────────────────────────────────────────────
# An availability entry is "I can sub in <league>, for these games". Keyed by
# (user_id, league_id) so one person can offer in several leagues. `games` is a
# list of ISO datetimes (the draws they can cover); empty = the whole league.
def find_availability(state: dict, user_id: int, league_id) -> Optional[dict]:
league_id = str(league_id) if league_id not in (None, "") else ""
return next(
(a for a in state["availability"]
if a["user_id"] == user_id and a.get("league_id", "") == league_id),
None,
)
def upsert_availability(
state: dict,
*,
user_id: int,
name: str,
league_id="",
league: str = "",
games: Optional[list[str]] = None,
note: str = "",
now: Optional[datetime] = None,
) -> str:
"""Add or update this user's offer to sub in a league. Returns "added"|"updated"."""
payload = {
"user_id": user_id,
"name": name,
"league_id": str(league_id) if league_id not in (None, "") else "",
"league": league.strip(),
"games": list(games or []),
"note": note.strip(),
"created_ts": _now_iso(now or datetime.now()),
}
existing = find_availability(state, user_id, league_id)
if existing:
existing.update(payload)
return "updated"
state["availability"].append(payload)
return "added"
def remove_availability(state: dict, user_id: int, league_id) -> bool:
league_id = str(league_id) if league_id not in (None, "") else ""
before = len(state["availability"])
state["availability"] = [
a for a in state["availability"]
if not (a["user_id"] == user_id and a.get("league_id", "") == league_id)
]
return len(state["availability"]) < before
def availability_for_user(state: dict, user_id: int) -> list[dict]:
return [a for a in state["availability"] if a["user_id"] == user_id]
# ── Expiry ──────────────────────────────────────────────────────────────────
def expire(
state: dict,
now: datetime,
grace_hours: int = DEFAULT_GRACE_HOURS,
avail_days: int = DEFAULT_AVAIL_DAYS,
) -> dict:
"""
Drop played-out requests and stale availability sign-ups. Mutates `state`
and returns the removed items: {"requests": [...], "availability": [...]}.
A request whose game_ts can't be parsed is kept (never silently lost).
"""
cutoff = now - timedelta(hours=grace_hours)
kept_reqs, dropped_reqs = [], []
for r in state["requests"]:
try:
game = datetime.fromisoformat(r["game_ts"])
except (ValueError, KeyError):
kept_reqs.append(r)
continue
(dropped_reqs if game < cutoff else kept_reqs).append(r)
state["requests"] = kept_reqs
# Availability: if it names specific games, drop it once the LAST game has
# passed (+grace). Otherwise (whole-league offer) age it out by created date.
acutoff = now - timedelta(days=avail_days)
kept_av, dropped_av = [], []
for a in state["availability"]:
games = a.get("games") or []
if games:
try:
last = max(datetime.fromisoformat(g) for g in games)
except (ValueError, TypeError):
kept_av.append(a)
continue
(dropped_av if last < cutoff else kept_av).append(a)
else:
try:
created = datetime.fromisoformat(a["created_ts"])
except (ValueError, KeyError):
kept_av.append(a)
continue
(dropped_av if created < acutoff else kept_av).append(a)
state["availability"] = kept_av
return {"requests": dropped_reqs, "availability": dropped_av}