save move to db

This commit is contained in:
Sorrel Bri 2019-10-13 16:08:05 -07:00
parent f6fc5764aa
commit 9d6098d30a
2 changed files with 42 additions and 7 deletions

View file

@ -22,11 +22,23 @@ class Move(db.Model):
game = db.Column(db.ForeignKey("games.id"), nullable=False) game = db.Column(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(
def __init__(self, game_id, player, x_point, y_point, move_number, is_pass=True, is_main=True, preceding_move=None):
self.game = game_id
if player == 'black':
self.player = 'BLACK'
else:
self.player = 'WHITE'
self.x_point = x_point
self.y_point = y_point
self.move_number = move_number
self.is_pass = is_pass
self.is_main = is_main
self.preceding_move = preceding_move
Move.succeeding_moves = db.relationship(
'Move', 'Move',
lazy='subquery', lazy='subquery',
backref=db.backref('moves', lazy=True) backref=db.backref('moves', lazy=True),
remote_side=Move.id
) )
def __init__(self):
pass

View file

@ -3,6 +3,9 @@ from flask_socketio import send, emit, join_room, leave_room
import json import json
from models.Game import Game from models.Game import Game
from models.User import User, user_schema from models.User import User, user_schema
from models.Move import Move
from database import db
def join_room_notice(room): def join_room_notice(room):
@socketio.on('join room', namespace=f'/{room}') @socketio.on('join room', namespace=f'/{room}')
@ -31,3 +34,23 @@ def join_game_notice(game):
print('emit join game') print('emit join game')
join_room(game_id) join_room(game_id)
emit('join game', {'black': black, 'white': white}, broadcast=True) emit('join game', {'black': black, 'white': white}, broadcast=True)
@socketio.on('add move', namespace=f'/{room_id}')
def handle_new_move(data):
game_id = data['game']
player = data['move']['player']
x_point = data['move']['x_point']
y_point = data['move']['y_point']
move_number = data['move_number']
try:
move = Move(
game_id=game_id,
player=player,
x_point=x_point,
y_point=y_point,
move_number=move_number
)
db.session.add(move)
db.session.commit()
except Exception as e:
emit('move rejected', e)