From 7f9b3b4d66f1c4b993c81fba70074494e55c5c49 Mon Sep 17 00:00:00 2001 From: Hung IRC Bot Date: Wed, 16 May 2012 10:54:56 +0200 Subject: [PATCH] +scores (dans un fichier) --- hung.py | 37 ++++++++++++++++++++++++++++++++++++- scores.pickle | 5 +++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 scores.pickle diff --git a/hung.py b/hung.py index f9917cc..b49fc62 100755 --- a/hung.py +++ b/hung.py @@ -38,6 +38,9 @@ config_report_bugs_to=["[20-100]"] config_dico_mots="mots.txt" config_dico_defs="definitions.txt" +config_scores_file="scores.pickle" + + def log(serveur,channel,auteur=None,message=None): f=open(get_config_logfile(serveur),"a") if auteur==message==None: @@ -248,6 +251,8 @@ class Hung(ircbot.SingleServerIRCBot): serv.privmsg(auteur,"Syntaxe : DO ") else: notunderstood=True + elif cmd in ["score","scores"]: + self.send_scores(serv,auteur) else: notunderstood=True if notunderstood: @@ -337,10 +342,13 @@ class Hung(ircbot.SingleServerIRCBot): 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]] - + 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)) else: @@ -358,6 +366,33 @@ class Hung(ircbot.SingleServerIRCBot): return mypseudo=serv.get_nickname() + def get_scores(self): + f=open(config_scores_file) + scores=pickle.load(f) + f.close() + return scores + def save_scores(self,scores): + f=open(config_scores_file,'w') + pickle.dump(scores,f) + f.close() + def add_score(self,dico): + scores=self.get_scores() + for k,v in dico.items(): + if scores.has_key(k): + scores[k]+=v + else: + scores[k]=v + self.save_scores(scores) + def send_scores(self, serv, destinataire): + scores=self.get_scores() + scores=scores.items() + scores.sort(lambda x,y:cmp(x[1],y[1])) + scores.reverse() + serv.privmsg(destinataire,"Scores by score : "+" ; ".join(["%s %s"%(k,v) for (k,v) in scores]) ) + 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]) ) + + if __name__=="__main__": import sys if len(sys.argv)==1: diff --git a/scores.pickle b/scores.pickle new file mode 100644 index 0000000..242f633 --- /dev/null +++ b/scores.pickle @@ -0,0 +1,5 @@ +(dp0 +S'[20-100]' +p1 +I20 +s. \ No newline at end of file -- 2.39.2