Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mgaertne committed Jul 23, 2024
2 parents e2c77e8 + 89087b9 commit 8bdf823
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 139 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repos:
hooks:
- id: yesqa
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: 'v0.0.255'
rev: v0.5.1
hooks:
- id: ruff
- repo: https://github.com/psf/black
Expand Down
40 changes: 27 additions & 13 deletions src/autoready.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ def __init__(self) -> None:
self.set_cvar_once("qlx_autoready_disable_manual_readyup", "0")

self.min_players = self.get_cvar("qlx_autoready_min_players", int) or 10
self.autostart_delay = self.get_cvar("qlx_autoready_autostart_delay", int) or 180
self.autostart_delay = (
self.get_cvar("qlx_autoready_autostart_delay", int) or 180
)
self.min_counter = self.get_cvar("qlx_autoready_min_seconds", int) or 30
self.timer_visible = self.get_cvar("qlx_autoready_timer_visible", int) or 60
self.disable_player_ready = self.get_cvar("qlx_autoready_disable_manual_readyup", bool) or False
self.disable_player_ready = (
self.get_cvar("qlx_autoready_disable_manual_readyup", bool) or False
)

self.timer: Union["CountdownThread", None] = None
self.current_timer = -1
Expand All @@ -51,10 +55,7 @@ def handle_client_command(self, _player, command):
if len(self.players()) < self.min_players:
return True

if self.disable_player_ready and command == "readyup":
return False

return True
return not self.disable_player_ready or command != "readyup"

def handle_map_change(self, _mapname, _factory):
if self.timer is None or not self.timer.is_alive():
Expand Down Expand Up @@ -87,7 +88,9 @@ def handle_team_switch(self, _player, _old_team, new_team):
else:
self.current_timer = self.autostart_delay

self.timer = CountdownThread(self.current_timer, timed_actions=self.timed_actions())
self.timer = CountdownThread(
self.current_timer, timed_actions=self.timed_actions()
)
self.timer.start()

def timed_actions(self):
Expand Down Expand Up @@ -129,14 +132,18 @@ def make_sure_game_really_starts(self, mapname):

if self.game and self.game.state == "warmup":
pending_players = [
player for player in self.players() if player.stats.ping == -1 and player.team in ["red", "blue"]
player
for player in self.players()
if player.stats.ping == -1 and player.team in ["red", "blue"]
]
for player in pending_players:
player.put("spectator")

with self.timer_lock:
self.timer.stop()
self.timer = CountdownThread(self.min_counter, timed_actions=self.timed_actions())
self.timer = CountdownThread(
self.min_counter, timed_actions=self.timed_actions()
)
self.timer.start()

def handle_game_start(self, _data):
Expand Down Expand Up @@ -170,7 +177,8 @@ def __init__(self, duration: int, *, timed_actions):
self._remaining = -1
self._lock = RLock()
self.timed_actions = {
duration: timed_actions[duration] for duration in sorted(timed_actions.keys(), reverse=True)
duration: timed_actions[duration]
for duration in sorted(timed_actions.keys(), reverse=True)
}
self._now = None

Expand All @@ -192,7 +200,9 @@ def stop(self):
return

with self._lock:
self._remaining = max(int((self._target_time - self._determine_now()).total_seconds()), 0)
self._remaining = max(
int((self._target_time - self._determine_now()).total_seconds()), 0
)

def run(self):
self._target_time = self.calculate_target_time()
Expand All @@ -206,10 +216,14 @@ def run_loop_step(self):

remaining = int((self._target_time - self._determine_now()).total_seconds())

remaining_function = self.determine_timed_action_for_remaining_seconds(remaining)
remaining_function = self.determine_timed_action_for_remaining_seconds(
remaining
)
remaining_function(remaining)

sleep_delay = self._target_time - timedelta(seconds=remaining) - self._determine_now()
sleep_delay = (
self._target_time - timedelta(seconds=remaining) - self._determine_now()
)
if sleep_delay < timedelta(seconds=0.0):
return

Expand Down
19 changes: 6 additions & 13 deletions src/experimental/balancetwo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
You are free to modify this plugin to your own one.
"""

import contextlib
import os
import math
import random
Expand Down Expand Up @@ -2444,12 +2444,9 @@ def has_exception_to_play(self, steam_id):
if steam_id in self.exceptions:
return True

if self.db is not None and self.db.get_flag(
return self.db is not None and self.db.get_flag(
steam_id, "balancetwo:ratinglimit_exception", default=False
):
return True

return False
)

def handle_player_disconnect(self, player, _reason):
if self.last_new_player_id == player.steam_id:
Expand Down Expand Up @@ -3013,7 +3010,7 @@ def find_player_movements_to_even_teams(self):

return [(player, moved_to) for player in sorted_team[:amount_players_moved]]

sorted_team = sorted( # noqa: C414
sorted_team = sorted(
sorted(relevant_players, key=lambda player: player.stats.damage_dealt),
key=lambda player: max(player.score, 0),
)
Expand Down Expand Up @@ -3555,10 +3552,8 @@ def try_msg(self):
def try_mute(self):
@minqlx.next_frame
def execute(_player):
try: # noqa: SIM105
with contextlib.suppress(ValueError):
_player.mute()
except ValueError:
pass

time.sleep(5)
player = Plugin.player(self.steam_id)
Expand All @@ -3573,10 +3568,8 @@ def execute(_player):
def try_kick(self):
@minqlx.next_frame
def execute(_player):
try: # noqa: SIM105
with contextlib.suppress(ValueError):
_player.kick(f"^1GOT KICKED!^7 {self.kickmsg}")
except ValueError:
pass

time.sleep(30)
player = Plugin.player(self.steam_id)
Expand Down
16 changes: 5 additions & 11 deletions src/experimental/showdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,7 @@ def should_allow_automatic_showdown(self):
if len(alive_opponents) < self.min_opp:
return False

if showdown_player.health < 100:
return False

return True
return showdown_player.health >= 100

# noinspection PyMethodMayBeStatic
def alive_players(self, players):
Expand Down Expand Up @@ -403,13 +400,10 @@ def allow_music(self):
if not self.game:
return False

if self.game.blue_score == self.game.roundlimit - 1:
return False

if self.game.red_score == self.game.roundlimit - 1:
return False

return True
return (
self.game.blue_score != self.game.roundlimit - 1
and self.game.red_score != self.game.roundlimit - 1
)

def announce_player_died(self, alive_r, alive_b):
if self.showdown_votes is not None:
Expand Down
80 changes: 56 additions & 24 deletions src/frag_stats.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from collections import Counter

import redis

import minqlx
from minqlx import Plugin, NonexistentPlayerError
Expand Down Expand Up @@ -39,7 +38,9 @@ def __init__(self):

self.add_command("mapsoulz", self.cmd_mapsoulz)
self.add_command("mapreaperz", self.cmd_mapreaperz)
self.add_command(("soulz", "reaperz", "soulzbalance", "fragbalance"), self.cmd_soulzbalance)
self.add_command(
("soulz", "reaperz", "soulzbalance", "fragbalance"), self.cmd_soulzbalance
)

self.frag_log = []

Expand All @@ -58,7 +59,11 @@ def handle_death(self, victim, killer, data):
if data["MOD"] == "SWITCHTEAM":
return

if killer is not None and victim is not None and victim.steam_id == killer.steam_id:
if (
killer is not None
and victim is not None
and victim.steam_id == killer.steam_id
):
return
if victim is None:
return
Expand All @@ -76,12 +81,12 @@ def record_frag(self, recorded_killer, victim):
if self.db is None:
return

if redis.VERSION >= (3,):
self.db.zincrby(COLLECTED_SOULZ_KEY.format(recorded_killer), 1, str(victim))
self.db.zincrby(REAPERZ_KEY.format(victim), 1, str(recorded_killer))
else:
self.db.zincrby(COLLECTED_SOULZ_KEY.format(recorded_killer), str(victim), 1)
self.db.zincrby(REAPERZ_KEY.format(victim), str(recorded_killer), 1)
self.db.zincrby(
COLLECTED_SOULZ_KEY.format(recorded_killer), value=str(victim), amount=1
)
self.db.zincrby(
REAPERZ_KEY.format(victim), value=str(recorded_killer), amount=1
)

# noinspection PyMethodMayBeStatic
def determine_killer(self, killer, means_of_death):
Expand Down Expand Up @@ -118,9 +123,12 @@ def cmd_mapsoulz(self, player, msg, channel):
return

formatted_stats = ", ".join(
f"{victim}^7 ({kill_count})" for victim, kill_count in fragged_statistics.most_common(self.toplimit)
f"{victim}^7 ({kill_count})"
for victim, kill_count in fragged_statistics.most_common(self.toplimit)
)
reply_channel.reply(
f"Top {self.toplimit} reaped soulz for {fragger_name}^7: {formatted_stats}"
)
reply_channel.reply(f"Top {self.toplimit} reaped soulz for {fragger_name}^7: {formatted_stats}")

def identify_target(self, player, target):
if isinstance(target, minqlx.Player):
Expand Down Expand Up @@ -155,7 +163,9 @@ def identify_target(self, player, target):
return fragging_player.name, fragging_player.steam_id

def mapfrag_statistics_for(self, fragger_identifier):
player_fragged_log = [killed for killer, killed in self.frag_log if killer == fragger_identifier]
player_fragged_log = [
killed for killer, killed in self.frag_log if killer == fragger_identifier
]

resolved_fragged_log = self.resolve_player_names(player_fragged_log)
return Counter(resolved_fragged_log)
Expand All @@ -170,16 +180,23 @@ def cmd_mapreaperz(self, player, msg, channel):

reply_channel = self.identify_reply_channel(channel)
if len(fragged_statistics) == 0:
reply_channel.reply(f"{fragged_name}^7's soul was not reaped by anyone, yet.")
reply_channel.reply(
f"{fragged_name}^7's soul was not reaped by anyone, yet."
)
return

formatted_stats = ", ".join(
f"{victim}^7 ({kill_count})" for victim, kill_count in fragged_statistics.most_common(self.toplimit)
f"{victim}^7 ({kill_count})"
for victim, kill_count in fragged_statistics.most_common(self.toplimit)
)
reply_channel.reply(
f"Top {self.toplimit} reaperz of {fragged_name}^7's soul: {formatted_stats}"
)
reply_channel.reply(f"Top {self.toplimit} reaperz of {fragged_name}^7's soul: {formatted_stats}")

def mapfraggers_of(self, fragged_identifier):
player_fragged_log = [killer for killer, killed in self.frag_log if killed == fragged_identifier]
player_fragged_log = [
killer for killer, killed in self.frag_log if killed == fragged_identifier
]

resolved_fragged_log = self.resolve_player_names(player_fragged_log)
return Counter(resolved_fragged_log)
Expand All @@ -188,7 +205,10 @@ def resolve_player_names(self, entries):
if len(entries) == 0:
return []
if isinstance(entries[0], tuple):
return {self.resolve_player_name(steam_id): int(value) for steam_id, value in entries}
return {
self.resolve_player_name(steam_id): int(value)
for steam_id, value in entries
}
return [self.resolve_player_name(item) for item in entries]

def resolve_player_name(self, item):
Expand Down Expand Up @@ -323,14 +343,18 @@ def report_top_soulzbalance(self, player, channel):
f"({fragged_statistics[victim]}/{reaper_statistics[victim]})"
for victim, kill_count in soulz_statistics.most_common(self.toplimit // 2)
)
reply_channel.reply(f"Best {self.toplimit // 2} soul balance for {fragger_name}^7: {formatted_souls}")
reply_channel.reply(
f"Best {self.toplimit // 2} soul balance for {fragger_name}^7: {formatted_souls}"
)

formatted_reapers = ", ".join(
f"{self.resolve_player_name(victim)}^7({self.color_coded_balance_diff(soulz_statistics[victim])})"
f"({fragged_statistics[victim]}/{reaper_statistics[victim]})"
for victim, kill_count in reaped_statistics.most_common(self.toplimit // 2)
)
reply_channel.reply(f"Worst {self.toplimit // 2} soul balance for {fragger_name}^7: {formatted_reapers}")
reply_channel.reply(
f"Worst {self.toplimit // 2} soul balance for {fragger_name}^7: {formatted_reapers}"
)

@minqlx.thread
def report_single_soulzbalance(self, player, opponent, channel):
Expand All @@ -342,21 +366,29 @@ def report_single_soulzbalance(self, player, opponent, channel):
if opponent_name is None and opponent_identifier is None:
return

soulz = self.db.zscore(COLLECTED_SOULZ_KEY.format(fragger_identifier), opponent_identifier)
soulz = self.db.zscore(
COLLECTED_SOULZ_KEY.format(fragger_identifier), opponent_identifier
)
soulz = int(soulz) if soulz is not None else 0
reapz = self.db.zscore(REAPERZ_KEY.format(fragger_identifier), opponent_identifier)
reapz = self.db.zscore(
REAPERZ_KEY.format(fragger_identifier), opponent_identifier
)
reapz = int(reapz) if reapz is not None else 0

if soulz > reapz:
reply_message = (
f"{fragger_name}^7 leads by ^2{soulz - reapz}^7 soulz vs. " f"{opponent_name}^7 ({soulz}/{reapz})"
f"{fragger_name}^7 leads by ^2{soulz - reapz}^7 soulz vs. "
f"{opponent_name}^7 ({soulz}/{reapz})"
)
elif reapz > soulz:
reply_message = (
f"{opponent_name}^7 leads by ^1{reapz - soulz}^7 soulz vs. " f"{fragger_name}^7 ({reapz}/{soulz})"
f"{opponent_name}^7 leads by ^1{reapz - soulz}^7 soulz vs. "
f"{fragger_name}^7 ({reapz}/{soulz})"
)
else:
reply_message = f"{fragger_name}^7 is even with {opponent_name}^7 ({soulz}/{reapz})"
reply_message = (
f"{fragger_name}^7 is even with {opponent_name}^7 ({soulz}/{reapz})"
)

reply_channel.reply(reply_message)

Expand Down
14 changes: 7 additions & 7 deletions src/minqlx/_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,19 @@ def get_cvar(cls, name, return_type=str):
"""
res = minqlx.get_cvar(name)
if return_type == str:
if return_type is str:
return res
if return_type == int:
if return_type is int:
return int(res) if res else None
if return_type == float:
if return_type is float:
return float(res) if res else None
if return_type == bool:
if return_type is bool:
return bool(int(res)) if res else False
if return_type == list:
if return_type is list:
return [s.strip() for s in res.split(",")] if res else []
if return_type == set:
if return_type is set:
return {s.strip() for s in res.split(",")} if res else set()
if return_type == tuple:
if return_type is tuple:
return ([s.strip() for s in res.split(",")]) if res else ()

raise ValueError(f"Invalid return type: {return_type}")
Expand Down
Loading

0 comments on commit 8bdf823

Please sign in to comment.