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. 19
      oyoyo/client.py
  2. 33
      wolfgame.py

19
oyoyo/client.py

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

33
wolfgame.py

@ -43,9 +43,13 @@ def connect_callback(cli): @@ -43,9 +43,13 @@ def connect_callback(cli):
cli.msg("ChanServ", "op "+botconfig.CHANNEL)
@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):
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.CLOAKS.append(cloak)
@ -412,7 +416,8 @@ def stats(cli, nick, chan, rest): @@ -412,7 +416,8 @@ def stats(cli, nick, chan, rest):
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
var.LAST_STATS + timedelta(seconds=var.STATS_RATE_LIMIT) > datetime.now()):
cli.msg(chan, nick+": This command is rate-limited.")
@ -427,7 +432,7 @@ def stats(cli, nick, chan, rest): @@ -427,7 +432,7 @@ def stats(cli, nick, chan, rest):
else:
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)
var.LOGGER.logMessage(msg.replace("\02", ""))
else:
@ -471,7 +476,7 @@ def stats(cli, nick, chan, rest): @@ -471,7 +476,7 @@ def stats(cli, nick, chan, rest):
", ".join(message[0:-1]),
message[-1],
vb)
if nick in pl:
if nick in pl or var.PHASE == "join":
cli.msg(chan, stats_mssg)
var.LOGGER.logMessage(stats_mssg.replace("\02", ""))
else:
@ -727,20 +732,24 @@ def chk_win(cli): @@ -727,20 +732,24 @@ def chk_win(cli):
chan = botconfig.CHANNEL
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:
cli.msg(chan, "No more players remaining. Game ended.")
reset(cli)
return True
if var.PHASE == "join":
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 "+
"villagers. The wolves eat everyone, and win."))
var.LOGGER.logMessage(("Game over! There are the same number of wolves as "+
@ -2163,7 +2172,7 @@ def start(cli, nick, chan, rest): @@ -2163,7 +2172,7 @@ def start(cli, nick, chan, rest):
def on_error(cli, pfx, msg):
if msg.endswith("(Excess Flood)"):
restart_program(cli, "excess flood")
elif msg.endswith("(Client Quit)"):
elif msg.startswith("Closing Link:"):
raise SystemExit

Loading…
Cancel
Save