import functools
import threading
import websocket
import graphics
import requests
import driver
import game
import json
from time import time as now
import sys
import re

def on_message(game, ws, message):
    a = json.loads(message)
    game.reset_time(a["payload"]["seconds_left"])
    game.reset_participants(a["payload"]["participants_text"])

class Button(game.Game):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.timer = graphics.Rectangle(112-12, 7, x=12, y=0)
        self.text_top = graphics.TextSprite("60", x=0, y=0, width=5, height=7)
        self.text_bottom = graphics.TextSprite("00", x=0, y=8, width=5, height=7)
        self.text_participants = graphics.TextSprite("???,???", x=16, y=8, width=5, height=7)
        self.at = now()
        self.time = 60.00
        self.total_width = 112-12

        self.sprites.add(self.timer)
        self.sprites.add(self.text_top)
        self.sprites.add(self.text_bottom)
        self.sprites.add(self.text_participants)


        regex = re.compile(r"(wss://wss\.redditmedia\.com/thebutton\?h=[^\"]*)")
        url = requests.get("https://www.reddit.com/r/thebutton")
        try:
            url = re.search(regex, requests.get("https://www.reddit.com/r/thebutton").text).group(1)
        except:
            url = "wss://wss.redditmedia.com/thebutton?h=7f66bf82878e6151f7688ead7085eb63a0baff0b&e=1428621271"
        self.ws = websocket.WebSocketApp(url)
        self.ws.on_message = functools.partial(on_message, self)
        self.ws_thread = threading.Thread(target=self.ws.run_forever, daemon=True)
        self.ws_thread.start()

    def reset_time(self, time=60.00):
        self.time = time
        self.at = now()

    def reset_participants(self, participants="???,???"):
        self.text_participants.set_text(participants)
        self.text_participants.x = 112 - 6 * (len(participants) + 1)

    def loop(self):
        self.time -= (now() - self.at)
        self.at = now()
        self.text_top.set_text(str(int(self.time)))
        self.text_bottom.set_text(str(int(self.time % 1 * 100)))
        self.timer.width = int(self.total_width * self.time / 60)
        super().loop()

GAME = Button

@@ -342,16 +342,287 @@
 ext%22%5D)%0A%0A
+def color(secs):%0A    if secs %3C= 11:%0A        return 6%0A    elif secs %3C= 21:%0A        return 5%0A    elif secs %3C= 31:%0A        return 4%0A    elif secs %3C= 41:%0A        return 3%0A    elif secs %3C= 51:%0A        return 2%0A    elif secs %3C= 60:%0A        return 1%0A    else:%0A        return 1%0A%0A
 class Bu
@@ -1012,17 +1012,22 @@
 ??%22, x=1
-6
+12-6*7
 , y=8, w
@@ -1129,16 +1129,96 @@
 112-12%0A%0A
+        self.last_time = 61%0A        self.real_time = 61%0A        self.bars = %5B%5D%0A%0A
         
@@ -1739,16 +1739,17 @@
 688ead70
+;
 85eb63a0
@@ -2050,42 +2050,537 @@
 elf.
-time = time%0A        self.at = now(
+last_time = self.real_time%0A        self.real_time = time%0A        self.time = time%0A        self.at = now()%0A%0A        if self.real_time %3E= self.last_time:%0A            bar = graphics.Rectangle(1, color(self.last_time), x=112-6*len(self.text_participants.text)-2, y=15-color(self.last_time))%0A            for s in list(self.bars):%0A                s.x -= 1%0A                if s.x %3C= 13:%0A                    self.bars.remove(s)%0A                    self.sprites.remove(s)%0A            self.bars.append(bar)%0A            self.sprites.add(bar
 )%0A%0A 
@@ -2735,17 +2735,16 @@
 2 - 6 * 
-(
 len(part
@@ -2756,13 +2756,8 @@
 nts)
- + 1)
 %0A%0A  
@@ -2792,18 +2792,33 @@
 lf.time 
--
 =
+ max(self.time -
  (now() 
@@ -2827,16 +2827,20 @@
 self.at)
+, 0)
 %0A       
@@ -2879,35 +2879,46 @@
 xt_top.set_text(
-str
+%22%7B:02%7D%22.format
 (int(self.time))
@@ -2957,11 +2957,22 @@
 ext(
-str
+%22%7B:02%7D%22.format
 (int

Add histogram