-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
38 lines (31 loc) · 1.15 KB
/
Copy pathhandler.py
File metadata and controls
38 lines (31 loc) · 1.15 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
import time
import functools
import random
def loot_generator(pool, luck_factor=1.0):
weights = [item.get('weight', 1) * luck_factor for item in pool]
return random.choices(pool, weights=weights, k=1)[0]
def mana_throttle(rate_limit=1.0):
def decorator(func):
last_called = [0.0]
@functools.wraps(func)
def wrapper(*args, **kwargs):
elapsed = time.time() - last_called[0]
if elapsed < rate_limit:
time.sleep(rate_limit - elapsed)
result = func(*args, **kwargs)
last_called[0] = time.time()
return result
return wrapper
return decorator
def sanitize_player_name(name):
return ''.join(c for c in name if c.isalnum() or c in ['_', '-'])[:16]
class GameStateSync:
def __init__(self):
self._registry = {}
def update_entity(self, eid, data):
self._registry.update({eid: {'ts': time.time(), **data}})
def get_snapshot(self):
return self._registry.copy()
def purge_stale(self, timeout=5.0):
now = time.time()
self._registry = {k: v for k, v in self._registry.items() if now - v['ts'] < timeout}