This repository has been archived by the owner on Mar 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
83 lines (64 loc) · 2.58 KB
/
database.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
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
db = SQLAlchemy()
class DatabaseInit:
def __init__(self, db):
self.db = db
def create_database(self):
if not db.engine.dialect.has_table(db.engine.connect(), 'game'):
self.db.create_all()
pins = [
'purple',
'blue',
'green',
'yellow',
'orange',
'red',
'black',
'brown',
'lightblue',
'pink',
]
for color in pins:
self.db.session.add(Pin(color))
self.db.session.commit()
class GamePin(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
game_id = db.Column(db.Integer, db.ForeignKey('game.id'), nullable=False)
pin_id = db.Column(db.Integer, db.ForeignKey('pin.id'), nullable=False)
turn = db.Column(db.Integer, nullable=False)
pin = db.relationship('Pin')
game = db.relationship('Game')
def __init__(self, game_id, pin_id, turn):
self.game_id = game_id
self.pin_id = pin_id
self.turn = turn
class Game(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
player_id = db.Column(db.Integer, db.ForeignKey('player.id'), nullable=False)
positions = db.Column(db.Integer, nullable=False, default=6)
colors = db.Column(db.Integer, nullable=False, default=6)
double_colors = db.Column(db.Boolean, nullable=False, default=False)
winner = db.Column(db.Boolean, nullable=True)
code = db.Column(db.String(40), nullable=True)
ingame_colors = db.Column(db.String(40), nullable=True)
created_at = db.Column(db.DateTime, nullable=False, default=datetime.now)
pins = db.relationship('GamePin')
player = db.relationship('Player')
def __init__(self, player_id, positions, double_colors, colors, code, ingame_colors):
self.player_id = player_id
self.positions = positions
self.double_colors = double_colors
self.colors = colors
self.code = code
self.ingame_colors = ingame_colors
class Pin(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
color = db.Column(db.String(40), nullable=False, unique=True)
def __init__(self, color):
self.color = color
class Player(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String(255), nullable=False, unique=True)
def __init__(self, name):
self.username = name