establish game socketio room connection
This commit is contained in:
parent
e378697974
commit
7eeac76af9
3 changed files with 19 additions and 15 deletions
|
@ -7,7 +7,7 @@ from ..decorators import jwt_required
|
|||
import jwt
|
||||
import os
|
||||
import json
|
||||
from websockets.socket import new_game_notice
|
||||
from websockets.socket import new_game_notice, join_game_notice
|
||||
|
||||
api_games = Blueprint('api_games', __name__, url_prefix='/api/games')
|
||||
|
||||
|
@ -16,20 +16,20 @@ def get_room(game_id):
|
|||
print(game_id)
|
||||
game = Game.query.filter_by(id=game_id).first()
|
||||
response = game_schema.dumps(game)
|
||||
# join_game_notice(game_id)
|
||||
# TODO create decorator that returns user from header
|
||||
auth_header = request.headers.get('Authorization')
|
||||
user = jwt.decode(auth_header.split(" ")[1], os.environ.get('SECRET_KEY'))['user']
|
||||
print(user)
|
||||
join_game_notice(game_id, user)
|
||||
return jsonify(response)
|
||||
|
||||
@api_games.route('/', methods=['POST'])
|
||||
@jwt_required()
|
||||
def post_game():
|
||||
print('Hey it\'s a post request!')
|
||||
data = request.get_json()
|
||||
# TODO create decorator that returns user from header
|
||||
auth_header = request.headers.get('Authorization')
|
||||
user = jwt.decode(auth_header.split(" ")[1], os.environ.get('SECRET_KEY'))['user']
|
||||
print(user)
|
||||
print('data')
|
||||
print(data)
|
||||
user_id = json.loads(user)['id']
|
||||
try:
|
||||
game = Game(
|
||||
|
@ -39,12 +39,8 @@ def post_game():
|
|||
game_room = data['gameRoom'],
|
||||
player_white = user_id
|
||||
)
|
||||
print(game)
|
||||
db.session.add(game)
|
||||
print('game added')
|
||||
db.session.commit()
|
||||
print('game')
|
||||
print(game_schema.dumps(game))
|
||||
new_game_notice(room=game.game_room, game=game_schema.dumps(game))
|
||||
response = {
|
||||
'status': 'success',
|
||||
|
|
|
@ -79,6 +79,7 @@ class GameSchema(ma.ModelSchema):
|
|||
description = fields.Str()
|
||||
board_size = fields.Int()
|
||||
player = fields.Nested(user_schema)
|
||||
game_room = fields.Int()
|
||||
|
||||
game_schema = GameSchema()
|
||||
games_schema = GameSchema(many=True)
|
|
@ -1,6 +1,8 @@
|
|||
from app import socketio
|
||||
from flask_socketio import send, emit
|
||||
from flask_socketio import send, emit, join_room, leave_room
|
||||
import json
|
||||
|
||||
# ! Basic Connection
|
||||
@socketio.on('connect')
|
||||
def handle_connection():
|
||||
print('''
|
||||
|
@ -24,11 +26,9 @@ def handle_connection():
|
|||
|
||||
''')
|
||||
|
||||
# ! Game Room Messages
|
||||
|
||||
def join_room_notice(room):
|
||||
@socketio.on('connect', namespace=f'/{room}')
|
||||
def handle_connection():
|
||||
print(f'joined room at {room}')
|
||||
emit('join room', {'roomspace': f'{room}'})
|
||||
@socketio.on('join room', namespace=f'/{room}')
|
||||
def connect_room(message):
|
||||
print(f'connected with ${message}')
|
||||
|
@ -41,6 +41,13 @@ def new_game_notice(room, game):
|
|||
def new_room_notice(room):
|
||||
socketio.emit('new room', room, broadcast=True)
|
||||
|
||||
def join_game_notice(game_id, user):
|
||||
@socketio.on('join game')
|
||||
def return_join_game_notice(data):
|
||||
game = data['game']
|
||||
join_room(game)
|
||||
emit('join game', data, room=f'game')
|
||||
|
||||
# @socketio.on
|
||||
# def room_socket(roomspace):
|
||||
# pass
|
Loading…
Reference in a new issue