diff --git a/IRCUFC/fight.py b/IRCUFC/fight.py index 6b01a93..e7d44cd 100644 --- a/IRCUFC/fight.py +++ b/IRCUFC/fight.py @@ -1,9 +1,12 @@ #!/usr/bin/env python3 +import random, json, os + from fighter import Fighter -import random, json CONFIG_FILE = "assets/config.json" +PALMARES_FILE = "assets/palmares.json" + class Fight(object): def __init__(self, IRC): @@ -123,17 +126,34 @@ class Fight(object): winner = self.fighters[0] if self.fighters[1].hp <= 0 else self.fighters[1] looser = self.fighters[0] if self.fighters[0].hp <= 0 else self.fighters[1] - winner.wins += 1 - looser.looses += 1 + + self.updatePalmares(winner, looser) + self.shout("{}{}".format(self.IRC.mirc.BOLD, self.IRC.mirc.color("Fight is over.", self.IRC.mirc.colors.LIGHTGREY))) - self.shout("{}{} won the fight against {} with {} hp left.".format( \ + + self.IRC.privmsg(self.IRC.channel, "{}{} won the fight against {} with {} hp left.".format( \ self.IRC.mirc.BOLD, self.IRC.mirc.color(winner.nick, winner.colour), \ self.IRC.mirc.color(looser.nick, looser.colour), \ self.IRC.mirc.color(int(winner.hp), self.IRC.mirc.colors.GREEN) ) \ ) + self.IRC.privmsg(self.IRC.channel, "{}{} wins: {}, looses: {}".format( \ + self.IRC.mirc.BOLD, \ + self.IRC.mirc.color(winner.nick, self.IRC.mirc.colors.YELLOW), \ + self.IRC.mirc.color(winner.wins, self.IRC.mirc.colors.GREEN), \ + self.IRC.mirc.color(winner.looses, self.IRC.mirc.colors.RED) \ + )) + + self.IRC.privmsg(self.IRC.channel, "{}{} wins: {}, looses: {}".format( \ + self.IRC.mirc.BOLD, \ + self.IRC.mirc.color(looser.nick, self.IRC.mirc.colors.YELLOW), \ + self.IRC.mirc.color(looser.wins, self.IRC.mirc.colors.GREEN), \ + self.IRC.mirc.color(looser.looses, self.IRC.mirc.colors.RED) \ + )) + + self.state = 'inactive' self.fighters = [] @@ -169,7 +189,6 @@ class Fight(object): return dmg, mindmg, blockidx, fallchance, standchance, texts def attack(self): - roll1 = roll2 = 0 while roll1 == roll2: roll1 = random.random() @@ -264,3 +283,38 @@ class Fight(object): for f in self.fighters: self.IRC.privmsg(f.nick, msg) self.IRC.privmsg(self.IRC.channel, msg) + + def updatePalmares(self, winner, looser): + + mode = "r" if os.path.exists(PALMARES_FILE) else "w+" + with open(PALMARES_FILE, mode) as f: + try: + palmares = json.loads(f.read()) + except Exception as e: + self.IRC.log.error("Error in json.loads() backuping palmares file to {}.bkp".format(PALMARES_FILE), e) + from shutil import copyfile + copyfile(PALMARES_FILE, "{}.bkp".format(PALMARES_FILE)) + palmares = {} + + if winner.nick not in palmares: + palmares[winner.nick] = {} + palmares[winner.nick]["wins"] = 0 + palmares[winner.nick]["looses"] = 0 + + palmares[winner.nick]["wins"] += 1 + + if looser.nick not in palmares: + palmares[looser.nick] = {} + palmares[looser.nick]["wins"] = 0 + palmares[looser.nick]["looses"] = 0 + + palmares[looser.nick]["looses"] += 1 + + winner.wins = palmares[winner.nick]["wins"] + winner.looses = palmares[winner.nick]["looses"] + looser.wins = palmares[looser.nick]["wins"] + looser.looses = palmares[looser.nick]["looses"] + + with open(PALMARES_FILE, "w+") as f: + f.write(json.dumps(palmares)) + diff --git a/assets/config.json b/assets/config.json index 35fd112..8b42043 100644 --- a/assets/config.json +++ b/assets/config.json @@ -24,7 +24,7 @@ "dmgidx": 40, "mindmg": 20, "fallchance": 5, - "blockidx": 40, + "blockidx": 50, "text": [ "%attacker% head kicks %defender%" ] @@ -43,7 +43,7 @@ "dmgidx": 30, "mindmg": 10, "fallchance": 8, - "blockidx": 28, + "blockidx": 38, "text": [ "%attacker% middle kicks %defender%" ] @@ -51,7 +51,7 @@ "ground": { "dmgidx": 45, "mindmg": 20, - "blockidx": 34, + "blockidx": 32, "text": [ "%attacker% stomps %defender% in the body!" ] @@ -94,7 +94,7 @@ "dmgidx": 20, "mindmg": 5, "fallchance": 4, - "blockidx": 15, + "blockidx": 21, "text": [ "%attacker% punches %defender% to the body" ] @@ -125,7 +125,7 @@ "ground": { "standup": { "stand": { - "chance": 30 + "chance": 20 }, "ground": { "chance": 70 @@ -137,7 +137,7 @@ "dmgidx": 30, "mindmg": 10, "fallchance": 8, - "blockidx": 40, + "blockidx": 58, "text": [ "%attacker% head kicks %defender% from the ground!" ] @@ -145,7 +145,7 @@ "ground": { "dmgidx": 40, "mindmg": 23, - "blockidx": 55, + "blockidx": 42, "text": [ "%attacker% hit %defender% with huge kneel kicks to the head!" ] @@ -156,7 +156,7 @@ "dmgidx": 20, "mindmg": 5, "fallchance": 5, - "blockidx": 40, + "blockidx": 43, "text": [ "%attacker% middle kicks %defender% from the ground!" ] @@ -164,7 +164,7 @@ "ground": { "dmgidx": 25, "mindmg": 17, - "blockidx": 50, + "blockidx": 48, "text": [ "%attacker% hit %defender% with kneel kicks to the body" ] @@ -175,7 +175,7 @@ "dmgidx": 15, "mindmg": 3, "fallchance": 18, - "blockidx": 45, + "blockidx": 33, "text": [ "%attacker% low kicks %defender% from the ground!" ] @@ -183,7 +183,7 @@ "ground": { "dmgidx": 15, "mindmg": 2, - "blockidx": 30, + "blockidx": 40, "text": [ "%attacker% hit %defender% with kneel kicks to the legs" ] diff --git a/irc/irc.py b/irc/irc.py index d753121..5b4725e 100644 --- a/irc/irc.py +++ b/irc/irc.py @@ -96,8 +96,10 @@ class IrcBot(object): else: try: - getattr(ircEvents, "event{}".format(line[1].upper()))(self, line) - except: + if hasattr(ircEvents, "event{}".format(line[1].upper())): + getattr(ircEvents, "event{}".format(line[1].upper()))(self, line) + except Exception as e: + self.log.error("Error in getattr()", e) pass except (UnicodeDecodeError, UnicodeEncodeError): diff --git a/irc/ircEvents.py b/irc/ircEvents.py index 90bcf88..1866c8f 100644 --- a/irc/ircEvents.py +++ b/irc/ircEvents.py @@ -8,16 +8,16 @@ def eventPING(IRC, line): def event001(IRC, line): IRC.join() +def event433(IRC, line): + IRC.nick += "_" + IRC.updateNick() + def eventJOIN(IRC, line): IRC.log.info("{} JOIN to {}".format(line[0], line[2])) def eventPART(IRC, line): IRC.log.info("{} PART from {}".format(line[0], line[2])) -def event433(IRC, line): - IRC.nick += "_" - IRC.updateNick() - def eventKICK(IRC, line): if line[3] == IRC.nick: IRC.log.warn("Got kicked from {} !".format(line[2]))