Created
November 22, 2012 03:05
-
-
Save passcod/4129243 to your computer and use it in GitHub Desktop.
xbot nanowrimo sprint module/script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Append to: nanowrime.py | |
import time | |
import datetime | |
from threading import Thread | |
class nanotimer(Thread): | |
def set(self, time = 15, func = lambda: 0, *args): | |
self.time = time * 60 | |
self.func = func | |
self.args = args | |
def run(self): | |
time.sleep(self.time) | |
self.func(self.args) | |
def nanosprint(bot, args): | |
if len(args) != 2: | |
return util.give_help(bot, args[0], "[minutes]") | |
if 'sprint' in bot.inv: | |
diff = bot.inv['sprint']['duration'] - (datetime.datetime.now() - bot.inv['sprint']['start']).seconds) / 60.0 | |
util.answer(bot, "Sprint is running... %.f minutes left!" % diff) | |
return | |
try: | |
t = int(args[1]) | |
except ValueError: | |
return | |
tim = nanotimer() | |
tim.set(time=1, lambda bot, duration: | |
t = nanotimer() | |
t.set(duration, lambda bot: | |
del bot.inv['sprint'] | |
util.answer(bot, "Sprint over!") | |
, bot) | |
bot.inv['sprint'] = {'start': datetime.datetime.now(), 'duration': duration} | |
util.answer(bot, "...set... GO!") | |
t.start() | |
, t, bot) | |
util.answer(bot, "%d-minute sprint starts in one minute. On your marks..." % t) | |
tim.start() | |
util.register(nanosprint, "common", "sprint") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment