121 lines
4.2 KiB
Python
121 lines
4.2 KiB
Python
# Python
|
|
from flask import Flask, render_template, request
|
|
import requests
|
|
|
|
ozai_url = 'http://localhost:8000/api/'
|
|
app = Flask(__name__)
|
|
|
|
@app.route('/')
|
|
def home():
|
|
return render_template('home.html')
|
|
|
|
@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)
|
|
else:
|
|
print("Error:", response.status_code, response.text)#log the game id
|
|
|
|
print('Game ID:', game_id)
|
|
#return the game id to the user
|
|
return render_template('create.html', game_id=game_id)
|
|
pass
|
|
return render_template('create.html')
|
|
|
|
|
|
|
|
|
|
|
|
@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):
|
|
|
|
#get players from servre api and return them to the user
|
|
response = requests.get(ozai_url + 'game/' + game_id)
|
|
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)
|
|
|
|
|
|
@app.route('/game/<game_id>/player/<player_name>', methods=['GET', 'POST'])
|
|
def game(game_id, player_name):
|
|
gamestate = requests.get(ozai_url + 'game/' + game_id).json()
|
|
move_status = False
|
|
|
|
if request.method == 'POST':
|
|
|
|
source = request.form['source']
|
|
color = request.form['color']
|
|
destination = request.form['destination']
|
|
#convert source to int if it is not market
|
|
if source != 'market':
|
|
source = int(source)
|
|
#convert destination to int if it is not floor
|
|
if destination != 'floor':
|
|
destination = int(destination)
|
|
|
|
move = {
|
|
'player': player_name,
|
|
'policy': 'strict', # or 'loose' depending on your needs
|
|
'color': color,
|
|
'source': source,
|
|
'destination': destination
|
|
}
|
|
response = requests.put(ozai_url + 'game/' + game_id, json=move)
|
|
|
|
if response.status_code == 200:
|
|
move_status = "Move Successful"
|
|
else:
|
|
move_status = response.text
|
|
|
|
|
|
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
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True) |