diff --git a/plugins/minercoin.py b/plugins/minercoin.py new file mode 100644 index 0000000..ab8f23c --- /dev/null +++ b/plugins/minercoin.py @@ -0,0 +1,87 @@ +"""MinerCoin. + +How it works: + + - On !minercoin, generate a mathematical challenge and present it in a non-bot-parseable way. Store the answer for later. + - On the next message in the channel the challenges are presented and issued from by said user, check and see if the answer is correct. + - If the answer is correct, reward the user with MinerCoins. + - If not, no reward is issued. + - Only let the user try a challenge every hour.""" +import plugin, time, random +from pluralslib import plural +from dictdata import DictData + +challenges = DictData("challenges.json") +last_used = DictData("times.json") +count = DictData("minercoin.json") + +def get(source): + username = source.user + return count.get(username,0), last_used.get(username,0), challenges.get(username) + +def set(source,ncount,nlast_used,nchallenge=None): + username = source.user + count[username]=ncount + last_used[username]=nlast_used + challenges[username]=nchallenge + +# OK, this needs explaining. +# [int count, lambda verify, lambda answer, string question] +# count = how many numbers to generate +questions = [ + [2,lambda x,y: (x>=0 and x<=100),lambda x,y: int(round((x/100.)*y)),"Calculate {}% of {}. (round to the nearest whole number)"] +] + +def get_question(): + q = random.choice(questions) + nums = [random.randint(0,200) for x in range(q[0])] + while not q[1](*nums): + nums = [random.randint(0,200) for x in range(q[0])] + chq = q[3].format(*nums) + cha = str(q[2](*nums)) + return chq, cha + +@plugin.group("minercoin","") +def minercoin(bot,channel,nick,*args): + if args[0] not in "ask count cooldown".split(): + bot.say(channel,"{}: Usage: !minercoin ".format(nick)) + return True # only allow selected subcommands + elif channel!="#bots": + bot.say(channel,"{}: MinerCoin is only awarded in #bots, same as tildes!".format(nick)) + return True + return False + +leadins = ["pro-minercoin question","anti-autoasker question","pro-login question","pro-minerobber challenge"] + +@minercoin.command("ask") +def minercoin_ask(bot,channel,nick,*args): + cnt, last, challenge = get(bot.event.source) + if (int(time.time())-last)<(60*60) and nick!="minerobber": # if it hasn't been an hour since the last one... + bot.say(channel,"{} is a doodoo head and needs to wait longer between attempts! (1 hour between attempts)".format(nick)) + return + question, challenge = get_question() + bot.say(channel,"{}: {}: {}".format(nick,random.choice(leadins),question)) + set(bot.event.source,cnt,time.time(),challenge) + +@plugin.listener("minercoin_answer") +def minercoin_listen(bot,channel,nick,message): + cnt, last, challenge = get(bot.event.source) + if challenge is None: return + elif message==challenge: + bot.say(channel,"{}: You are a winner and get 1 MinerCoin!".format(nick)) + cnt+=1 + else: + bot.say(channel,"{} is a quitter and gets no MinerCoin!".format(nick)) + set(bot.event.source,cnt,last,None) + +@minercoin.command("count") +def minercoin_count(bot,channel,nick,*args): + if bot.event.source.user in count: + cnt = get(bot.event.source)[0] + bot.say(channel,"{}: You have {}".format(nick,plural(cnt,"MinerCoin"))) + else: + bot.say(channel,"{}: You don't have any MinerCoin! Get some by asking! (!minercoin ask)".format(nick)) + +@minercoin.command("cooldown") +def minercoin_cooldown(bot,channel,nick,*args): + bot.say(channel,NYI) diff --git a/pluralslib.py b/pluralslib.py new file mode 100644 index 0000000..315f53d --- /dev/null +++ b/pluralslib.py @@ -0,0 +1,10 @@ +def are(amount): + if amount == 1: + return 'is' + else: + return 'are' +def plural(amount, base, plural='s', singular=''): + if amount == 1: + return str(amount) + ' ' + base + singular + else: + return str(amount) + ' ' + base + plural