From 0924b4ee45fbf7dedcebbbb212be604c00b7966c Mon Sep 17 00:00:00 2001 From: Adrian Gunnar Lauterer Date: Sun, 25 Feb 2024 20:41:38 +0100 Subject: [PATCH] header cleanup + fixes + websocket moves --- main.py | 118 ++++++++++++++++++++++++--------------- static/azul-flake.png | Bin 0 -> 5540 bytes templates/create.html | 8 ++- templates/game.html | 63 +++++++++++++++------ templates/home.html | 3 +- templates/join.html | 16 +++++- templates/join_game.html | 4 ++ 7 files changed, 143 insertions(+), 69 deletions(-) create mode 100644 static/azul-flake.png 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 0000000000000000000000000000000000000000..80402f1282d115aa0bcf52aa418b3443a15a90b5 GIT binary patch literal 5540 zcmZ`-XIN87)IRqnkU$^=v492%$l4VIM8J};qM*2nii(x2qNpHJRItFkE7nDYbybRJ zf)y*+u_h=2D;;!MQHp|e1Wjlmx$_O*@9%q_JUMgbJ@dY2&fGG0j{7Y3G&US*2mrlb*q`$koDRPQ~rYO|$&SL7CRef>Y! z*~S&@u$Y1mwm+>y^x?Jr1J?x^UP+Bs1DU8QwJQJu4_TxZ!SYF`#xa}SFCuOGX(A=joh z`puQstgec+ar;$Mp6rJ%)zJ~%HAf3oQ1_DB+_bSqbINWnM48b3uCIyJL(5C5^1iDQR%yeet$fi5xBj0oa8FRI#A3z-w zmJA4QV*-&ie2B=*#kpRAp(WXCaL^D|m-pG??3BTHmX8}Js07Col5fJ>jyT>J4o74T zPS+P+C|<>1d%peI*p_HQQeNoe#4#(xp*%^MP^d}b&|RP{UvD(1W+M4VxX2lgX2Gxs z`!9I(WEfhQeFaN65D9ZQe`AS!u0$)kz(gRMq*b^;Pr>VaY)g|BG_Xb6`CF1GNEks=zZMD$9WdPtm=8OoC@sY!e}G3y?C)6HgDu$1*T)xxz)W(;)qEcg@`bb}X&(;yi(p@fHN~ts`hrk? zE-s3|Qj+QFvi%+hP6An!?SiGb&{w?ar+?iS%hB+V{@_c6=a1e_rtCH60zkq?=VN$_%t^b+>) zb8$5!MxqZwp?v|1o(>ZlMQ^n%2#1)Kt?pR9nXpZbtzQ`hnFgXVp=}BUNMD-5k^69__;oChhu|$m~IhMW=Psr48W&WD;Pvb>#wN(|3H2io~<+QS4jv zb3$mTK1rMpGn5j_D)fw1ej>Ybe?94fVShL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zGKs`$_VC9CE+*>4_>gl$Qq}xHHk9ChW)NPdVI#SmXRRP6EWJj z>6o)X8|Q>gkv7f|n-XoD12$#axIw)NZJa$eAGEx|xqZ~~W^)KprFQ0@NT1&UFcl`{ zd1u8Qz>`xKDdgDPFHwA1_T9P!XK_+xSBK46mEC7-uBz4KltPV4kxDS=D4cW@dq$`zY*8TInP zl2MRDbiFHf3z_f%Q?gWcgQ`50-Qc2_sq6-@8Kbgm!)B7ot{xl5|E&v$wugs>ikgpM zrf>*v=9taNu^`>XbT2OGzy~?XhWSO2t0!+T;a~#gtkPL%S)n`FyP|iEb++K8CqY_oe2pnv2bT+z4y8}_CkEru)%}TE`J`lZ zqsfzf8TiNm{4P=k*d$$~y79csWmGrz)Jc?QmEz0XDndRI&Z{y8S9m~`(TFQX7An@@ zGXH6hwqwQdIfDXbgq%Z{ub%h3^S43B0o}w<)J`V)89rkS!*XGb{%>(T^-`$K5usBd x>(?TMVMJyZ+zpxhvwFscj{j$K=T0`&_L24<-bv;>#K*3%VBTVnyT3{g{txte;rajo literal 0 HcmV?d00001 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 {