nemubot/modules/qd/__init__.py

225 lines
7.3 KiB
Python
Raw Normal View History

2012-03-21 22:12:16 +00:00
# coding=utf-8
2012-04-09 02:19:39 +00:00
import re
2012-07-06 08:51:30 +00:00
import imp
2012-04-09 02:19:39 +00:00
from datetime import datetime
nemubotversion = 3.0
from . import GameUpdater
2012-07-06 08:51:30 +00:00
from . import QDWrapper
from . import Score
2012-05-18 09:38:50 +00:00
channels = "#nemutest #42sh #ykar #epitagueule"
LASTSEEN = dict ()
2012-04-09 02:19:39 +00:00
temps = dict ()
2012-06-16 20:49:31 +00:00
SCORES = None
2012-06-16 20:49:31 +00:00
def load():
global DATAS, SCORES, CONF
2012-06-16 20:49:31 +00:00
DATAS.setIndex("name", "player")
2012-07-06 08:51:30 +00:00
SCORES = QDWrapper.QDWrapper(DATAS)
GameUpdater.SCORES = SCORES
GameUpdater.CONF = CONF
GameUpdater.save = save
GameUpdater.getUser = getUser
2012-07-06 08:51:30 +00:00
def reload():
imp.reload(GameUpdater)
imp.reload(QDWrapper)
imp.reload(Score)
2012-06-16 20:49:31 +00:00
def help_tiny ():
"""Line inserted in the response to the command !help"""
return "42 game!"
def help_full ():
return "!42: display scores\n!42 help: display the performed calculate\n!42 manche: display information about current round\n!42 /who/: show the /who/'s scores"
2012-04-09 02:19:39 +00:00
def parseanswer (msg):
if msg.cmd[0] == "42" or msg.cmd[0] == "score" or msg.cmd[0] == "scores":
global SCORES
if len(msg.cmd) > 2 and msg.is_owner and ((msg.cmd[1] == "merge" and len(msg.cmd) > 3) or msg.cmd[1] == "oupstriche"):
if msg.cmd[2] in SCORES and (len(msg.cmd) <= 3 or msg.cmd[3] in SCORES):
if msg.cmd[1] == "merge":
SCORES[msg.cmd[2]].merge (SCORES[msg.cmd[3]])
del SCORES[msg.cmd[3]]
msg.send_chn ("%s a été correctement fusionné avec %s."%(msg.cmd[3], msg.cmd[2]))
elif msg.cmd[1] == "oupstriche":
SCORES[msg.cmd[2]].oupsTriche()
2012-05-03 14:16:38 +00:00
else:
if msg.cmd[2] not in SCORES:
msg.send_chn ("%s n'est pas un joueur connu."%msg.cmd[2])
elif msg.cmd[3] not in SCORES:
msg.send_chn ("%s n'est pas un joueur connu."%msg.cmd[3])
elif len(msg.cmd) > 1 and (msg.cmd[1] == "help" or msg.cmd[1] == "aide"):
msg.send_chn ("Formule : \"42\" * 2 + great * 5 + leet * 13.37 + (pi + 1) * 3.1415 * (not_found + 1) + tententen * 10 + \"23\" - (bad + 1) * 10 * (triche * 5 + 1) + 7")
2012-04-09 02:19:39 +00:00
elif len(msg.cmd) > 1 and (msg.cmd[1] == "manche" or msg.cmd[1] == "round"):
manche = DATAS.getNode("manche")
msg.send_chn ("Nous sommes dans la %de manche, gagnée par %s avec %d points et commencée par %s le %s." % (manche.getInt("number"), manche["winner"], manche.getInt("winner_score"), manche["who"], manche.getDate("date")))
#elif msg.channel == "#nemutest":
2012-03-21 22:12:16 +00:00
else:
2012-04-09 02:19:39 +00:00
phrase = ""
2012-03-21 22:12:16 +00:00
2012-04-09 02:19:39 +00:00
if len(msg.cmd) > 1:
2012-05-03 14:16:38 +00:00
if msg.cmd[1] in SCORES:
phrase += " " + msg.cmd[1] + ": " + SCORES[msg.cmd[1]].details()
2012-04-09 02:19:39 +00:00
else:
phrase = " %s n'a encore jamais joué,"%(msg.cmd[1])
2012-03-21 22:12:16 +00:00
else:
for nom, scr in sorted(SCORES.items(), key=rev, reverse=True):
score = scr.score()
if score != 0:
if phrase == "":
phrase = " *%s.%s: %d*,"%(nom[0:1], nom[1:len(nom)], score)
else:
phrase += " %s.%s: %d,"%(nom[0:1], nom[1:len(nom)], score)
2012-03-21 22:12:16 +00:00
2012-04-09 02:19:39 +00:00
msg.send_chn ("Scores :%s" % (phrase[0:len(phrase)-1]))
return True
else:
return False
2012-03-21 22:12:16 +00:00
def win(msg):
global SCORES
2012-07-23 10:07:26 +00:00
who = msg.nick
2012-03-21 22:12:16 +00:00
manche = DATAS.getNode("manche")
2012-03-21 22:12:16 +00:00
maxi_scor = 0
maxi_name = None
for player in DATAS.index.keys():
scr = SCORES[player].score()
2012-03-21 22:12:16 +00:00
if scr > maxi_scor:
maxi_scor = scr
maxi_name = player
2012-06-16 20:49:31 +00:00
for player in DATAS.index.keys():
scr = SCORES[player].score()
if scr > maxi_scor / 3:
del SCORES[player]
else:
DATAS.index[player]["great"] = 0
SCORES.flush()
2012-04-09 02:19:39 +00:00
if who != maxi_name:
msg.send_chn ("Félicitations %s, tu remportes cette manche terminée par %s, avec un score de %d !"%(maxi_name, who, maxi_scor))
2012-04-09 02:19:39 +00:00
else:
msg.send_chn ("Félicitations %s, tu remportes cette manche avec %d points !"%(maxi_name, maxi_scor))
2012-04-09 02:19:39 +00:00
manche.setAttribute("number", manche.getInt("number") + 1)
manche.setAttribute("winner", maxi_name)
manche.setAttribute("winner_score", maxi_scor)
manche.setAttribute("who", who)
manche.setAttribute("date", datetime.now())
2012-03-21 22:12:16 +00:00
print ("Nouvelle manche !")
save()
2012-03-21 22:12:16 +00:00
2012-04-09 02:19:39 +00:00
def parseask (msg):
if len(GameUpdater.DELAYED) > 0:
2012-07-23 10:07:26 +00:00
if msg.nick in GameUpdater.DELAYED:
GameUpdater.DELAYED[msg.nick].msg = msg.content
GameUpdater.DELAYED[msg.nick].delayEvnt.set()
return True
2012-04-09 02:19:39 +00:00
return False
2012-03-21 22:12:16 +00:00
def rev (tupl):
(k, v) = tupl
return (v.score(), k)
def getUser(name):
global SCORES
if name not in SCORES:
2012-07-06 08:51:30 +00:00
SCORES[name] = Score.Score()
return SCORES[name]
2012-07-23 10:07:26 +00:00
2012-04-09 02:19:39 +00:00
def parselisten (msg):
2012-07-23 10:07:26 +00:00
if len(GameUpdater.DELAYED) > 0 and msg.nick in GameUpdater.DELAYED and GameUpdater.DELAYED[msg.nick].good(msg.content):
msg.send_chn("%s: n'oublie pas le nemubot: devant ta réponse pour qu'elle soit prise en compte !" % msg.nick)
2012-05-18 09:38:50 +00:00
bfrseen = None
if msg.realname in LASTSEEN:
bfrseen = LASTSEEN[msg.realname]
LASTSEEN[msg.realname] = datetime.now()
2012-07-23 10:07:26 +00:00
# if msg.channel == "#nemutest" and msg.nick not in GameUpdater.DELAYED:
if msg.channel != "#nemutest" and msg.nick not in GameUpdater.DELAYED:
if re.match("^(42|quarante[- ]?deux).{,2}$", msg.content.strip().lower()):
if msg.time.minute == 10 and msg.time.second == 10 and msg.time.hour == 10:
2012-07-23 10:07:26 +00:00
getUser(msg.nick).playTen()
getUser(msg.nick).playGreat()
elif msg.time.minute == 42:
if msg.time.second == 0:
2012-07-23 10:07:26 +00:00
getUser(msg.nick).playGreat()
getUser(msg.nick).playFtt()
2012-04-09 02:19:39 +00:00
else:
2012-07-23 10:07:26 +00:00
getUser(msg.nick).playBad()
2012-04-09 02:19:39 +00:00
if re.match("^(23|vingt[ -]?trois).{,2}$", msg.content.strip().lower()):
if msg.time.minute == 23:
if msg.time.second == 0:
2012-07-23 10:07:26 +00:00
getUser(msg.nick).playGreat()
getUser(msg.nick).playTwt()
2012-04-09 02:19:39 +00:00
else:
2012-07-23 10:07:26 +00:00
getUser(msg.nick).playBad()
2012-04-09 02:19:39 +00:00
if re.match("^(10){3}.{,2}$", msg.content.strip().lower()):
if msg.time.minute == 10 and msg.time.hour == 10:
if msg.time.second == 10:
2012-07-23 10:07:26 +00:00
getUser(msg.nick).playGreat()
getUser(msg.nick).playTen()
2012-04-09 02:19:39 +00:00
else:
2012-07-23 10:07:26 +00:00
getUser(msg.nick).playBad()
2012-04-09 02:19:39 +00:00
if re.match("^0?12345.{,2}$", msg.content.strip().lower()):
if msg.time.hour == 1 and msg.time.minute == 23 and (msg.time.second == 45 or (msg.time.second == 46 and msg.time.microsecond < 330000)):
2012-07-23 10:07:26 +00:00
getUser(msg.nick).playSuite()
2012-04-09 02:19:39 +00:00
else:
2012-07-23 10:07:26 +00:00
getUser(msg.nick).playBad()
2012-04-09 02:19:39 +00:00
if re.match("^[1l][e3]{2}[t7] ?t?ime.{,2}$", msg.content.strip().lower()):
if msg.time.hour == 13 and msg.time.minute == 37:
if msg.time.second == 0:
2012-07-23 10:07:26 +00:00
getUser(msg.nick).playGreat()
getUser(msg.nick).playLeet()
2012-04-09 02:19:39 +00:00
else:
2012-07-23 10:07:26 +00:00
getUser(msg.nick).playBad()
2012-03-21 22:12:16 +00:00
if re.match("^(pi|3.14) ?time.{,2}$", msg.content.strip().lower()):
if msg.time.hour == 3 and msg.time.minute == 14:
if msg.time.second == 15 or msg.time.second == 16:
2012-07-23 10:07:26 +00:00
getUser(msg.nick).playGreat()
getUser(msg.nick).playPi()
2012-04-09 02:19:39 +00:00
else:
2012-07-23 10:07:26 +00:00
getUser(msg.nick).playBad()
2012-04-09 02:19:39 +00:00
if re.match("^(404( ?time)?|time ?not ?found).{,2}$", msg.content.strip().lower()):
if msg.time.hour == 4 and msg.time.minute == 4:
if msg.time.second == 0 or msg.time.second == 4:
2012-07-23 10:07:26 +00:00
getUser(msg.nick).playGreat()
getUser(msg.nick).playNotfound()
2012-04-09 02:19:39 +00:00
else:
2012-07-23 10:07:26 +00:00
getUser(msg.nick).playBad()
2012-04-09 02:19:39 +00:00
2012-07-23 10:07:26 +00:00
if getUser(msg.nick).isWinner():
print ("Nous avons un vainqueur ! Nouvelle manche :p")
win(msg)
return True
2012-07-23 10:07:26 +00:00
elif getUser(msg.nick).hasChanged():
2012-06-30 02:06:59 +00:00
gu = GameUpdater.GameUpdater(msg, bfrseen)
gu.start()
return True
2012-04-09 02:19:39 +00:00
return False