RoboRallyGUI/app.py

53 lines
1.4 KiB
Python

from flask import Flask, render_template, request
import random
app = Flask(__name__)
moves = ['forward', 'backward', 'turn left', 'turn right', 'turn around']
class Card:
global_counter = 0
def __init__(self):
self.number = Card.global_counter
Card.global_counter += 1
self.action = random.choice(moves)
self.priority = random.randint(0, 100)
def __str__(self):
return "Card No. " + str(self.number) + " " + self.action + " " + str(self.priority)
card_deck = {}
for i in range(0,20):
card_deck[i] = Card()
player_hand = [card_deck[i] for i in range(9)]
@app.route('/', methods=['GET', 'POST'])
def hello_world():
if request.method == 'GET':
return render_template('drag_example.html', cmds=player_hand)
elif request.method == 'POST':
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
card1 = card_deck[i1] # get card by number
card2 = card_deck[i2]
j1 = player_hand.index(card1) # get index of card in the hand
j2 = player_hand.index(card2)
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])
return 'OK'
if __name__ == '__main__':
app.run()