forked from cocochief4/LLMafia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen_mafia.py
More file actions
117 lines (104 loc) · 3.6 KB
/
open_mafia.py
File metadata and controls
117 lines (104 loc) · 3.6 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python3
import os
import subprocess
import sys
import game_constants
# ─────────── EDIT THESE ───────────
ANACONDA_ROOT = "C:\\Users\\cococ\\anaconda3" # Full path to Anaconda/Miniconda root
ENV_NAME = "LLMafia-env" # Name of the Conda environment to activate
TARGET_DIR = "C:\\Users\\cococ\\Documents\\GitHub\\LLMafia" # Root project folder
CONFIG_DIR = os.path.join(
TARGET_DIR, "configurations"
) # Directory where config files are stored
CONFIG_FILE_NAME = "humanMafia_5_1.json" # Config file in format word_n_m.json
GAME_ID = "" # Game ID to start from, if empty will use the latest one
# ────────────────────────────────────
def open_ps_window(commands):
"""
Opens a new PowerShell window and runs a list of commands in one session.
Keeps the window open afterwards.
"""
# 1) join commands with semicolons
body = "; ".join(commands)
# 2) wrap in a script block so PowerShell -Command executes them all sequentially
script = f"& {{ {body} }}"
try:
subprocess.Popen(
[
"powershell.exe",
# "-NoExit", # Keep the window open after commands are executed
"-Command",
script,
],
creationflags=subprocess.CREATE_NEW_CONSOLE,
)
except Exception as e:
print(f"Failed to open PowerShell window: {e}")
def main(config=CONFIG_FILE_NAME, game_id=GAME_ID):
# Resolve and validate paths
hook_script = os.path.join(ANACONDA_ROOT, "shell", "condabin", "conda-hook.ps1")
config_path = os.path.join(CONFIG_DIR, config)
base_name = os.path.splitext(config)[0]
if GAME_ID == "":
game_id = str(int(game_constants.get_latest_game_id())+1).zfill(4) # This is a padded number that is a string
# Extract n (total players) and m (number of LLMs)
match = config.split(".")[0].split("_")
if not match[1].isdigit() or not match[2].isdigit():
print("CONFIG_FILE_NAME must be in the form word_n_m.json (e.g. abcd_8_3.json)")
sys.exit(1)
n = int(match[1])
m = int(match[2])
# Common commands: hook, activate, cd
hook = f"& '{hook_script}'"
activate = f"conda activate '{ENV_NAME}'"
cd_cmd = f"Set-Location -Path '{TARGET_DIR}'"
# 1) Setup window: prepare_game -> mafia_main
open_ps_window(
[
hook,
activate,
cd_cmd,
f"python prepare_game.py -i '{game_id}' -c '{config_path}'",
f"python mafia_main.py -i '{game_id}'",
]
)
# 2) m windows for LLM players
for i in range(1, m + 1):
open_ps_window(
[
hook,
activate,
cd_cmd,
f"echo {i} | python llm_interface.py -i '{game_id}'",
]
)
# 3) (n-m) windows for human players
human_count = n - m
for j in range(1, human_count + 1):
open_ps_window(
[
hook,
activate,
cd_cmd,
f"echo {j} | python player_input.py -i '{game_id}'",
]
)
open_ps_window(
[
hook,
activate,
cd_cmd,
f"echo {j} | python player_chat.py -i '{game_id}'",
]
)
# Create a spectator window
open_ps_window(
[
hook,
activate,
cd_cmd,
f"python spectator_chat.py -i '{game_id}'",
]
)
if __name__ == "__main__":
main()