Repository navigation
Expand file tree
/
Copy pathaction_points.py
More file actions
134 lines (107 loc) · 4.52 KB
/
Copy pathaction_points.py
File metadata and controls
134 lines (107 loc) · 4.52 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
"""
Action-point ("Turns") economy — engine option.
A regenerating per-player pool that actions spend (the BBS / Trade-Wars / Solar
Empire "Turns" mechanic). OPT-IN: inert unless the active game definition sets
`engine.economy_actions == "action_points"`. When off, every function here is a
no-op, so action-site calls (debit_action_points) are safe to land everywhere
without affecting time-economy games.
Config lives in the active game definition so a ruleset mod (e.g. Solar Empire)
turns it on and sets costs:
"engine": {
"economy_actions": "action_points",
"action_points": {
"start": 40, "regen_per_hour": 10, "max": 250,
"apply_to_bots": false,
"costs": { "fleet_attack": 2, "fleet_send": 1, "colonize": 3, ... }
}
}
The `costs` map is also the per-action mod-override table.
"""
from datetime import datetime
from fastapi import HTTPException
from game_definition import get_game_definition
_DEFAULTS = {"start": 0.0, "regen_per_hour": 0.0, "max": 0.0, "apply_to_bots": False}
def _engine(db) -> dict:
return get_game_definition().get("engine", {}) or {}
def ap_enabled(db) -> bool:
"""True when the active definition selects the action-point economy."""
return _engine(db).get("economy_actions") == "action_points"
def ap_config(db) -> dict:
cfg = dict(_DEFAULTS)
cfg.update(_engine(db).get("action_points", {}) or {})
return cfg
def ap_cost(db, action_key: str) -> float:
"""Cost for an action key; 0 (free) when unlisted. Negative costs are
clamped to 0 so a bad definition can't make an action grant turns."""
costs = ap_config(db).get("costs", {}) or {}
try:
return max(0.0, float(costs.get(action_key, 0) or 0))
except (TypeError, ValueError):
return 0.0
def _applies_to(user, db) -> bool:
if not ap_enabled(db):
return False
if getattr(user, "is_bot", False) and not ap_config(db).get("apply_to_bots", False):
return False
return True
def accrue_action_points(user, db, now: datetime = None) -> float:
"""Lazily regenerate a player's action points up to the cap. Returns current.
No-op (returns current) when the economy is off or the user is exempt. On the
first ever accrual the player is seeded with the configured `start`.
"""
if not _applies_to(user, db):
return getattr(user, "action_points", 0.0) or 0.0
cfg = ap_config(db)
cap = float(cfg["max"])
now = now or datetime.utcnow()
if user.last_ap_accrual is None:
# First contact — seed starting turns (never above the cap).
user.action_points = min(cap, max(user.action_points or 0.0, float(cfg["start"])))
user.last_ap_accrual = now
return user.action_points
hours = (now - user.last_ap_accrual).total_seconds() / 3600.0
if hours > 0:
gained = hours * float(cfg["regen_per_hour"])
user.action_points = (user.action_points or 0.0) + gained
user.last_ap_accrual = now
elif hours < 0:
# Clock skew / imported future timestamp — recover instead of stranding.
user.last_ap_accrual = now
# Always enforce the cap (handles a lowered max or over-cap imported data).
user.action_points = min(cap, user.action_points or 0.0)
return user.action_points
def can_afford_action(user, db, action_key: str) -> bool:
if not _applies_to(user, db):
return True
cost = ap_cost(db, action_key)
if cost <= 0:
return True
return accrue_action_points(user, db) >= cost
def debit_action_points(user, db, action_key: str) -> None:
"""Accrue, then spend `action_key`'s cost. Raises HTTP 400 if short.
No-op when the economy is off, the user is exempt (e.g. a bot), or the action
is free. Call AFTER validation and BEFORE the first irreversible mutation.
"""
if not _applies_to(user, db):
return
cost = ap_cost(db, action_key)
if cost <= 0:
return
current = accrue_action_points(user, db)
if current < cost:
raise HTTPException(
400,
f"Not enough turns: {action_key} costs {cost:g}, you have {current:.0f}.",
)
user.action_points = current - cost
def ap_state(user, db) -> dict:
"""Player-facing AP snapshot for the state endpoint/HUD (empty when off)."""
if not _applies_to(user, db):
return {"enabled": False}
cfg = ap_config(db)
return {
"enabled": True,
"current": round(accrue_action_points(user, db), 1),
"max": float(cfg["max"]),
"regen_per_hour": float(cfg["regen_per_hour"]),
}