def remove_player(self, player_sid): if self.should_emergency_replace_player(player_sid): player = Plugin.player(player_sid) if player is not None: self.insert_next_player(player.team) if player_sid in self.playerset: self.playerset.remove(player_sid) self.validate_players() self.check_for_activation_or_abortion()
def print_results(self): Plugin.msg("DuelArena results:") place = 0 prev_score = -1 for pscore in sorted(self.scores.items(), key=itemgetter(1), reverse=True): if pscore[1] != prev_score: place += 1 prev_score = pscore[1] player = Plugin.player(pscore[0]) if player and len(player.name) != 0: Plugin.msg( f"Place ^3{place}.^7 {player.name} ^7(Wins:^2{pscore[1]}^7)" )
def should_emergency_replace_player(self, player_sid): if not self.game: return False if self.game.state != "in_progress": return False if not self.is_activated(): return False if not self.is_player(player_sid): return False player = Plugin.player(player_sid) if player is None: return False return player.team in ["red", "blue"]
def insert_next_player(self, team): loser_team_score = getattr(self.game, f"{team}_score") next_sid = self.next_player_sid() if next_sid is None: Plugin.msg("Problem fetching next player. Aborting DuelArena ...") self.deactivate() return next_player = Plugin.player(next_sid) if next_player is None: Plugin.msg("Problem fetching next player. Aborting DuelArena ...") self.deactivate() return setattr(self, f"player_{team}", next_sid) next_player.put(team) self.game.addteamscore( team, self.scores[next_player.steam_id] - loser_team_score)
def validate_players(self): self.playerset[:] = [ sid for sid in self.playerset if Plugin.player(sid) and Plugin.player(sid).ping < 990 ] self.queue[:] = [sid for sid in self.queue if sid in self.playerset]
def put_active_players_on_the_right_teams(self, red_sid, blue_sid): red_player = Plugin.player(red_sid) blue_player = Plugin.player(blue_sid) if red_player.team == "red" and blue_player.team == "blue": self.game.addteamscore("red", self.scores[red_sid] - self.game.red_score) self.game.addteamscore( "blue", self.scores[blue_sid] - self.game.blue_score) return if red_player.team == "blue" and blue_player.team == "red": self.game.addteamscore( "red", self.scores[blue_player.steam_id] - self.game.red_score) self.game.addteamscore( "blue", self.scores[red_player.steam_id] - self.game.blue_score) return if red_player.team == "red": self.game.addteamscore("red", self.scores[red_sid] - self.game.red_score) self.game.addteamscore( "blue", self.scores[blue_sid] - self.game.blue_score) self.player_blue = blue_sid blue_player.put("blue") return if red_player.team == "blue": self.game.addteamscore("red", self.scores[blue_sid] - self.game.red_score) self.game.addteamscore("blue", self.scores[red_sid] - self.game.blue_score) self.player_red = blue_sid blue_player.put("red") return if blue_player.team == "blue": self.game.addteamscore("red", self.scores[red_sid] - self.game.red_score) self.game.addteamscore( "blue", self.scores[blue_sid] - self.game.blue_score) self.player_red = red_sid red_player.put("red") return if blue_player.team == "red": self.game.addteamscore("red", self.scores[blue_sid] - self.game.red_score) self.game.addteamscore("blue", self.scores[red_sid] - self.game.blue_score) self.player_blue = red_sid red_player.put("blue") return self.game.addteamscore("red", self.scores[red_sid] - self.game.red_score) self.game.addteamscore("blue", self.scores[blue_sid] - self.game.blue_score) self.player_red = red_sid red_player.put("red") self.player_blue = blue_sid blue_player.put("blue")
def handle_team_switch_attempt(self, player, old, new): """ Handles the case where a player switches from spectators to "red", "blue", or "any" team, and the resulting teams would be suboptimal balanced. :param: player: the player that attempted the switch :param: old: the old team of the switching player :param: new: the new team that swicthting player would be put onto :return: minqlx.RET_NONE if the team switch should be allowed or minqlx.RET_STOP_EVENT if the switch should not be allowed, and we put the player on a better-suited team. """ if not self.game: return minqlx.RET_NONE if self.last_new_player_id == player.steam_id and new in [ "spectator", "free" ]: self.last_new_player_id = None if self.game.state != "in_progress": return minqlx.RET_NONE if old not in ["spectator", "free" ] or new not in ['red', 'blue', 'any']: return minqlx.RET_NONE if "balance" not in self.plugins: Plugin.msg( "^1balance^7 plugin not loaded, ^1auto rebalance^7 not possible." ) return minqlx.RET_NONE gametype = self.game.type_short if gametype not in SUPPORTED_GAMETYPES: return minqlx.RET_NONE teams = self.teams() if len(teams["red"]) == len(teams["blue"]): self.last_new_player_id = player.steam_id return minqlx.RET_NONE if not self.last_new_player_id: return minqlx.RET_NONE last_new_player = Plugin.player(self.last_new_player_id) if not last_new_player: self.last_new_player_id = None return minqlx.RET_NONE other_than_last_players_team = self.other_team(last_new_player.team) new_player_team = teams[other_than_last_players_team].copy() + [player] proposed_diff = self.calculate_player_average_difference( gametype, teams[last_new_player.team].copy(), new_player_team) alternative_team_a = [player for player in teams[last_new_player.team] if player != last_new_player] + \ [player] alternative_team_b = teams[other_than_last_players_team].copy() + [ last_new_player ] alternative_diff = self.calculate_player_average_difference( gametype, alternative_team_a, alternative_team_b) self.last_new_player_id = None if proposed_diff > alternative_diff: last_new_player.tell( f"{last_new_player.clean_name}, you have been moved to " f"{self.format_team(other_than_last_players_team)} to maintain team balance." ) last_new_player.put(other_than_last_players_team) if new in [last_new_player.team]: return minqlx.RET_NONE if new not in ["any"]: player.tell( f"{player.clean_name}, you have been moved to {self.format_team(last_new_player.team)} " f"to maintain team balance.") player.put(last_new_player.team) return minqlx.RET_STOP_ALL if new not in ["any", other_than_last_players_team]: player.put(other_than_last_players_team) return minqlx.RET_STOP_ALL return minqlx.RET_NONE