started implementing interface between core game logic and web app

master
Simon Pirkelmann 2020-09-23 23:33:51 +02:00
parent e73c4f8f63
commit 5359dfc4ac
2 changed files with 40 additions and 28 deletions

39
app.py
View File

@ -47,42 +47,48 @@ class Game:
l.append(zip([p] * len(self.action_stack[p]), self.action_stack[p]))
chosen_cards = list(zip(*l))
# apply the chosen commands to the board which generates a list of movement commands to send to the control program
# process the chosen commands and generate a list of robot commands to send to the controller program
cmd_list = self.board.apply_actions(chosen_cards)
if False:
# send movements to the program
for c in current_actions:
if c[0] == 0:
print("{}, {}\n".format(c[1].action, 11))
self.comm_socket.sendall("{}, {}\n".format(c[1].action, 11).encode())
elif c[0] == 1:
print("{}, {}\n".format(c[1].action, 14))
self.comm_socket.sendall("{}, {}\n".format(c[1].action, 14).encode())
# send movements to the controller program
for c in cmd_list:
self.comm_socket.sendall(c.encode())
data = self.comm_socket.recv(32)
if data == b'OK\n':
print("an error occured while processing the commands")
if data != b'OK\n':
print("an error occurred while processing the commands")
self.processing_done = True
self.action_stack = {}
return
# for c in current_actions:
# if c[0] == 0:
# print("{}, {}\n".format(c[1].action, 11))
# self.comm_socket.sendall("{}, {}\n".format(c[1].action, 11).encode())
# elif c[0] == 1:
# print("{}, {}\n".format(c[1].action, 14))
# self.comm_socket.sendall("{}, {}\n".format(c[1].action, 14).encode())
# data = self.comm_socket.recv(32)
#
time.sleep(0.5)
#self.comm_socket.send()
# clear the action stack for the next round
self.action_stack = {}
self.processing_done = True
players = {}
game = Game()
class Player:
MAX_PLAYERS = 3
player_counter = 0
def __init__(self):
if Player.player_counter < Player.MAX_PLAYERS:
self.id = Player.player_counter
self.marker_id = 11
Player.player_counter += 1
self.max_cards = 9
@ -93,15 +99,14 @@ class Player:
self.action_count = 5
self.action_chosen = False
self.robot = game.board.create_robot(1,1,'>', self.id, self.marker_id)
else:
print("max players reached!")
def draw_new_cards(self):
self.player_hand += deck.draw_cards(self.max_cards - len(self.player_hand))
players = {}
game = Game()
@app.route('/send_cmds', methods=['POST', 'GET'])
def send_cmds():

View File

@ -78,11 +78,11 @@ class Robot:
# dictionary mapping an orientation to its opposite
opposites = {'^': 'v', '>': '<', 'v': '^', '<': '>'}
def __init__(self, x, y, orientation, id, board):
def __init__(self, x, y, orientation, marker_id, board):
self.x = x
self.y = y
self.orientation = orientation
self.id = id
self.marker_id = marker_id
self.damage = 0
self.collected_flags = set()
@ -146,7 +146,7 @@ class Robot:
# change the orientation of the robot
self.orientation = Robot.resulting_orientation[self.orientation][type]
return "{}, {}".format(self.id, type)
return "{}, {}".format(self.marker_id, type)
def move(self, type):
# move the robot forward or backward
@ -165,7 +165,7 @@ class Robot:
self.y = target_tile.y
# return the move for sending to the controller
return "{}, forward".format(self.id)
return "{}, forward".format(self.marker_id)
elif type == 'backward':
opposite_orientation = self.get_opposite_orientation()
target_tile = self.get_adjecent_tile(opposite_orientation)
@ -180,14 +180,14 @@ class Robot:
self.y = target_tile.y
# return the move for sending to the controller
return "{}, backward".format(self.id)
return "{}, backward".format(self.marker_id)
else:
print("error: invalid move")
sys.exit(1)
def nop(self):
# do nothing command
return "{}, nop".format(self.id)
return "{}, nop".format(self.marker_id)
def board_element_processable(self):
# check if we can directly process the board element for the tile the current robot is located on
@ -210,7 +210,7 @@ class Robot:
self.collected_flags.add(flag)
def __str__(self):
return str(self.id)
return str(self.marker_id)
class Tile:
@ -222,6 +222,8 @@ class Tile:
# p : pit (robot takes damage)
# r : repair station (robot heals damage)
# [a,b,c,d] : flag (robot scores)
#
# occupant: Robot that is standing on the tile
def __init__(self, x, y, modifier=None):
self.modifier = modifier
self.occupant = None
@ -302,10 +304,15 @@ class Board:
# self.board[(2, 1)].modifier = '<'
self.robots = {}
self.robots[0] = Robot(1, 1, 'v', 0, self.board)
#self.robots[0] = Robot(1, 1, 'v', 0, self.board)
#self.robots[1] = Robot(1, 2, 'v', 1, self.board)
#self.robots[2] = Robot(2, 1, '>', 2, self.board)
#self.robots[3] = Robot(2, 2, 'v', 3, self.board)
#self.create_robot(1,1,'>', 7, 11)
def create_robot(self, x, y, orientation, player_id, marker_id):
self.robots[player_id] = Robot(x, y, orientation, marker_id, self.board)
def handle_push(self, direction, pushed_robot, forward=True, pushing_robot=None):
cmd_list = []
@ -522,9 +529,9 @@ class Board:
output = ''
for y in range(Board.y_dims+2):
for x in range(Board.x_dims+2):
if any((r.x, r.y) == (x,y) for r in self.robots.values()):
r = list(filter(lambda r: (r.x,r.y) == (x,y), self.robots.values()))[0]
output += str(r.id)
if any((r.x, r.y) == (x,y) for (r_id, r) in self.robots.items()):
r = next(filter(lambda r: (r[1].x,r[1].y) == (x,y), self.robots.items()))
output += str(r[0])
else:
output += str(self.board[(x, y)])
output += '\n'