processing submitted commands on the server side and dealing new cards when commands have been submitted

master
Simon Pirkelmann 2020-09-14 14:56:30 +02:00
parent 108c996a0a
commit e8bf27d2ef
3 changed files with 124 additions and 34 deletions

131
app.py
View File

@ -1,12 +1,44 @@
from flask import Flask, render_template, request, session
import random
import time
app = Flask(__name__)
app.secret_key = b'RoboRallyRolling'
random.seed(0)
moves = ['forward', 'backward', 'turn left', 'turn right', 'turn around']
moves = ['forward', 'forward x2', 'forward x3', 'backward', 'turn left', 'turn right', 'turn around']
class Game:
def __init__(self):
self.action_stack = {}
self.processing_done = False
def ready(self):
# have all players chosen an action?
return len(self.action_stack.keys()) == Player.player_counter
def register_actions(self, player_id, actions):
if not player_id in self.action_stack.keys():
self.action_stack[player_id] = actions
self.processing_done = False
return True
else:
print("actions already chosen!")
return False
def process_actions(self):
# send commands to the robots in the order of priority
for i in range(5):
current_actions = []
for p in self.action_stack.keys():
current_actions += [(p, self.action_stack[p][i])]
print("actions in step {}: {}".format(i, ["robot {} action {}".format(c[0], c[1]) for c in current_actions]))
# clear the action stack for the next round
self.action_stack = {}
self.processing_done = True
class Card:
card_counter = 0
@ -23,47 +55,96 @@ card_deck = {}
for i in range(0,20):
card_deck[i] = Card()
player_counter = 0
MAX_PLAYERS = 3
player_hand = {}
class Player:
MAX_PLAYERS = 3
player_counter = 0
def __init__(self):
if Player.player_counter < Player.MAX_PLAYERS:
self.id = Player.player_counter
Player.player_counter += 1
self.max_cards = 9
self.player_hand = random.sample(list(card_deck.values()), self.max_cards)
print("current hand: ", [str(c) for c in self.player_hand])
self.action_count = 5
self.action_chosen = False
else:
print("max players reached!")
def draw_new_cards(self):
self.player_hand += random.sample(list(card_deck.values()), self.max_cards - len(self.player_hand))
players = {}
game = Game()
@app.route('/send_cmds', methods=['POST', 'GET'])
def send_cmds():
if request.method == 'POST':
# POST is used for submitting commands
player_id = session['player_id']
p = players[player_id]
if game.register_actions(p.id, p.player_hand[0:p.action_count]):
p.player_hand = p.player_hand[p.action_count:] # discard used cards
p.draw_new_cards()
if game.ready():
game.process_actions()
return 'OK'
else:
return 'please wait'
elif request.method == 'GET':
# GET is used when we have to wait for other players to finish
while not game.processing_done: # wait for other players to choose commands and processing to finish
pass
return 'OK'
@app.route('/', methods=['GET', 'POST'])
def hello_world():
global player_counter
if not 'player_id' in session:
if player_counter < MAX_PLAYERS:
if Player.player_counter < Player.MAX_PLAYERS:
# new player
session['player_id'] = player_counter
player_counter += 1
# give him some cards
player_hand[session['player_id']] = random.sample(list(card_deck.values()), 9)
print("current hand: ", [str(c) for c in player_hand[session['player_id']]])
p = Player()
session['player_id'] = p.id
players[p.id] = p
else:
return "Sorry, maximum number of players reached!"
player_id = session['player_id']
player_hand = players[player_id].player_hand
if request.method == 'GET':
return render_template('drag_example.html', cmds=player_hand[player_id], player_id=player_id)
return render_template('drag_example.html', cmds=player_hand, player_id=player_id)
elif request.method == 'POST':
print(request.form)
#print(request.form)
# swap cards in the current hand
i1 = int(request.form.get('drag')) # number of first card
i2 = int(request.form.get('drop')) # number of second card
if request.form.get('drag') and request.form.get('drop'):
card1 = card_deck[i1] # get card by number
card2 = card_deck[i2]
# swap cards in the current hand
i1 = int(request.form.get('drag')) # number of first card
i2 = int(request.form.get('drop')) # number of second card
j1 = player_hand[player_id].index(card1) # get index of card in the hand
j2 = player_hand[player_id].index(card2)
card1 = card_deck[i1] # get card by number
card2 = card_deck[i2]
player_hand[player_id][j1], player_hand[player_id][j2] = player_hand[player_id][j2], player_hand[player_id][j1] # swap the cards in the list
j1 = player_hand.index(card1) # get index of card in the hand
j2 = player_hand.index(card2)
print("current hand: ", [str(c) for c in player_hand[player_id]])
player_hand[j1], player_hand[j2] = player_hand[j2], player_hand[j1] # swap the cards in the list
return 'OK'
#print("current hand: ", [str(c) for c in player_hand[player_id]])
return 'OK'
else:
return render_template('drag_example.html', cmds=player_hand, player_id=player_id)

View File

@ -26,4 +26,6 @@
.card5 { left: 50px; top: 120px; background-color: #F39C12; }
.card6 { left: 150px; top: 120px; background-color: #34495E; }
.card7 { left: 250px; top: 120px; background-color: #FF00FF; }
.card8 { left: 350px; top: 120px; background-color: #008080; }
.card8 { left: 350px; top: 120px; background-color: #008080; }
.card_hidden { background-color: #dddddd; }

View File

@ -28,7 +28,7 @@
</div><!-- container -->
<input id="btnSubmit" type="submit" value="Click Me" />
<input id="btnSubmit" type="submit" value="Befehle abschicken" />
<div class="container notification" hidden>
@ -45,20 +45,27 @@
$("#btnSubmit").click(function () {
//alert("button");
for (i = 0; i < 5; i++) {
for (i = 0; i < 9; i++) {
var c = document.getElementsByClassName("box card" + String(i));
$(c).hide();
if (c[0].offsetTop === 0) {
$(c).hide();
}
}
$(please_wait).show();
$.post("/send_cmds", "", function (data) {
console.log(data);
$(please_wait).hide();
for (i = 0; i < 5; i++) {
var c = document.getElementsByClassName("box card" + String(i));
$(c).show();
if (data === 'OK') {
location.reload(); // reload page to get new cards for next round
}
else {
console.log('waiting...')
$.get("/send_cmds", "", function (data) {
if (data === 'OK') {
location.reload(); // reload page to get new cards for next round
}
})
}
});