From 8243e861c8da302b95eb7cb11943895c70046ec1 Mon Sep 17 00:00:00 2001 From: Vincent Le Gallic Date: Mon, 21 May 2012 02:04:23 +0200 Subject: [PATCH] =?utf8?q?Cr=C3=A9ation=20des=20modes=20level=202=20et=203?= =?utf8?q?=20(avec=20respectivement=20Petite-Peste=20et=20[20-100])?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- deconnaisseur.py | 38 +++++++++++++++++++------------------- remplace_accents.py | 25 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 19 deletions(-) create mode 100644 remplace_accents.py diff --git a/deconnaisseur.py b/deconnaisseur.py index 9726d66..02310f7 100755 --- a/deconnaisseur.py +++ b/deconnaisseur.py @@ -12,6 +12,7 @@ import random import time import pickle import re +from remplace_accents import remplace_accents config_password="PatrickSébastien" config_pseudo="deconnaisseur" @@ -41,6 +42,9 @@ u"Héhé, try again", u"Non, j'ai pas envie", u"Peut-être quand toi tu la fermeras, et encore…"] +config_level2=["Petite-Peste"] +config_level3=["[20-100]"] + class UnicodeBotError(Exception): pass def bot_unicode(chain): @@ -61,17 +65,13 @@ def log(serveur,channel="prout",auteur=None,message=None): a=0 # does nothing -def tolere(regexp): - """Renvoie une regexp plus tolérante""" - reg=unicode(regexp,"utf8").lower() - reg=reg.replace(u"á",u"(á|a)").replace(u"à",u"(à|a)").replace(u"â",u"(â|a)").replace(u"ä",u"(ä|a)") - reg=reg.replace(u"é",u"(é|e)").replace(u"è",u"(è|e)").replace(u"ê",u"(ê|e)").replace(u"ë",u"(ë|e)") - reg=reg.replace(u"í",u"(í|i)").replace(u"ì",u"(ì|i)").replace(u"î",u"(î|i)").replace(u"ï",u"(ï|i)") - reg=reg.replace(u"ó",u"(ó|o)").replace(u"ò",u"(ò|o)").replace(u"ô",u"(ô|o)").replace(u"ö",u"(ö|o)") - reg=reg.replace(u"ú",u"(ú|u)").replace(u"ù",u"(ù|u)").replace(u"û",u"(û|u)").replace(u"ü",u"(ü|u)") - reg=reg.replace(u"ý",u"(ý|y)").replace(u"ỳ",u"(ỳ|y)").replace(u"ŷ",u"(ŷ|y)").replace(u"ÿ",u"(ÿ|y)") - reg=reg.replace(u"œ",u"(œ|oe)").replace(u"æ",u"(æ|ae)") - return reg +def reussi(message,answer,answer_regexp,auteur): + if auteur in config_level3: + return answer in message + if auteur in config_level2: + return remplace_accents(answer) in message + else: + return re.match(answer_regexp.lower(),remplace_accents(message).lower()) def is_something(chain,matches,avant=u".*(?:^| )",apres=u"(?:$|\.| |,|;).*",case_sensitive=False,debug=False): if case_sensitive: @@ -138,7 +138,7 @@ class Deconnaisseur(ircbot.SingleServerIRCBot): if ok=="do_it": enigme,indice,answer_reg,answer=self.get_enigme() print "%s; %s; %s; %s"%(enigme, indice, answer_reg, answer) - serv.privmsg(channel,enigme) + serv.privmsg(channel,enigme.encode("utf8")) token=time.time() self.play_status[channel]=[1,enigme,indice,answer_reg,answer,token] serv.execute_delayed(random.randrange(ttrig*3,ttrig*5),self.give_indice,(serv,channel,token)) @@ -152,13 +152,13 @@ class Deconnaisseur(ircbot.SingleServerIRCBot): token=self.play_status[channel][-1] if self.play_status[channel][-1]==token: indice=self.play_status[channel][2] - serv.privmsg(channel,"indice : %s"%(indice)) + serv.privmsg(channel,"indice : %s"%(indice).encode("utf8")) self.play_status[channel][0]=2 serv.execute_delayed(random.randrange(ttrig*1,ttrig*3),self.give_answer,(serv,channel,token)) def give_answer(self,serv,channel,token): if self.play_status[channel][0]==2 and self.play_status[channel][-1]==token: answer=self.play_status[channel][4] - serv.privmsg(channel,"C'était : %s"%(answer)) + serv.privmsg(channel,"C'était : %s"%(answer).encode("utf8")) token=time.time() self.play_status[channel]=[0,token] serv.execute_delayed(random.randrange(Ttrig*5,Ttrig*10),self.start_enigme,(serv,channel,token)) @@ -188,7 +188,7 @@ class Deconnaisseur(ircbot.SingleServerIRCBot): f=open(played_file,"w") f.write("\n".join(["%-3s : %s"%(k,v) for k,v in played.items()])) f.close() - return enigme,indice,answer_reg,answer + return enigme.decode("utf8"),indice.decode("utf8"),answer_reg.decode("utf8"),answer.decode("utf8") def pourmoi(self, serv, message): pseudo=serv.get_nickname() @@ -346,7 +346,7 @@ class Deconnaisseur(ircbot.SingleServerIRCBot): elif cmd in ["states","status"]: if auteur in self.overops: for k in self.play_status.keys(): - serv.privmsg(auteur,"%s : %s"%(k,"; ".join([str(i) for i in self.play_status[k]]))) + serv.privmsg(auteur,(u"%s : %s"%(k,"; ".join([str(i) for i in self.play_status[k]]))).encode("utf8") ) elif cmd=="say": if auteur in self.overops and len(message)>2: serv.privmsg(message[1]," ".join(message[2:])) @@ -513,9 +513,9 @@ class Deconnaisseur(ircbot.SingleServerIRCBot): if tryother: if self.play_status.get(canal,[-1])[0] in [1,2]: answer_regexp=self.play_status[canal][3] - if re.match(tolere(answer_regexp),unicode(message,"utf8").lower()): - answer=self.play_status[canal][4] - serv.privmsg(canal,"%s: bravo ! (C'était %s)"%(auteur,answer)) + answer=self.play_status[canal][4] + if reussi(message.decode("utf8"),answer,answer_regexp,auteur): + serv.privmsg(canal,(u"%s: bravo ! (C'était %s)"%(auteur,answer)).encode("utf8")) self.add_score(auteur,1) token=time.time() self.play_status[canal]=[0,token] diff --git a/remplace_accents.py b/remplace_accents.py new file mode 100644 index 0000000..85ba24e --- /dev/null +++ b/remplace_accents.py @@ -0,0 +1,25 @@ +#!/usr/bin/python +# -*- coding:utf8 -*- + +# Tout petit script à importé partout où on en a besoin +# pour enlevé les accents/œ/æ/trémas d'une chaîne unicode + +def remplace_accents(chaine): + remplacements = { + u"á":u"a", u"à":u"a", u"â":u"a", u"ä":u"a", + 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"E", u"È":u"E", u"Ê":u"E", u"Ë":u"E", + u"í":u"i", u"ì":u"i", u"î":u"i", u"ï":u"i", + 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"O", u"Ò":u"O", u"Ô":u"O", u"Ö":u"O", + u"ú":u"u", u"ù":u"u", u"û":u"u", u"ü":u"u", + 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"Y", u"Ỳ":u"Y", u"Ŷ":u"Y", u"Ÿ":u"Y", + u"œ":u"oe", u"æ":u"ae", + u"Œ":u"OE", u"Æ":u"AE"} + for avant,apres in remplacements.items(): + chaine=chaine.replace(avant,apres) + return chaine -- 2.39.2