browser-go-api/websockets/roomSocket.py

56 lines
1.8 KiB
Python
Raw Normal View History

2019-10-12 07:16:00 +00:00
from app import socketio
from flask_socketio import send, emit, join_room, leave_room
import json
2019-10-13 17:15:03 +00:00
from models.Game import Game
from models.User import User, user_schema
2019-10-13 23:08:05 +00:00
from models.Move import Move
from database import db
2019-10-12 07:16:00 +00:00
def join_room_notice(room):
@socketio.on('join room', namespace=f'/{room}')
def connect_room(message):
print(f'connected with ${message}')
emit('connected', {'roomspace': f'/{room}'})
def new_game_notice(room, game):
print('sending new game notice')
socketio.emit('new game', game, broadcast=True, namespace=f'/{room}')
def new_room_notice(room):
socketio.emit('new room', room, broadcast=True)
2019-10-13 19:56:46 +00:00
def join_game_notice(game):
print('join game')
print(game)
black = user_schema.dumps(User.query.filter_by(id=game.player_black).first())
white = user_schema.dumps(User.query.filter_by(id=game.player_white).first())
room_id = game.game_room
game_id = game.id
print(black)
@socketio.on('join game', namespace=f'/{room_id}')
2019-10-13 17:15:03 +00:00
def handle_join_game(data):
2019-10-13 19:56:46 +00:00
print('emit join game')
2019-10-13 17:15:03 +00:00
join_room(game_id)
2019-10-13 19:56:46 +00:00
emit('join game', {'black': black, 'white': white}, broadcast=True)
2019-10-13 23:08:05 +00:00
@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)