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