init - fix redirect

This commit is contained in:
qvalentin 2022-03-28 16:29:53 +02:00
parent 50dca30a57
commit 331e463592
Signed by: qvalentin
GPG Key ID: C979FA1EAFCABF1C
4 changed files with 16 additions and 7 deletions

View File

@ -20,4 +20,5 @@ class APIRequests:
def toggle_pause(self, neighbour: Member, new_state: bool):
action = "start" if new_state else "stop"
print(f"sending {action} to {neighbour}")
response = requests.post(f"http://{neighbour.ip}:{neighbour.port}/pause/{action}")

View File

@ -27,6 +27,7 @@ class Neighbours:
def get_edge(self, direction: Direction):
if direction in self.neighbours:
print(f"Getting ghost edge from {self.neighbours[direction]}")
return self.api.get_edge(self.neighbours[direction], mirror(direction))
elif direction == Direction.RIGHT or direction.LEFT:
return [False] * GeneralConfig.fields_amount_y
@ -34,4 +35,5 @@ class Neighbours:
def toggle_pause(self,new_state:bool):
for neighbour in self.neighbours.values():
print(f"Telling member {neighbour} toggle pause with {new_state}")
self.api.toggle_pause(neighbour,new_state)

View File

@ -25,8 +25,10 @@ class RequestHandler(BaseHTTPRequestHandler):
self.end_headers()
self.wfile.write(json.dumps(cells).encode('utf8'))
def handle(self) -> None:
super().handle()
else:
self.send_response(404)
self.end_headers()
"""
/connect/right
@ -52,21 +54,24 @@ class RequestHandler(BaseHTTPRequestHandler):
self.end_headers()
self.wfile.write(json.dumps(asdict(neighbour)).encode('utf8'))
else:
self.send_response(303)
self.send_header('Location', f"http://{neighbour.ip}:{neighbour.port}")
self.send_response(307)
self.send_header('Location', f"http://{neighbour.ip}:{neighbour.port}{self.path}")
self.end_headers()
elif self.path.startswith("/pause/"):
new_state = re.findall("/pause/(stop|start)", self.path, flags=re.IGNORECASE)[0] == "start"
print(f"pause endpoint {new_state} old state")
print(f"pause endpoint {new_state} old state {self.game_state.pause_for_input}")
if new_state == self.game_state.pause_for_input:
print("got pause signal but already in the correct state")
self.send_response(200)
self.end_headers()
else:
self.send_response(200)
self.end_headers()
self.game_state.pause_for_input = new_state
self.neighbours.toggle_pause(new_state)
self.end_headers()
self.end_headers()
else:
self.send_response(404)
self.end_headers()

View File

@ -60,6 +60,7 @@ def run_game(game_state: GameState):
time_elapsed_since_last_action = 0
while game_state.run:
print(f"running {game_state.pause_for_input}")
game_state.event_handler()
for event in game_state.update_field_events: