diff --git a/main.py b/main.py index d432833..230072a 100644 --- a/main.py +++ b/main.py @@ -1,25 +1,34 @@ # Python from flask import Flask, render_template, request +from flask_socketio import SocketIO, join_room import requests ozai_url = 'http://localhost:8000/api/' app = Flask(__name__) +socketio = SocketIO(app) -# @app.route('/azul.webp') -# def background(): -# #return the background image -# return app.send_static_file('azul.webp') +#get tis from env variable +import os +app.config['SECRET_KEY'] = os.environ.get('OZAI_WEBUI_SECRET_KEY') + +#home page @app.route('/') def home(): return render_template('home.html') - +#static files @app.route('/azul.webp') def background(): #return the background image return app.send_static_file('azul.webp') +@app.route('/azul-flake.png') +def logo(): + #return the background image + return app.send_static_file('azul-flake.png') + +#game creation page @app.route('/create', methods=['GET', 'POST']) def create(): if request.method == 'POST': @@ -57,6 +66,7 @@ def create(): return render_template('create.html') +#join page, for when the user has gotten a game id. @app.route('/join', methods=['GET', 'POST']) def join(): if request.method == 'POST': @@ -65,7 +75,7 @@ def join(): return render_template('join.html', game_id=game_id) return render_template('join.html') - +#select player name page @app.route('/join_game//', methods=['GET','POST']) def join_game(game_id): @@ -85,49 +95,67 @@ def join_game(game_id): return render_template('join_game.html', game_id=game_id, players=players) -@app.route('/game//player/', methods=['GET', 'POST']) +#play game page +@app.route('/game//player/', methods=['GET']) def game(game_id, player_name): gamestate = requests.get(ozai_url + 'game/' + game_id).json() - move_status = False + #dont send player parameter if the player is a spectator + if player_name == 'spectator': + gamestate = requests.get(ozai_url + 'game/' + game_id).json() + return render_template('game.html', game_id=game_id, gamestate=gamestate) + elif player_name in gamestate['players']: + gamestate = requests.get(ozai_url + 'game/' + game_id + '?player=' + player_name).json() + return render_template('game.html', game_id=game_id, gamestate=gamestate, player_name=player_name) + else: + return 'Player not found', 404 + + +@socketio.on('connect') +def ws_connect(message): + print('Client connected' + str(message)) + pass + +@socketio.on('join') +def ws_message(message): + print('Client message' + str(message)) + game_id = message['game_id'] + print('Game ID: ', game_id) + join_room(game_id) + +@socketio.on('move') +def ws_message(data): + print('Client move' + str(data)) + + game_id = data['game_id'] + player_name = data['player_name'] #if a move was made. - if request.method == 'POST': - - source = request.form['source'] - color = request.form['color'] - destination = request.form['destination'] - - if source != "market": - source = int(source)-1 - if destination != "floor": - destination = int(destination)-1 + source = data['move']['source'] + color = data['move']['color'] + destination = data['move']['destination'] + if source != "market": + source = int(source)-1 + if destination != "floor": + destination = int(destination)-1 + move = { + 'player': str(player_name), + 'policy': 'strict', # or 'loose' depending on your needs + 'color': str(color), + 'source': source, + 'destination': destination + } + print(move) + response = requests.put(ozai_url + 'game/' + game_id, json=move) + if response.status_code == 200: + socketio.emit('move', data, room=game_id) + #send message to the other players, that the move was made + socketio.emit('move_status', 'sucsess', room=game_id) + else: + #send message to the player that the move was invalid + socketio.emit('move_status', response.text, room=game_id) - move = { - 'player': str(player_name), - 'policy': 'strict', # or 'loose' depending on your needs - 'color': str(color), - 'source': source, - 'destination': destination - } - - print(move) - response = requests.put(ozai_url + 'game/' + game_id, json=move) - if response.status_code == 200: - move_status = "Move Successful" - else: - move_status = response.text - - #dont send player parameter if the player is a spectator - while True: - if player_name == 'spectator': - gamestate = requests.get(ozai_url + 'game/' + game_id).json() - return render_template('game.html', game_id=game_id, gamestate=gamestate) - elif player_name in gamestate['players']: - gamestate = requests.get(ozai_url + 'game/' + game_id + '?player=' + player_name).json() - return render_template('game.html', game_id=game_id, gamestate=gamestate, player_name=player_name, move_status=move_status) - else: - return 'Player not found', 404 - return 'Player not found', 404 if __name__ == '__main__': - app.run(debug=True, host='0.0.0.0', port=5000) \ No newline at end of file + # app.run(debug=True, host='0.0.0.0', port=5000,ssl_context='adhoc') + + socketio.run(app, debug=True, host='0.0.0.0', port=5000) \ No newline at end of file diff --git a/static/azul-flake.png b/static/azul-flake.png new file mode 100644 index 0000000..80402f1 Binary files /dev/null and b/static/azul-flake.png differ diff --git a/templates/create.html b/templates/create.html index 7bc69ec..646a69f 100644 --- a/templates/create.html +++ b/templates/create.html @@ -2,7 +2,8 @@ - Create Game + Ozai:create + diff --git a/templates/join_game.html b/templates/join_game.html index 11633e8..acd1135 100644 --- a/templates/join_game.html +++ b/templates/join_game.html @@ -12,6 +12,10 @@ font-family: Arial, sans-serif; margin: 0; padding: 0; + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; } ul {