Merge pull request #14 from sorrelbri/sj-model-refactor

sj model refactor
This commit is contained in:
sorrelbri 2019-10-10 17:55:59 -07:00 committed by GitHub
commit e4d7c1ad46
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 60 additions and 90 deletions

View file

@ -1,7 +1,6 @@
if __name__ == '__main__': if __name__ == '__main__':
from ..models.User import User from ..models.User import User
from ..models.GameRoom import GameRoom from ..models.GameRoom import GameRoom
from ..models.TimeSettings import TimeSettings
from ..models.Game import Game from ..models.Game import Game
from ..models.Move import Move from ..models.Move import Move
from ..models.Message import Message from ..models.Message import Message

View file

@ -15,7 +15,6 @@ from models.Game import Game
from models.GameRoom import GameRoom from models.GameRoom import GameRoom
from models.Message import Message from models.Message import Message
from models.Move import Move from models.Move import Move
from models.TimeSettings import TimeSettings
from models.User import User from models.User import User
# migrations # migrations

View file

@ -1,8 +1,8 @@
"""empty message """empty message
Revision ID: acabfc6d158d Revision ID: 45f01fb15e26
Revises: Revises:
Create Date: 2019-10-10 14:25:27.198731 Create Date: 2019-10-10 17:50:40.846864
""" """
from alembic import op from alembic import op
@ -10,7 +10,7 @@ import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision = 'acabfc6d158d' revision = '45f01fb15e26'
down_revision = None down_revision = None
branch_labels = None branch_labels = None
depends_on = None depends_on = None
@ -18,20 +18,12 @@ depends_on = None
def upgrade(): def upgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
op.create_table('room_languages', op.create_table('game_rooms',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(length=40), nullable=False), sa.Column('name', sa.String(length=40), nullable=False),
sa.Column('iso', sa.String(length=2), nullable=False), sa.Column('description', sa.String(length=200), nullable=False),
sa.PrimaryKeyConstraint('id') sa.Column('private', sa.Boolean(), nullable=False),
) sa.Column('language', sa.Enum('EN', name='languages'), nullable=False),
op.create_table('time_settings',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('main_time', sa.Enum('BYOYOMI', 'ABSOLUTE', 'HOURGLASS', 'NONE', name='timetypes'), nullable=False),
sa.Column('time_period', sa.Integer(), nullable=True),
sa.Column('period_length', sa.Integer(), nullable=True),
sa.Column('overtime', sa.Enum('BYOYOMI', 'ABSOLUTE', 'HOURGLASS', 'NONE', name='timetypes'), nullable=False),
sa.Column('overtime_period', sa.Integer(), nullable=True),
sa.Column('overtime_length', sa.Integer(), nullable=True),
sa.PrimaryKeyConstraint('id') sa.PrimaryKeyConstraint('id')
) )
op.create_table('users', op.create_table('users',
@ -48,15 +40,6 @@ def upgrade():
sa.UniqueConstraint('email'), sa.UniqueConstraint('email'),
sa.UniqueConstraint('username') sa.UniqueConstraint('username')
) )
op.create_table('game_rooms',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(length=40), nullable=False),
sa.Column('description', sa.String(length=200), nullable=False),
sa.Column('private', sa.Boolean(), nullable=False),
sa.Column('language', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['language'], ['room_languages.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('game_rooms_users', op.create_table('game_rooms_users',
sa.Column('user_id', sa.Integer(), nullable=False), sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('game_rooms_id', sa.Integer(), nullable=False), sa.Column('game_rooms_id', sa.Integer(), nullable=False),
@ -81,16 +64,27 @@ def upgrade():
sa.Column('name', sa.String(length=40), nullable=True), sa.Column('name', sa.String(length=40), nullable=True),
sa.Column('description', sa.String(length=200), nullable=True), sa.Column('description', sa.String(length=200), nullable=True),
sa.Column('round', sa.Integer(), nullable=True), sa.Column('round', sa.Integer(), nullable=True),
sa.Column('main_time', sa.Enum('BYOYOMI', 'ABSOLUTE', 'HOURGLASS', 'NONE', name='timetypes'), nullable=False),
sa.Column('time_period', sa.Integer(), nullable=True),
sa.Column('period_length', sa.Integer(), nullable=True),
sa.Column('overtime', sa.Enum('BYOYOMI', 'ABSOLUTE', 'HOURGLASS', 'NONE', name='timetypes'), nullable=False),
sa.Column('overtime_period', sa.Integer(), nullable=True),
sa.Column('overtime_length', sa.Integer(), nullable=True),
sa.Column('game_room', sa.Integer(), nullable=True), sa.Column('game_room', sa.Integer(), nullable=True),
sa.Column('time_settings', sa.Integer(), nullable=True),
sa.Column('player_black', sa.Integer(), nullable=True), sa.Column('player_black', sa.Integer(), nullable=True),
sa.Column('player_white', sa.Integer(), nullable=True), sa.Column('player_white', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['game_room'], ['game_rooms.id'], ), sa.ForeignKeyConstraint(['game_room'], ['game_rooms.id'], ),
sa.ForeignKeyConstraint(['player_black'], ['users.id'], ), sa.ForeignKeyConstraint(['player_black'], ['users.id'], ),
sa.ForeignKeyConstraint(['player_white'], ['users.id'], ), sa.ForeignKeyConstraint(['player_white'], ['users.id'], ),
sa.ForeignKeyConstraint(['time_settings'], ['time_settings.id'], ),
sa.PrimaryKeyConstraint('id') sa.PrimaryKeyConstraint('id')
) )
op.create_table('games_users',
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('game_rooms_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['game_rooms_id'], ['games.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('user_id', 'game_rooms_id')
)
op.create_table('moves', op.create_table('moves',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('player', sa.Enum('BLACK', 'WHITE', name='players'), nullable=True), sa.Column('player', sa.Enum('BLACK', 'WHITE', name='players'), nullable=True),
@ -120,10 +114,9 @@ def downgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
op.drop_table('messages') op.drop_table('messages')
op.drop_table('moves') op.drop_table('moves')
op.drop_table('games_users')
op.drop_table('games') op.drop_table('games')
op.drop_table('game_rooms_users') op.drop_table('game_rooms_users')
op.drop_table('game_rooms')
op.drop_table('users') op.drop_table('users')
op.drop_table('time_settings') op.drop_table('game_rooms')
op.drop_table('room_languages')
# ### end Alembic commands ### # ### end Alembic commands ###

View file

@ -3,6 +3,11 @@ from marshmallow import fields
import enum import enum
from models.User import user_schema from models.User import user_schema
# ! 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)
)
class Game(db.Model): class Game(db.Model):
__tablename__ = "games" __tablename__ = "games"
@ -20,6 +25,12 @@ class Game(db.Model):
TIME = "The game ended in loss by time out" TIME = "The game ended in loss by time out"
VOID = "The game was suspended" VOID = "The game was suspended"
class TimeTypes(enum.Enum):
BYOYOMI = "Counting by time period"
ABSOLUTE = "One period to use time"
HOURGLASS = "Absolute time for both players"
NONE = "Untimed"
id = db.Column(db.Integer, primary_key=True, autoincrement=True) id = db.Column(db.Integer, primary_key=True, autoincrement=True)
date = db.Column(db.DateTime()) date = db.Column(db.DateTime())
komi = db.Column(db.Numeric(2,1), nullable=False) komi = db.Column(db.Numeric(2,1), nullable=False)
@ -36,14 +47,22 @@ class Game(db.Model):
name = db.Column(db.String(40)) name = db.Column(db.String(40))
description = db.Column(db.String(200)) description = db.Column(db.String(200))
round = db.Column(db.Integer) round = db.Column(db.Integer)
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
# foreign keys # foreign keys
game_room = db.Column(db.Integer, db.ForeignKey("game_rooms.id")) game_room = db.Column(db.Integer, db.ForeignKey("game_rooms.id"))
time_settings = db.Column(db.Integer, db.ForeignKey("time_settings.id"))
player_black = db.Column(db.Integer, db.ForeignKey("users.id")) player_black = db.Column(db.Integer, db.ForeignKey("users.id"))
player_white = db.Column(db.Integer, db.ForeignKey("users.id")) player_white = db.Column(db.Integer, db.ForeignKey("users.id"))
def __init__(self, name, description, board_size, game_room, player_white, komi=0.5, handicap=0, time_settings=1): def __init__(
self, name, description, board_size, game_room, player_white,
komi=0.5, handicap=0, main_time=TimeTypes.NONE, overtime=TimeTypes.NONE
):
self.name = name self.name = name
self.description = description self.description = description
self.board_size = board_size self.board_size = board_size
@ -51,8 +70,8 @@ class Game(db.Model):
self.player_white = player_white self.player_white = player_white
self.komi = komi self.komi = komi
self.handicap = handicap self.handicap = handicap
self.time_settings = time_settings self.main_time = main_time
print('did it') self.overtime = overtime
class GameSchema(ma.ModelSchema): class GameSchema(ma.ModelSchema):
id = fields.Int() id = fields.Int()

View file

@ -1,60 +1,39 @@
from app import db, ma from app import db, ma
from marshmallow import fields from marshmallow import fields
import enum
# TODO User >---< GameRoom # TODO User >---< GameRoom
# ! Game Room >-< Users join table # ! Game Rooms >-< Users join table
game_rooms_users = db.Table('game_rooms_users', game_rooms_users = db.Table('game_rooms_users',
db.Column('user_id', db.Integer, db.ForeignKey('users.id'), primary_key=True), db.Column('user_id', db.Integer, db.ForeignKey('users.id'), primary_key=True),
db.Column('game_rooms_id', db.Integer, db.ForeignKey('game_rooms.id'), primary_key=True) db.Column('game_rooms_id', db.Integer, db.ForeignKey('game_rooms.id'), primary_key=True)
) )
class RoomLanguages(db.Model):
__tablename__ = "room_languages"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(40), nullable=False)
iso = db.Column(db.String(2), nullable=False)
class GameRoom(db.Model): class GameRoom(db.Model):
__tablename__ = "game_rooms" __tablename__ = "game_rooms"
__table_args__ = {'extend_existing': True} __table_args__ = {'extend_existing': True}
class Languages(enum.Enum):
EN = "English"
id = db.Column(db.Integer, primary_key=True, autoincrement=True) id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(40), nullable=False) name = db.Column(db.String(40), nullable=False)
description = db.Column(db.String(200), nullable=False) description = db.Column(db.String(200), nullable=False)
private = db.Column(db.Boolean(), nullable=False, default=False) private = db.Column(db.Boolean(), nullable=False, default=False)
language = db.Column(db.Enum(Languages), nullable=False, default=Languages.EN)
# ! Foreign Keys def __init__(self, name, description, private=False, language=Languages.EN):
users = db.relationship(
'User',
secondary=game_rooms_users,
lazy='subquery',
backref=db.backref('game_rooms', lazy=True)
)
language = db.Column(db.Integer, db.ForeignKey("room_languages.id"), nullable=False)
def __init__(self, name, description, private=False, language=1):
self.name = name self.name = name
self.description = description self.description = description
self.private = private self.private = private
self.language = language self.language = language
class LanguageSchema(ma.ModelSchema):
id = fields.Int()
name = fields.Str()
iso = fields.Str()
language_schema = LanguageSchema()
class RoomSchema(ma.ModelSchema): class RoomSchema(ma.ModelSchema):
id = fields.Int() id = fields.Int()
name = fields.Str() name = fields.Str()
description = fields.Str() description = fields.Str()
private = fields.Bool() private = fields.Bool()
language = fields.Nested(LanguageSchema) language = fields.Str()
room_schema = RoomSchema() room_schema = RoomSchema()

View file

@ -22,5 +22,11 @@ class Move(db.Model):
game = db.Column(db.Integer, db.ForeignKey("games.id"), nullable=False) game = db.Column(db.Integer, db.ForeignKey("games.id"), nullable=False)
preceding_move = db.Column(db.Integer, db.ForeignKey("moves.id")) preceding_move = db.Column(db.Integer, db.ForeignKey("moves.id"))
succeeding_moves = db.relationship(
'Move',
lazy='subquery',
backref=db.backref('moves', lazy=True)
)
def __init__(self): def __init__(self):
pass pass

View file

@ -1,26 +0,0 @@
from app import db, ma
import enum
class TimeSettings(db.Model):
__tablename__ = "time_settings"
__table_args__ = {'extend_existing': True}
class TimeTypes(enum.Enum):
BYOYOMI = "Counting by time period"
ABSOLUTE = "One period to use time"
HOURGLASS = "Absolute time for both players"
NONE = "Untimed"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
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
def __init__(self, main_time=TimeTypes.NONE, overtime=TimeTypes.NONE):
self.main_time = main_time
self.overtime = overtime

View file

@ -62,6 +62,7 @@ class User(db.Model):
elo = db.Column(db.Integer) elo = db.Column(db.Integer)
rank_certainty = db.Column(db.Boolean, nullable=False, default=False) rank_certainty = db.Column(db.Boolean, nullable=False, default=False)
def __init__(self, username, email, password, rank=Ranks.K1, admin=False): def __init__(self, username, email, password, rank=Ranks.K1, admin=False):
self.username = username self.username = username
self.email = email self.email = email