Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timer.py 1.2KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from django.db import models
  2. import threading
  3. import time
  4. # Timer mit Thread damit er seperat vom gamegeschehen läuft
  5. class TimerThread(threading.Thread):
  6. def __init__(self, timermodel, callback):
  7. super(TimerThread, self).__init__()
  8. self.timermodel = timermodel
  9. self.callback = callback
  10. def run(self):
  11. self.timermodel.running = True
  12. while self.timermodel.remainingtimer >= 0 and self.timermodel.running:
  13. time.sleep(1)
  14. self.timermodel.remainingtimer -= 1
  15. self.timermodel.save()
  16. if self.timermodel.remainingtimer <= 1:
  17. # self.stop()
  18. self.callback()
  19. self.reset()
  20. def reset(self):
  21. self.timermodel.remainingtimer = self.timermodel.timer
  22. self.timermodel.save()
  23. def stop(self):
  24. self.timermodel.running = False
  25. self.reset()
  26. # self.join()
  27. class Timer(models.Model):
  28. timer = models.IntegerField(default=60)
  29. remainingtimer = models.IntegerField(default=60)
  30. running = models.BooleanField(default=False)
  31. def start(self, callback):
  32. self.thread = TimerThread(self, callback)
  33. self.thread.start()
  34. def stop(self):
  35. self.thread.stop()