init
This commit is contained in:
parent
ef3bc600ed
commit
b8bd161577
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"eslint.enable": false,
|
||||
"standard.enable": false
|
||||
}
|
|
@ -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/<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'])
|
||||
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/<game_id>/player/<player_name>/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)
|
Binary file not shown.
After Width: | Height: | Size: 62 KiB |
|
@ -0,0 +1,53 @@
|
|||
<!-- HTML -->
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Create Game</title>
|
||||
</head>
|
||||
<body>
|
||||
{% if not game_id %}
|
||||
<form method="POST" action="/create">
|
||||
<div>Fill inn playernames for wanted players ...</div>
|
||||
<div>Leave empty for no player ...</div>
|
||||
<input type="text" name="player1" placeholder="Player 1" required>
|
||||
<input type="text" name="player2" placeholder="Player 2" required>
|
||||
<input type="text" name="player3" placeholder="Player 3">
|
||||
<input type="text" name="player4" placeholder="Player 4">\
|
||||
<!-- Add more input fields for more players -->
|
||||
<input type="submit" value="Create Game">
|
||||
</form>
|
||||
<button onclick="window.history.back();">Back</button>
|
||||
<script>
|
||||
// Select all player input elements
|
||||
var playerInputs = document.querySelectorAll('input[name^="player"]');
|
||||
|
||||
// Attach event listener to each input
|
||||
playerInputs.forEach(function(input) {
|
||||
input.addEventListener('change', function() {
|
||||
var currentValue = this.value;
|
||||
console.log(currentValue);
|
||||
var isUnique = Array.from(playerInputs).every(function(otherInput) {
|
||||
return otherInput === input || otherInput.value !== currentValue;
|
||||
});
|
||||
if (!isUnique && currentValue !== "") {
|
||||
alert('Player names must be unique!');
|
||||
// append a number to the name
|
||||
this.value = "";
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
{% endif %}
|
||||
{% if game_id %}
|
||||
<p>Your game ID is: {{ game_id }}</p>
|
||||
<!-- button to redirect to /join/gameid -->
|
||||
<a href="/join_game/{{ game_id }}">Join Game</a>
|
||||
|
||||
<script>
|
||||
// Save game ID in browser session for auto fill in join page
|
||||
sessionStorage.setItem('game_id', '{{ game_id }}');
|
||||
alert('Game created with ID: {{ game_id }}');
|
||||
</script>
|
||||
{% endif %}
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,67 @@
|
|||
<!-- HTML for game.html -->
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Game</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Game</h1>
|
||||
<p>Game ID: {{ game_id }}</p>
|
||||
<p>gamestate: {{ gamestate }}</p>
|
||||
|
||||
<p>Number of Players: {{ gamestate.n_players }}</p>
|
||||
<p>Current Player: {{ gamestate.current_player }}</p>
|
||||
<p>Starting Player: {{ gamestate.starting_player }}</p>
|
||||
<p>Player Names: {{ gamestate.player_names }}</p>
|
||||
<p>Game End: {{ gamestate.game_end }}</p>
|
||||
<p>Rounds: {{ gamestate.rounds }}</p>
|
||||
<p>Days: {{ gamestate.days }}</p>
|
||||
|
||||
|
||||
<h2>Market</h2>
|
||||
{% for color, number in gamestate.market.items() %}
|
||||
<p>{{ color }}: {{ number }}</p>
|
||||
{% endfor %}
|
||||
|
||||
<h1>Factories</h1>
|
||||
{% for factory in gamestate.factories %}
|
||||
<h2>factory nr: {{ loop.index }}</h2>
|
||||
{% for color, number in factory.items() %}
|
||||
<p>{{ color }}: {{ number }}</p>
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
<h1>Players:</h1>
|
||||
|
||||
{% for name, player_data in gamestate.players.items() %}
|
||||
{% if player_name == name %}
|
||||
<h2>{{ player_name }} (You)</h2>
|
||||
{% else %}
|
||||
<h2>{{ name }}</h2>
|
||||
{% endif %}
|
||||
<p>Ready: {{ player_data.ready }}</p>
|
||||
<p>Points: {{ player_data.points }}</p>
|
||||
<h3>Pattern Lines</h3>
|
||||
{% for line in player_data.pattern_lines %}
|
||||
<p>Color: {{ line.color }}, Number: {{ line.number }}</p>
|
||||
{% endfor %}
|
||||
<h3>Wall</h3>
|
||||
{% for row in player_data.wall %}
|
||||
<p>{{ row }}</p>
|
||||
{% endfor %}
|
||||
<h3>Floor</h3>
|
||||
{% for color, number in player_data.floor.items() %}
|
||||
<p>{{ color }}: {{ number }}</p>
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
|
||||
|
||||
{% if player_name %}
|
||||
<h1>Play area</h1>
|
||||
<p>Playing as: {{ player_name }}</p>
|
||||
<!-- form for submitting a move -->
|
||||
<!-- <form method="POST" action="/game/{{ game_id }}/player/{{ player_name }}">
|
||||
<input type="text" name="move" placeholder="Move" required>
|
||||
<input type="submit" value="Submit Move"> -->
|
||||
{% endif %}
|
||||
</body>
|
||||
</html>
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,47 @@
|
|||
<!-- HTML -->
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Join Game</title>
|
||||
</head>
|
||||
<body>
|
||||
<!-- a field to add a game id -->
|
||||
<form id="joinGameForm" method="POST" action="/join" onsubmit="submitForm(event)">
|
||||
<input type="text" name="game_id" placeholder="Game ID" required value="{{ game_id }}">
|
||||
<input type="submit" value="Join Game">
|
||||
</form>
|
||||
<button onclick="window.history.back();">Back</button>
|
||||
<!-- script to get from localstorage and prefill join game -->
|
||||
<script>
|
||||
function submitForm(e) {
|
||||
e.preventDefault();
|
||||
var game_id = document.querySelector('input[name="game_id"]').value;
|
||||
sessionStorage.setItem('game_id', game_id);
|
||||
document.getElementById('joinGameForm').submit();
|
||||
}
|
||||
|
||||
var game_id = sessionStorage.getItem('game_id');
|
||||
if (game_id) {
|
||||
document.querySelector('input[name="game_id"]').value = game_id;
|
||||
}
|
||||
</script>
|
||||
|
||||
{% if game_id %}
|
||||
<script>
|
||||
var game_id = '{{ game_id }}';
|
||||
sessionStorage.setItem('game_id', game_id);
|
||||
//redirect to join game
|
||||
window.location.href = '/join_game/' + game_id;
|
||||
</script>
|
||||
<noscript>
|
||||
<p>manual redirect</p>
|
||||
<a href="/join_game/+{{ game_id }}">Go to player selection</a>
|
||||
</noscript>
|
||||
{% endif %}
|
||||
|
||||
|
||||
<noscript>
|
||||
<p>JavaScript is not enabled. Some convenience migth not work.</p>
|
||||
</noscript>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,51 @@
|
|||
<!-- HTML -->
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Join Game</title>
|
||||
</head>
|
||||
<body>
|
||||
{% if game_id %}
|
||||
<p>Selected game to join is: {{ game_id }}</p>
|
||||
{% endif %}
|
||||
|
||||
{% if players %}
|
||||
<p>Players in game:</p>
|
||||
<ul>
|
||||
{% for player in players %}
|
||||
<li>{{ player }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<!-- form to select one player to play as, or to select spectator too play as -->
|
||||
<form method="POST" action="/join_game/{{ game_id }}">
|
||||
<select name="player">
|
||||
<option value="" selected disabled>Select player</option>
|
||||
<option value="spectator">Spectator</option>
|
||||
{% for player in players %}
|
||||
<option value="{{ player }}">{{ player }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
<input type="submit" value="Join Game">
|
||||
</form>
|
||||
{% else %}
|
||||
<p>No players found in game, please recreate the game.</p>
|
||||
{% endif %}
|
||||
|
||||
{% if player_name %}
|
||||
<p>Selected player to join as: {{ player_name }}</p>
|
||||
<script>
|
||||
// Save player name in browser session for auto fill in game page
|
||||
sessionStorage.setItem('player_name', '{{ player_name }}');
|
||||
//redirect to game
|
||||
window.location.href = '/game/{{ game_id }}/player/{{ player_name }}';
|
||||
</script>
|
||||
<noscript>
|
||||
<p>manual redirect</p>
|
||||
<a href="/game/{{ game_id }}/player/{{ player_name }}">Go to game</a>
|
||||
</noscript>
|
||||
{% endif %}
|
||||
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue