2020-09-14 13:36:32 +00:00
|
|
|
from flask import Flask, render_template, request, session, make_response
|
2020-09-11 13:44:38 +00:00
|
|
|
import random
|
2020-09-14 13:36:32 +00:00
|
|
|
import socket
|
2020-09-14 12:56:30 +00:00
|
|
|
import time
|
2020-09-11 12:30:40 +00:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
2020-09-11 14:40:20 +00:00
|
|
|
app.secret_key = b'RoboRallyRolling'
|
|
|
|
|
|
|
|
random.seed(0)
|
2020-09-11 12:30:40 +00:00
|
|
|
|
2020-09-14 12:56:30 +00:00
|
|
|
moves = ['forward', 'forward x2', 'forward x3', 'backward', 'turn left', 'turn right', 'turn around']
|
|
|
|
|
|
|
|
class Game:
|
|
|
|
def __init__(self):
|
|
|
|
self.action_stack = {}
|
2020-09-14 13:36:32 +00:00
|
|
|
self.processing_done = False # indicates whether all commands in the current round have been processed
|
|
|
|
|
|
|
|
self.comm_socket = socket.socket() # socket for communicating with the program controlling the robots
|
|
|
|
|
2020-09-14 12:56:30 +00:00
|
|
|
|
|
|
|
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]))
|
2020-09-14 15:21:37 +00:00
|
|
|
time.sleep(1)
|
2020-09-14 12:56:30 +00:00
|
|
|
|
2020-09-14 13:36:32 +00:00
|
|
|
#self.comm_socket.send()
|
|
|
|
|
|
|
|
|
2020-09-14 12:56:30 +00:00
|
|
|
# clear the action stack for the next round
|
|
|
|
self.action_stack = {}
|
|
|
|
|
|
|
|
self.processing_done = True
|
2020-09-11 12:30:40 +00:00
|
|
|
|
2020-09-11 13:44:38 +00:00
|
|
|
class Card:
|
2020-09-11 14:40:20 +00:00
|
|
|
card_counter = 0
|
2020-09-11 13:44:38 +00:00
|
|
|
def __init__(self):
|
2020-09-11 14:40:20 +00:00
|
|
|
self.number = Card.card_counter
|
|
|
|
Card.card_counter += 1
|
2020-09-11 13:44:38 +00:00
|
|
|
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()
|
|
|
|
|
2020-09-14 12:56:30 +00:00
|
|
|
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'
|
2020-09-11 13:44:38 +00:00
|
|
|
|
|
|
|
@app.route('/', methods=['GET', 'POST'])
|
2020-09-11 12:30:40 +00:00
|
|
|
def hello_world():
|
2020-09-11 14:40:20 +00:00
|
|
|
if not 'player_id' in session:
|
2020-09-14 12:56:30 +00:00
|
|
|
if Player.player_counter < Player.MAX_PLAYERS:
|
2020-09-11 14:40:20 +00:00
|
|
|
# new player
|
2020-09-14 12:56:30 +00:00
|
|
|
p = Player()
|
|
|
|
session['player_id'] = p.id
|
|
|
|
players[p.id] = p
|
2020-09-14 13:36:32 +00:00
|
|
|
player_id = session['player_id']
|
2020-09-11 14:40:20 +00:00
|
|
|
else:
|
|
|
|
return "Sorry, maximum number of players reached!"
|
2020-09-14 13:36:32 +00:00
|
|
|
else:
|
|
|
|
player_id = session['player_id']
|
|
|
|
if Player.player_counter < player_id + 1:
|
|
|
|
session.clear()
|
|
|
|
response = make_response('Please reload the page!')
|
|
|
|
#response.set_cookie('player_id', '', expires=0)
|
|
|
|
return response
|
2020-09-14 12:56:30 +00:00
|
|
|
player_hand = players[player_id].player_hand
|
2020-09-14 13:36:32 +00:00
|
|
|
|
|
|
|
|
2020-09-11 13:44:38 +00:00
|
|
|
if request.method == 'GET':
|
2020-09-14 12:56:30 +00:00
|
|
|
return render_template('drag_example.html', cmds=player_hand, player_id=player_id)
|
2020-09-11 13:44:38 +00:00
|
|
|
elif request.method == 'POST':
|
2020-09-14 12:56:30 +00:00
|
|
|
#print(request.form)
|
2020-09-11 13:44:38 +00:00
|
|
|
|
2020-09-14 12:56:30 +00:00
|
|
|
if request.form.get('drag') and request.form.get('drop'):
|
2020-09-11 13:44:38 +00:00
|
|
|
|
2020-09-14 12:56:30 +00:00
|
|
|
# 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
|
2020-09-11 13:44:38 +00:00
|
|
|
|
2020-09-14 12:56:30 +00:00
|
|
|
card1 = card_deck[i1] # get card by number
|
|
|
|
card2 = card_deck[i2]
|
2020-09-11 13:44:38 +00:00
|
|
|
|
2020-09-14 12:56:30 +00:00
|
|
|
j1 = player_hand.index(card1) # get index of card in the hand
|
|
|
|
j2 = player_hand.index(card2)
|
2020-09-11 13:44:38 +00:00
|
|
|
|
2020-09-14 12:56:30 +00:00
|
|
|
player_hand[j1], player_hand[j2] = player_hand[j2], player_hand[j1] # swap the cards in the list
|
2020-09-11 13:44:38 +00:00
|
|
|
|
2020-09-14 12:56:30 +00:00
|
|
|
#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)
|
2020-09-11 13:44:38 +00:00
|
|
|
|
2020-09-11 12:30:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-09-11 14:40:20 +00:00
|
|
|
app.run(host='0.0.0.0', port=5000)
|