seopardy/gamestate.py

121 lines
2.8 KiB
Python
Raw Permalink Normal View History

2015-08-19 11:20:51 +02:00
# Licensed under GPLv3
# Written by Sebastian Lohff (seba@someserver.de)
# http://seba-geek.de/projects/seopardy/
2013-11-06 20:42:00 +01:00
from __future__ import print_function
2013-12-01 22:41:44 +01:00
import datetime
2013-11-13 21:16:50 +01:00
import os
2013-12-01 22:41:44 +01:00
import yaml
2013-11-13 21:16:50 +01:00
from collections import defaultdict
2013-11-06 20:42:00 +01:00
class GameState(object):
2013-11-13 21:16:50 +01:00
""" Represent current gamestate.
players: list of player names + current points
savedir: dir to save gamestate into, should be overwritable
board: dict of section with list of questions
"""
2013-12-01 22:41:44 +01:00
GAMESTATE = ["start", "playing", "end"]
2013-11-13 21:16:50 +01:00
def __init__(self, savedir):
self.savedir = savedir
2013-12-01 22:41:44 +01:00
#self._board = defaultdict(lambda: [None]*5)
self._board = defaultdict(_mk_empty_board_column)
self._players = []
self._state = self.GAMESTATE[0]
2013-11-13 21:16:50 +01:00
if not os.path.exists(self.savedir):
os.mkdir(self.savedir)
elif not os.path.isdir(self.savedir):
raise ValueError("'%s' is not a directory but something else!" % self.savedir)
2013-12-01 22:41:44 +01:00
def set_state(self, state):
if state not in self.GAMESTATE:
raise ValueError("'%s' is not a valid gamestate, valid choices are %s" % (state, self.GAMESTATE))
self._state = state
def get_state(self):
return self._state
2013-11-13 21:16:50 +01:00
def set_answers(self, section, qnumber, answers):
self._board[section][qnumber-1] = answers
def get_answers(self, section, qnumber):
return self._board[section][qnumber-1]
def set_players(self, players):
2013-12-01 22:41:44 +01:00
self._players = players
def get_players(self):
return self._players
def _make_structure(self):
return {
"state": self._state,
"players": self._players,
"board": self._board
}
2013-11-13 21:16:50 +01:00
def save(self):
2013-12-01 22:41:44 +01:00
fname = datetime.datetime.now().strftime("seopardy.%Y-%m-%d-_%H:%M:%S.save")
fstream = open("%s/%s" % (self.savedir, fname), "w")
yaml.dump(self._make_structure(), fstream)
fstream.close()
2013-11-13 21:16:50 +01:00
@classmethod
2013-12-01 22:41:44 +01:00
def load(clazz, sstream, savedir):
gs = clazz(savedir)
data = yaml.load(sstream)
gs._state = data["state"]
gs._players = data["players"]
gs._board = data["board"]
return gs
def _mk_empty_board_column():
""" We can't use a lambda here, as we want the default dict to be
easily serializeable by yaml. """
return [None]*5
2013-11-07 22:59:18 +01:00
2013-11-11 00:52:37 +01:00
class QuestionAnswers(object):
2013-11-28 20:13:30 +01:00
def __init__(self, section, qnumber, dj_points=None):
2013-11-07 22:59:18 +01:00
self.section = section
2013-11-11 00:52:37 +01:00
self.qnumber = qnumber
self.nobody_answered = False
2013-11-28 20:13:30 +01:00
self.dj_points = dj_points
2013-11-11 00:52:37 +01:00
self.tries = []
2013-11-28 20:13:30 +01:00
def set_dj_points(self, points):
self.dj_points = points
2013-11-11 00:52:37 +01:00
def add_try(self, player, correct):
self.tries.append((player, correct))
def get_winner(self):
for player, correct in self.tries:
if correct:
return player
return None
2013-11-13 21:16:50 +01:00
def points(self):
return self.qnumber*100
2013-11-28 20:13:30 +01:00
def get_dj_points(self):
return self.dj_points
2013-11-11 00:52:37 +01:00
def nobody_knew(self):
self.nobody_answered = True
def got_answered(self):
return not self.nobody_answered
def get_tries(self):
return self.tries
def is_answered(self):
return self.nobody_answered or any([c for (u, c) in self.tries])