diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..d83c345 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "eslint.enable": false, + "standard.enable": false +} \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..eacbd7a --- /dev/null +++ b/main.py @@ -0,0 +1,99 @@ +# 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//', 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//player/', methods=['GET']) +def game(game_id, player_name): + if player_name == 'spectator': + gamestate = requests.get(ozai_url + 'game/' + game_id).json() + return render_template('game.html', game_id=game_id, gamestate=gamestate) + else: + 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) + + + +@app.route('/game//player//move', methods=['POST']) +def move(game_id, player_name): + move = request.form['move'] + response = requests.post(ozai_url + 'game/' + game_id + '/player/' + player_name + '/move', json={'move': move}) + if response.status_code == 200: + return 'Move successful' + else: + return 'Move failed' + +if __name__ == '__main__': + app.run(debug=True) \ No newline at end of file diff --git a/templates/azul.webp b/templates/azul.webp new file mode 100644 index 0000000..97c7103 Binary files /dev/null and b/templates/azul.webp differ diff --git a/templates/create.html b/templates/create.html new file mode 100644 index 0000000..ed8a5bb --- /dev/null +++ b/templates/create.html @@ -0,0 +1,53 @@ + + + + + Create Game + + + {% if not game_id %} +
+
Fill inn playernames for wanted players ...
+
Leave empty for no player ...
+ + + + \ + + +
+ + + {% endif %} + {% if game_id %} +

Your game ID is: {{ game_id }}

+ + Join Game + + + {% endif %} + + \ No newline at end of file diff --git a/templates/game.html b/templates/game.html new file mode 100644 index 0000000..52715c4 --- /dev/null +++ b/templates/game.html @@ -0,0 +1,67 @@ + + + + + Game + + +

Game

+

Game ID: {{ game_id }}

+

gamestate: {{ gamestate }}

+ +

Number of Players: {{ gamestate.n_players }}

+

Current Player: {{ gamestate.current_player }}

+

Starting Player: {{ gamestate.starting_player }}

+

Player Names: {{ gamestate.player_names }}

+

Game End: {{ gamestate.game_end }}

+

Rounds: {{ gamestate.rounds }}

+

Days: {{ gamestate.days }}

+ + +

Market

+ {% for color, number in gamestate.market.items() %} +

{{ color }}: {{ number }}

+ {% endfor %} + +

Factories

+ {% for factory in gamestate.factories %} +

factory nr: {{ loop.index }}

+ {% for color, number in factory.items() %} +

{{ color }}: {{ number }}

+ {% endfor %} + {% endfor %} +

Players:

+ + {% for name, player_data in gamestate.players.items() %} + {% if player_name == name %} +

{{ player_name }} (You)

+ {% else %} +

{{ name }}

+ {% endif %} +

Ready: {{ player_data.ready }}

+

Points: {{ player_data.points }}

+

Pattern Lines

+ {% for line in player_data.pattern_lines %} +

Color: {{ line.color }}, Number: {{ line.number }}

+ {% endfor %} +

Wall

+ {% for row in player_data.wall %} +

{{ row }}

+ {% endfor %} +

Floor

+ {% for color, number in player_data.floor.items() %} +

{{ color }}: {{ number }}

+ {% endfor %} + {% endfor %} + + + {% if player_name %} +

Play area

+

Playing as: {{ player_name }}

+ + + {% endif %} + + \ No newline at end of file diff --git a/templates/home.html b/templates/home.html new file mode 100644 index 0000000..904eb9c --- /dev/null +++ b/templates/home.html @@ -0,0 +1,36 @@ + + + + API Interface + + + + Create Game + Join Game + + \ No newline at end of file diff --git a/templates/join.html b/templates/join.html new file mode 100644 index 0000000..6c3afaa --- /dev/null +++ b/templates/join.html @@ -0,0 +1,47 @@ + + + + + Join Game + + + +
+ + +
+ + + + + {% if game_id %} + + + {% endif %} + + + + + \ No newline at end of file diff --git a/templates/join_game.html b/templates/join_game.html new file mode 100644 index 0000000..0564847 --- /dev/null +++ b/templates/join_game.html @@ -0,0 +1,51 @@ + + + + + Join Game + + + {% if game_id %} +

Selected game to join is: {{ game_id }}

+ {% endif %} + + {% if players %} +

Players in game:

+
    + {% for player in players %} +
  • {{ player }}
  • + {% endfor %} +
+ +
+ + +
+ {% else %} +

No players found in game, please recreate the game.

+ {% endif %} + + {% if player_name %} +

Selected player to join as: {{ player_name }}

+ + + {% endif %} + + + + + \ No newline at end of file