Files
ai-town/engine-python/app/voting.py
empty 75e84f2ba3 feat(engine): 实现阵营投票系统
- 新增 Votes 模型,包含 optimists/fearful 计数和 voted_users 去重列表
- 扩展 Event 模型,添加可选 faction 字段支持投票事件
- 新增 voting.py 模块处理投票逻辑
- 投票规则:每用户每 tick 限投一次,下一 tick 生效
- 投票累加到 factions.power,达到 threshold 触发阵营技能
- 添加 5 个投票系统测试用例,全部 26 个测试通过

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-30 13:12:22 +08:00

47 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""阵营投票系统 - 允许用户通过投票影响阵营能量"""
from typing import List
from .models import WorldState, Event, Votes
def process_votes(state: WorldState, events: List[Event]) -> None:
"""处理投票事件
规则:
1. 每个用户在一个 tick 内只能投 1 次票(按 user 去重)
2. 每票增加对应 faction 的 votes += 1
"""
for event in events:
if event.type != "vote":
continue
# 验证 faction 有效性
if event.faction not in ("optimists", "fearful"):
continue
# 检查用户是否已投票(去重)
if event.user in state.votes.voted_users:
continue
# 记录投票
state.votes.voted_users.append(event.user)
if event.faction == "optimists":
state.votes.optimists += 1
elif event.faction == "fearful":
state.votes.fearful += 1
def apply_votes_to_factions(state: WorldState) -> None:
"""将投票累加到阵营能量,然后清空投票
在每个 tick 开始时调用:
1. 将 votes 累加进 factions.power
2. 清空 votes包括 voted_users
"""
# 累加投票到阵营能量
state.factions.optimists.power += state.votes.optimists
state.factions.fearful.power += state.votes.fearful
# 清空投票
state.votes = Votes()