From d05bdecaa4ebce56cc4c961e90dae24c9400bf34 Mon Sep 17 00:00:00 2001 From: jcao219 Date: Mon, 4 Jul 2011 14:48:02 -0500 Subject: [PATCH] seperated the wolfbot file --- decorators.py | 7 +++ wolfbot.py | 142 +++++--------------------------------------------- wolfgame.py | 120 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+), 129 deletions(-) create mode 100644 decorators.py create mode 100644 wolfgame.py diff --git a/decorators.py b/decorators.py new file mode 100644 index 0000000..9720991 --- /dev/null +++ b/decorators.py @@ -0,0 +1,7 @@ +def generate(fdict): + def cmd(s): + def dec(f): + fdict[s] = f + return f + return dec + return cmd \ No newline at end of file diff --git a/wolfbot.py b/wolfbot.py index b5c15d2..2b19619 100644 --- a/wolfbot.py +++ b/wolfbot.py @@ -3,26 +3,14 @@ from oyoyo.cmdhandler import DefaultCommandHandler, protected from oyoyo.parse import parse_nick import logging import botconfig - -def connect_callback(cli): - cli.identify(botconfig.PASS) - cli.join(botconfig.CHANNEL) - cli.msg("ChanServ", "op "+botconfig.CHANNEL) - cli.msg(botconfig.CHANNEL, "\u0002Wolfbot2 is here.\u0002") - -G_PM_COMMAND = [] -G_COMMAND = [] -COMMANDS = {} -PM_COMMANDS = {} - -HOOKS = {} +import wolfgame def cmd(s, pm = False): def dec(f): if s is None and pm: - G_PM_COMMAND = f + G_PM_COMMAND.append(f) elif s is None and not pm: - G_COMMAND = f + G_COMMAND.append(f) elif pm: PM_COMMANDS[s] = f else: @@ -42,139 +30,35 @@ class WolfBotHandler(DefaultCommandHandler): def privmsg(self, rawnick, chan, msg): if chan != botconfig.NICK: #not a PM - for x in COMMANDS.keys(): + for x in wolfgame.COMMANDS.keys(): if msg.startswith(x): msg = msg.replace(x, "", 1) - COMMANDS[x](self.client, rawnick, chan, msg.lstrip()) + wolfgame.COMMANDS[x](self.client, rawnick, chan, msg.lstrip()) else: - for x in PM_COMMANDS.keys(): + for x in wolfgame.PM_COMMANDS.keys(): if msg.startswith(x): msg = msg.replace(x, "", 1) - PM_COMMANDS[x](self.client, rawnick, msg.lstrip()) + wolfgame.PM_COMMANDS[x](self.client, rawnick, msg.lstrip()) + print(wolfgame.COMMANDS) @protected def __unhandled__(self, cmd, *args): - if cmd in HOOKS.keys(): + if cmd in wolfgame.HOOKS.keys(): largs = list(args) for i,arg in enumerate(largs): if arg: largs[i] = arg.decode('ascii') - HOOKS[cmd](*largs) + wolfgame.HOOKS[cmd](*largs) else: logging.debug('unhandled command %s(%s)' % (cmd, args)) def main(): logging.basicConfig(level=logging.DEBUG) - cli = IRCClient(WolfBotHandler, host="irc.freenode.net", port=6667, nickname=botconfig.NICK, - connect_cb=connect_callback) + cli = IRCClient(WolfBotHandler, host=botconfig.HOST, port=botconfig.PORT, nickname=botconfig.NICK, + connect_cb=wolfgame.connect_callback) conn = cli.connect() while True: - next(conn) - - -# Game Logic Begins: - -import vars - -def reset_game(): - vars.GAME_STARTED = False - vars.ROLES = {"person" : []} - vars.PHASE = "none" - -# Command Handlers: -@cmd("!say", pm=True) -def say(cli, rawnick, rest): # To be removed later - cli.msg(botconfig.CHANNEL, "{0} says: {1}".format(parse_nick(rawnick)[0], rest)) - - -@cmd("!bye", pm=True) -@cmd("!bye", pm=False) -def forced_exit(cli, rawnick, *rest): # Admin Only - if parse_nick(rawnick)[0] in botconfig.ADMINS: - cli.quit("Forced quit from admin") - raise SystemExit - -@cmd("!exec", pm=False) -def py(cli, rawnick, chan, rest): - if parse_nick(rawnick)[0] in botconfig.ADMINS: - exec(rest) - -@cmd("!ping", pm=False) -def pinger(cli, rawnick, chan, rest): - vars.PINGING = True - TO_PING = [] - - @hook("whoreply") - def on_whoreply(server, dunno, chan, dunno1, dunno2, dunno3, user, status, dunno4): - if not vars.PINGING: return - if user in (botconfig.NICK, parse_nick(rawnick)[0]): return # Don't ping self. - - if vars.PINGING and 'G' not in status and '+' not in status: - # TODO: check if the user has AWAY'D himself - TO_PING.append(user) - - @hook("endofwho") - def do_ping(*args): - if not vars.PINGING: return - - chan = args[2] - cli.msg(chan, "PING! "+" ".join(TO_PING)) - vars.PINGING = False - - HOOKS.pop("whoreply") - HOOKS.pop("endofwho") - - cli.send("WHO "+chan) - - -@cmd("!join", pm=False) -def join(cli, rawnick, chan, rest): - if vars.PHASE != "none": - return - - vars.GAME_STARTED = True - - 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 "!wait" to increase join wait time.'.format(nick)) - - vars.ROLES["person"].append(nick) - vars.PHASE = "join" - - - -@cmd("!stats", pm=False) -def stats(cli, rawnick, chan, rest): - if vars.PHASE == "none": - return - nick = parse_nick(rawnick)[0] - pl = [] - for x in vars.ROLES.values(): pl.extend(x) - if len(pl) > 1: - cli.msg(chan, '{0}: {1} players: {2}'.format(nick, - len(pl), ", ".join(pl))) - else: - cli.msg(chan, '{0}: 1 player: {1}'.format(nick, pl[0])) - - msg = [] - for role in vars.ROLES.keys(): - num = len(vars.ROLES[role]) - if num > 1: - msg.append("{0} {1}".format(num, plural(role))) - else: - msg.append("{0} {1}".format(num, role)) - if len(msg) > 2: # More than 2 roles to say - msg[-1] = "and "+msg[-1]+"." - msg[0] = "{0}: There are ".format(nick) + msg[0] - cli.msg(chan, ", ".join(msg)) - elif len(msg) == 2: # 2 roles to say - cli.msg(chan, "{0}: There are ".format(nick) + msg[0], - "and", msg[1] + ".") - elif len(msg) == 1: - cli.msg(chan, "{0}: There is ".format(nick) + msg[0] + ".") - -# Game Logic Ends + next(conn) if __name__ == "__main__": diff --git a/wolfgame.py b/wolfgame.py new file mode 100644 index 0000000..edbd613 --- /dev/null +++ b/wolfgame.py @@ -0,0 +1,120 @@ +from oyoyo.parse import parse_nick +import vars +import botconfig +import decorators + +COMMANDS = {} +PM_COMMANDS = {} +HOOKS = {} + +cmd = decorators.generate(COMMANDS) +pmcmd = decorators.generate(PM_COMMANDS) +hook = decorators.generate(HOOKS) + +# Game Logic Begins: + +def connect_callback(cli): + cli.identify(botconfig.PASS) + cli.join(botconfig.CHANNEL) + cli.msg("ChanServ", "op "+botconfig.CHANNEL) + cli.msg(botconfig.CHANNEL, "\u0002Wolfbot2 is here.\u0002") + +def reset_game(): + vars.GAME_STARTED = False + vars.ROLES = {"person" : []} + vars.PHASE = "none" + +# Command Handlers: +@cmd("!say") +def say(cli, rawnick, rest): # To be removed later + cli.msg(botconfig.CHANNEL, "{0} says: {1}".format(parse_nick(rawnick)[0], rest)) + + +@pmcmd("!bye") +@cmd("!bye") +def forced_exit(cli, rawnick, *rest): # Admin Only + if parse_nick(rawnick)[0] in botconfig.ADMINS: + cli.quit("Forced quit from admin") + raise SystemExit + +@cmd("!exec") +def py(cli, rawnick, chan, rest): + if parse_nick(rawnick)[0] in botconfig.ADMINS: + exec(rest) + +@cmd("!ping") +def pinger(cli, rawnick, chan, rest): + vars.PINGING = True + TO_PING = [] + + @hook("whoreply") + def on_whoreply(server, dunno, chan, dunno1, dunno2, dunno3, user, status, dunno4): + if not vars.PINGING: return + if user in (botconfig.NICK, parse_nick(rawnick)[0]): return # Don't ping self. + + if vars.PINGING and 'G' not in status and '+' not in status: + # TODO: check if the user has AWAY'D himself + TO_PING.append(user) + + @hook("endofwho") + def do_ping(*args): + if not vars.PINGING: return + + chan = args[2] + cli.msg(chan, "PING! "+" ".join(TO_PING)) + vars.PINGING = False + + HOOKS.pop("whoreply") + HOOKS.pop("endofwho") + + cli.send("WHO "+chan) + + +@cmd("!join") +def join(cli, rawnick, chan, rest): + if vars.PHASE != "none": + return + + vars.GAME_STARTED = True + + 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 "!wait" to increase join wait time.'.format(nick)) + + vars.ROLES["person"].append(nick) + vars.PHASE = "join" + + + +@cmd("!stats") +def stats(cli, rawnick, chan, rest): + if vars.PHASE == "none": + return + nick = parse_nick(rawnick)[0] + pl = [] + for x in vars.ROLES.values(): pl.extend(x) + if len(pl) > 1: + cli.msg(chan, '{0}: {1} players: {2}'.format(nick, + len(pl), ", ".join(pl))) + else: + cli.msg(chan, '{0}: 1 player: {1}'.format(nick, pl[0])) + + msg = [] + for role in vars.ROLES.keys(): + num = len(vars.ROLES[role]) + if num > 1: + msg.append("{0} {1}".format(num, plural(role))) + else: + msg.append("{0} {1}".format(num, role)) + if len(msg) > 2: # More than 2 roles to say + msg[-1] = "and "+msg[-1]+"." + msg[0] = "{0}: There are ".format(nick) + msg[0] + cli.msg(chan, ", ".join(msg)) + elif len(msg) == 2: # 2 roles to say + cli.msg(chan, "{0}: There are ".format(nick) + msg[0], + "and", msg[1] + ".") + elif len(msg) == 1: + cli.msg(chan, "{0}: There is ".format(nick) + msg[0] + ".") + +# Game Logic Ends \ No newline at end of file