From e8bf27d2efcffdc5391abe9c955593c2fd75770c Mon Sep 17 00:00:00 2001 From: Simon Pirkelmann Date: Mon, 14 Sep 2020 14:56:30 +0200 Subject: [PATCH] processing submitted commands on the server side and dealing new cards when commands have been submitted --- app.py | 131 +++++++++++++++++++++++++++++------- static/style.css | 4 +- templates/drag_example.html | 23 ++++--- 3 files changed, 124 insertions(+), 34 deletions(-) diff --git a/app.py b/app.py index 40c5b14..42f95ce 100644 --- a/app.py +++ b/app.py @@ -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) diff --git a/static/style.css b/static/style.css index f07d8c5..2601a3f 100644 --- a/static/style.css +++ b/static/style.css @@ -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; } \ No newline at end of file +.card8 { left: 350px; top: 120px; background-color: #008080; } + +.card_hidden { background-color: #dddddd; } \ No newline at end of file diff --git a/templates/drag_example.html b/templates/drag_example.html index 363c724..9ea0b93 100644 --- a/templates/drag_example.html +++ b/templates/drag_example.html @@ -28,7 +28,7 @@ - +