save move to db
This commit is contained in:
parent
f6fc5764aa
commit
9d6098d30a
2 changed files with 42 additions and 7 deletions
|
@ -22,11 +22,23 @@ class Move(db.Model):
|
|||
game = db.Column(db.ForeignKey("games.id"), nullable=False)
|
||||
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):
|
||||
pass
|
||||
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',
|
||||
lazy='subquery',
|
||||
backref=db.backref('moves', lazy=True),
|
||||
remote_side=Move.id
|
||||
)
|
|
@ -3,6 +3,9 @@ from flask_socketio import send, emit, join_room, leave_room
|
|||
import json
|
||||
from models.Game import Game
|
||||
from models.User import User, user_schema
|
||||
from models.Move import Move
|
||||
from database import db
|
||||
|
||||
|
||||
def join_room_notice(room):
|
||||
@socketio.on('join room', namespace=f'/{room}')
|
||||
|
@ -31,3 +34,23 @@ def join_game_notice(game):
|
|||
print('emit join game')
|
||||
join_room(game_id)
|
||||
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)
|
Loading…
Reference in a new issue