From cab54b2b900984ac191f47a8e61f4488aafae718 Mon Sep 17 00:00:00 2001 From: Stefan Krulj Date: Fri, 7 Dec 2012 00:19:29 +0100 Subject: [PATCH] vote module added --- modules/active/logger.py | 2 +- modules/active/vote.py | 102 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 modules/active/vote.py diff --git a/modules/active/logger.py b/modules/active/logger.py index 3a4afc3..de8f165 100644 --- a/modules/active/logger.py +++ b/modules/active/logger.py @@ -70,7 +70,7 @@ def status_logger(mfrom,msg,status): if status==brathuhn.STATUS_OFFLINE: log.write(mfrom.getResource()+" left\n") if status==brathuhn.STATUS_RENAME: - log.write(mfrom.getResource()+" is now known as "+msg.getName()+"\n") + log.write(mfrom.getResource()+" is now known as "+msg.getNick()+"\n") if status==brathuhn.STATUS_XAWAY: log.write(mfrom.getResource()+" is now extended away\n") if status==brathuhn.STATUS_AWAY: diff --git a/modules/active/vote.py b/modules/active/vote.py new file mode 100644 index 0000000..a143468 --- /dev/null +++ b/modules/active/vote.py @@ -0,0 +1,102 @@ +import brathuhn, datetime, time +from StringIO import StringIO + +from persistent import PersistentContainer +import config +pcontainer = PersistentContainer(__name__,config.PCACHE) + +if not "vote_pending" in pcontainer: + pcontainer["vote_pending"] = False + + +def new_vote(args): + if pcontainer["vote_pending"]: + brathuhn.sendMsg(brathuhn.ROOM,"There's already a voting going on!",'groupchat') + return + + #args[0] = "!vote", args[1] = new, args[2] = topic, args[3...n] = options + pcontainer["topic"] = args[2] + pcontainer["votes"] = dict() + tmp = dict() + for i in args[3:]: + tmp[i] = 0 + pcontainer["options"] = tmp + + pcontainer["vote_pending"] = True + brathuhn.sendMsg(brathuhn.ROOM,'Hear, hear folks of %s! Thou\'ve been called to a voting on "%s".' % (brathuhn.ROOM, args[2]),'groupchat' ) + +def attend_vote( mfrom ,args): + #args[0] = "!vote", args[1] = attend, args[2] = option + if not pcontainer["vote_pending"]: + brathuhn.sendMsg(brathuhn.ROOM,"There is no vote at the moment.",'groupchat') + return + if mfrom in pcontainer["votes"]: + brathuhn.sendMsg(brathuhn.ROOM,"You already voted!",'groupchat') + return + + tmp = pcontainer["options"] + if not args[2] in tmp: + brathuhn.sendMsg(brathuhn.ROOM,'I don\'t understand your option!','groupchat') + return + tmp[args[2]] += 1 + pcontainer["options"] = tmp + + tmp = pcontainer["votes"] + tmp[mfrom] = True + pcontainer["votes"] = tmp + + brathuhn.sendMsg(brathuhn.ROOM,'Thank you for your vote!','groupchat') + + +def vote_statistics(): + if not pcontainer["vote_pending"]: + brathuhn.sendMsg(brathuhn.ROOM,"There is no vote at the moment.",'groupchat') + return + tmp = pcontainer["options"] + + brathuhn.sendMsg(brathuhn.ROOM, 'There is a vote on "'+pcontainer["topic"]+'"','groupchat' ) + brathuhn.sendMsg(brathuhn.ROOM, 'Votes so far:','groupchat' ) + for k in tmp: + brathuhn.sendMsg(brathuhn.ROOM,' %s: %d' % (k, tmp[k]),'groupchat' ) + +def end(): + vote_statistics() + pcontainer["topic"] = None + pcontainer["options"] = None + pcontainer["vote_pending"] = False + +def usage(args): + brathuhn.sendMsg(brathuhn.ROOM,"Usage: "+args[0]+" (new topic option1 option2 ... option_n|attend num|show|end)",'groupchat') + +def status_logger(mfrom,msg,status): + if pcontainer["vote_pending"] and status==brathuhn.STATUS_RENAME and mfrom.getResource() in pcontainer["votes"]: + tmp = pcontainer["votes"] + tmp[msg.getNick()] = tmp[mfrom.getResource()] + del tmp[mfrom.getResource()] + pcontainer["votes"] = tmp + brathuhn.sendMsg(brathuhn.ROOM,"%s, the vote comissioner is watching you!" % msg.getNick() ,'groupchat' ) + +def vote(msg,args): + if len(args)<2: + return usage(args) + if args[1] == "new": + if len(args)<5: + return usage(args) + new_vote(args) + elif args[1] == "attend": + if len(args)!=3: + return usage(args) + attend_vote( unicode(msg.getFrom().getResource()) ,args) + elif args[1] == "show": + if len(args)!=2: + return usage(args) + vote_statistics() + elif args[1] == "end": + if len(args)!=2: + return usage(args) + end() + else: + return usage(args) + +brathuhn.addCommand("vote","Usage: !vote (new option1 option2 ... option_n|attend num|show|stats)",vote) +brathuhn.addStatusListener(status_logger)