From d3502efac3fa66741243f994e29440efef03e5ea Mon Sep 17 00:00:00 2001 From: jcao219 Date: Mon, 4 Jul 2011 17:04:14 -0500 Subject: [PATCH] added default nick parsing fixed IRCClient.notice removed some extra junk added some ping/wait vars started writing !start --- decorators.py | 11 ++++++-- oyoyo/client.py | 10 +++----- oyoyo/cmdhandler.py | 7 ------ vars.py | 20 +++++++++++++-- wolfbot.py | 20 --------------- wolfgame.py | 61 ++++++++++++++++++++++++--------------------- 6 files changed, 63 insertions(+), 66 deletions(-) diff --git a/decorators.py b/decorators.py index 9720991..4d636f3 100644 --- a/decorators.py +++ b/decorators.py @@ -1,7 +1,14 @@ +from oyoyo.parse import parse_nick + def generate(fdict): - def cmd(s): + def cmd(s, raw_nick=False): 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 dec return cmd \ No newline at end of file diff --git a/oyoyo/client.py b/oyoyo/client.py index ddd388c..cd6da60 100644 --- a/oyoyo/client.py +++ b/oyoyo/client.py @@ -29,12 +29,6 @@ from oyoyo.parse import * from oyoyo.cmdhandler import CommandError 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): pass @@ -52,7 +46,6 @@ def add_commands(d): @add_commands(("join", "mode", "nick", - "notice", "part")) class IRCClient: """ IRC Client class. This handles one connection to a server. @@ -197,6 +190,9 @@ class IRCClient: def msg(self, user, msg): for line in msg.split('\n'): 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): self.send("QUIT :" + msg) def identify(self, passwd, authuser="NickServ"): diff --git a/oyoyo/cmdhandler.py b/oyoyo/cmdhandler.py index 025a3b0..99bb463 100644 --- a/oyoyo/cmdhandler.py +++ b/oyoyo/cmdhandler.py @@ -22,13 +22,6 @@ import traceback 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): """ decorator to protect functions from being called """ func.protected = True diff --git a/vars.py b/vars.py index 66c5030..ef7e411 100644 --- a/vars.py +++ b/vars.py @@ -1,7 +1,10 @@ -GAME_STARTED = False ROLES = {"person" : []} ORIGINAL_ROLES = None PHASE = "none" # "join", "day", or "night" +LAST_PING = 0 +PING_WAIT = 300 # Seconds +WAIT = 60 +WAITED = 0 GUNNERS = {} MAX_SHOTS = 2 @@ -10,4 +13,17 @@ is_role = lambda plyr, rol: rol in ROLES and plyr in ROLES[rol] def plural(role): if role == "wolf": return "wolves" elif role == "person": return "people" - else: return role + "s" \ No newline at end of file + 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 \ No newline at end of file diff --git a/wolfbot.py b/wolfbot.py index 2b19619..1db17d5 100644 --- a/wolfbot.py +++ b/wolfbot.py @@ -5,25 +5,6 @@ import logging import botconfig 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): def __init__(self, client): super().__init__(client) @@ -39,7 +20,6 @@ class WolfBotHandler(DefaultCommandHandler): if msg.startswith(x): msg = msg.replace(x, "", 1) wolfgame.PM_COMMANDS[x](self.client, rawnick, msg.lstrip()) - print(wolfgame.COMMANDS) @protected def __unhandled__(self, cmd, *args): diff --git a/wolfgame.py b/wolfgame.py index edbd613..2596907 100644 --- a/wolfgame.py +++ b/wolfgame.py @@ -20,37 +20,36 @@ def connect_callback(cli): 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)) +def say(cli, nick, rest): # To be removed later + cli.msg(botconfig.CHANNEL, "{0} says: {1}".format(nick, rest)) @pmcmd("!bye") @cmd("!bye") -def forced_exit(cli, rawnick, *rest): # Admin Only - if parse_nick(rawnick)[0] in botconfig.ADMINS: +def forced_exit(cli, nick, *rest): # Admin Only + if nick 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: +def py(cli, nick, chan, rest): + if nick in botconfig.ADMINS: exec(rest) @cmd("!ping") -def pinger(cli, rawnick, chan, rest): +def pinger(cli, nick, 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 user in (botconfig.NICK, nick): 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 @@ -68,38 +67,37 @@ def pinger(cli, rawnick, chan, rest): 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. \ +def join(cli, nick, chan, rest): + if vars.PHASE == "none": + vars.ROLES["person"].append(nick) + vars.PHASE = "join" + cli.msg(chan, '\u0002{0}\u0002 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" - - + elif nick in list_players(): + cli.notice(nick, "You're already playing!") + 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") -def stats(cli, rawnick, chan, rest): +def stats(cli, nick, chan, rest): if vars.PHASE == "none": return - nick = parse_nick(rawnick)[0] - pl = [] - for x in vars.ROLES.values(): pl.extend(x) + pl = list_players() 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])) + if vars.PHASE == "join": + return + msg = [] for role in vars.ROLES.keys(): num = len(vars.ROLES[role]) @@ -117,4 +115,11 @@ def stats(cli, rawnick, chan, rest): elif len(msg) == 1: cli.msg(chan, "{0}: There is ".format(nick) + msg[0] + ".") -# Game Logic Ends \ No newline at end of file +@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 + \ No newline at end of file