X-Git-Url: http://gitweb.pimeys.fr/?a=blobdiff_plain;f=hung.py;h=2e0355bbd4287891a8c661e5a88069491a818068;hb=6a20db1444800b9c4023d74e9c4b1e87358ab6b8;hp=96c8a58a6e4b3dd020f36f5dfdbefb6a18778f45;hpb=6df672cc4767aaa6e1510a4d681f4d4f9108fc0c;p=bots%2Fhung.git diff --git a/hung.py b/hung.py index 96c8a58..2e0355b 100755 --- a/hung.py +++ b/hung.py @@ -91,6 +91,8 @@ class Hung(ircbot.SingleServerIRCBot): self.stay_channels=config.stay_channels self.play_channels=config.play_channels self.play_status={i:[None,None,None] for i in self.play_channels} + self.lives={} + self.tried_letters={} self.quiet_channels=config.quiet_channels @@ -128,7 +130,7 @@ class Hung(ircbot.SingleServerIRCBot): test=bot_unicode(message) except UnicodeBotError: if config.utf8_trigger: - serv.privmsg(auteur, config.utf8_fail_answers) + serv.privmsg(auteur, random.choice(config.utf8_fail_answers).encode("utf8")) return message=message.split() cmd=message[0].lower() @@ -192,8 +194,11 @@ class Hung(ircbot.SingleServerIRCBot): if auteur in self.ops: if len(message)>1: if message[1] in self.play_channels: - serv.privmsg(auteur,"Je play déjà sur %s."%(message[1])) - log(self.serveur,"priv",auteur," ".join(message)+"[failed]") + if len(message) > 2: + self.start_partie(serv, message[1], " ".join(message[2:])) + else: + serv.privmsg(auteur,"Je play déjà sur %s."%(message[1])) + log(self.serveur,"priv",auteur," ".join(message)+"[failed]") else: self.play_channels.append(message[1]) self.play_status[message[1]]=[None,None,None] @@ -317,14 +322,20 @@ class Hung(ircbot.SingleServerIRCBot): obfuskated=" ".join([lettre[0] if lettre[1] else "_" for lettre in mot]) serv.privmsg(canal,"%s : %s"%(begin,obfuskated)) - def start_partie(self, serv, canal): - mots=[mot.strip() for mot in open(config.dico_mots).readlines()] - defs=[defi.strip() for defi in open(config.dico_defs).readlines()] - indice = random.randrange(0,len(mots)) - mot,definition=mots[indice],defs[indice] + def start_partie(self, serv, canal, mot=None): + if mot is None: + mots=[mot.strip() for mot in open(config.dico_mots).readlines()] + defs=[defi.strip() for defi in open(config.dico_defs).readlines()] + indice = random.randrange(0,len(mots)) + mot,definition=mots[indice],defs[indice] + else: + definition = "(custom word)" # ' et - sont considérés comme déjà devinés - mot = [(lettre,lettre in "'-()") for lettre in list(mot)] + mot = mot.upper() + mot = [(lettre,lettre in config.non_guess_chars) for lettre in list(mot)] self.play_status[canal]=[mot,definition,{}] + self.tried_letters[canal] = set () + self.lives[canal] = config.lives self.affiche_mot(serv, canal, begin="Devinez") def on_pubmsg(self, serv, ev): @@ -334,8 +345,8 @@ class Hung(ircbot.SingleServerIRCBot): try: test=bot_unicode(message) except UnicodeBotError: - if not canal in self.quiet_channels and config.utf8_trigger: - serv.privmsg(canal, "%s: %s"%(auteur,config.utf8_fail_answers)) + if config.utf8_trigger and not canal in self.quiet_channels: + serv.privmsg(canal, (u"%s: %s"%(auteur,random.choice(config.utf8_fail_answers))).encode("utf8")) return pour_moi,message=self.pourmoi(serv,message) if pour_moi and message.split()!=[]: @@ -391,6 +402,17 @@ class Hung(ircbot.SingleServerIRCBot): self.play_status[canal][2][auteur] = nbtrouvees self.play_status[canal][0]=listeapres self.affiche_mot(serv, canal, begin="%s placé"%(giv_let)) + else: + if not giv_let in self.tried_letters[canal]: + # On perd une chance + self.lives[canal] -= 1 + if self.lives[canal] > 0: + serv.privmsg(canal, "Pas de %s. Plus que %s chances…" % (giv_let, self.lives[canal])) + if self.lives[canal] == 0: + serv.privmsg(canal, "Pas de %s." % (giv_let)) + self.perd(serv, canal) + return + self.tried_letters[canal].add(giv_let) if all([lettre[1] for lettre in listeapres]): self.gagne(serv, canal) @@ -409,7 +431,7 @@ class Hung(ircbot.SingleServerIRCBot): serv.privmsg(canal,"%s: %s"%(auteur,ligne.encode("utf8"))) # on essaye de voir si le mot fourni matche la partie en cours mot = cmd - if self.play_status[canal][0]!=None and is_mot(mot, self.play_status[canal][0]): + if canal in self.play_channels and self.play_status[canal][0]!=None and is_mot(mot, self.play_status[canal][0]): # on a trouvé le mot # on regarde combien de lettre il manquait manquait = sum([not lettre[1] for lettre in self.play_status[canal][0]]) @@ -487,17 +509,27 @@ class Hung(ircbot.SingleServerIRCBot): serv.privmsg(destinataire,"Scores by pseudo : "+" ; ".join(["%s %s"%(k,v) for (k,v) in scores]) ) def gagne(self, serv, canal, bonus=None, bonusvalue=2): - realword="".join([lettre[0] for lettre in self.play_status[canal][0]]) - definition = self.play_status[canal][1] - serv.privmsg(canal,"Bravo ! C'était %s"%(realword)) - serv.privmsg(canal,definition) + serv.privmsg(canal,"Bravo !") + realword = self.reveal_word(serv, canal) nlettre=float(len(realword.replace("'","").replace("-",""))) contribs=["%s:%s%%%s"%(pseudo,str(int(100*contrib/nlettre)),("+bonus(%s)"%(bonusvalue))*(bonus==pseudo)) for pseudo,contrib in self.play_status[canal][2].items()] contribs_score={pseudo:int(10*contrib/nlettre) for pseudo,contrib in self.play_status[canal][2].items()} self.add_score(contribs_score) serv.privmsg(canal,"Contributions : %s"%(" ".join(contribs)) ) self.play_status[canal]=[None,None,None] + + def reveal_word(self, serv, canal): + realword="".join([lettre[0] for lettre in self.play_status[canal][0]]) + serv.privmsg(canal, "C'était %s." % (realword)) + definition = self.play_status[canal][1] + serv.privmsg(canal,definition) + return realword + def perd(self, serv, canal): + serv.privmsg(canal,"Pendu !") + self.reveal_word(serv, canal) + self.play_status[canal]=[None,None,None] + def reload(self, auteur=None): reload(config) if auteur in [None, "SIGHUP"]: