No Description
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.

gamestate.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Licensed under GPLv3
  2. # Written by Sebastian Lohff (seba@someserver.de)
  3. # http://seba-geek.de/projects/seopardy/
  4. from __future__ import print_function
  5. import datetime
  6. import os
  7. import yaml
  8. from collections import defaultdict
  9. class GameState(object):
  10. """ Represent current gamestate.
  11. players: list of player names + current points
  12. savedir: dir to save gamestate into, should be overwritable
  13. board: dict of section with list of questions
  14. """
  15. GAMESTATE = ["start", "playing", "end"]
  16. def __init__(self, savedir):
  17. self.savedir = savedir
  18. #self._board = defaultdict(lambda: [None]*5)
  19. self._board = defaultdict(_mk_empty_board_column)
  20. self._players = []
  21. self._state = self.GAMESTATE[0]
  22. if not os.path.exists(self.savedir):
  23. os.mkdir(self.savedir)
  24. elif not os.path.isdir(self.savedir):
  25. raise ValueError("'%s' is not a directory but something else!" % self.savedir)
  26. def set_state(self, state):
  27. if state not in self.GAMESTATE:
  28. raise ValueError("'%s' is not a valid gamestate, valid choices are %s" % (state, self.GAMESTATE))
  29. self._state = state
  30. def get_state(self):
  31. return self._state
  32. def set_answers(self, section, qnumber, answers):
  33. self._board[section][qnumber-1] = answers
  34. def get_answers(self, section, qnumber):
  35. return self._board[section][qnumber-1]
  36. def set_players(self, players):
  37. self._players = players
  38. def get_players(self):
  39. return self._players
  40. def _make_structure(self):
  41. return {
  42. "state": self._state,
  43. "players": self._players,
  44. "board": self._board
  45. }
  46. def save(self):
  47. fname = datetime.datetime.now().strftime("seopardy.%Y-%m-%d-_%H:%M:%S.save")
  48. fstream = open("%s/%s" % (self.savedir, fname), "w")
  49. yaml.dump(self._make_structure(), fstream)
  50. fstream.close()
  51. @classmethod
  52. def load(clazz, sstream, savedir):
  53. gs = clazz(savedir)
  54. data = yaml.load(sstream)
  55. gs._state = data["state"]
  56. gs._players = data["players"]
  57. gs._board = data["board"]
  58. return gs
  59. def _mk_empty_board_column():
  60. """ We can't use a lambda here, as we want the default dict to be
  61. easily serializeable by yaml. """
  62. return [None]*5
  63. class QuestionAnswers(object):
  64. def __init__(self, section, qnumber, dj_points=None):
  65. self.section = section
  66. self.qnumber = qnumber
  67. self.nobody_answered = False
  68. self.dj_points = dj_points
  69. self.tries = []
  70. def set_dj_points(self, points):
  71. self.dj_points = points
  72. def add_try(self, player, correct):
  73. self.tries.append((player, correct))
  74. def get_winner(self):
  75. for player, correct in self.tries:
  76. if correct:
  77. return player
  78. return None
  79. def points(self):
  80. return self.qnumber*100
  81. def get_dj_points(self):
  82. return self.dj_points
  83. def nobody_knew(self):
  84. self.nobody_answered = True
  85. def got_answered(self):
  86. return not self.nobody_answered
  87. def get_tries(self):
  88. return self.tries
  89. def is_answered(self):
  90. return self.nobody_answered or any([c for (u, c) in self.tries])