]> gitweb.pimeys.fr Git - bots/hung.git/commitdiff
Gestion du mot complet + score bonus si on le donne
authorHung IRC Bot <hung@crans.org>
Wed, 16 May 2012 11:37:23 +0000 (13:37 +0200)
committerHung IRC Bot <hung@crans.org>
Wed, 16 May 2012 11:37:23 +0000 (13:37 +0200)
hung.py
scores.pickle

diff --git a/hung.py b/hung.py
index b49fc62a8317fd6dc2e0729eab905f70026e05d3..6e1b5cf44f52184a6988584fe2c0cd719cf792e8 100755 (executable)
--- a/hung.py
+++ b/hung.py
@@ -61,6 +61,14 @@ def bot_unicode(chain):
     except UnicodeDecodeError as exc:
         raise UnicodeBotError
 
+def remplace_accents(chaine):
+    chaine=chaine.lower()
+    remplacements = {u"á":u"a",u"à":u"a",u"â":u"a",u"ä":u"a",u"é":u"e",u"è":u"e",u"ê":u"e",u"ë":u"e",u"í":u"i",u"ì":u"i",u"î":u"i",u"ï":u"i",u"ó":u"o",   u"ò":u"o",u"ô":u"o",u"ö":u"o",u"ú":u"u",u"ù":u"u",u"û":u"u",u"ü":u"u",u"ý":u"y",u"ỳ":u"y",u"ŷ":u"y",u"ÿ":u"y",u"œ":u"oe",u"æ":u"ae"}
+    for avant,apres in remplacements.items():
+        chaine=chaine.replace(avant,apres)
+    return chaine
+
+
 class Hung(ircbot.SingleServerIRCBot):
     def __init__(self,serveur,debug=False):
         temporary_pseudo=config_irc_pseudo+str(random.randrange(10000,100000))
@@ -152,7 +160,7 @@ class Hung(ircbot.SingleServerIRCBot):
                         log(self.serveur,"priv",auteur," ".join(message)+"[failed]")
                     else:
                         self.play_channels.append(message[1])
-                        self.play_status[message[1]]=[[None,None,None]]
+                        self.play_status[message[1]]=[None,None,None]
                         serv.privmsg(auteur,"Play channels : "+" ".join(self.play_channels))
                         log(self.serveur,"priv",auteur," ".join(message)+"[successful]")
                 else:
@@ -201,7 +209,10 @@ class Hung(ircbot.SingleServerIRCBot):
         elif cmd in ["states","status"]:
             if auteur in self.overops:
                 for k in self.play_status.keys():
-                    serv.privmsg(auteur,"%s : %s (%s) [%s]"%(k,"".join([str(i[0]) for i in self.play_status[k][0]])
+                    if self.play_status[k]==[None,None,None]:
+                        serv.privmsg(auteur,"None")
+                    else:
+                        serv.privmsg(auteur,"%s : %s (%s) [%s]"%(k,"".join([str(i[0]) for i in self.play_status[k][0]])
                                                      ,self.play_status[k][1], self.play_status[k][2]))
         elif cmd=="die":
             if auteur in self.overops:
@@ -312,17 +323,17 @@ class Hung(ircbot.SingleServerIRCBot):
             elif cmd in ["play","jeu","encore","again","partie","pendu","game","mot","go","allez"]:
                 if not canal in self.quiet_channels and canal in self.play_channels:
                     if self.play_status.has_key(canal):
-                        if self.play_status[canal]==[[None,None,None]]:
+                        if self.play_status[canal]==[None,None,None]:
                             self.start_partie(serv, canal)
                         else:
                             self.affiche_mot(serv, canal, begin="%s: Rappel"%(auteur))
                     else:
-                        self.play_status[canal]=[[None,None,None]]
+                        self.play_status[canal]=[None,None,None]
                         self.start_partie(serv, canal)
                 elif not canal in self.play_channels:
                     serv.privmsg(canal,"%s: pas ici…"%(auteur))
             elif (cmd in list("azertyuiopqsdfghjklmwxcvbn") and canal in self.play_channels 
-                  and self.play_status.has_key(canal) and self.play_status[canal]!=[[None,None,None]]):
+                  and self.play_status.has_key(canal) and self.play_status[canal]!=[None,None,None]):
                 giv_let=cmd.upper()
                 liste=self.play_status[canal][0]
                 listeapres=[(lettre[0],lettre[1] or lettre[0]==giv_let) for lettre in liste]
@@ -336,24 +347,33 @@ class Hung(ircbot.SingleServerIRCBot):
                     self.play_status[canal][0]=listeapres
                     self.affiche_mot(serv, canal, begin="%s placé"%(giv_let))
                 if all([lettre[1] for lettre in listeapres]):
-                    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)
-                    nlettre=float(len(realword.replace("'","").replace("-","")))
-                    contribs=["%s:%s%%"%(pseudo,int(100*contrib/nlettre)) 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]]
+                    self.gagne(serv, canal)
                     
             elif cmd in ["score","scores","!score","!scores"]:
                 self.send_scores(serv,auteur)
             if cmd in ["meur", "meurt","meurre","meurres"] and not canal in self.quiet_channels:
                 serv.privmsg(canal,'%s: Mourir, impératif, 2ème personne du singulier : "meurs" (de rien)'%(auteur))
+            # on essaye de voir si le mot fourni matche la partie en cours
+            mot = cmd
+            # bon, ce teste merde et j'arrive pas à trouver pourquoi, alors j'ai craqué, je l'ai mis dans un try
+            # bouh ! beurk ! pas bien ! promis, j'irai me flageller…
+            try:
+                if remplace_accents(mot)==("".join([i[0] for i in self.play_status[canal][0]])).lower():
+                    # 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]])
+                    self.add_score({auteur: manquait})
+                    if self.play_status[canal][2].has_key(auteur):
+                        self.play_status[canal][2][auteur]+=manquait
+                    else:
+                        self.play_status[canal][2][auteur]=manquait
+                    self.gagne(serv, canal, bonus=auteur, bonusvalue=manquait)
+            except:
+                pass
         else:
             pass
 
+
     def on_action(self, serv, ev):
         action = ev.arguments()[0]
         auteur = irclib.nm_to_n(ev.source())
@@ -392,6 +412,18 @@ class Hung(ircbot.SingleServerIRCBot):
         scores.sort(lambda x,y:cmp(x[0].lower(),y[0].lower()))
         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)
+        nlettre=float(len(realword.replace("'","").replace("-","")))
+        contribs=["%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]
+
 
 if __name__=="__main__":
     import sys
index 242f633269bab0fbaf18ff304da83d4e09ca5357..0700664c83b65d7985560f98b1c9510b7554efaf 100644 (file)
@@ -1,5 +1,17 @@
 (dp0
-S'[20-100]'
+S'Gaetan'
 p1
-I20
-s.
\ No newline at end of file
+I10
+sS'Petite-Peste'
+p2
+I21
+sS'[20-100]'
+p3
+I10
+sS'Echec'
+p4
+I10
+sS'pika'
+p5
+I8
+s.