Skip to content

Instantly share code, notes, and snippets.

@jsbain
Created May 11, 2016 17:48
Show Gist options
  • Select an option

  • Save jsbain/cd4126c5d98a5cbf94085d9d947a0039 to your computer and use it in GitHub Desktop.

Select an option

Save jsbain/cd4126c5d98a5cbf94085d9d947a0039 to your computer and use it in GitHub Desktop.
clk.py
import ui,time
import bz2
from base64 import b64decode
import threading
import math
from objc_util import on_main_thread
#https://gist.github.com/b8e6f213d19e9d4668a7f28bbd1efe9f
def run_async(func):
from threading import Thread
from functools import wraps
@wraps(func)
def async_func(*args, **kwargs):
func_hl = Thread(target = func, args = args, kwargs = kwargs)
func_hl.start()
return func_hl
return async_func
class clockster(object):
def __init__(self):
#self.v = ui.load_view()
compressedui = '''\
QlpoOTFBWSZTWUc9NZEAAyzfgFUQUGd/9T+E3Qq/r976QAL8wYDa1TCSQIZMptTU9U9T1A
A0xGTQAA9IESJSm9KYyjI0B6gHqfqmmg0ZAbSaBzTEwAJgAmAAEwABMEUo1MU2INIaeoAA
AABoBpXQDYpCgnMlgf2mycngJQSSASdLEzlBE5ICGEGigpRUClAG9AcCEJCOSCFKfaGiDh
SsfT89nQ2zcEg7mdbgkZNxVIFjoWDp1dnjaTI1uRycNb7nnbm6zJ2+GcDwENYpsTVsTYQs
jG7KAq2mEMMSkuwJSUW4hREinC29Pg1yR0haUlpFTbJAfX57Ye3OtskxEvGlTtjiG/o5O1
y81U2oR68XN1cHU6FvutEzMm/AyR01Pa1mGUPZ0Mw5JEJpFNMKIa138TPQzFWCJsSK2OyA
4HOW09a8cJT3yMr5jxDYXpkIyiSpoz7PudZRMPhnrQdFRcUkjIzUeFoBjuxhhVt5Pg/HcO
wNGq04b69+xCsnEVEmieM6Y4ak91LUOZ2kIQ0fqgQPX8ULqlLasbsYz1tKzYU5pRn2rDO9
Q2rXbPdptKEJ0aD4VxleN50jnWw+ON2hltb5LIXqDxDl707pG9cQj4Bejn50dsursYDwHp
0VkiHnSQFKlqkhgSNCx9DNMp8OUbMJS729xDWYaYSI/5IXHo/sCVoMEHUkRDjGgCdxUOkS
5ZDiqmYM6UgmNxH6UvdcWiOU1yRmLRHdtUTOSOR/gJFiQtrri8yVzCTMKFw52QyvaUu/gr
ZxwNVIH2GoNpoojgqBqtzzagE1RIwSHRFmxqkRBZIwAyJlUvcb+NjMhYZBrcw4W+JEFx7G
QJyBwFAWjcTJhzD1BUma8ICoTS2pWfKQmhiQCDkpfsTbjcpgP3zM+tE+ZcE/4u5IpwoSCO
emsiA=
'''
pyui = bz2.decompress(b64decode(compressedui))
v = ui.load_view_str(pyui.decode('utf-8'))
self.v = v
self.v.present('sheet')
self.startTime = time.time()
self.gameBegun = False
self.playerOne = self.v["playerOne"]
self.playerTwo = self.v["playerTwo"]
self.playerOne.tint_color='red'
self.playerTwo.tint_color='red'
self.playerOne.totalTimeElapsed = 0
self.playerTwo.totalTimeElapsed = 0
self.playerOne.latestTurnElapsed = 0
self.playerTwo.latestTurnElapsed = 0
self.currentActivePlayer = "NOBODY"
self.lastTimeToggled=self.startTime
self.lock=threading.RLock()
self.mainLoop()
#in_background seems to be required otherwise fast clicking results in lockup
@ui.in_background
def togglePlayer(self,sender):
with self.lock:
triggerTime=time.time()
if self.currentActivePlayer == sender:
pass
elif self.currentActivePlayer == "NOBODY":
print("this meNS THE GME HAS BEGUN")
self.gameBegun = True
self.startTime = time.time()
self.currentActivePlayer = sender
self.currentActivePlayer.tint_color=''
self.lastTimeToggled = time.time()
#print('game has begun with',sender.name)
else:
#cleaning up old active plyayer
latestTurnElapsed= (triggerTime-self.lastTimeToggled)
self.currentActivePlayer.totalTimeElapsed = self.currentActivePlayer.totalTimeElapsed+latestTurnElapsed
self.currentActivePlayer.latestTurnElapsed=0
self.currentActivePlayer.tint_color='red'
self.currentActivePlayer = sender
self.currentActivePlayer.tint_color='green'
self.lastTimeToggled = triggerTime
#update labels
self.updateLabels()
def formatTime(self,t):
minutes=t//60
sec=t%60
hundreths=(sec*100)%100
return'{:02.0f}:{:02.0f}:{:02.0f}'.format(math.floor(minutes),math.floor(sec),hundreths)
def updateLabels(self):
with self.lock:
if self.currentActivePlayer=='NOBODY':
return
self.currentActivePlayer.latestTurnElapsed= (time.time()-self.lastTimeToggled)
#update BOTH players labels ensure we have accurate time shown
self.playerOne.timeToDisplay = self.playerOne.totalTimeElapsed +self.playerOne.latestTurnElapsed
self.v["playerOneTime"].text = self.formatTime((self.playerOne.timeToDisplay))
self.playerTwo.timeToDisplay = self.playerTwo.totalTimeElapsed +self.playerTwo.latestTurnElapsed
self.v["playerTwoTime"].text = self.formatTime((self.playerTwo.timeToDisplay))
@run_async
def mainLoop(self):
while self.v.on_screen:
if self.gameBegun is True:
self.updateLabels()
timeElapsed = (time.time()-self.startTime)
self.v["totalTime"].text = str(timeElapsed)
#print(time.time())
time.sleep(0.1)
if __name__ == "__main__":
a = clockster()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment