Skip to content

Commit

Permalink
Add minimum duel amount setting to the duel module (#2508)
Browse files Browse the repository at this point in the history
* typing: `get_duel_stats` return bool

* typing: `pajbot.utils` - define `__all__` for explicit exports

* typing: `pajbot.managers.websocket` typing for `emit` function

* format: move each attribute to its own line for `show_on_clr`

* Add minimum point amount to the duel module

* Add changelog entry
  • Loading branch information
pajlada committed Jul 29, 2023
1 parent ae4faa9 commit 749a117
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

Remember to bring your dependencies up to date with `./scripts/venvinstall.sh` when updating to this version!

- Minor: Add minimum duel amount setting to the duel module. (#2508)
- Bugfix: Fix playsounds tab in the top navigation bar not being visible on the admin page when the module was disabled. (#2469)
- Bugfix: Multi-Raffle no longer raises an exception without picking any winners when the raffle ends. (#2492)
- Dev: Fix deprecated use of `redis.hmset`. (#2501)
Expand Down
4 changes: 2 additions & 2 deletions pajbot/managers/websocket.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, List
from typing import Any, Dict, List

import json
import logging
Expand Down Expand Up @@ -114,7 +114,7 @@ def __init__(self, bot):
except:
log.exception("Uncaught exception in WebSocketManager")

def emit(self, event, data={}):
def emit(self, event: Any, data: Dict[str, Any] = {}) -> None:
if self.server:
payload = json.dumps({"event": event, "data": data}).encode("utf8")
for client in self.server.clients:
Expand Down
39 changes: 37 additions & 2 deletions pajbot/modules/duel.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ class DuelModule(BaseModule):
default=420,
constraints={"min_value": 0, "max_value": 1000000},
),
ModuleSetting(
key="min_pot",
label="The minimum amount of points that must be used for a duel",
type="number",
required=True,
placeholder="",
default=0,
constraints={"min_value": 0, "max_value": 1000000},
),
ModuleSetting(
key="message_won",
label="Winner message | Available arguments: {winner}, {loser}",
Expand Down Expand Up @@ -82,7 +91,11 @@ class DuelModule(BaseModule):
constraints={"min_value": 0, "max_value": 240},
),
ModuleSetting(
key="show_on_clr", label="Show duels on the clr overlay", type="boolean", required=True, default=True
key="show_on_clr",
label="Show duels on the clr overlay",
type="boolean",
required=True,
default=True,
),
ModuleSetting(
key="max_duel_age",
Expand Down Expand Up @@ -162,6 +175,19 @@ def initiate_duel(self, bot: Bot, source: User, message: str, **rest: Any) -> bo
return False

max_pot = self.settings["max_pot"]
assert isinstance(max_pot, int)
min_pot = self.settings["min_pot"]
assert isinstance(min_pot, int)

msg_id = rest["args"]["msg_id"]
assert isinstance(msg_id, str)

if min_pot > max_pot:
bot.reply(
msg_id,
f"Duel module is misconfigured, the minimum duel amount ({min_pot}) is higher than the maximum duel amount ({max_pot})",
)
return False

msg_split = message.split()
input = msg_split[0]
Expand All @@ -184,6 +210,13 @@ def initiate_duel(self, bot: Bot, source: User, message: str, **rest: Any) -> bo
except ValueError:
pass

if duel_price < min_pot:
bot.whisper(
source,
f"You must duel for at least {min_pot} points.",
)
return False

if source.id in self.duel_requests:
currently_duelling = User.find_by_id(db_session, self.duel_requests[source.id])
if currently_duelling is None:
Expand Down Expand Up @@ -385,7 +418,7 @@ def status_duel(self, bot: Bot, source: User, **rest: Any) -> None:
bot.whisper(source, "You have no duel request or duel target. Type !duel USERNAME POT to duel someone!")

@staticmethod
def get_duel_stats(bot: Bot, source: User, **rest: Any) -> None:
def get_duel_stats(bot: Bot, source: User, **rest: Any) -> bool:
"""
Whispers the users duel winratio to the user
"""
Expand All @@ -398,6 +431,8 @@ def get_duel_stats(bot: Bot, source: User, **rest: Any) -> None:
f"duels: {source.duel_stats.duels_total} winrate: {source.duel_stats.winrate:.2f}% streak: {source.duel_stats.current_streak} profit: {source.duel_stats.profit}",
)

return True

def _cancel_expired_duels(self) -> None:
if self.bot is None:
log.warn("_cancel_expired_duels of DuelModule failed because bot is None")
Expand Down
28 changes: 28 additions & 0 deletions pajbot/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,31 @@
from .time_method import time_method
from .time_since import time_since
from .wait_for_redis_data_loaded import wait_for_redis_data_loaded

__all__ = [
"clean_up_message",
"datetime_from_utc_milliseconds",
"dump_threads",
"extend_version_if_possible",
"extend_version_with_git_data",
"find",
"get_class_that_defined_method",
"init_logging",
"iterate_in_chunks",
"iterate_split_with_index",
"load_config",
"now",
"parse_args",
"parse_number_from_string",
"parse_points_amount",
"print_traceback",
"remove_none_values",
"split_into_chunks_with_prefix",
"stringify_tweet",
"time_ago",
"time_limit",
"time_method",
"time_since",
"tweet_provider_stringify_tweet",
"wait_for_redis_data_loaded",
]

0 comments on commit 749a117

Please sign in to comment.