forked from nortti/tea_cah
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:
parent
adfd2c81c8
commit
b38912b41d
20
botcmd.py
20
botcmd.py
|
@ -5,6 +5,9 @@ import channel
|
||||||
|
|
||||||
import gameloop
|
import gameloop
|
||||||
|
|
||||||
|
import random
|
||||||
|
import re
|
||||||
|
|
||||||
nickserv_pass = None
|
nickserv_pass = None
|
||||||
irc_chan = None
|
irc_chan = None
|
||||||
|
|
||||||
|
@ -139,7 +142,7 @@ def usage(command):
|
||||||
elif command[0] == 'kick':
|
elif command[0] == 'kick':
|
||||||
return 'Usage: !kick <nick>'
|
return 'Usage: !kick <nick>'
|
||||||
elif command[0] == 'card':
|
elif command[0] == 'card':
|
||||||
return 'Usage: [!card] <number> ...'
|
return 'Usage: [!card] <number[,number,...]> ...'
|
||||||
elif command[0] == 'deck':
|
elif command[0] == 'deck':
|
||||||
return 'Subcommands: !deck add | remove | list'
|
return 'Subcommands: !deck add | remove | list'
|
||||||
elif command[0] == 'bot':
|
elif command[0] == 'bot':
|
||||||
|
@ -190,6 +193,9 @@ def parse_command(message, nick, irc):
|
||||||
|
|
||||||
return message[index:]
|
return message[index:]
|
||||||
|
|
||||||
|
def valid_choice(c):
|
||||||
|
return re.fullmatch(r'\d+(,\d+)*', c)
|
||||||
|
|
||||||
events = gameloop.events
|
events = gameloop.events
|
||||||
|
|
||||||
message = message.split()
|
message = message.split()
|
||||||
|
@ -323,17 +329,23 @@ def parse_command(message, nick, irc):
|
||||||
else:
|
else:
|
||||||
send_event((events.limit, gameloop.limit_types.points, num))
|
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':
|
if c == '!card':
|
||||||
args = message[1:]
|
args = message[1:]
|
||||||
else:
|
else:
|
||||||
args = message
|
args = message
|
||||||
|
|
||||||
if not all(i.isdecimal() for i in args):
|
if not all(valid_choice(i) for i in args):
|
||||||
send(usage('!card'))
|
send(usage('!card'))
|
||||||
return
|
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))
|
send_event((events.card, nick, choices))
|
||||||
|
|
||||||
elif c == '!cards':
|
elif c == '!cards':
|
||||||
|
|
Loading…
Reference in New Issue