Skip to content

Instantly share code, notes, and snippets.

@Tkizzy
Created May 11, 2016 23:57
Show Gist options
  • Save Tkizzy/712b62011ad0b76409f8faf77a3add8c to your computer and use it in GitHub Desktop.
Save Tkizzy/712b62011ad0b76409f8faf77a3add8c to your computer and use it in GitHub Desktop.
ChessClock.py
import ui,time
import bz2
from base64 import b64decode
#https://gist.github.com/c61951cc318e7beaf378f6c292f94883
#https://gist.github.com/b8e6f213d19e9d4668a7f28bbd1efe9f
class clockster(object):
def __init__(self):
#_______PACKAGED UI ______
compressedui = '''\
QlpoOTFBWSZTWeGcOhAAAz3fgFUQUGd/9T+E3Yq/r976QAL82kAisQyQ0mJhNRlT01DQPU
e1T0mE0AAbUCJRqVP0U09pTygAPUaAPUAMh6TeqYHMAmmATIYAAmCYAAAIpTU9IT9JMm1A
9Q0AAAAABpSsGxSEROVLA/s9kiUJei8kAkksSaVEhIYQZzK1AQrUBuUGYwQQwLiohYHlmE
GlLV+A/vRmcjTMwkHNmBgqD+wxhADgXkoDGvhvLCICx2jSuNSNdcDqff28dZk5ENgjuTXu
TcU2KIxqiwl7wswixLUMsli7ULESKam7yePbJHSF0sl0hAOtEQMBGjSg19jAAvtVAsSRIF
6DhPl0Q93YzqpQL2wYnXvZj1HPablEQp+AhI0Gyo0QWOAf18kQMJIXEkTvCBCW2N3zUdkw
aPDyJVBr3ODSrA1Duxob4YBHsjW+gxvdpGDJxIis9Ews2YGwVKBrp7UcXMEmBSICZjuq4x
tYyWEE2CW5sZWDYCQyZVrJrsgeiwdV7lNmTAnGdIQjGVc0ybkHZNuHEggOP8K3OmNYIKwW
W2j6Jhjm9Zd5wnlQDOlM52yze50sUc11IPut5YrOlQ1YXR0c68D9ZA6npDWn4HlyV5U+QH
hx4kd7+76wDeO/hxFEfgNAYqZKUMAnBZzjG0U5nYNyksuOO0feXiaYSI9SX/XTErAqDkM1
jgBdy7Dm3eoDuLYdelsw8I6BTI6B553ByI5xPM4g5EW00BRhI8B/gXioeDpvLBA3YNnY2U
J4GhrxPdTb0vE3HB0YgWc3SOo7bWVBiAtccjUoNGgN9SqY2RapMHCN8DImkpc/hkYhChkH
QZNo4W6JkGU9TGEiBnWFg5akpaPSGBMtzA4AM7iSh8pCe5DBBiw0KX9aeyudSg3nyI8vNY
nAn3kTrF3JFOFCQ4Zw6EA=
'''
pyui = bz2.decompress(b64decode(compressedui))
v = ui.load_view_str(pyui.decode('utf-8'))
self.v = v
#_______END PACKAGED UI________
#self.v = ui.load_view()
self.v.present('sheet')
print(self.v["playerOne"])
self.startTime = time.time()
self.gameBegun = False
self.playerOne = self.v["playerOne"]
self.playerTwo = self.v["playerTwo"]
self.playerOne.timeLabel = self.v["playerOneTime"]
self.playerTwo.timeLabel = self.v["playerTwoTime"]
self.playerOne.totalTimeElapsed = 0
self.playerTwo.totalTimeElapsed = 0
self.playerOne.latestTurnElapsed = 0
self.playerTwo.latestTurnElapsed = 0
self.playerOne.otherPlayer = self.playerTwo
self.playerTwo.otherPlayer = self.playerOne
self.currentActivePlayer = "NOBODY"
self.mainLoop()
def togglePlayer(self,sender):
print(sender)
print(self.currentActivePlayer)
if self.currentActivePlayer == sender:
print("no change....")
pass
elif self.currentActivePlayer == "NOBODY":
print("this means THE GAME HAS BEGUN!")
self.gameBegun = True
self.startTime = time.time()
self.currentActivePlayer = sender
self.currentInactivePlayer = self.currentActivePlayer.otherPlayer
self.lastTimeToggled = time.time()
print('...first move by: ',sender.name)
else:
print('currentActivePlayer changing....')
#cleaning up old active plyayer
self.currentActivePlayer.totalTimeElapsed = self.currentActivePlayer.totalTimeElapsed+self.currentActivePlayer.latestTurnElapsed
#new active/inactive player
self.currentActivePlayer = sender
self.currentInactivePlayer = sender.otherPlayer
self.lastTimeToggled = time.time()
print("player toggled to",sender.name)
def switchClockOnForSelectedPlayer(self):
self.currentActivePlayer.latestTurnElapsed= time.time()-self.lastTimeToggled
self.currentActivePlayer.timeToDisplay= self.currentActivePlayer.totalTimeElapsed + self.currentActivePlayer.latestTurnElapsed
self.currentActivePlayer.timeLabel.text = str(round(self.currentActivePlayer.timeToDisplay))
self.totalElapsedTimeBySumming = self.currentActivePlayer.timeToDisplay + self.currentInactivePlayer.totalTimeElapsed
@ui.in_background
def mainLoop(self):
while self.v.on_screen:
if self.gameBegun is True:
self.switchClockOnForSelectedPlayer()
timeElapsed = round(time.time()-self.startTime,0)
print(round(time.time()-self.startTime))
#self.v["totalTime"].text = str(timeElapsed)
#CHANGED THE ABOVE TO BELOW SO TIMES ADD UP!
self.v["totalTime"].text = str(round(self.totalElapsedTimeBySumming))
time.sleep(1)
#self.mainLoop()
if __name__ == "__main__":
a = clockster()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment