Init - Randaustausch almost working in 1D

This commit is contained in:
qvalentin 2022-03-27 16:43:15 +02:00
parent 8cf2d4d7f1
commit 1bca8c3c89
Signed by: qvalentin
GPG Key ID: C979FA1EAFCABF1C
10 changed files with 170 additions and 57 deletions

View File

@ -0,0 +1,19 @@
from dataclasses import asdict
import requests as requests
from Code.Communication.Direction import Direction
from Code.Communication.Member import Member
class APIRequests:
def connectToMember(self, own_process: Member, ip, port, direction: Direction) -> Member:
body = asdict(own_process)
response = requests.post(f"http://{ip}:{port}/connect/{direction.name}", json=body)
jsonValue = response.json()
return Member(jsonValue["ip"], jsonValue["port"])
def get_edge(self, target: Member, direction: Direction):
response = requests.get(f"http://{target.ip}:{target.port}/border/{direction.name}")
return response.json()

View File

@ -4,3 +4,5 @@ from enum import Enum
class Direction(Enum): class Direction(Enum):
LEFT = 1 LEFT = 1
RIGHT = 2 RIGHT = 2
TOP = 3
BOTTOM = 4

View File

@ -5,3 +5,4 @@ from dataclasses import dataclass
class Member: class Member:
ip: str ip: str
port: int port: int

View File

@ -1,5 +1,7 @@
from Code.Communication.APIRequests import APIRequests
from Code.Communication.Direction import Direction from Code.Communication.Direction import Direction
from Code.Communication.Member import Member from Code.Communication.Member import Member
from Code.Config import GeneralConfig
class Neighbours: class Neighbours:
@ -7,16 +9,24 @@ class Neighbours:
def __init__(self, own_process: Member): def __init__(self, own_process: Member):
self.neighbours = {} self.neighbours = {}
self.own_process = own_process self.own_process = own_process
self.api = APIRequests()
def connect(self, direction, ip, port): def connect(self, direction, ip, port):
print(f"connecting to {ip}:{port} on {direction} side") print(f"connecting to {ip}:{port} on {direction} side")
pass new_neighbour = self.api.connectToMember(self.own_process, ip, port, direction)
self.neighbours[direction] = new_neighbour
def acceptConnection(self, direction: Direction, ip, port) -> tuple[Member, bool]: def accept_connection(self, direction: Direction, ip, port) -> tuple[Member, bool]:
if direction in self.neighbours: if direction in self.neighbours:
return (self.neighbours[direction],False) return self.neighbours[direction], False
member = Member(ip, port) member = Member(ip, port)
print(f"Adding neighbour {member.__repr__()}") print(f"Adding neighbour {member.__repr__()}")
self.neighbours[direction] = member self.neighbours[direction] = member
return (self.own_process,True) return self.own_process, True
def get_edge(self, direction: Direction):
if direction in self.neighbours:
return self.api.get_edge(self.neighbours[direction], direction)
elif direction == Direction.RIGHT or direction.LEFT:
return [False] * GeneralConfig.fields_amount_y

View File

@ -6,16 +6,26 @@ from http.server import BaseHTTPRequestHandler, HTTPServer
from Code.Communication.Direction import Direction from Code.Communication.Direction import Direction
from Code.Communication.Neighbours import Neighbours from Code.Communication.Neighbours import Neighbours
from Code.UI.Field import Field
from Code.UI.PlayingField import GameState
class RequestHandler(BaseHTTPRequestHandler): class RequestHandler(BaseHTTPRequestHandler):
neighbours: Neighbours = None neighbours: Neighbours = None
game_state: GameState = None
def do_GET(self): def do_GET(self):
print("got Get request") print("got Get request")
if self.path == "/": if self.path == "/":
self.send_response(200, "running") self.send_response(200, "running")
self.end_headers() elif self.path.startswith("/border/"):
direction = re.findall("/border/(left|right)", self.path, flags=re.IGNORECASE)[0]
cells = self.game_state.field.get_edge(Direction[direction.upper()])
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(cells).encode('utf8'))
def handle(self) -> None: def handle(self) -> None:
super().handle() super().handle()
@ -29,12 +39,12 @@ class RequestHandler(BaseHTTPRequestHandler):
""" """
def do_POST(self): def do_POST(self):
print("Got post request")
if self.path.startswith("/connect/"): if self.path.startswith("/connect/"):
direction = re.findall("/connect/(left|right)", self.path, flags=re.IGNORECASE)[0] direction = re.findall("/connect/(left|right)", self.path, flags=re.IGNORECASE)[0]
data_string = self.rfile.read(int(self.headers['Content-Length'])) data_string = self.rfile.read(int(self.headers['Content-Length']))
print(f"Got conenction request with value {data_string}")
data = json.loads(data_string) data = json.loads(data_string)
neighbour, accepted = self.neighbours.acceptConnection(Direction[direction.upper()], data["ip"], data["port"]) neighbour, accepted = self.neighbours.accept_connection(Direction[direction.upper()], data["ip"], data["port"])
print(f"Sending neighbour: {neighbour}") print(f"Sending neighbour: {neighbour}")
if accepted: if accepted:

View File

@ -2,11 +2,14 @@ from http.server import HTTPServer
from Code.Communication.Neighbours import Neighbours from Code.Communication.Neighbours import Neighbours
from Code.Communication.RequestHandler import RequestHandler from Code.Communication.RequestHandler import RequestHandler
from Code.UI.PlayingField import GameState
class Server: class Server:
def __init__(self, neighbours: Neighbours): def __init__(self, neighbours: Neighbours,game_state:GameState):
self.server = None
self.neighbours = neighbours self.neighbours = neighbours
self.game_state = game_state
self.port = neighbours.own_process.port self.port = neighbours.own_process.port
self.ip = neighbours.own_process.ip self.ip = neighbours.own_process.ip
@ -16,6 +19,7 @@ class Server:
def start(self): def start(self):
RequestHandler.neighbours = self.neighbours RequestHandler.neighbours = self.neighbours
RequestHandler.game_state = self.game_state
print(f"HTTP Server Running on {self.ip}: {self.port}") print(f"HTTP Server Running on {self.ip}: {self.port}")
self.server = HTTPServer((self.ip, self.port), RequestHandler) self.server = HTTPServer((self.ip, self.port), RequestHandler)
self.server.serve_forever() self.server.serve_forever()

View File

@ -1,22 +1,28 @@
class GeneralConfig: import math
width = 1000
height = 1000
fps = 150
window_caption = "GOL"
evolve_speed= 360# ziemlich slow das updated abhänig davon wie viele mill sec das game seit dem eltzten mal gelaufen ist im schnit sind das so 60
class Colors: class Colors:
ORANGE = (255, 140, 0) ORANGE = (255, 140, 0)
RED = (255, 0, 0) RED = (255, 0, 0)
GREEN = (5, 112, 0) GREEN = (5, 112, 0)
BLACK = (0, 0, 0) BLACK = (0, 0, 0)
WHITE = (255, 255, 255) WHITE = (255, 255, 255)
GREY = (84, 84, 84) GREY = (84, 84, 84)
class SquareConfig: class SquareConfig:
width = 10 width = 10
height = 10 height = 10
unclicked_color = Colors.BLACK
clicked_color = Colors.WHITE
unclicked_color = Colors.BLACK class GeneralConfig:
clicked_color = Colors.WHITE width = 1000
height = 1000
fields_amount_x = math.trunc(width / SquareConfig.width)
fields_amount_y = math.trunc(height / SquareConfig.height)
fps = 150
window_caption = "GOL"
evolve_speed = 360 # ziemlich slow das updated abhänig davon wie viele mill sec das game seit dem eltzten mal gelaufen ist im schnit sind das so 60

View File

@ -5,7 +5,7 @@ from Code.Communication.Direction import Direction
from Code.Communication.Member import Member from Code.Communication.Member import Member
from Code.Communication.Neighbours import Neighbours from Code.Communication.Neighbours import Neighbours
from Code.Communication.Server import Server from Code.Communication.Server import Server
from Code.UI.PlayingField import run_game from Code.UI.PlayingField import run_game, GameState
if __name__ == "__main__": if __name__ == "__main__":
@ -25,24 +25,25 @@ if __name__ == "__main__":
print(args) print(args)
if len(args) >= 2: if len(args) >= 2:
own_port =int(args[1]) own_port = int(args[1])
else: else:
print("using default port 8080") print("using default port 8080")
own_port = 8080 own_port = 8080
neighbours = Neighbours(own_process=Member("0.0.0.0", own_port))
game_state = GameState(neighbours)
server = Server(neighbours, game_state)
n_direction = Direction.LEFT
if len(args) > 4:
n_direction = args[4]
if len(args) >= 4: if len(args) >= 4:
n_ip = args[2] n_ip = args[2]
n_port = int(args[3]) n_port = int(args[3])
if len(args) > 4: neighbours.connect(Direction[n_direction], n_ip, n_port)
n_direction = args[4]
neighbours = Neighbours(own_process=Member("0.0.0.0", own_port))
server = Server(neighbours)
neighbours.connect(Direction[n_direction], n_ip, n_port)
serverThread = threading.Thread(target=server.start) serverThread = threading.Thread(target=server.start)
serverThread.start() serverThread.start()
run_game() run_game(game_state)
print("finished game") print("finished game")
server.stop_server() server.stop_server()

View File

@ -1,29 +1,84 @@
from Code.Config import GeneralConfig, SquareConfig
import math import math
from Code.Communication.Direction import Direction
from Code.Config import GeneralConfig, SquareConfig
from Code.UI import Square from Code.UI import Square
from Code.UI.Square import Square from Code.UI.Square import Square
class Field: class Field:
def __init__(self): def __init__(self):
self.width = math.trunc(GeneralConfig.width / SquareConfig.width) + 2 self.width = GeneralConfig.fields_amount_x+2
self.height = math.trunc(GeneralConfig.height / SquareConfig.height) + 2 self.height = GeneralConfig.fields_amount_y+2
self.field_shift = -10 self.field_shift = -10
self.squares = self._creat_squares() self.squares = self._creat_squares()
def _creat_squares(self): def _creat_squares(self):
squares = [ squares = [
[Square(x_pos=j * SquareConfig.width + self.field_shift, y_pos=i * SquareConfig.height + self.field_shift) [Square(x_pos=j * SquareConfig.width + self.field_shift, y_pos=i * SquareConfig.height + self.field_shift)
for i in range(self.height)] for for i in range(self.height)] for
j in range(self.width)] j in range(self.width)]
return squares
def update_squares(self,squares): return squares
self.squares=squares
def draw_squares(self, window): def update_squares(self, squares):
for x in self.squares: self.squares = squares
for y in x:
y.draw(window) def draw_squares(self, window):
for x in self.squares:
for y in x:
y.draw(window)
def get_edge(self, edg_pos: Direction) -> list:
edge = {Direction.LEFT: self.get_left_edge(), Direction.RIGHT: self.get_right_edge(),
Direction.TOP: self.get_top(), Direction.BOTTOM: self.get_bottom_edge()}
return edge[edg_pos]
def get_top(self):
top_squares = []
for y in self.squares:
top_squares.append(y[1].active)
return top_squares
def get_bottom_edge(self):
right_squares = []
for y in self.squares:
right_squares.append(y[len(y) - 2].active)
return right_squares
def get_left_edge(self):
left_squares = []
for x in self.squares[1]:
left_squares.append(x.active)
return left_squares
def get_right_edge(self):
right_squares = []
for x in self.squares[len(self.squares[0]) - 2]:
right_squares.append(x.active)
return right_squares
def fill_ghost_edge(self, value: list):
edge_fn = {Direction.LEFT: self.fill_left_ghost_edge, Direction.RIGHT: self.fill_right_ghost_edge,
Direction.TOP: self.fill_top_ghost_edge, Direction.BOTTOM: self.fill_bottom_ghost_edge}
edge_fn(value)
def fill_right_ghost_edge(self, value: list):
#if len(value)== self.square.
for i in range(len(value)):
self.squares[len(self.squares[0]) - 1][i].active = value[i]
def fill_left_ghost_edge(self, value: list):
for i in range(len(value)):
self.squares[0][i].active = value[i]
def fill_top_ghost_edge(self,value):
pass
def fill_bottom_ghost_edge(self,value):
pass

View File

@ -1,12 +1,15 @@
import pygame as pygame import pygame as pygame
from Code.Communication.Direction import Direction
from Code.Communication.Neighbours import Neighbours
from Code.Config import GeneralConfig, Colors from Code.Config import GeneralConfig, Colors
from Code.GameLogic.Rules import Rules from Code.GameLogic.Rules import Rules
from Code.UI.Field import Field from Code.UI.Field import Field
class GameState: class GameState:
def __init__(self): def __init__(self, neighbours: Neighbours):
self.neighbours = neighbours
self.run = True self.run = True
self.pause_for_input = False self.pause_for_input = False
self.field = Field() self.field = Field()
@ -29,7 +32,6 @@ class GameState:
square.update(not square.active) square.update(not square.active)
def evolve(self): def evolve(self):
rules = Rules() rules = Rules()
self.field.update_squares(rules.evolve_field(self.field)) self.field.update_squares(rules.evolve_field(self.field))
@ -37,13 +39,15 @@ class GameState:
window.fill(Colors.BLACK) window.fill(Colors.BLACK)
self.field.draw_squares(window) self.field.draw_squares(window)
def update_borders(self):
self.field.fill_right_ghost_edge(self.neighbours.get_edge(Direction.RIGHT))
self.field.fill_left_ghost_edge(self.neighbours.get_edge(Direction.LEFT))
def run_game(): def run_game(game_state: GameState):
pygame.init() pygame.init()
pygame.display.set_caption(GeneralConfig.window_caption) pygame.display.set_caption(GeneralConfig.window_caption)
window = pygame.display.set_mode((GeneralConfig.width, GeneralConfig.height)) window = pygame.display.set_mode((GeneralConfig.width, GeneralConfig.height))
clock = pygame.time.Clock() clock = pygame.time.Clock()
game_state = GameState()
time_elapsed_since_last_action = 0 time_elapsed_since_last_action = 0
while game_state.run: while game_state.run:
game_state.event_handler() game_state.event_handler()
@ -59,6 +63,7 @@ def run_game():
if time_elapsed_since_last_action > 100: if time_elapsed_since_last_action > 100:
# start = ti.time() # start = ti.time()
game_state.update_borders()
game_state.evolve() game_state.evolve()
# end = ti.time() # end = ti.time()
# print(end - start) # print(end - start)