Parse results and determine winners.
(self, agents: list[Player], round_num: int, stats: RoundStats)
| 103 | future.result() |
| 104 | |
| 105 | def get_results(self, agents: list[Player], round_num: int, stats: RoundStats): |
| 106 | """Parse results and determine winners.""" |
| 107 | # Initialize team scores |
| 108 | team_scores = {"NS": 0.0, "EW": 0.0} |
| 109 | games_played = 0 |
| 110 | |
| 111 | # Parse all simulation logs |
| 112 | for idx in range(self.game_config.get("sims_per_round", 10)): |
| 113 | log_file = self.log_round(round_num) / f"sim_{idx}.json" |
| 114 | |
| 115 | if not log_file.exists(): |
| 116 | self.logger.warning(f"Log file {log_file} not found, skipping") |
| 117 | continue |
| 118 | |
| 119 | try: |
| 120 | with open(log_file) as f: |
| 121 | result = json.load(f) |
| 122 | |
| 123 | # Check for error |
| 124 | if "error" in result: |
| 125 | self.logger.warning(f"Simulation {idx} had error: {result['error']}") |
| 126 | continue |
| 127 | |
| 128 | # Extract VP scores for each team |
| 129 | vp_scores = result.get("normalized_score", {}) |
| 130 | if vp_scores: |
| 131 | team_scores["NS"] += vp_scores.get("NS", 0.0) |
| 132 | team_scores["EW"] += vp_scores.get("EW", 0.0) |
| 133 | games_played += 1 |
| 134 | except (json.JSONDecodeError, KeyError) as e: |
| 135 | self.logger.warning(f"Error parsing {log_file}: {e}") |
| 136 | continue |
| 137 | |
| 138 | if games_played == 0: |
| 139 | self.logger.error("No valid game results found") |
| 140 | stats.winner = RESULT_TIE |
| 141 | for agent in agents: |
| 142 | stats.scores[agent.name] = 0.0 |
| 143 | stats.player_stats[agent.name].score = 0.0 |
| 144 | return |
| 145 | |
| 146 | # Average the scores |
| 147 | team_scores["NS"] /= games_played |
| 148 | team_scores["EW"] /= games_played |
| 149 | |
| 150 | # Determine winning team |
| 151 | if abs(team_scores["NS"] - team_scores["EW"]) < 0.01: # Tie threshold |
| 152 | stats.winner = RESULT_TIE |
| 153 | elif team_scores["NS"] > team_scores["EW"]: |
| 154 | stats.winner = f"{agents[0].name}/{agents[2].name}" |
| 155 | else: |
| 156 | stats.winner = f"{agents[1].name}/{agents[3].name}" |
| 157 | |
| 158 | # Assign scores to individual players based on their team |
| 159 | for position, agent in enumerate(agents): |
| 160 | team = "NS" if position % 2 == 0 else "EW" |
| 161 | score = team_scores[team] |
| 162 | stats.scores[agent.name] = score |