from django.db import models import threading import time # Timer mit Thread damit er seperat vom gamegeschehen läuft class TimerThread(threading.Thread): def __init__(self, timermodel, callback): super(TimerThread, self).__init__() self.timermodel = timermodel self.callback = callback def run(self): self.timermodel.running = True while self.timermodel.remainingtimer >= 0 and self.timermodel.running: time.sleep(1) self.timermodel.remainingtimer -= 1 self.timermodel.save() if self.timermodel.remainingtimer <= 1: # self.stop() self.callback() self.reset() def reset(self): self.timermodel.remainingtimer = self.timermodel.timer self.timermodel.save() def stop(self): self.timermodel.running = False self.reset() # self.join() class Timer(models.Model): timer = models.IntegerField(default=60) remainingtimer = models.IntegerField(default=60) running = models.BooleanField(default=False) def start(self, callback): self.thread = TimerThread(self, callback) self.thread.start() def stop(self): self.thread.stop()