Browse Source

added default nick parsing

fixed IRCClient.notice
removed some extra junk
added some ping/wait vars
started writing !start
master
jcao219 14 years ago
parent
commit
d3502efac3
  1. 11
      decorators.py
  2. 10
      oyoyo/client.py
  3. 7
      oyoyo/cmdhandler.py
  4. 20
      vars.py
  5. 20
      wolfbot.py
  6. 61
      wolfgame.py

11
decorators.py

@ -1,7 +1,14 @@
from oyoyo.parse import parse_nick
def generate(fdict): def generate(fdict):
def cmd(s): def cmd(s, raw_nick=False):
def dec(f): def dec(f):
fdict[s] = f def innerf(*args):
largs = list(args)
largs[1] = parse_nick(largs[1])[0]
return f(*largs)
if raw_nick: fdict[s] = f
else: fdict[s] = innerf
return f return f
return dec return dec
return cmd return cmd

10
oyoyo/client.py

@ -29,12 +29,6 @@ from oyoyo.parse import *
from oyoyo.cmdhandler import CommandError from oyoyo.cmdhandler import CommandError
import collections import collections
# Python < 3 compatibility
if sys.version_info < (3,):
class bytes(object):
def __new__(self, b='', encoding='utf8'):
return str(b)
class IRCClientError(Exception): class IRCClientError(Exception):
pass pass
@ -52,7 +46,6 @@ def add_commands(d):
@add_commands(("join", @add_commands(("join",
"mode", "mode",
"nick", "nick",
"notice",
"part")) "part"))
class IRCClient: class IRCClient:
""" IRC Client class. This handles one connection to a server. """ IRC Client class. This handles one connection to a server.
@ -197,6 +190,9 @@ class IRCClient:
def msg(self, user, msg): def msg(self, user, msg):
for line in msg.split('\n'): for line in msg.split('\n'):
self.send("PRIVMSG", user, ":{0}".format(line)) self.send("PRIVMSG", user, ":{0}".format(line))
def notice(self, user, msg):
for line in msg.split('\n'):
self.send("NOTICE", user, ":{0}".format(line))
def quit(self, msg): def quit(self, msg):
self.send("QUIT :" + msg) self.send("QUIT :" + msg)
def identify(self, passwd, authuser="NickServ"): def identify(self, passwd, authuser="NickServ"):

7
oyoyo/cmdhandler.py

@ -22,13 +22,6 @@ import traceback
from oyoyo.parse import parse_nick from oyoyo.parse import parse_nick
# Python < 3 compatibility
if sys.version_info < (3,):
class bytes(object):
def __new__(self, b='', encoding='utf8'):
return str(b)
def protected(func): def protected(func):
""" decorator to protect functions from being called """ """ decorator to protect functions from being called """
func.protected = True func.protected = True

20
vars.py

@ -1,7 +1,10 @@
GAME_STARTED = False
ROLES = {"person" : []} ROLES = {"person" : []}
ORIGINAL_ROLES = None ORIGINAL_ROLES = None
PHASE = "none" # "join", "day", or "night" PHASE = "none" # "join", "day", or "night"
LAST_PING = 0
PING_WAIT = 300 # Seconds
WAIT = 60
WAITED = 0
GUNNERS = {} GUNNERS = {}
MAX_SHOTS = 2 MAX_SHOTS = 2
@ -10,4 +13,17 @@ is_role = lambda plyr, rol: rol in ROLES and plyr in ROLES[rol]
def plural(role): def plural(role):
if role == "wolf": return "wolves" if role == "wolf": return "wolves"
elif role == "person": return "people" elif role == "person": return "people"
else: return role + "s" else: return role + "s"
def list_players():
pl = []
for x in ROLES.values():
pl.extend(x)
return pl
def list_players_and_roles():
plr = {}
for x in ROLES.keys():
for p in ROLES[x]:
plr[p] = x
return plr

20
wolfbot.py

@ -5,25 +5,6 @@ import logging
import botconfig import botconfig
import wolfgame import wolfgame
def cmd(s, pm = False):
def dec(f):
if s is None and pm:
G_PM_COMMAND.append(f)
elif s is None and not pm:
G_COMMAND.append(f)
elif pm:
PM_COMMANDS[s] = f
else:
COMMANDS[s] = f
return f
return dec
def hook(s):
def dec(f):
HOOKS[s] = f
return f
return dec
class WolfBotHandler(DefaultCommandHandler): class WolfBotHandler(DefaultCommandHandler):
def __init__(self, client): def __init__(self, client):
super().__init__(client) super().__init__(client)
@ -39,7 +20,6 @@ class WolfBotHandler(DefaultCommandHandler):
if msg.startswith(x): if msg.startswith(x):
msg = msg.replace(x, "", 1) msg = msg.replace(x, "", 1)
wolfgame.PM_COMMANDS[x](self.client, rawnick, msg.lstrip()) wolfgame.PM_COMMANDS[x](self.client, rawnick, msg.lstrip())
print(wolfgame.COMMANDS)
@protected @protected
def __unhandled__(self, cmd, *args): def __unhandled__(self, cmd, *args):

61
wolfgame.py

@ -20,37 +20,36 @@ def connect_callback(cli):
cli.msg(botconfig.CHANNEL, "\u0002Wolfbot2 is here.\u0002") cli.msg(botconfig.CHANNEL, "\u0002Wolfbot2 is here.\u0002")
def reset_game(): def reset_game():
vars.GAME_STARTED = False
vars.ROLES = {"person" : []} vars.ROLES = {"person" : []}
vars.PHASE = "none" vars.PHASE = "none"
# Command Handlers: # Command Handlers:
@cmd("!say") @cmd("!say")
def say(cli, rawnick, rest): # To be removed later def say(cli, nick, rest): # To be removed later
cli.msg(botconfig.CHANNEL, "{0} says: {1}".format(parse_nick(rawnick)[0], rest)) cli.msg(botconfig.CHANNEL, "{0} says: {1}".format(nick, rest))
@pmcmd("!bye") @pmcmd("!bye")
@cmd("!bye") @cmd("!bye")
def forced_exit(cli, rawnick, *rest): # Admin Only def forced_exit(cli, nick, *rest): # Admin Only
if parse_nick(rawnick)[0] in botconfig.ADMINS: if nick in botconfig.ADMINS:
cli.quit("Forced quit from admin") cli.quit("Forced quit from admin")
raise SystemExit raise SystemExit
@cmd("!exec") @cmd("!exec")
def py(cli, rawnick, chan, rest): def py(cli, nick, chan, rest):
if parse_nick(rawnick)[0] in botconfig.ADMINS: if nick in botconfig.ADMINS:
exec(rest) exec(rest)
@cmd("!ping") @cmd("!ping")
def pinger(cli, rawnick, chan, rest): def pinger(cli, nick, chan, rest):
vars.PINGING = True vars.PINGING = True
TO_PING = [] TO_PING = []
@hook("whoreply") @hook("whoreply")
def on_whoreply(server, dunno, chan, dunno1, dunno2, dunno3, user, status, dunno4): def on_whoreply(server, dunno, chan, dunno1, dunno2, dunno3, user, status, dunno4):
if not vars.PINGING: return if not vars.PINGING: return
if user in (botconfig.NICK, parse_nick(rawnick)[0]): return # Don't ping self. if user in (botconfig.NICK, nick): return # Don't ping self.
if vars.PINGING and 'G' not in status and '+' not in status: if vars.PINGING and 'G' not in status and '+' not in status:
# TODO: check if the user has AWAY'D himself # TODO: check if the user has AWAY'D himself
@ -68,38 +67,37 @@ def pinger(cli, rawnick, chan, rest):
HOOKS.pop("endofwho") HOOKS.pop("endofwho")
cli.send("WHO "+chan) cli.send("WHO "+chan)
@cmd("!join") @cmd("!join")
def join(cli, rawnick, chan, rest): def join(cli, nick, chan, rest):
if vars.PHASE != "none": if vars.PHASE == "none":
return vars.ROLES["person"].append(nick)
vars.PHASE = "join"
vars.GAME_STARTED = True cli.msg(chan, '\u0002{0}\u0002 has started a game of Werewolf. \
nick = parse_nick(rawnick)[0]
cli.msg(chan, '{0} has started a game of Werewolf. \
Type "!join" to join. Type "!start" to start the game. \ Type "!join" to join. Type "!start" to start the game. \
Type "!wait" to increase join wait time.'.format(nick)) Type "!wait" to increase join wait time.'.format(nick))
elif nick in list_players():
vars.ROLES["person"].append(nick) cli.notice(nick, "You're already playing!")
vars.PHASE = "join" elif vars.PHASE != "join":
cli.notice(nick, "Sorry but the game is already running. Try again next time.")
else:
vars.ROLES["person"].append(nick)
cli.msg(chan, '\u0002{0}\u0002 has joined the game.'.format(nick))
@cmd("!stats") @cmd("!stats")
def stats(cli, rawnick, chan, rest): def stats(cli, nick, chan, rest):
if vars.PHASE == "none": if vars.PHASE == "none":
return return
nick = parse_nick(rawnick)[0] pl = list_players()
pl = []
for x in vars.ROLES.values(): pl.extend(x)
if len(pl) > 1: if len(pl) > 1:
cli.msg(chan, '{0}: {1} players: {2}'.format(nick, cli.msg(chan, '{0}: {1} players: {2}'.format(nick,
len(pl), ", ".join(pl))) len(pl), ", ".join(pl)))
else: else:
cli.msg(chan, '{0}: 1 player: {1}'.format(nick, pl[0])) cli.msg(chan, '{0}: 1 player: {1}'.format(nick, pl[0]))
if vars.PHASE == "join":
return
msg = [] msg = []
for role in vars.ROLES.keys(): for role in vars.ROLES.keys():
num = len(vars.ROLES[role]) num = len(vars.ROLES[role])
@ -117,4 +115,11 @@ def stats(cli, rawnick, chan, rest):
elif len(msg) == 1: elif len(msg) == 1:
cli.msg(chan, "{0}: There is ".format(nick) + msg[0] + ".") cli.msg(chan, "{0}: There is ".format(nick) + msg[0] + ".")
# Game Logic Ends @cmd("!start")
def start(cli, nick, chan, rest):
pl = list_players()
if(len(pl)) < 4:
cli.msg(chan, "{0}: Four or more players are required to play.".format(nick))
return
Loading…
Cancel
Save