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.
This commit is contained in:
Wolfgang Müller 2019-05-13 20:39:22 +02:00 committed by Juhani Krekelä
parent adfd2c81c8
commit b38912b41d
1 changed files with 16 additions and 4 deletions

View File

@ -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 <nick>'
elif command[0] == 'card':
return 'Usage: [!card] <number> ...'
return 'Usage: [!card] <number[,number,...]> ...'
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':