From b38912b41d4bd55db775ee80ded187ace42c3f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20M=C3=BCller?= Date: Mon, 13 May 2019 20:39:22 +0200 Subject: [PATCH] Add support for a random pick from a selection of cards !card now accepts a comma-separated list of numbers from which it will select one randomly, with equal probability. This selection is made before the card event is sent out and is fully transparent to the rest of the game logic. --- botcmd.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/botcmd.py b/botcmd.py index 7073518..c3e1832 100644 --- a/botcmd.py +++ b/botcmd.py @@ -5,6 +5,9 @@ import channel import gameloop +import random +import re + nickserv_pass = None irc_chan = None @@ -139,7 +142,7 @@ def usage(command): elif command[0] == 'kick': return 'Usage: !kick ' elif command[0] == 'card': - return 'Usage: [!card] ...' + return 'Usage: [!card] ...' elif command[0] == 'deck': return 'Subcommands: !deck add | remove | list' elif command[0] == 'bot': @@ -190,6 +193,9 @@ def parse_command(message, nick, irc): return message[index:] + def valid_choice(c): + return re.fullmatch(r'\d+(,\d+)*', c) + events = gameloop.events message = message.split() @@ -323,17 +329,23 @@ def parse_command(message, nick, irc): else: send_event((events.limit, gameloop.limit_types.points, num)) - elif c == '!card' or all(i.isdecimal() for i in message): + elif c == '!card' or all(valid_choice(i) for i in message): if c == '!card': args = message[1:] else: args = message - if not all(i.isdecimal() for i in args): + if not all(valid_choice(i) for i in args): send(usage('!card')) return - choices = [int(i) for i in args] + def pick(c): + if c.isdecimal(): + return c + else: + return random.choice(c.split(',')) + + choices = [int(pick(i)) for i in args] send_event((events.card, nick, choices)) elif c == '!cards':