65 lines
1.4 KiB
Python
65 lines
1.4 KiB
Python
from __future__ import print_function
|
|
|
|
import os
|
|
from collections import defaultdict
|
|
|
|
class GameState(object):
|
|
""" 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
|
|
"""
|
|
def __init__(self, savedir):
|
|
self.savedir = savedir
|
|
|
|
self._board = defaultdict(lambda: [None]*5)
|
|
self._players = None
|
|
|
|
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)
|
|
|
|
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):
|
|
self.players = players
|
|
|
|
def save(self):
|
|
pass
|
|
|
|
@classmethod
|
|
def load(clazz, fpath):
|
|
pass
|
|
|
|
class QuestionAnswers(object):
|
|
def __init__(self, section, qnumber):
|
|
self.section = section
|
|
self.qnumber = qnumber
|
|
self.nobody_answered = False
|
|
|
|
self.tries = []
|
|
|
|
def add_try(self, player, correct):
|
|
self.tries.append((player, correct))
|
|
|
|
def points(self):
|
|
return self.qnumber*100
|
|
|
|
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])
|