Part of gamestate keeping
This commit is contained in:
parent
a6742ddfc5
commit
280544c66d
39
game.py
39
game.py
|
@ -12,7 +12,7 @@ class SeopardyGame(QtGui.QWidget):
|
|||
self.questions = questions
|
||||
self.gamestate = gamestate
|
||||
self.players = [Player.gen_player(i) for i in range(1, 4)]
|
||||
self._inQuestion = False
|
||||
self._inOtherWindow = False
|
||||
|
||||
self._createGui()
|
||||
|
||||
|
@ -57,35 +57,54 @@ class SeopardyGame(QtGui.QWidget):
|
|||
self.setLayout(layout)
|
||||
|
||||
|
||||
#def event(self, e):
|
||||
# print("Event", e)
|
||||
# super(SeopardyGame, self).event(e)
|
||||
|
||||
def keyPressEvent(self, e):
|
||||
print("Mainwindow", e)
|
||||
print("current widget", e.key(), self.focusWidget())
|
||||
if e.key() == QtCore.Qt.Key_Escape:
|
||||
self.close()
|
||||
elif e.key() == QtCore.Qt.Key_E:
|
||||
if self._inOtherWindow:
|
||||
return
|
||||
self._inOtherWindow = True
|
||||
#editResultsWin = EditResultsWindow()
|
||||
#editResultsWin.exec_()
|
||||
self._inOtherWindow = False
|
||||
|
||||
def go_to_question(self, section, number):
|
||||
if self._inQuestion:
|
||||
if self._inOtherWindow:
|
||||
return
|
||||
self._inQuestion = True
|
||||
self._inOtherWindow = True
|
||||
print("question window", section, number)
|
||||
answers = self.gamestate.get_answers(section, number)
|
||||
|
||||
qwin = QuestionWindow(self.players, section, number, self.questions.get_question(section, number), self)
|
||||
qwin = QuestionWindow(self.players, section, number, self.questions.get_question(section, number), answers, self)
|
||||
qwin.showFullScreen()
|
||||
qwin.exec_()
|
||||
|
||||
self._inQuestion = False
|
||||
self._inOtherWindow = False
|
||||
|
||||
answers = qwin.get_answers()
|
||||
self.gamestate.set_answers(section, number, answers)
|
||||
|
||||
# add points to players
|
||||
# FIXME: Note that we cannot add points when revisiting a question
|
||||
for player, correct in answers.get_tries():
|
||||
points = number*100
|
||||
if not correct:
|
||||
points *= -1
|
||||
player.add_points(points)
|
||||
self._set_player_points(answers)
|
||||
|
||||
# restyle the button
|
||||
self._restyle_button(section, number, answers)
|
||||
|
||||
def _set_player_points(self, answers, rollback=False):
|
||||
for player, correct in answers.get_tries():
|
||||
prefix = 1
|
||||
if rollback:
|
||||
prefix *= -1
|
||||
if not correct:
|
||||
prefix *= -1
|
||||
player.add_points(answers.points()*prefix)
|
||||
|
||||
def _restyle_button(self, sec, qno, answers):
|
||||
secno = self.questions.get_number_from_section(sec)
|
||||
|
|
38
gamestate.py
38
gamestate.py
|
@ -1,7 +1,40 @@
|
|||
from __future__ import print_function
|
||||
|
||||
import os
|
||||
from collections import defaultdict
|
||||
|
||||
class GameState(object):
|
||||
def __init__(self, statefile=None):
|
||||
""" 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):
|
||||
|
@ -15,6 +48,9 @@ class QuestionAnswers(object):
|
|||
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
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@ def _parser():
|
|||
parser = argparse.ArgumentParser(description="Sebas jeopardy/beopardy clone")
|
||||
parser.add_argument("--fifo", action="store", default="/tmp/seopardy-fifo", help="Name of the fifo to create")
|
||||
parser.add_argument("--gamestate", action="store", default=None, help="Gamestate to load to recover a crashed game")
|
||||
parser.add_argument("--savedir", action="store", default="/tmp/seopardy/", help="Directory where to save the temporary gamestates to, in case of game-crash")
|
||||
parser.add_argument("questions", action="store", help="Path to questionfile")
|
||||
|
||||
return parser
|
||||
|
@ -32,7 +33,7 @@ if __name__ == '__main__':
|
|||
print("Error: Could not create fifo: %s" % (str(e),), file=sys.stderr)
|
||||
|
||||
questions = Questions(args.questions)
|
||||
gamestate = GameState(args.gamestate)
|
||||
gamestate = GameState(args.savedir)
|
||||
|
||||
# start gui
|
||||
app = QtGui.QApplication([])
|
||||
|
|
|
@ -4,14 +4,17 @@ from gamestate import QuestionAnswers
|
|||
|
||||
#class QuestionWindow(QtGui.QWidget):
|
||||
class QuestionWindow(QtGui.QDialog):
|
||||
def __init__(self, players, section, qnumber, question, parent=None):
|
||||
def __init__(self, players, section, qnumber, question, answers=None, parent=None):
|
||||
super(QuestionWindow, self).__init__(parent)
|
||||
|
||||
self.players = players
|
||||
self.section = section
|
||||
self.qnumber = qnumber
|
||||
self.question = question
|
||||
self.answers = QuestionAnswers(self.section, self.qnumber)
|
||||
if answers is not None:
|
||||
self.answers = answers
|
||||
else:
|
||||
self.answers = QuestionAnswers(self.section, self.qnumber)
|
||||
|
||||
self._setupGui()
|
||||
self.setWindowTitle("Seopardy - %s - %d" % (section, qnumber*100))
|
||||
|
|
Loading…
Reference in New Issue