Browse Source

split messages that are too large, and also fix a bug where the bot doesn't completely exit on fbye

master
jcao219 13 years ago
parent
commit
9c775996ac
  1. 25
      oyoyo/client.py
  2. 35
      wolfgame.py

25
oyoyo/client.py

@ -96,6 +96,8 @@ class IRCClient(object):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.nickname = "" self.nickname = ""
self.hostmask = ""
self.ident = ""
self.real_name = "" self.real_name = ""
self.host = None self.host = None
self.port = None self.port = None
@ -106,7 +108,6 @@ class IRCClient(object):
self.lock = threading.RLock() self.lock = threading.RLock()
self.tokenbucket = TokenBucket(28, 1.73) self.tokenbucket = TokenBucket(28, 1.73)
self.last_messaged = ""
self.__dict__.update(kwargs) self.__dict__.update(kwargs)
self.command_handler = cmd_handler self.command_handler = cmd_handler
@ -145,10 +146,9 @@ class IRCClient(object):
msg = bytes(" ", "utf_8").join(bargs) msg = bytes(" ", "utf_8").join(bargs)
logging.info('---> send "{0}"'.format(msg)) logging.info('---> send "{0}"'.format(msg))
while not self.tokenbucket.consume(1): while not self.tokenbucket.consume(1):
pass pass
self.socket.send(msg + bytes("\r\n", "utf_8")) self.socket.send(msg + bytes("\r\n", "utf_8"))
def connect(self): def connect(self):
@ -230,12 +230,25 @@ class IRCClient(object):
self.socket.close() self.socket.close()
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)) maxchars = 494 - len(self.nickname+self.ident+self.hostmask+user)
self.last_messaged = user while line:
extra = ""
if len(line) > maxchars:
extra = line[maxchars:]
line = line[:maxchars]
self.send("PRIVMSG", user, ":{0}".format(line))
line = extra
privmsg = msg # Same thing privmsg = msg # Same thing
def notice(self, user, msg): def notice(self, user, msg):
for line in msg.split('\n'): for line in msg.split('\n'):
self.send("NOTICE", user, ":{0}".format(line)) maxchars = 495 - len(self.nickname+self.ident+self.hostmask+user)
while line:
extra = ""
if len(line) > maxchars:
extra = line[maxchars:]
line = line[:maxchars]
self.send("NOTICE", user, ":{0}".format(line))
line = extra
def quit(self, msg=""): def quit(self, msg=""):
self.send("QUIT :{0}".format(msg)) self.send("QUIT :{0}".format(msg))
def part(self, chan, msg=""): def part(self, chan, msg=""):

35
wolfgame.py

@ -43,9 +43,13 @@ def connect_callback(cli):
cli.msg("ChanServ", "op "+botconfig.CHANNEL) cli.msg("ChanServ", "op "+botconfig.CHANNEL)
@hook("whoreply", id=294) @hook("whoreply", id=294)
def on_whoreply(cli, server, dunno, chan, dunno1, def on_whoreply(cli, server, dunno, chan, ident,
cloak, dunno3, user, status, dunno4): cloak, dunno3, user, status, dunno4):
if user in var.USERS: return # Don't add someone who is already there if user in var.USERS: return # Don't add someone who is already there
if user == botconfig.NICK:
cli.nickname = user
cli.ident = ident
cli.hostmask = cloak
var.USERS.append(user) var.USERS.append(user)
var.CLOAKS.append(cloak) var.CLOAKS.append(cloak)
@ -412,7 +416,8 @@ def stats(cli, nick, chan, rest):
pl = var.list_players() pl = var.list_players()
if nick in pl: # only do this rate-limiting stuff if the person is in game if nick in pl or var.PHASE == "join":
# only do this rate-limiting stuff if the person is in game
if (var.LAST_STATS and if (var.LAST_STATS and
var.LAST_STATS + timedelta(seconds=var.STATS_RATE_LIMIT) > datetime.now()): var.LAST_STATS + timedelta(seconds=var.STATS_RATE_LIMIT) > datetime.now()):
cli.msg(chan, nick+": This command is rate-limited.") cli.msg(chan, nick+": This command is rate-limited.")
@ -427,7 +432,7 @@ def stats(cli, nick, chan, rest):
else: else:
msg = '{0}: \u00021\u0002 player: {1}'.format(nick, pl[0]) msg = '{0}: \u00021\u0002 player: {1}'.format(nick, pl[0])
if nick in pl: if nick in pl or var.PHASE == "join":
cli.msg(chan, msg) cli.msg(chan, msg)
var.LOGGER.logMessage(msg.replace("\02", "")) var.LOGGER.logMessage(msg.replace("\02", ""))
else: else:
@ -471,7 +476,7 @@ def stats(cli, nick, chan, rest):
", ".join(message[0:-1]), ", ".join(message[0:-1]),
message[-1], message[-1],
vb) vb)
if nick in pl: if nick in pl or var.PHASE == "join":
cli.msg(chan, stats_mssg) cli.msg(chan, stats_mssg)
var.LOGGER.logMessage(stats_mssg.replace("\02", "")) var.LOGGER.logMessage(stats_mssg.replace("\02", ""))
else: else:
@ -724,23 +729,27 @@ def stop_game(cli, winner = ""):
def chk_win(cli): def chk_win(cli):
""" Returns True if someone won """ """ Returns True if someone won """
chan = botconfig.CHANNEL chan = botconfig.CHANNEL
lpl = len(var.list_players()) lpl = len(var.list_players())
lwolves = (len(var.ROLES["wolf"])+
len(var.ROLES["traitor"])+
len(var.ROLES["werecrow"]))
if var.PHASE == "day":
lpl -= len([x for x in var.WOUNDED if x not in var.ROLES["traitor"]])
lwolves -= len([x for x in var.WOUNDED if x in var.ROLES["traitor"]])
if lpl == 0: if lpl == 0:
cli.msg(chan, "No more players remaining. Game ended.") cli.msg(chan, "No more players remaining. Game ended.")
reset(cli) reset(cli)
return True return True
if var.PHASE == "join": if var.PHASE == "join":
return False return False
elif lwolves == lpl / 2:
lwolves = (len(var.ROLES["wolf"])+
len(var.ROLES["traitor"])+
len(var.ROLES["werecrow"]))
if var.PHASE == "day":
lpl -= len([x for x in var.WOUNDED if x not in var.ROLES["traitor"]])
lwolves -= len([x for x in var.WOUNDED if x in var.ROLES["traitor"]])
if lwolves == lpl / 2:
cli.msg(chan, ("Game over! There are the same number of wolves as "+ cli.msg(chan, ("Game over! There are the same number of wolves as "+
"villagers. The wolves eat everyone, and win.")) "villagers. The wolves eat everyone, and win."))
var.LOGGER.logMessage(("Game over! There are the same number of wolves as "+ var.LOGGER.logMessage(("Game over! There are the same number of wolves as "+
@ -2163,7 +2172,7 @@ def start(cli, nick, chan, rest):
def on_error(cli, pfx, msg): def on_error(cli, pfx, msg):
if msg.endswith("(Excess Flood)"): if msg.endswith("(Excess Flood)"):
restart_program(cli, "excess flood") restart_program(cli, "excess flood")
elif msg.endswith("(Client Quit)"): elif msg.startswith("Closing Link:"):
raise SystemExit raise SystemExit

Loading…
Cancel
Save