-
Notifications
You must be signed in to change notification settings - Fork 352
/
werewolf_utils.py
78 lines (61 loc) · 2.17 KB
/
werewolf_utils.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
"""utils."""
import re
from typing import Union, Any, Sequence
import numpy as np
from loguru import logger
from prompt import Prompts
from agentscope.agents import AgentBase
from agentscope.message import Msg
def check_winning(alive_agents: list, wolf_agents: list, host: str) -> bool:
"""check which group wins"""
if len(wolf_agents) * 2 >= len(alive_agents):
msg = Msg(host, Prompts.to_all_wolf_win, role="assistant")
logger.chat(msg)
return True
if alive_agents and not wolf_agents:
msg = Msg(host, Prompts.to_all_village_win, role="assistant")
logger.chat(msg)
return True
return False
def update_alive_players(
survivors: Sequence[AgentBase],
wolves: Sequence[AgentBase],
dead_names: Union[str, list[str]],
) -> tuple[list, list]:
"""update the list of alive agents"""
if not isinstance(dead_names, list):
dead_names = [dead_names]
return [_ for _ in survivors if _.name not in dead_names], [
_ for _ in wolves if _.name not in dead_names
]
def majority_vote(votes: list) -> Any:
"""majority_vote function"""
unit, counts = np.unique(votes, return_counts=True)
return unit[np.argmax(counts)]
def extract_name_and_id(name: str) -> tuple[str, int]:
"""extract player name and id from a string"""
name = re.search(r"\b[Pp]layer\d+\b", name).group(0)
idx = int(re.search(r"[Pp]layer(\d+)", name).group(1)) - 1
return name, idx
def n2s(agents: Sequence[Union[AgentBase, str]]) -> str:
"""combine agent names into a string, and use "and" to connect the last
two names."""
def _get_name(agent_: Union[AgentBase, str]) -> str:
return agent_.name if isinstance(agent_, AgentBase) else agent_
if len(agents) == 1:
return _get_name(agents[0])
return (
", ".join([_get_name(_) for _ in agents[:-1]])
+ " and "
+ _get_name(agents[-1])
)
def set_parsers(
agents: Union[AgentBase, list[AgentBase]],
parser_name: str,
) -> None:
"""Add parser to agents"""
if not isinstance(agents, list):
agents = [agents]
for agent in agents:
agent.set_parser(parser_name)