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.

consumers.py 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import asyncio
  2. from channels.generic.websocket import AsyncWebsocketConsumer
  3. from .backend.game import Game
  4. from channels.db import database_sync_to_async
  5. class TimerConsumer(AsyncWebsocketConsumer):
  6. async def connect(self):
  7. await self.accept()
  8. self.gameid = self.scope["url_route"]["kwargs"]["gameid"]
  9. self.game = await self.getGame()
  10. timer = await self.getRemainingTimer()
  11. await self.send(text_data=str(timer))
  12. self.timerTask = asyncio.ensure_future(self.timer(timer))
  13. async def timer(self, timer):
  14. await asyncio.sleep(1)
  15. try:
  16. while timer >= 0:
  17. if self.game.running:
  18. await self.send(text_data=str(timer))
  19. timer -= 1
  20. await asyncio.sleep(1)
  21. else:
  22. await self.send(text_data="game not started")
  23. self.game = await self.getGame()
  24. await asyncio.sleep(1)
  25. except asyncio.CancelledError:
  26. pass
  27. finally:
  28. self.game = await self.getGame()
  29. if self.game.currentRound > self.game.maxRounds:
  30. await self.send(text_data="endGame")
  31. else:
  32. await self.send(text_data="nextRound")
  33. @database_sync_to_async
  34. def getRemainingTimer(self):
  35. return self.game.timer.remainingtimer
  36. @database_sync_to_async
  37. def getGame(self):
  38. return Game.objects.get(id=self.gameid)
  39. @database_sync_to_async
  40. def processGameround(self, game):
  41. gameend = game.processGameround()
  42. async def receive(self, event):
  43. pass
  44. async def disconnect(self, event):
  45. self.timerTask.cancel()