Skip to content

Commit

Permalink
clean game messages
Browse files Browse the repository at this point in the history
  • Loading branch information
hmasdev committed Oct 27, 2024
1 parent 5519e80 commit f359ee2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
4 changes: 2 additions & 2 deletions langchain_werewolf/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class ESide(Enum):


class EResult(Enum):
VillagersWin: str = 'VillagersWin'
WerewolvesWin: str = 'WerewolvesWin'
VillagersWin: str = 'Villagers Win'
WerewolvesWin: str = 'Werewolves Win'


class ESideVictoryCondition(Enum):
Expand Down
6 changes: 3 additions & 3 deletions langchain_werewolf/game/check_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
REVEAL_ROLES_NODE_NAME: str = 'reveal_roles'

GAME_RESULT_MESSAGE_TEMPLATE: str = 'The game has ended: {result}'
PLAYER_ROLE_MESSAGE_TEMPLATE: str = '- {name} is {role} (State: {state})'
PLAYER_ROLE_MESSAGE_TEMPLATE: str = '- The role of {name} is {role} (State: {state})'
REVEAL_ALL_PLAYER_ROLES_MESSAGE_TEMPLATE: str = '''The roles of the players are as follows:
{roles}
''' # noqa
Expand Down Expand Up @@ -79,7 +79,7 @@ def create_check_victory_condition_subgraph(
lambda state: create_dict_to_record_chat(
sender=GAME_MASTER_NAME,
participants=[GAME_MASTER_NAME]+[p.name for p in players],
message=GAME_RESULT_MESSAGE_TEMPLATE.format(result=state.result), # noqa
message=GAME_RESULT_MESSAGE_TEMPLATE.format(result=state.result.value), # noqa
),
)
workflow.add_node(
Expand All @@ -91,7 +91,7 @@ def create_check_victory_condition_subgraph(
roles='\n'.join([
PLAYER_ROLE_MESSAGE_TEMPLATE.format(
name=player.name,
role=player.role,
role=player.role.value,
state=(
'Alive'
if player.name in state.alive_players_names else
Expand Down
2 changes: 1 addition & 1 deletion langchain_werewolf/game/night_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def _master_ask_player_to_act_in_night(
participants=[player.name, GAME_MASTER_NAME],
message=generate_prompt(
GeneratePromptInputForNightAction(
role=player.role,
role=player.role.value,
night_action=player.night_action or '',
question_to_decide_night_action=player.question_to_decide_night_action or '', # noqa
alive_players_names=state.alive_players_names,
Expand Down
30 changes: 15 additions & 15 deletions langchain_werewolf/game/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
ROLE_ANNOUNCE_NODE_NAME_TEMPLATE: str = "role_announcement_{name}" # noqa


WELCOME_TO_GAME_MESSAGE: str = f' WELCOME TO {PACKAGE_NAME} GAME '
WELCOME_TO_GAME_MESSAGE: str = f' WELCOME TO {PACKAGE_NAME} GAME '

# GAME_RULE_TEMPLATE is used to explain the game rule.
GAME_RULE_TEMPLATE: str = '''====== Game Rule ======
This is a werewolf game.
There are two teams: villagers and werewolves.
The villagers win if all werewolves are exclude from the game.
The werewolves win if they equal or outnumber half of the total number of players.,
There are 4 roles in this game.
There are {n_roles} roles in this game.
{roles}
Expand Down Expand Up @@ -64,21 +64,23 @@ def _announce_game_rule(
game_rule_template: str,
role_explanation_template: str,
) -> dict[str, dict[frozenset[str], ChatHistoryModel]]:
roles_explanation = {
role_explanation_template.format(
role=player.role.value,
side=player.side.value,
victory_condition=player.victory_condition, # noqa
night_action=player.night_action,
)
for player in players
}
return create_dict_to_record_chat(
sender=GAME_MASTER_NAME,
participants=[GAME_MASTER_NAME]+[player.name for player in players],
message=game_rule_template.format(
n_roles=len(players),
roles='\n'.join([
f'{idx+1}. {role_exp}'
for idx, role_exp in enumerate({
role_explanation_template.format(
role=player.role,
side=player.side,
victory_condition=player.victory_condition, # noqa
night_action=player.night_action,
)
for player in players
})
for idx, role_exp in enumerate(roles_explanation)
]),
n_werewolves=len([p for p in players if p.role == ERole.Werewolf]), # noqa
n_knights=len([p for p in players if p.role == ERole.Knight]), # noqa
Expand All @@ -96,8 +98,8 @@ def _announce_role(
sender=GAME_MASTER_NAME,
participants=[player.name, GAME_MASTER_NAME],
message=role_announce_template.format(
role=player.role,
side=player.side,
role=player.role.value,
side=player.side.value,
victory_condition=player.victory_condition,
night_action=player.night_action,
),
Expand Down Expand Up @@ -139,9 +141,7 @@ def create_game_preparation_graph(
participants=[GAME_MASTER_NAME]+[p.name for p in players],
message='\n'.join([
(len(WELCOME_TO_GAME_MESSAGE) + 2) * '=',
'=' + len(WELCOME_TO_GAME_MESSAGE) * ' ' + '=',
'=' + WELCOME_TO_GAME_MESSAGE + '=',
'=' + len(WELCOME_TO_GAME_MESSAGE) * ' ' + '=',
(len(WELCOME_TO_GAME_MESSAGE) + 2) * '=',
]),
),
Expand Down
9 changes: 5 additions & 4 deletions tests/game/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ def test__announce_game_rule() -> None:
names = frozenset([GAME_MASTER_NAME]+[player.name for player in players])
state = StateModel(alive_players_names=[player.name for player in players]) # noqa
expected_message = GAME_RULE_TEMPLATE.format(
n_roles=len({p.role for p in players}),
roles='\n'.join([
f'{idx+1}. {role_exp}'
for idx, role_exp in enumerate({
ROLE_EXPLANATION_TEMPLATE.format(
role=player.role,
side=player.side,
role=player.role.value,
side=player.side.value,
victory_condition=player.victory_condition,
night_action=player.night_action,
)
Expand Down Expand Up @@ -64,8 +65,8 @@ def test__announce_role(player: BaseGamePlayer) -> None:
# preparation
state = StateModel(alive_players_names=[player.name])
expected_message = ROLE_ANNOUNCE_TEMPLATE.format(
role=player.role,
side=player.side,
role=player.role.value,
side=player.side.value,
victory_condition=player.victory_condition,
night_action=player.night_action,
)
Expand Down

0 comments on commit f359ee2

Please sign in to comment.