"""Daily spawn selection, independent of global random state and call order.""" from datetime import date, datetime, timedelta, timezone import hashlib import json def daily_spawn_date(server=False): if server: return datetime.now(timezone(timedelta(hours=8))).date() return date.today() def parse_daily_spawn(params): if not isinstance(params, dict): raise ValueError('DailyRandomPointSpawn requires dictionary params') candidates = params.get('candidates') if (not isinstance(candidates, list) or not candidates or any(not isinstance(name, str) or not name.strip() for name in candidates)): raise ValueError('candidates must be a nonempty list of NPC names') point = params.get('point') if (not isinstance(point, (list, tuple)) or len(point) != 2 or any(type(value) is not int or value < 0 for value in point)): raise ValueError('point must contain two nonnegative integer grid coordinates') key = params.get('seed_key', '') if not isinstance(key, str): raise ValueError('seed_key must be a string') return tuple(candidates), tuple(point), key def daily_spawn_choice(params, map_name, day): candidates, point, key = parse_daily_spawn(params) payload = json.dumps([day.isoformat(), str(map_name), point, key], ensure_ascii=True, separators=(',', ':')).encode('ascii') index = int.from_bytes(hashlib.sha256(payload).digest(), 'big') % len(candidates) return candidates[index]