processing submitted commands on the server side and dealing new cards when commands have been submitted
This commit is contained in:
parent
108c996a0a
commit
e8bf27d2ef
119
app.py
119
app.py
|
@ -1,12 +1,44 @@
|
||||||
from flask import Flask, render_template, request, session
|
from flask import Flask, render_template, request, session
|
||||||
import random
|
import random
|
||||||
|
import time
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.secret_key = b'RoboRallyRolling'
|
app.secret_key = b'RoboRallyRolling'
|
||||||
|
|
||||||
random.seed(0)
|
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:
|
class Card:
|
||||||
card_counter = 0
|
card_counter = 0
|
||||||
|
@ -23,31 +55,78 @@ card_deck = {}
|
||||||
for i in range(0,20):
|
for i in range(0,20):
|
||||||
card_deck[i] = Card()
|
card_deck[i] = Card()
|
||||||
|
|
||||||
player_counter = 0
|
class Player:
|
||||||
MAX_PLAYERS = 3
|
MAX_PLAYERS = 3
|
||||||
player_hand = {}
|
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'])
|
@app.route('/', methods=['GET', 'POST'])
|
||||||
def hello_world():
|
def hello_world():
|
||||||
global player_counter
|
|
||||||
if not 'player_id' in session:
|
if not 'player_id' in session:
|
||||||
if player_counter < MAX_PLAYERS:
|
if Player.player_counter < Player.MAX_PLAYERS:
|
||||||
# new player
|
# new player
|
||||||
session['player_id'] = player_counter
|
p = Player()
|
||||||
player_counter += 1
|
session['player_id'] = p.id
|
||||||
|
players[p.id] = p
|
||||||
# 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']]])
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return "Sorry, maximum number of players reached!"
|
return "Sorry, maximum number of players reached!"
|
||||||
|
|
||||||
player_id = session['player_id']
|
player_id = session['player_id']
|
||||||
|
player_hand = players[player_id].player_hand
|
||||||
if request.method == 'GET':
|
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':
|
elif request.method == 'POST':
|
||||||
print(request.form)
|
#print(request.form)
|
||||||
|
|
||||||
|
if request.form.get('drag') and request.form.get('drop'):
|
||||||
|
|
||||||
# swap cards in the current hand
|
# swap cards in the current hand
|
||||||
i1 = int(request.form.get('drag')) # number of first card
|
i1 = int(request.form.get('drag')) # number of first card
|
||||||
|
@ -56,14 +135,16 @@ def hello_world():
|
||||||
card1 = card_deck[i1] # get card by number
|
card1 = card_deck[i1] # get card by number
|
||||||
card2 = card_deck[i2]
|
card2 = card_deck[i2]
|
||||||
|
|
||||||
j1 = player_hand[player_id].index(card1) # get index of card in the hand
|
j1 = player_hand.index(card1) # get index of card in the hand
|
||||||
j2 = player_hand[player_id].index(card2)
|
j2 = player_hand.index(card2)
|
||||||
|
|
||||||
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
|
player_hand[j1], player_hand[j2] = player_hand[j2], player_hand[j1] # swap the cards in the list
|
||||||
|
|
||||||
print("current hand: ", [str(c) for c in player_hand[player_id]])
|
#print("current hand: ", [str(c) for c in player_hand[player_id]])
|
||||||
|
|
||||||
return 'OK'
|
return 'OK'
|
||||||
|
else:
|
||||||
|
return render_template('drag_example.html', cmds=player_hand, player_id=player_id)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -27,3 +27,5 @@
|
||||||
.card6 { left: 150px; top: 120px; background-color: #34495E; }
|
.card6 { left: 150px; top: 120px; background-color: #34495E; }
|
||||||
.card7 { left: 250px; top: 120px; background-color: #FF00FF; }
|
.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; }
|
|
@ -28,7 +28,7 @@
|
||||||
|
|
||||||
</div><!-- container -->
|
</div><!-- container -->
|
||||||
|
|
||||||
<input id="btnSubmit" type="submit" value="Click Me" />
|
<input id="btnSubmit" type="submit" value="Befehle abschicken" />
|
||||||
|
|
||||||
|
|
||||||
<div class="container notification" hidden>
|
<div class="container notification" hidden>
|
||||||
|
@ -45,20 +45,27 @@
|
||||||
|
|
||||||
$("#btnSubmit").click(function () {
|
$("#btnSubmit").click(function () {
|
||||||
//alert("button");
|
//alert("button");
|
||||||
for (i = 0; i < 5; i++) {
|
for (i = 0; i < 9; i++) {
|
||||||
var c = document.getElementsByClassName("box card" + String(i));
|
var c = document.getElementsByClassName("box card" + String(i));
|
||||||
|
if (c[0].offsetTop === 0) {
|
||||||
$(c).hide();
|
$(c).hide();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$(please_wait).show();
|
$(please_wait).show();
|
||||||
|
|
||||||
$.post("/send_cmds", "", function (data) {
|
$.post("/send_cmds", "", function (data) {
|
||||||
console.log(data);
|
console.log(data);
|
||||||
$(please_wait).hide();
|
if (data === 'OK') {
|
||||||
|
location.reload(); // reload page to get new cards for next round
|
||||||
for (i = 0; i < 5; i++) {
|
}
|
||||||
var c = document.getElementsByClassName("box card" + String(i));
|
else {
|
||||||
$(c).show();
|
console.log('waiting...')
|
||||||
|
$.get("/send_cmds", "", function (data) {
|
||||||
|
if (data === 'OK') {
|
||||||
|
location.reload(); // reload page to get new cards for next round
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user