229 lines
8.1 KiB
Python
Executable File
229 lines
8.1 KiB
Python
Executable File
#!/usr/bin/env python
|
|
from flask import Flask, render_template, request
|
|
from flask_socketio import SocketIO, join_room
|
|
import requests
|
|
#import argparse
|
|
import os
|
|
|
|
|
|
ozai_url = 'http://localhost:8000/api/'
|
|
ozai_webui_host = '0.0.0.0'
|
|
ozai_webui_port = 5000
|
|
#static_folder = '../static'
|
|
#template_folder = '../templates'
|
|
|
|
#check if the environment variables are set
|
|
if os.getenv('OZAI_URL') is not None:
|
|
ozai_url = os.getenv('OZAI_URL')
|
|
if os.getenv('OZAI_WEBUI_HOST') is not None:
|
|
ozai_webui_host = os.getenv('OZAI_WEBUI_HOST')
|
|
if os.getenv('OZAI_WEBUI_PORT') is not None:
|
|
ozai_webui_port = int(os.getenv('OZAI_WEBUI_PORT'))
|
|
if os.getenv('OZAI_WEBUI_SECRET_KEY') is not None:
|
|
app.config['OZAI_WEBUI_SECRET_KEY'] = os.getenv('OZAI_WEBUI_SECRET_KEY')
|
|
#if os.getenv('OZAI_WEBUI_STATIC_FOLDER') is not None:
|
|
# static_folder = os.getenv('OZAI_WEBUI_STATIC_FOLDER')
|
|
#if os.getenv('OZAI_WEBUI_TEMPLATE_FOLDER') is not None:
|
|
# template_folder = os.getenv('OZAI_WEBUI_TEMPLATE_FOLDER')
|
|
|
|
|
|
print(f"using host { ozai_webui_host }")
|
|
print(f"using port { ozai_webui_port }")
|
|
print(f"using ozai url { ozai_url }")
|
|
#print(f"using template folder { template_folder }")
|
|
#print(f"using static folder { static_folder }")
|
|
|
|
app = Flask(__name__,template_folder="../templates",static_folder="../static" )
|
|
socketio = SocketIO(app)
|
|
app.config['SECRET_KEY'] = "secret"
|
|
|
|
#parser = argparse.ArgumentParser(description="Run the Ozai WebUI server")
|
|
#
|
|
#parser.add_argument('-H', '--host', type=str, default=ozai_webui_host, help='The host to run the server on')
|
|
#parser.add_argument('-P', '--port', type=int, default=ozai_webui_port, help='The port to run the server on')
|
|
#parser.add_argument('-O', '--ozai_url', type=str, default=ozai_url, help='The URL of the Ozai server')
|
|
#parser.add_argument('-S', '--secret_key', type=str, default=app.config['SECRET_KEY'], help='The secret key for the Flask app')
|
|
#parser.add_argument('--static_folder', type=str, default=static_folder, help='The location of the static folder')
|
|
#parser.add_argument('--template_folder', type=str, default=template_folder, help='The location of the template folder')
|
|
#
|
|
#
|
|
#args = parser.parse_args()
|
|
#
|
|
#if args.host:
|
|
# ozai_webui_host = args.host
|
|
#if args.port:
|
|
# ozai_webui_port = args.port
|
|
#if args.ozai_url:
|
|
# ozai_url = args.ozai_url
|
|
#if args.secret_key:
|
|
# app.config['SECRET_KEY'] = args.secret_key
|
|
#if args.static_folder:
|
|
# app.static_folder = args.static_folder
|
|
#if args.template_folder:
|
|
# app.template_folder = args.template_folder
|
|
#
|
|
|
|
#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('/socket.io.js')
|
|
def socketio_js():
|
|
#return the background image
|
|
return app.send_static_file('socket.io.js')
|
|
|
|
@app.route('/browser-polyfill.js')
|
|
def polyfill_js():
|
|
#return the background image
|
|
return app.send_static_file('browser-polyfill.js')
|
|
|
|
@app.route('/browser-polyfill.js.map')
|
|
def polyfill_map_js():
|
|
#return the background image
|
|
return app.send_static_file('browser-polyfill.js.map')
|
|
|
|
@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':
|
|
# Handle the post request here
|
|
#get all the player names data from the form
|
|
player_names = []
|
|
for i in range(1, 5):
|
|
player_name = request.form['player' + str(i)]
|
|
#purge to long names
|
|
if len(player_name) > 20:
|
|
player_name = player_name[:20]
|
|
#sanitize the input
|
|
player_name = player_name.strip()
|
|
#replace spaces with underscores
|
|
player_name = player_name.replace(' ', '_')
|
|
# replace illegal characters with nothing
|
|
illegal_chars = ['<', '>', '(', ')', '[', ']', ',', '.', ':', ';', '-', '/', '|', '\\','=', 'exec', 'spectator']
|
|
for char in illegal_chars:
|
|
player_name = player_name.replace(char, '')
|
|
if player_name:
|
|
player_names.append(player_name)
|
|
|
|
player_names = {"player_names": player_names}
|
|
# print(player_names)
|
|
response = requests.post(ozai_url + 'game', json=player_names)
|
|
if response.status_code == 200:
|
|
game_id = response.json()
|
|
# print("Game ID:", game_id)
|
|
return render_template('create.html', game_id=game_id)
|
|
else:
|
|
pass
|
|
# print("Error:", response.status_code, response.text)#log the game id
|
|
#return the game id to the user
|
|
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':
|
|
#get game id from the form and redirect to the join_game page
|
|
game_id = request.form['game_id']
|
|
return render_template('join.html', game_id=game_id)
|
|
return render_template('join.html')
|
|
|
|
|
|
|
|
|
|
@app.route('/join_game/<game_id>/', methods=['GET', 'POST'])
|
|
def join_game(game_id):
|
|
response = requests.get(ozai_url + 'game/' + game_id)
|
|
if response.status_code != 200:
|
|
return 'Game not found', 404
|
|
players = response.json().get('players')
|
|
|
|
return render_template('join_game.html', game_id=game_id, players=players)
|
|
|
|
|
|
#play game page
|
|
@app.route('/game/<game_id>/player/<player_name>', methods=['GET'])
|
|
@app.route('/game/<game_id>/player/<player_name>/local/<local_game>', methods=['GET'])
|
|
def game(game_id, player_name, local_game=None):
|
|
local_game = local_game == 'True'
|
|
gamestate = requests.get(ozai_url + 'game/' + game_id).json()
|
|
#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, local_game=False)
|
|
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, local_game=local_game)
|
|
else:
|
|
return 'Player not found', 404
|
|
|
|
@app.route('/gametitle/<game_id>', methods=['GET'])
|
|
def gametitle(game_id):
|
|
gamestate = requests.get(ozai_url + 'game/' + game_id).json()
|
|
return render_template('gametitle.html', game_id=game_id, gamestate=gamestate)
|
|
|
|
|
|
@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.
|
|
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)
|
|
|
|
def main ():
|
|
# app.run(debug=True, host='0.0.0.0', port=5000,ssl_context='adhoc')
|
|
return socketio.run(app, debug=False, host=ozai_webui_host, port=ozai_webui_port, allow_unsafe_werkzeug=True)
|
|
|
|
if __name__ == '__main__':
|
|
main()
|