Fixed status command + log module

This commit is contained in:
wr34k 2018-06-29 14:16:26 +02:00
parent 2e6e55a499
commit b021bd8ae8
4 changed files with 60 additions and 43 deletions

View File

@ -100,12 +100,16 @@ class Fight(object):
def getStatus(self, nick): def getStatus(self, nick):
for f in self.fighters: if self.state == 'inactive':
if f.nick == nick: self.IRC.privmsg(nick, "Standby, waiting for a fight to start...")
self.IRC.privmsg(nick, "Status for {} -> Current health: {} | Current stance: {} | Next action: {}".format(f.nick, f.hp, f.stance, f.nextAction)) elif self.state == 'waiting_fighter':
else: self.IRC.privmsg(nick, "Waiting for 2nd opponent, type {}fight to register for the next fight!".format(self.IRC.optkey))
self.IRC.privmsg(nick, "Status for {} -> Current health: {} | Current stance: {}".format(f.nick, f.hp, f.stance)) else:
for f in self.fighters:
if f.nick == nick:
self.IRC.privmsg(nick, "Status for {} -> Current health: {} | Current stance: {} | Next action: {}".format(f.nick, f.hp, f.stance, f.nextAction))
else:
self.IRC.privmsg(nick, "Status for {} -> Current health: {} | Current stance: {}".format(f.nick, f.hp, f.stance))
def fightOver(self): def fightOver(self):

78
irc.py
View File

@ -85,50 +85,62 @@ class Irc(object):
def listen(self): def listen(self):
while True: while True:
if self.lag: try:
self.lag=False if self.lag:
data += self.sock.recv(1024).decode('utf-8', 'ignore') self.lag=False
else: data += self.sock.recv(1024).decode('utf-8', 'ignore')
data = self.sock.recv(1024).decode('utf-8', 'ignore') else:
data = self.sock.recv(1024).decode('utf-8', 'ignore')
for line in [x.split() for x in data.split("\r\n") if len(x.split()) > 1]: for line in [x.split() for x in data.split("\r\n") if len(x.split()) > 1]:
self.log.info("<< {}".format(' '.join(line))) self.log.info("<< {}".format(' '.join(line)))
if line[0][1:] == 'ING': if line[0][1:] == 'ING':
self.raw("PONG {}".format(line[1])) self.raw("PONG {}".format(line[1]))
elif line[1] == '001': # connected elif line[1] == '001': # connected
self.join() self.join()
elif line[1] == 'JOIN': # Someone joined a channel elif line[1] == 'JOIN': # Someone joined a channel
self.log.info("{} JOIN to {}".format(line[0], line[2])) self.log.info("{} JOIN to {}".format(line[0], line[2]))
pass pass
elif line[1] == 'PART': # Someone emopart a channel elif line[1] == 'PART': # Someone emopart a channel
self.log.info("{} PART from {}".format(line[0], line[2])) self.log.info("{} PART from {}".format(line[0], line[2]))
pass pass
elif line[1] == '433': # Nick already in use elif line[1] == '433': # Nick already in use
self.nick += "_" self.nick += "_"
self.updateNick() self.updateNick()
elif line[1] == 'KICK': #Got kicked lmao elif line[1] == 'KICK': #Got kicked lmao
if line[0][1:].split("@")[0].split("!") == self.nick: if line[0][1:].split("@")[0].split("!") == self.nick:
self.log.warn("Got kicked from {} !".format(line[2])) self.log.warn("Got kicked from {} !".format(line[2]))
chan = line[2] chan = line[2]
if chan == self.channel: if chan == self.channel:
self.join() self.join()
elif line[1] == 'INVITE': elif line[1] == 'INVITE':
self.log.info("{} invited the bot to {}".format(line[0][1:].split("!")[0], line[3][1:])) self.log.info("{} invited the bot to {}".format(line[0][1:].split("!")[0], line[3][1:]))
self.join(line[3][1:]) self.join(line[3][1:])
elif line[1] == 'PRIVMSG': elif line[1] == 'PRIVMSG':
nick,user = line[0][1:].split("@")[0].split("!") nick,user = line[0][1:].split("@")[0].split("!")
user = user[1:] if user[0] == '~' else user user = user[1:] if user[0] == '~' else user
host = line[0].split("@")[1] host = line[0].split("@")[1]
self.handle_msg(line[2], self.isAdmin(line[0][1:]), nick, user, host, ' '.join(line[3:])[1:]) self.handle_msg(line[2], self.isAdmin(line[0][1:]), nick, user, host, ' '.join(line[3:])[1:])
except (UnicodeDecodeError, UnicodeEncodeError):
pass
except KeyboardInterrupt:
self.log.warn("^C, Exiting...")
return
except Exception as e:
self.log.error("Exception in listen()", e)
pass
def join(self): def join(self):
self.log.info("Now joining {} ...".format(self.channel)) self.log.info("Now joining {} ...".format(self.channel))

View File

@ -55,6 +55,8 @@ class IrcCommands(object):
self.IRC.raw(" ".join(args)) self.IRC.raw(" ".join(args))
if cmd == 'status':
self.fight.getStatus(chan)
if self.fight.state in ('inactive', 'waiting_fighter'): if self.fight.state in ('inactive', 'waiting_fighter'):
if cmd == 'fight': if cmd == 'fight':
@ -69,9 +71,6 @@ class IrcCommands(object):
else: else:
self.IRC.privmsg(self.IRC.channel, "{}".format(self.IRC.mirc.color("Not here retard your opponent can see your next move!", self.IRC.mirc.colors.LIGHTRED))) self.IRC.privmsg(self.IRC.channel, "{}".format(self.IRC.mirc.color("Not here retard your opponent can see your next move!", self.IRC.mirc.colors.LIGHTRED)))
else:
if cmd == 'status':
self.fight.getStatus(chan)
self.IRC.flood_flag[chan] = False self.IRC.flood_flag[chan] = False
self.IRC.flood_count[chan] = 0 self.IRC.flood_count[chan] = 0

4
log.py
View File

@ -1,5 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import traceback
class Colors(object): class Colors(object):
class Format(object): class Format(object):
@ -60,7 +61,7 @@ class Log(object):
self.func = func self.func = func
def construct(self, *args): def construct(self, *args):
return "".join(a for a in args) return "".join(args)
def info(self, msg): def info(self, msg):
if self.debug: if self.debug:
@ -75,3 +76,4 @@ class Log(object):
self.func( self.construct( "[", self.colors.fg.LIGHTRED, "x", self.colors.fg.DEFAULT, "] ", msg ) ) self.func( self.construct( "[", self.colors.fg.LIGHTRED, "x", self.colors.fg.DEFAULT, "] ", msg ) )
if exception: if exception:
self.func( self.construct( "[", self.colors.fg.LIGHTRED, "x", self.colors.fg.DEFAULT, "] ", str(exception) ) ) self.func( self.construct( "[", self.colors.fg.LIGHTRED, "x", self.colors.fg.DEFAULT, "] ", str(exception) ) )
traceback.print_tb(exception.__traceback__)