72 lines
2.1 KiB
Python
72 lines
2.1 KiB
Python
from flask import Flask, render_template, request, session
|
|
import random
|
|
|
|
app = Flask(__name__)
|
|
app.secret_key = b'RoboRallyRolling'
|
|
|
|
random.seed(0)
|
|
|
|
moves = ['forward', 'backward', 'turn left', 'turn right', 'turn around']
|
|
|
|
class Card:
|
|
card_counter = 0
|
|
def __init__(self):
|
|
self.number = Card.card_counter
|
|
Card.card_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_counter = 0
|
|
MAX_PLAYERS = 3
|
|
player_hand = {}
|
|
|
|
@app.route('/', methods=['GET', 'POST'])
|
|
def hello_world():
|
|
global player_counter
|
|
if not 'player_id' in session:
|
|
if player_counter < 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']]])
|
|
|
|
else:
|
|
return "Sorry, maximum number of players reached!"
|
|
|
|
player_id = session['player_id']
|
|
if request.method == 'GET':
|
|
return render_template('drag_example.html', cmds=player_hand[player_id], player_id=player_id)
|
|
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[player_id].index(card1) # get index of card in the hand
|
|
j2 = player_hand[player_id].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
|
|
|
|
print("current hand: ", [str(c) for c in player_hand[player_id]])
|
|
|
|
return 'OK'
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=5000)
|