Palmares! And lot of new stuff

This commit is contained in:
wr34k 2018-07-03 17:20:29 +02:00
parent 3cac7225e3
commit 4993f405b4
4 changed files with 78 additions and 22 deletions

View File

@ -1,9 +1,12 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import random, json, os
from fighter import Fighter from fighter import Fighter
import random, json
CONFIG_FILE = "assets/config.json" CONFIG_FILE = "assets/config.json"
PALMARES_FILE = "assets/palmares.json"
class Fight(object): class Fight(object):
def __init__(self, IRC): 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] 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] 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("{}{}".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.BOLD,
self.IRC.mirc.color(winner.nick, winner.colour), \ self.IRC.mirc.color(winner.nick, winner.colour), \
self.IRC.mirc.color(looser.nick, looser.colour), \ self.IRC.mirc.color(looser.nick, looser.colour), \
self.IRC.mirc.color(int(winner.hp), self.IRC.mirc.colors.GREEN) ) \ 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.state = 'inactive'
self.fighters = [] self.fighters = []
@ -169,7 +189,6 @@ class Fight(object):
return dmg, mindmg, blockidx, fallchance, standchance, texts return dmg, mindmg, blockidx, fallchance, standchance, texts
def attack(self): def attack(self):
roll1 = roll2 = 0 roll1 = roll2 = 0
while roll1 == roll2: while roll1 == roll2:
roll1 = random.random() roll1 = random.random()
@ -264,3 +283,38 @@ class Fight(object):
for f in self.fighters: for f in self.fighters:
self.IRC.privmsg(f.nick, msg) self.IRC.privmsg(f.nick, msg)
self.IRC.privmsg(self.IRC.channel, 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))

View File

@ -24,7 +24,7 @@
"dmgidx": 40, "dmgidx": 40,
"mindmg": 20, "mindmg": 20,
"fallchance": 5, "fallchance": 5,
"blockidx": 40, "blockidx": 50,
"text": [ "text": [
"%attacker% head kicks %defender%" "%attacker% head kicks %defender%"
] ]
@ -43,7 +43,7 @@
"dmgidx": 30, "dmgidx": 30,
"mindmg": 10, "mindmg": 10,
"fallchance": 8, "fallchance": 8,
"blockidx": 28, "blockidx": 38,
"text": [ "text": [
"%attacker% middle kicks %defender%" "%attacker% middle kicks %defender%"
] ]
@ -51,7 +51,7 @@
"ground": { "ground": {
"dmgidx": 45, "dmgidx": 45,
"mindmg": 20, "mindmg": 20,
"blockidx": 34, "blockidx": 32,
"text": [ "text": [
"%attacker% stomps %defender% in the body!" "%attacker% stomps %defender% in the body!"
] ]
@ -94,7 +94,7 @@
"dmgidx": 20, "dmgidx": 20,
"mindmg": 5, "mindmg": 5,
"fallchance": 4, "fallchance": 4,
"blockidx": 15, "blockidx": 21,
"text": [ "text": [
"%attacker% punches %defender% to the body" "%attacker% punches %defender% to the body"
] ]
@ -125,7 +125,7 @@
"ground": { "ground": {
"standup": { "standup": {
"stand": { "stand": {
"chance": 30 "chance": 20
}, },
"ground": { "ground": {
"chance": 70 "chance": 70
@ -137,7 +137,7 @@
"dmgidx": 30, "dmgidx": 30,
"mindmg": 10, "mindmg": 10,
"fallchance": 8, "fallchance": 8,
"blockidx": 40, "blockidx": 58,
"text": [ "text": [
"%attacker% head kicks %defender% from the ground!" "%attacker% head kicks %defender% from the ground!"
] ]
@ -145,7 +145,7 @@
"ground": { "ground": {
"dmgidx": 40, "dmgidx": 40,
"mindmg": 23, "mindmg": 23,
"blockidx": 55, "blockidx": 42,
"text": [ "text": [
"%attacker% hit %defender% with huge kneel kicks to the head!" "%attacker% hit %defender% with huge kneel kicks to the head!"
] ]
@ -156,7 +156,7 @@
"dmgidx": 20, "dmgidx": 20,
"mindmg": 5, "mindmg": 5,
"fallchance": 5, "fallchance": 5,
"blockidx": 40, "blockidx": 43,
"text": [ "text": [
"%attacker% middle kicks %defender% from the ground!" "%attacker% middle kicks %defender% from the ground!"
] ]
@ -164,7 +164,7 @@
"ground": { "ground": {
"dmgidx": 25, "dmgidx": 25,
"mindmg": 17, "mindmg": 17,
"blockidx": 50, "blockidx": 48,
"text": [ "text": [
"%attacker% hit %defender% with kneel kicks to the body" "%attacker% hit %defender% with kneel kicks to the body"
] ]
@ -175,7 +175,7 @@
"dmgidx": 15, "dmgidx": 15,
"mindmg": 3, "mindmg": 3,
"fallchance": 18, "fallchance": 18,
"blockidx": 45, "blockidx": 33,
"text": [ "text": [
"%attacker% low kicks %defender% from the ground!" "%attacker% low kicks %defender% from the ground!"
] ]
@ -183,7 +183,7 @@
"ground": { "ground": {
"dmgidx": 15, "dmgidx": 15,
"mindmg": 2, "mindmg": 2,
"blockidx": 30, "blockidx": 40,
"text": [ "text": [
"%attacker% hit %defender% with kneel kicks to the legs" "%attacker% hit %defender% with kneel kicks to the legs"
] ]

View File

@ -96,8 +96,10 @@ class IrcBot(object):
else: else:
try: try:
getattr(ircEvents, "event{}".format(line[1].upper()))(self, line) if hasattr(ircEvents, "event{}".format(line[1].upper())):
except: getattr(ircEvents, "event{}".format(line[1].upper()))(self, line)
except Exception as e:
self.log.error("Error in getattr()", e)
pass pass
except (UnicodeDecodeError, UnicodeEncodeError): except (UnicodeDecodeError, UnicodeEncodeError):

View File

@ -8,16 +8,16 @@ def eventPING(IRC, line):
def event001(IRC, line): def event001(IRC, line):
IRC.join() IRC.join()
def event433(IRC, line):
IRC.nick += "_"
IRC.updateNick()
def eventJOIN(IRC, line): def eventJOIN(IRC, line):
IRC.log.info("{} JOIN to {}".format(line[0], line[2])) IRC.log.info("{} JOIN to {}".format(line[0], line[2]))
def eventPART(IRC, line): def eventPART(IRC, line):
IRC.log.info("{} PART from {}".format(line[0], line[2])) IRC.log.info("{} PART from {}".format(line[0], line[2]))
def event433(IRC, line):
IRC.nick += "_"
IRC.updateNick()
def eventKICK(IRC, line): def eventKICK(IRC, line):
if line[3] == IRC.nick: if line[3] == IRC.nick:
IRC.log.warn("Got kicked from {} !".format(line[2])) IRC.log.warn("Got kicked from {} !".format(line[2]))