2024-02-14 21:52:29 +01:00
|
|
|
# Python
|
|
|
|
from flask import Flask, render_template, request
|
2024-02-25 20:41:38 +01:00
|
|
|
from flask_socketio import SocketIO, join_room
|
2024-02-14 21:52:29 +01:00
|
|
|
import requests
|
|
|
|
|
|
|
|
ozai_url = 'http://localhost:8000/api/'
|
|
|
|
app = Flask(__name__)
|
2024-02-25 20:41:38 +01:00
|
|
|
socketio = SocketIO(app)
|
2024-02-14 21:52:29 +01:00
|
|
|
|
2024-02-25 20:41:38 +01:00
|
|
|
#get tis from env variable
|
|
|
|
import os
|
|
|
|
app.config['SECRET_KEY'] = os.environ.get('OZAI_WEBUI_SECRET_KEY')
|
2024-02-24 21:33:28 +01:00
|
|
|
|
2024-02-25 20:41:38 +01:00
|
|
|
|
|
|
|
#home page
|
2024-02-14 21:52:29 +01:00
|
|
|
@app.route('/')
|
|
|
|
def home():
|
|
|
|
return render_template('home.html')
|
|
|
|
|
2024-02-25 20:41:38 +01:00
|
|
|
#static files
|
2024-02-24 21:33:28 +01:00
|
|
|
@app.route('/azul.webp')
|
|
|
|
def background():
|
|
|
|
#return the background image
|
|
|
|
return app.send_static_file('azul.webp')
|
2024-02-25 20:41:38 +01:00
|
|
|
@app.route('/azul-flake.png')
|
|
|
|
def logo():
|
|
|
|
#return the background image
|
|
|
|
return app.send_static_file('azul-flake.png')
|
2024-02-24 21:33:28 +01:00
|
|
|
|
2024-02-25 20:41:38 +01:00
|
|
|
|
|
|
|
#game creation page
|
2024-02-14 21:52:29 +01:00
|
|
|
@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
|
2024-02-24 22:55:26 +01:00
|
|
|
illegal_chars = ['<', '>', '(', ')', '[', ']', ',', '.', ':', ';', '-', '/', '|', '\\','=', 'exec', 'spectator']
|
2024-02-14 21:52:29 +01:00
|
|
|
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)
|
2024-02-24 21:33:28 +01:00
|
|
|
return render_template('create.html', game_id=game_id)
|
2024-02-14 21:52:29 +01:00
|
|
|
else:
|
|
|
|
print("Error:", response.status_code, response.text)#log the game id
|
2024-02-24 21:33:28 +01:00
|
|
|
|
2024-02-14 21:52:29 +01:00
|
|
|
#return the game id to the user
|
|
|
|
pass
|
|
|
|
return render_template('create.html')
|
|
|
|
|
|
|
|
|
2024-02-25 20:41:38 +01:00
|
|
|
#join page, for when the user has gotten a game id.
|
2024-02-14 21:52:29 +01:00
|
|
|
@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')
|
|
|
|
|
2024-02-25 20:41:38 +01:00
|
|
|
#select player name page
|
2024-02-14 21:52:29 +01:00
|
|
|
@app.route('/join_game/<game_id>/', methods=['GET','POST'])
|
|
|
|
def join_game(game_id):
|
|
|
|
|
|
|
|
#get players from servre api and return them to the user
|
|
|
|
response = requests.get(ozai_url + 'game/' + game_id)
|
2024-02-24 21:33:28 +01:00
|
|
|
if response.status_code != 200:
|
|
|
|
#return error message
|
|
|
|
return 'Game not found', 404
|
2024-02-14 21:52:29 +01:00
|
|
|
players = response.json().get('players')
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
#get the chosen player name from the form and redirect to the game page
|
|
|
|
player_name = request.form['player']
|
|
|
|
print('Player Name:', player_name)
|
|
|
|
return render_template('join_game.html', game_id=game_id, players=players, player_name=player_name)
|
|
|
|
|
|
|
|
return render_template('join_game.html', game_id=game_id, players=players)
|
|
|
|
|
|
|
|
|
2024-02-25 20:41:38 +01:00
|
|
|
#play game page
|
|
|
|
@app.route('/game/<game_id>/player/<player_name>', methods=['GET'])
|
2024-02-14 21:52:29 +01:00
|
|
|
def game(game_id, player_name):
|
2024-02-15 12:58:18 +01:00
|
|
|
gamestate = requests.get(ozai_url + 'game/' + game_id).json()
|
2024-02-25 20:41:38 +01:00
|
|
|
#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))
|
2024-02-15 12:58:18 +01:00
|
|
|
|
2024-02-25 20:41:38 +01:00
|
|
|
game_id = data['game_id']
|
|
|
|
player_name = data['player_name']
|
2024-02-15 12:58:18 +01:00
|
|
|
|
2024-02-25 20:41:38 +01:00
|
|
|
#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)
|
|
|
|
|
2024-02-14 21:52:29 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2024-02-25 20:41:38 +01:00
|
|
|
# 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)
|