-
Notifications
You must be signed in to change notification settings - Fork 12
/
model.py
157 lines (119 loc) · 4.61 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Ceopardy
# https://github.com/obilodeau/ceopardy/
#
# Olivier Bilodeau <[email protected]>
# Copyright (C) 2017 Olivier Bilodeau
# All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
import collections
from enum import Enum
from flask import current_app as app
from ceopardy import db, VERSION
SCHEMA_VERSION = 1
# TODO save a game in progress
# TODO load a game in progress
# TODO refactor to GameModel?
class GameState(Enum):
uninitialized = 0
in_round = 1
in_final = 2
finished = 3
class Game(db.Model):
id = db.Column(db.Integer, primary_key=True)
state = db.Column(db.Enum(GameState))
ceopardy_version = db.Column(db.String(16))
schema_version = db.Column(db.Integer)
round_filename = db.Column(db.String(255))
def __init__(self):
self.state = GameState.uninitialized
self.ceopardy_version = VERSION
self.schema_version = SCHEMA_VERSION
def __repr__(self):
return '<Game in state %r>' % self.state
class Team(db.Model):
id = db.Column(db.Integer, primary_key=True)
tid = db.Column(db.String(7), unique=True)
name = db.Column(db.String(80), unique=True)
handicap = db.Column(db.Integer)
answers = db.relationship('Answer', back_populates='team')
def __init__(self, tid, name, handicap=0):
self.tid = tid
self.name = name
self.handicap = handicap
app.logger.debug("Team created: name is {}, handicap is {}"
.format(self.name, self.handicap))
def __repr__(self):
return '<Team %r>' % self.name
class Question(db.Model):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.String(255))
score_original = db.Column(db.Integer)
category = db.Column(db.String(80))
final = db.Column(db.Boolean)
double = db.Column(db.Boolean)
row = db.Column(db.Integer)
col = db.Column(db.Integer)
answers = db.relationship('Answer', back_populates="question")
def __init__(self, text, score_original, category, row, col, final=False,
double=False):
self.text = text
self.score_original = score_original
self.category = category
self.row = row
self.col = col
self.final = final
self.double = double
def __repr__(self):
return '<Question {} for {} at col {} row {}>'\
.format(self.category, self.score_original, self.col, self.row)
# For the database, a final question is just a flag on a regular question.
# This convenience object is created so that we manage it in a more OO-ish way
# after parsing and before database insertion.
FinalQuestion = collections.namedtuple('FinalQuestion', 'category question')
class Response(Enum):
bad = -1
nop = 0
good = 1
class Answer(db.Model):
id = db.Column(db.Integer, primary_key=True)
score_attributed = db.Column(db.Integer)
response = db.Column(db.Enum(Response))
team_id = db.Column(db.Integer, db.ForeignKey('team.id'))
team = db.relationship('Team', back_populates="answers")
question_id = db.Column(db.Integer, db.ForeignKey('question.id'))
question = db.relationship('Question', back_populates="answers")
def __init__(self, response, team, question):
"""Meant to be used on normal questions where score is not changeable"""
self.score_attributed = question.score_original
self.response = response
self.team = team
self.question = question
def __repr__(self):
return '<Answer by team id {} to question id {} is {}. Points {}>'\
.format(self.team_id, self.question_id, self.response.name,
self.score_attributed)
class Category():
def __init__(self, value, column):
self.value = value
self.column = column
class State(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(10), unique=True)
value = db.Column(db.String(4096))
def __init__(self, name, value):
self.name = name
self.value = value
def __repr__(self):
return '<State {} is {}.>'.format(self.name, self.value)
# This creates the database if it doesn't already exist
db.create_all()