browser-go-api/models/Game.py

84 lines
3 KiB
Python
Raw Normal View History

2019-10-04 22:00:47 +00:00
from app import db, ma
2019-10-10 21:57:29 +00:00
from marshmallow import fields
2019-10-02 03:09:46 +00:00
import enum
2019-10-10 21:57:29 +00:00
from models.User import user_schema
2019-10-02 03:09:46 +00:00
2019-10-11 00:41:49 +00:00
# ! Games >-< Users join table
games_users = db.Table('games_users',
db.Column('user_id', db.Integer, db.ForeignKey('users.id'), primary_key=True),
db.Column('game_rooms_id', db.Integer, db.ForeignKey('games.id'), primary_key=True)
)
2019-10-02 03:09:46 +00:00
class Game(db.Model):
2019-10-02 20:55:09 +00:00
__tablename__ = "games"
__table_args__ = {'extend_existing': True}
2019-10-10 21:57:29 +00:00
class Players(enum.Enum):
BLACK = "The player taking black stones"
WHITE = "The player taking white stones"
VOID = "The game was a draw or voided"
class WinType(enum.Enum):
DRAW = "The game is a draw"
RESIGN = "The game ended in resignation"
SCORE = "The game ended by counting points"
TIME = "The game ended in loss by time out"
VOID = "The game was suspended"
2019-10-02 03:09:46 +00:00
2019-10-11 00:41:49 +00:00
class TimeTypes(enum.Enum):
BYOYOMI = "Counting by time period"
ABSOLUTE = "One period to use time"
HOURGLASS = "Absolute time for both players"
NONE = "Untimed"
2019-10-02 20:55:09 +00:00
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
2019-10-02 03:09:46 +00:00
date = db.Column(db.DateTime())
2019-10-02 20:55:09 +00:00
komi = db.Column(db.Numeric(2,1), nullable=False)
handicap = db.Column(db.Integer, nullable=False)
board_size = db.Column(db.Integer, nullable=False)
2019-10-02 03:09:46 +00:00
win_type = db.Column(db.Enum(WinType))
winner = db.Column(db.Enum(Players))
2019-10-02 20:55:09 +00:00
score = db.Column(db.Numeric(2,1))
2019-10-02 03:09:46 +00:00
white_captures = db.Column(db.Integer)
black_captures = db.Column(db.Integer)
application = db.Column(db.String(40))
application_version = db.Column(db.String(20))
event = db.Column(db.String(40))
name = db.Column(db.String(40))
description = db.Column(db.String(200))
2019-10-02 20:55:09 +00:00
round = db.Column(db.Integer)
2019-10-11 00:41:49 +00:00
main_time = db.Column(db.Enum(TimeTypes), nullable=False)
time_period = db.Column(db.Integer) # number of periods
period_length = db.Column(db.Integer) # seconds
overtime = db.Column(db.Enum(TimeTypes), nullable=False)
overtime_period = db.Column(db.Integer) # number of overtime periods
overtime_length = db.Column(db.Integer) # seconds
2019-10-02 03:09:46 +00:00
# foreign keys
2019-10-11 00:52:43 +00:00
game_room = db.Column(db.Integer, db.ForeignKey("game_rooms.id"))
2019-10-02 20:55:09 +00:00
player_black = db.Column(db.Integer, db.ForeignKey("users.id"))
player_white = db.Column(db.Integer, db.ForeignKey("users.id"))
2019-10-02 03:09:46 +00:00
2019-10-11 00:41:49 +00:00
def __init__(
self, name, description, board_size, game_room, player_white,
komi=0.5, handicap=0, main_time=TimeTypes.NONE, overtime=TimeTypes.NONE
):
2019-10-10 21:57:29 +00:00
self.name = name
self.description = description
self.board_size = board_size
self.game_room = game_room
self.player_white = player_white
self.komi = komi
self.handicap = handicap
2019-10-11 00:41:49 +00:00
self.main_time = main_time
self.overtime = overtime
2019-10-10 21:57:29 +00:00
class GameSchema(ma.ModelSchema):
id = fields.Int()
name = fields.Str()
description = fields.Str()
2019-10-11 18:55:35 +00:00
board_size = fields.Int()
2019-10-10 21:57:29 +00:00
player = fields.Nested(user_schema)
game_schema = GameSchema()
games_schema = GameSchema(many=True)