From 8d3075adfb57fa76cb1841eb1aeab048cc41bd0d Mon Sep 17 00:00:00 2001 From: Vincent Le Gallic Date: Fri, 14 Jun 2013 03:12:42 +0200 Subject: [PATCH] On peut consommer des cocas \o/ --- basile.py | 13 ++++++++----- nk.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/basile.py b/basile.py index cd03e92..b467ac8 100755 --- a/basile.py +++ b/basile.py @@ -651,13 +651,16 @@ class Basile(ircbot.SingleServerIRCBot): elif cmd in [u"ping"] and not canal in self.quiet_channels: serv.privmsg(canal, "%s: pong" % (auteur)) - elif cmd in [u"solde", u"!solde"]: + elif cmd in [u"solde", u"!solde", u"!coca"]: if self.identities.has_key(auteur): idbde = self.identities[auteur]["idbde"] - success, solde, pseudo = nk.get_solde(self.nk, self.identities[auteur]["idbde"], serv, canal) - if success: - serv.privmsg(canal, "%s: %s (%s)" % (auteur, float(solde)/100, pseudo.encode("utf8"))) - log(self.serveur, canal, auteur, message + "[successful]" if success else "[failed]") + if cmd in [u"solde", u"!solde"]: + success, solde, pseudo = nk.get_solde(self.nk, self.identities[auteur]["idbde"], serv, canal) + if success: + serv.privmsg(canal, "%s: %s (%s)" % (auteur, float(solde)/100, pseudo.encode("utf8"))) + elif cmd in [u"!coca"]: + success = nk.consomme(self.nk, self.identities[auteur]["idbde"], u"Coca", serv, canal) + log(self.serveur, canal, auteur, message + ("[successful]" if success else "[failed]")) else: serv.privmsg(canal, "%s: Je ne connais pas votre pseudo note." % (auteur)) log(self.serveur, canal, auteur, message + "[unknown]") diff --git a/nk.py b/nk.py index 0ecf6f7..6de4691 100644 --- a/nk.py +++ b/nk.py @@ -107,3 +107,37 @@ def get_solde(sock, idbde, serv, canal): #log(self.serveur, "priv", auteur, " ".join(message) + "[failed]") return (False, None, None) +def consomme(sock, idbde, conso, serv, canal): + """Fais consommer une conso à l'utilisateur NK n°``idbde``""" + try: + sock.write(json.dumps(["get_boutons", ["", ""]])) + ret = full_read(sock) + retcode = ret["retcode"] + if retcode == 0: + boutons = ret["msg"] + ibouton = [b["label"].lower() for b in boutons].index(conso.lower()) + bouton = boutons[ibouton] + sock.write(json.dumps(["consos", [[bouton["id"], idbde, 1]]])) + ret = full_read(sock) + if ret["retcode"] == 0: + [[retcode, [idbouton, idbde], errmsg]] = ret["msg"] + if retcode != 0: + serv.privmsg(canal, errmsg) + else: + success, solde, pseudo = get_solde(sock, idbde, serv, canal) + if success: + serv.privmsg(canal, (u"%s consomme 1 %s (nouveau solde : %s)" % (pseudo, bouton["label"], solde)).encode("utf-8")) + else: + serv.privmsg(canal, (u"%s consommé mais impossible de récupérer le solde après transaction." % (bouton["label"])).encode("utf-8")) + return True # on a réussi à consommer la conso + else: + serv.privmsg(canal, ret["errmsg"].encode("utf-8")) + return False + else: + serv.privmsg(canal, ret["errmsg"]) + except Exception as exc: + trace = traceback.format_exc() + msg = "failed\n%s" % trace + for l in msg.split("\n"): + serv.privmsg(canal, l) + -- 2.39.2