"""阵营投票系统 - 允许用户通过投票影响阵营能量""" 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()