Skip to content

Commit 2e0603d

Browse files
committed
Merge branch 'main' of github.com:emagedoc/CodeClash
2 parents c7d021f + de14713 commit 2e0603d

1 file changed

Lines changed: 33 additions & 26 deletions

File tree

codeclash/ratings/win_rate.py

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55

66
from tqdm import tqdm
77

8-
from codeclash.constants import DIR_LOGS, FILE_RESULTS, RESULT_TIE
8+
from codeclash.constants import DIR_LOGS, RESULT_TIE
99

1010

1111
@dataclass
1212
class PlayerGameProfile:
1313
player_id: str
14+
model_name: str
1415
game_id: str
1516
wins: int = 0
1617
count: int = 0
@@ -24,59 +25,65 @@ def main(log_dir: Path):
2425
# Assuming directory structure is:
2526
# logs/<user_id>/<game_id>
2627
# - players/
27-
# - rounds/
2828
# - game.log
2929
# - metadata.json
30-
player_profiles = {}
30+
model_profiles = {}
3131
for user_folder in log_dir.iterdir():
3232
print(f"Processing games under user `{user_folder.name}`")
3333
for game_log_folder in tqdm(list(user_folder.iterdir())):
3434
if not game_log_folder.is_dir():
3535
continue
3636
game_id = game_log_folder.name.split(".")[1]
3737
player_ids = [x.name for x in (game_log_folder / "players").iterdir() if x.is_dir()]
38-
num_rounds = len(list((game_log_folder / "rounds").iterdir()))
38+
metadata = json.load(open(game_log_folder / "metadata.json"))
39+
player_to_model = {x["name"]: x["config"]["model"]["model_name"] for x in metadata["config"]["players"]}
40+
print(player_to_model)
41+
num_rounds = len(metadata["round_stats"])
3942

40-
for player in player_ids:
41-
if f"{game_id}.{player}" in player_profiles:
42-
player_profiles[f"{game_id}.{player}"].count += num_rounds
43+
# Only count each unique model once per game
44+
unique_models = {player_to_model[player] for player in player_ids}
45+
for model_name in unique_models:
46+
k = f"{game_id}.{model_name}"
47+
if k in model_profiles:
48+
model_profiles[k].count += num_rounds
4349
else:
44-
player_profiles[f"{game_id}.{player}"] = PlayerGameProfile(
45-
player_id=player, game_id=game_id, count=num_rounds
50+
# Use the first player_id that matches this model_name for display
51+
player_id = next(pid for pid in player_ids if player_to_model[pid] == model_name)
52+
model_profiles[k] = PlayerGameProfile(
53+
player_id=player_id, model_name=model_name, game_id=game_id, count=num_rounds
4654
)
4755

48-
for round_folder in (game_log_folder / "rounds").iterdir():
49-
if round_folder.name == "0":
56+
for round, details in metadata["round_stats"].items():
57+
if round == "0":
5058
# Skip initial round
5159
continue
52-
if not (round_folder / FILE_RESULTS).exists():
53-
continue
54-
round_results = json.load(open(round_folder / FILE_RESULTS))
55-
winner = round_results.get("winner")
60+
winner = details["winner"]
5661
if winner != RESULT_TIE:
57-
player_profiles[f"{game_id}.{winner}"].wins += 1
62+
model_profiles[f"{game_id}.{player_to_model[winner]}"].wins += 1
5863

5964
print("Player profiles:")
60-
for profile in player_profiles.values():
65+
for profile in model_profiles.values():
6166
print(
62-
f" - {profile.player_id} (Game: {profile.game_id}) - Win Rate: {profile.win_rate:.2%} ({profile.wins}/{profile.count})"
67+
f" - {profile.model_name} (Game: {profile.game_id}) - Win Rate: {profile.win_rate:.2%} ({profile.wins}/{profile.count})"
6368
)
6469

6570
# Player-specific (game-agnostic) win rates (micro average)
6671
total_wins = {}
6772
total_games = {}
68-
for profile in player_profiles.values():
69-
pid = profile.player_id
70-
total_wins[pid] = total_wins.get(pid, 0) + profile.wins
71-
total_games[pid] = total_games.get(pid, 0) + profile.count
73+
model_names = {}
74+
for profile in model_profiles.values():
75+
mid = profile.model_name
76+
total_wins[mid] = total_wins.get(mid, 0) + profile.wins
77+
total_games[mid] = total_games.get(mid, 0) + profile.count
78+
model_names[mid] = profile.model_name
7279

7380
print("\nPlayer-specific win rates (game-agnostic, micro average):")
74-
for pid in total_wins:
75-
if total_games[pid] > 0:
76-
win_rate = total_wins[pid] / total_games[pid]
81+
for mid in total_wins:
82+
if total_games[mid] > 0:
83+
win_rate = total_wins[mid] / total_games[mid]
7784
else:
7885
win_rate = 0.0
79-
print(f" - {pid}: Win Rate {win_rate:.2%} ({total_wins[pid]}/{total_games[pid]})")
86+
print(f" - {model_names[mid]}: Win Rate {win_rate:.2%} ({total_wins[mid]}/{total_games[mid]})")
8087

8188

8289
if __name__ == "__main__":

0 commit comments

Comments
 (0)