Spaces:
Paused
Paused
Download services/test_players.py from igardner/StockAnalysis: direct link, hf CLI and curl.
- Browser
- Download file 2.63 kB
-
https://huggingface.co/spaces/igardner/StockAnalysis/resolve/main/services/test_players.py
- Command line
-
hf download hf://spaces/igardner/StockAnalysis/services/test_players.py
-
curl -L -o test_players.py https://huggingface.co/spaces/igardner/StockAnalysis/resolve/main/services/test_players.py
2.63 kB
| """Stable simulated guest identities for the opt-in test environment. | |
| These helpers only create/reuse User rows. They never create rooms or match | |
| guests automatically; the shared queue and its test simulator own that flow. | |
| """ | |
| import logging | |
| import re | |
| from config import Config | |
| from extensions import db | |
| from models import User | |
| logger = logging.getLogger(__name__) | |
| _BOT_NAME = re.compile(r"^TestBot(\d+)$") | |
| class TestMode: | |
| def is_enabled(): | |
| return bool(Config.TEST_MODE) | |
| def ensure_test_players(count=4, start_index=0): | |
| """Return stable simulated accounts, never exceeding the configured pool.""" | |
| players = [] | |
| changed = False | |
| first = max(0, int(start_index)) | |
| stop = min( | |
| max(0, int(Config.TEST_BOT_MAX_ACCOUNTS)), | |
| first + max(0, int(count)), | |
| ) | |
| for index in range(first, stop): | |
| nickname = f"TestBot{index}" | |
| user = User.query.filter_by(nickname=nickname).first() | |
| if not user: | |
| user = User( | |
| nickname=nickname, | |
| avatar_style="bottts", | |
| avatar_seed=f"parlor-sim-{index}", | |
| avatar_bg="b6e3f4", | |
| is_test_bot=True, | |
| ) | |
| db.session.add(user) | |
| changed = True | |
| logger.info("TESTBOT_CREATED nickname=%s", nickname) | |
| elif not user.is_test_bot: | |
| # Never convert a real guest into a simulated account because | |
| # their chosen nickname happens to collide with a bot name. | |
| logger.error("TESTBOT_NICKNAME_CONFLICT nickname=%s user_id=%s", nickname, user.id) | |
| continue | |
| players.append(user) | |
| if changed: | |
| db.session.commit() | |
| return players | |
| create_test_players = ensure_test_players | |
| def bot_nicknames(): | |
| return [f"TestBot{index}" for index in range(max(0, int(Config.TEST_BOT_MAX_ACCOUNTS)))] | |
| def test_players(): | |
| return (User.query.filter( | |
| User.is_test_bot.is_(True), | |
| User.nickname.in_(TestMode.bot_nicknames()), | |
| ).order_by(User.id.asc()).all()) | |
| def next_bot_index(): | |
| highest = -1 | |
| for user in User.query.filter(User.is_test_bot.is_(True)).all(): | |
| match = _BOT_NAME.match(user.nickname or "") | |
| if match: | |
| highest = max(highest, int(match.group(1))) | |
| return min(highest + 1, max(0, int(Config.TEST_BOT_MAX_ACCOUNTS))) | |