From f52dabb691789fdb7a4e0665bdaf730e13de3e2a Mon Sep 17 00:00:00 2001 From: dmccoystephenson Date: Tue, 27 May 2025 21:21:28 -0600 Subject: [PATCH 01/16] feat: implement rendering with new renderer classes --- Viron | 2 +- main.py | 87 ++++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 65 insertions(+), 24 deletions(-) diff --git a/Viron b/Viron index f1f3da0..989eda1 160000 --- a/Viron +++ b/Viron @@ -1 +1 @@ -Subproject commit f1f3da02ba4b41675b13529c88ac8fc7d56fba2e +Subproject commit 989eda106ae151281033616bd780dbbf76c91f93 diff --git a/main.py b/main.py index bfcf442..d94fa82 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ import random import pygame from Viron.src.main.python.preponderous.viron.services.environmentService import EnvironmentService +from Viron.src.main.python.preponderous.viron.services.gridService import GridService from Viron.src.main.python.preponderous.viron.services.locationService import LocationService from graphik import Graphik import os @@ -31,19 +32,67 @@ def log(message): url = "http://localhost" port = 9999 -locationService = LocationService(url, port) -environmentService = EnvironmentService(url, port) exit_after_create = False if len(sys.argv) > 2 and sys.argv[2] == "--exit-after-create": exit_after_create = True -def drawEnvironment(locations, graphik, locationWidth, locationHeight): - for location in locations: + +class LocationRenderer: + def __init__(self, graphik): + self.graphik = graphik + + def draw(self, location, width, height): + x = location.get_x() * width + y = location.get_y() * height + color = self.get_random_color() + self.graphik.drawRectangle(x - 1, y - 1, width * 1.5, height * 1.5, color) + + def get_random_color(self): red = random.randrange(50, 200) green = random.randrange(50, 200) blue = random.randrange(50, 200) - x = location.get_x() * locationWidth - y = location.get_y() * locationHeight - graphik.drawRectangle(x - 1, y - 1, locationWidth * 1.5, locationHeight * 1.5, (red,green,blue)) + return (red, green, blue) + +class GridRenderer: + def __init__(self, graphik, url, port): + self.graphik = graphik + self.locationService = LocationService(url, port) + self.location_renderer = LocationRenderer(graphik) + self.locationsCache = {} + + def draw(self, grid): + gridId = grid.get_grid_id() + if gridId not in self.locationsCache: + self.locationsCache[gridId] = self.locationService.get_locations_in_grid(gridId) + + locations = self.locationsCache[gridId] + width = displayWidth / grid.get_columns() + height = displayHeight / grid.get_rows() + for location in locations: + self.location_renderer.draw(location, width, height) + +class EnvironmentRenderer: + def __init__(self, graphik, grid_size, url, port): + self.graphik = graphik + self.grid_size = grid_size + self.grid_service = GridService(url, port) + self.grid_renderer = GridRenderer(graphik, url, port) + + def draw(self, environment): + grids = self.grid_service.get_grids_in_environment(environment.getEnvironmentId()) + # assume one grid for now, can be extended later + if grids: + self.grid_renderer.draw(grids[0]) + else: + self.graphik.drawText("No grids found in environment.", displayWidth/2, displayHeight/2, 20, "red") + +def loadExistingEnvironment(env_file): + if os.path.exists(env_file): + log("Environments file exists, loading...") + with open(env_file, "r") as f: + return json.load(f) + else: + log("No existing environments found.") + return {} def main(): pygame.init() @@ -55,13 +104,12 @@ def main(): environments = {} # Load existing environments if file exists - if os.path.exists(env_file): - log("Environments file exists, loading...") - with open(env_file, "r") as f: - environments = json.load(f) + environments = loadExistingEnvironment(env_file) # Create a unique key for the environment based on grid size and numGrids env_key = f"{numGrids}x{gridSize}" + + environmentService = EnvironmentService(url, port) if env_key in environments: graphik.drawText("Loading existing environment, please wait...", displayWidth/2, displayHeight/2, 20, "white") @@ -95,19 +143,16 @@ def main(): if exit_after_create: log("Exiting after environment creation.") - locations = locationService.get_locations_in_environment(environment.getEnvironmentId()) - drawEnvironment(locations, graphik, displayWidth/gridSize, displayHeight/gridSize) + environmentRenderer = EnvironmentRenderer(graphik, gridSize, url, port) + environmentRenderer.draw(environment) pygame.display.update() time.sleep(2) pygame.quit() return - locationWidth = displayWidth/gridSize - locationHeight = displayHeight/gridSize - - locationsCache = {} - running = True + + environmentRenderer = EnvironmentRenderer(graphik, gridSize, url, port) while running: for event in pygame.event.get(): @@ -115,12 +160,8 @@ def main(): pygame.quit() quit() - if locationsCache == {}: - log("Fetching locations from service...") - locationsCache = locationService.get_locations_in_environment(environment.getEnvironmentId()) - gameDisplay.fill(white) - drawEnvironment(locationsCache, graphik, locationWidth, locationHeight) + environmentRenderer.draw(environment) pygame.display.update() main() \ No newline at end of file From dc6acba5d180f02377d9c2f39298119e5209cccb Mon Sep 17 00:00:00 2001 From: dmccoystephenson Date: Tue, 27 May 2025 21:27:40 -0600 Subject: [PATCH 02/16] refactor: improve environment loading logic --- main.py | 59 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/main.py b/main.py index d94fa82..57beefa 100644 --- a/main.py +++ b/main.py @@ -85,7 +85,7 @@ def draw(self, environment): else: self.graphik.drawText("No grids found in environment.", displayWidth/2, displayHeight/2, 20, "red") -def loadExistingEnvironment(env_file): +def loadEnvironmentsFile(env_file): if os.path.exists(env_file): log("Environments file exists, loading...") with open(env_file, "r") as f: @@ -94,6 +94,30 @@ def loadExistingEnvironment(env_file): log("No existing environments found.") return {} +def loadExistingEnvironment(graphik, env_key, environments, environmentService): + graphik.drawText("Loading existing environment, please wait...", displayWidth/2, displayHeight/2, 20, "white") + env_id = environments[env_key]["environment_id"] + try: + return environmentService.get_environment_by_id(env_id) + except Exception as e: + log(f"Error loading existing environment: {e}") + graphik.drawText("Error loading environment, please check logs.", displayWidth/2, displayHeight/2 + 30, 20, "red") + pygame.display.update() + time.sleep(2) + pygame.quit() + return + +def create_environment(graphik, numGrids, gridSize): + environmentService = EnvironmentService(url, port) + graphik.drawText("Creating environment, please wait...", 400, 400, 20,"white") + pygame.display.update() + log("Creating environment with " + str(numGrids) + " grid(s) of size " + str(gridSize) + "x" + str(gridSize)) + start_time = time.time() + environment = environmentService.create_environment("Test", numGrids, gridSize) + end_time = time.time() + log(f"Created new environment with id {environment.getEnvironmentId()} in {end_time - start_time:.2f} seconds.") + return environment + def main(): pygame.init() gameDisplay = pygame.display.set_mode((displayWidth, displayHeight)) @@ -104,7 +128,7 @@ def main(): environments = {} # Load existing environments if file exists - environments = loadExistingEnvironment(env_file) + environments = loadEnvironmentsFile(env_file) # Create a unique key for the environment based on grid size and numGrids env_key = f"{numGrids}x{gridSize}" @@ -112,34 +136,11 @@ def main(): environmentService = EnvironmentService(url, port) if env_key in environments: - graphik.drawText("Loading existing environment, please wait...", displayWidth/2, displayHeight/2, 20, "white") - env_id = environments[env_key]["environment_id"] - try: - environment = environmentService.get_environment_by_id(env_id) - log(f"Loaded existing environment with id {env_id} and size {gridSize}x{gridSize} with {numGrids} grid(s).") - except Exception as e: - log(f"Error loading existing environment: {e}") - graphik.drawText("Error loading environment, please check logs.", displayWidth/2, displayHeight/2 + 30, 20, "red") - pygame.display.update() - time.sleep(2) - pygame.quit() - return + log(f"Environment with key {env_key} already exists, loading...") + environment = loadExistingEnvironment(graphik, env_key, environments, environmentService) else: - graphik.drawText("Creating environment, please wait...", 400, 400, 20,"white") - pygame.display.update() - log("Creating environment with " + str(numGrids) + " grid(s) of size " + str(gridSize) + "x" + str(gridSize)) - start_time = time.time() - environment = environmentService.create_environment("Test", numGrids, gridSize) - end_time = time.time() - environments[env_key] = { - "environment_id": environment.getEnvironmentId(), - "grid_size": gridSize, - "num_grids": numGrids, - "creation_time_seconds": end_time - start_time - } - with open(env_file, "w") as f: - json.dump(environments, f, indent=2) - log(f"Created new environment with id {environment.getEnvironmentId()} in {end_time - start_time:.2f} seconds.") + log(f"No existing environment found with key {env_key}, creating new one.") + environment = create_environment(graphik, numGrids, gridSize) if exit_after_create: log("Exiting after environment creation.") From 0d39f8a1825cbf943990da2c3e665963fa70e569 Mon Sep 17 00:00:00 2001 From: dmccoystephenson Date: Tue, 27 May 2025 21:29:25 -0600 Subject: [PATCH 03/16] refactor: rename functions for consistency and clarity --- main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 57beefa..5642982 100644 --- a/main.py +++ b/main.py @@ -85,7 +85,7 @@ def draw(self, environment): else: self.graphik.drawText("No grids found in environment.", displayWidth/2, displayHeight/2, 20, "red") -def loadEnvironmentsFile(env_file): +def load_environments_file(env_file): if os.path.exists(env_file): log("Environments file exists, loading...") with open(env_file, "r") as f: @@ -94,7 +94,7 @@ def loadEnvironmentsFile(env_file): log("No existing environments found.") return {} -def loadExistingEnvironment(graphik, env_key, environments, environmentService): +def load_existing_environment(graphik, env_key, environments, environmentService): graphik.drawText("Loading existing environment, please wait...", displayWidth/2, displayHeight/2, 20, "white") env_id = environments[env_key]["environment_id"] try: @@ -128,7 +128,7 @@ def main(): environments = {} # Load existing environments if file exists - environments = loadEnvironmentsFile(env_file) + environments = load_environments_file(env_file) # Create a unique key for the environment based on grid size and numGrids env_key = f"{numGrids}x{gridSize}" @@ -137,7 +137,7 @@ def main(): if env_key in environments: log(f"Environment with key {env_key} already exists, loading...") - environment = loadExistingEnvironment(graphik, env_key, environments, environmentService) + environment = load_existing_environment(graphik, env_key, environments, environmentService) else: log(f"No existing environment found with key {env_key}, creating new one.") environment = create_environment(graphik, numGrids, gridSize) From 4549bc4e487d6cfd8fe577d5a9f8737131e367d7 Mon Sep 17 00:00:00 2001 From: dmccoystephenson Date: Thu, 29 May 2025 21:13:37 -0600 Subject: [PATCH 04/16] fix: save environments to file --- main.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 5642982..a44d6e4 100644 --- a/main.py +++ b/main.py @@ -116,7 +116,8 @@ def create_environment(graphik, numGrids, gridSize): environment = environmentService.create_environment("Test", numGrids, gridSize) end_time = time.time() log(f"Created new environment with id {environment.getEnvironmentId()} in {end_time - start_time:.2f} seconds.") - return environment + + return environment, start_time, end_time def main(): pygame.init() @@ -140,7 +141,16 @@ def main(): environment = load_existing_environment(graphik, env_key, environments, environmentService) else: log(f"No existing environment found with key {env_key}, creating new one.") - environment = create_environment(graphik, numGrids, gridSize) + environment, start_time, end_time = create_environment(graphik, numGrids, gridSize) + + environments[env_key] = { + "environment_id": environment.getEnvironmentId(), + "grid_size": gridSize, + "num_grids": numGrids, + "creation_time_seconds": end_time - start_time + } + with open(env_file, "w") as f: + json.dump(environments, f, indent=2) if exit_after_create: log("Exiting after environment creation.") From 18a75308bbaee0cf07ea1a0f6c3bfea7a26c31ba Mon Sep 17 00:00:00 2001 From: dmccoystephenson Date: Sun, 1 Jun 2025 16:14:30 -0600 Subject: [PATCH 05/16] chore: update subproject commit reference --- Viron | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Viron b/Viron index 989eda1..d77668b 160000 --- a/Viron +++ b/Viron @@ -1 +1 @@ -Subproject commit 989eda106ae151281033616bd780dbbf76c91f93 +Subproject commit d77668be9e45d5271c2dd10c23291561c3eae5f6 From 90c2bfa3dd2142eaff89787214746986198874e0 Mon Sep 17 00:00:00 2001 From: dmccoystephenson Date: Sun, 1 Jun 2025 16:28:48 -0600 Subject: [PATCH 06/16] chore: update subproject commit reference --- Viron | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Viron b/Viron index d77668b..f1f3da0 160000 --- a/Viron +++ b/Viron @@ -1 +1 @@ -Subproject commit d77668be9e45d5271c2dd10c23291561c3eae5f6 +Subproject commit f1f3da02ba4b41675b13529c88ac8fc7d56fba2e From a48ef7c6038354f9b432e44852cbede3ab8c9b13 Mon Sep 17 00:00:00 2001 From: dmccoystephenson Date: Sun, 1 Jun 2025 16:31:42 -0600 Subject: [PATCH 07/16] chore: update subproject commit reference --- Viron | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Viron b/Viron index f1f3da0..9d07465 160000 --- a/Viron +++ b/Viron @@ -1 +1 @@ -Subproject commit f1f3da02ba4b41675b13529c88ac8fc7d56fba2e +Subproject commit 9d07465f96d41f5c5bd1f740649a5d4c2980eccf From d06ecebc66ed63be1479725a0f75075d3d223563 Mon Sep 17 00:00:00 2001 From: dmccoystephenson Date: Sun, 1 Jun 2025 16:32:57 -0600 Subject: [PATCH 08/16] refactor: update `up.bat` to start program after spinning up dependencies --- up.bat | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/up.bat b/up.bat index 3c3ddd2..a646c2b 100644 --- a/up.bat +++ b/up.bat @@ -1,5 +1,3 @@ -# This script is used to start the Docker containers defined in the Docker Compose file. - docker compose -f .\viron\compose.yml up -d --build -REM The -d flag runs the containers in detached mode, allowing them to run in the background. -REM The --build flag forces a rebuild of the images before starting the containers. + +python -m pytest \ No newline at end of file From 386300ec31cf770486e80a9cbb9cb72756f00133 Mon Sep 17 00:00:00 2001 From: dmccoystephenson Date: Sun, 1 Jun 2025 16:33:13 -0600 Subject: [PATCH 09/16] refactor: update `up.bat` to start program after spinning up dependencies --- up.bat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/up.bat b/up.bat index a646c2b..8ffdbb3 100644 --- a/up.bat +++ b/up.bat @@ -1,3 +1,3 @@ docker compose -f .\viron\compose.yml up -d --build -python -m pytest \ No newline at end of file +python main.py \ No newline at end of file From 28bbc471dbf4ef3fade4741dc173f32fd1b606ca Mon Sep 17 00:00:00 2001 From: dmccoystephenson Date: Sun, 1 Jun 2025 16:47:09 -0600 Subject: [PATCH 10/16] chore: update subproject commit reference --- Viron | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Viron b/Viron index 9d07465..4edb465 160000 --- a/Viron +++ b/Viron @@ -1 +1 @@ -Subproject commit 9d07465f96d41f5c5bd1f740649a5d4c2980eccf +Subproject commit 4edb46560c6ae2cf762949049d491e3920dec9ef From 18ac18706083a923a6a58da9cdce5a0132b7156d Mon Sep 17 00:00:00 2001 From: dmccoystephenson Date: Sun, 1 Jun 2025 16:52:30 -0600 Subject: [PATCH 11/16] feat: extract EnvironmentRenderer, GridRenderer, and LocationRenderer classes --- environmentRenderer.py | 19 +++++++++++++++ gridRenderer.py | 22 ++++++++++++++++++ locationRenderer.py | 18 ++++++++++++++ main.py | 53 +----------------------------------------- 4 files changed, 60 insertions(+), 52 deletions(-) create mode 100644 environmentRenderer.py create mode 100644 gridRenderer.py create mode 100644 locationRenderer.py diff --git a/environmentRenderer.py b/environmentRenderer.py new file mode 100644 index 0000000..ce1b996 --- /dev/null +++ b/environmentRenderer.py @@ -0,0 +1,19 @@ +from Viron.src.main.python.preponderous.viron.services.gridService import GridService +from graphik import Graphik +from gridRenderer import GridRenderer + + +class EnvironmentRenderer: + def __init__(self, graphik: Graphik, grid_size, url, port): + self.graphik = graphik + self.grid_size = grid_size + self.grid_service = GridService(url, port) + self.grid_renderer = GridRenderer(graphik, url, port) + + def draw(self, environment): + grids = self.grid_service.get_grids_in_environment(environment.getEnvironmentId()) + # assume one grid for now, can be extended later + if grids: + self.grid_renderer.draw(grids[0]) + else: + self.graphik.drawText("No grids found in environment.", self.graphik.getGameDisplay().get_width()/2, self.graphik.getGameDisplay().get_height()/2, 20, "red") \ No newline at end of file diff --git a/gridRenderer.py b/gridRenderer.py new file mode 100644 index 0000000..8ea5059 --- /dev/null +++ b/gridRenderer.py @@ -0,0 +1,22 @@ +from Viron.src.main.python.preponderous.viron.services.locationService import LocationService +from graphik import Graphik +from locationRenderer import LocationRenderer + + +class GridRenderer: + def __init__(self, graphik: Graphik, url, port): + self.graphik = graphik + self.locationService = LocationService(url, port) + self.location_renderer = LocationRenderer(graphik) + self.locationsCache = {} + + def draw(self, grid): + gridId = grid.get_grid_id() + if gridId not in self.locationsCache: + self.locationsCache[gridId] = self.locationService.get_locations_in_grid(gridId) + + locations = self.locationsCache[gridId] + width = self.graphik.getGameDisplay().get_width() / grid.get_columns() + height = self.graphik.getGameDisplay().get_height() / grid.get_rows() + for location in locations: + self.location_renderer.draw(location, width, height) diff --git a/locationRenderer.py b/locationRenderer.py new file mode 100644 index 0000000..0b60fc6 --- /dev/null +++ b/locationRenderer.py @@ -0,0 +1,18 @@ +import random + + +class LocationRenderer: + def __init__(self, graphik): + self.graphik = graphik + + def draw(self, location, width, height): + x = location.get_x() * width + y = location.get_y() * height + color = self.get_random_color() + self.graphik.drawRectangle(x - 1, y - 1, width * 1.5, height * 1.5, color) + + def get_random_color(self): + red = random.randrange(50, 200) + green = random.randrange(50, 200) + blue = random.randrange(50, 200) + return (red, green, blue) \ No newline at end of file diff --git a/main.py b/main.py index a44d6e4..fa78258 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,6 @@ -import random import pygame from Viron.src.main.python.preponderous.viron.services.environmentService import EnvironmentService -from Viron.src.main.python.preponderous.viron.services.gridService import GridService -from Viron.src.main.python.preponderous.viron.services.locationService import LocationService +from environmentRenderer import EnvironmentRenderer from graphik import Graphik import os import json @@ -36,55 +34,6 @@ def log(message): if len(sys.argv) > 2 and sys.argv[2] == "--exit-after-create": exit_after_create = True -class LocationRenderer: - def __init__(self, graphik): - self.graphik = graphik - - def draw(self, location, width, height): - x = location.get_x() * width - y = location.get_y() * height - color = self.get_random_color() - self.graphik.drawRectangle(x - 1, y - 1, width * 1.5, height * 1.5, color) - - def get_random_color(self): - red = random.randrange(50, 200) - green = random.randrange(50, 200) - blue = random.randrange(50, 200) - return (red, green, blue) - -class GridRenderer: - def __init__(self, graphik, url, port): - self.graphik = graphik - self.locationService = LocationService(url, port) - self.location_renderer = LocationRenderer(graphik) - self.locationsCache = {} - - def draw(self, grid): - gridId = grid.get_grid_id() - if gridId not in self.locationsCache: - self.locationsCache[gridId] = self.locationService.get_locations_in_grid(gridId) - - locations = self.locationsCache[gridId] - width = displayWidth / grid.get_columns() - height = displayHeight / grid.get_rows() - for location in locations: - self.location_renderer.draw(location, width, height) - -class EnvironmentRenderer: - def __init__(self, graphik, grid_size, url, port): - self.graphik = graphik - self.grid_size = grid_size - self.grid_service = GridService(url, port) - self.grid_renderer = GridRenderer(graphik, url, port) - - def draw(self, environment): - grids = self.grid_service.get_grids_in_environment(environment.getEnvironmentId()) - # assume one grid for now, can be extended later - if grids: - self.grid_renderer.draw(grids[0]) - else: - self.graphik.drawText("No grids found in environment.", displayWidth/2, displayHeight/2, 20, "red") - def load_environments_file(env_file): if os.path.exists(env_file): log("Environments file exists, loading...") From d8e2afcd760bb60d247663c3a90a03d37c2d4edd Mon Sep 17 00:00:00 2001 From: dmccoystephenson Date: Sun, 1 Jun 2025 16:54:51 -0600 Subject: [PATCH 12/16] refactor: remove unused grid_size parameter from EnvironmentRenderer --- environmentRenderer.py | 3 +-- main.py | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/environmentRenderer.py b/environmentRenderer.py index ce1b996..da0d78b 100644 --- a/environmentRenderer.py +++ b/environmentRenderer.py @@ -4,9 +4,8 @@ class EnvironmentRenderer: - def __init__(self, graphik: Graphik, grid_size, url, port): + def __init__(self, graphik: Graphik, url, port): self.graphik = graphik - self.grid_size = grid_size self.grid_service = GridService(url, port) self.grid_renderer = GridRenderer(graphik, url, port) diff --git a/main.py b/main.py index fa78258..3e31b69 100644 --- a/main.py +++ b/main.py @@ -103,7 +103,7 @@ def main(): if exit_after_create: log("Exiting after environment creation.") - environmentRenderer = EnvironmentRenderer(graphik, gridSize, url, port) + environmentRenderer = EnvironmentRenderer(graphik, url, port) environmentRenderer.draw(environment) pygame.display.update() time.sleep(2) @@ -112,7 +112,7 @@ def main(): running = True - environmentRenderer = EnvironmentRenderer(graphik, gridSize, url, port) + environmentRenderer = EnvironmentRenderer(graphik, url, port) while running: for event in pygame.event.get(): From 8104e2f477616a57c5aff5dc3e297f85578dba7f Mon Sep 17 00:00:00 2001 From: dmccoystephenson Date: Sun, 1 Jun 2025 16:56:46 -0600 Subject: [PATCH 13/16] refactor: standardize variable naming for display dimensions and grid parameters --- main.py | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/main.py b/main.py index 3e31b69..3b42592 100644 --- a/main.py +++ b/main.py @@ -11,21 +11,21 @@ black = (0,0,0) white = (255,255,255) -displayWidth = 800 -displayHeight = 800 +display_width = 800 +display_height = 800 def log(message): print(message) -numGrids = 1 +num_grids = 1 if len(sys.argv) > 1: try: - gridSize = int(sys.argv[1]) + grid_size = int(sys.argv[1]) except ValueError: log("Invalid grid size argument, using default of 50.") - gridSize = 50 + grid_size = 50 else: - gridSize = 50 + grid_size = 50 url = "http://localhost" port = 9999 @@ -44,25 +44,25 @@ def load_environments_file(env_file): return {} def load_existing_environment(graphik, env_key, environments, environmentService): - graphik.drawText("Loading existing environment, please wait...", displayWidth/2, displayHeight/2, 20, "white") + graphik.drawText("Loading existing environment, please wait...", display_width/2, display_height/2, 20, "white") env_id = environments[env_key]["environment_id"] try: return environmentService.get_environment_by_id(env_id) except Exception as e: log(f"Error loading existing environment: {e}") - graphik.drawText("Error loading environment, please check logs.", displayWidth/2, displayHeight/2 + 30, 20, "red") + graphik.drawText("Error loading environment, please check logs.", display_width/2, display_height/2 + 30, 20, "red") pygame.display.update() time.sleep(2) pygame.quit() return -def create_environment(graphik, numGrids, gridSize): +def create_environment(graphik, num_grids, grid_size): environmentService = EnvironmentService(url, port) graphik.drawText("Creating environment, please wait...", 400, 400, 20,"white") pygame.display.update() - log("Creating environment with " + str(numGrids) + " grid(s) of size " + str(gridSize) + "x" + str(gridSize)) + log("Creating environment with " + str(num_grids) + " grid(s) of size " + str(grid_size) + "x" + str(grid_size)) start_time = time.time() - environment = environmentService.create_environment("Test", numGrids, gridSize) + environment = environmentService.create_environment("Test", num_grids, grid_size) end_time = time.time() log(f"Created new environment with id {environment.getEnvironmentId()} in {end_time - start_time:.2f} seconds.") @@ -70,8 +70,8 @@ def create_environment(graphik, numGrids, gridSize): def main(): pygame.init() - gameDisplay = pygame.display.set_mode((displayWidth, displayHeight)) - graphik = Graphik(gameDisplay) + game_display = pygame.display.set_mode((display_width, display_height)) + graphik = Graphik(game_display) pygame.display.set_caption("Visualizing Environment With Random Colors") env_file = "environments.json" @@ -81,21 +81,21 @@ def main(): environments = load_environments_file(env_file) # Create a unique key for the environment based on grid size and numGrids - env_key = f"{numGrids}x{gridSize}" + env_key = f"{num_grids}x{grid_size}" - environmentService = EnvironmentService(url, port) + environment_service = EnvironmentService(url, port) if env_key in environments: log(f"Environment with key {env_key} already exists, loading...") - environment = load_existing_environment(graphik, env_key, environments, environmentService) + environment = load_existing_environment(graphik, env_key, environments, environment_service) else: log(f"No existing environment found with key {env_key}, creating new one.") - environment, start_time, end_time = create_environment(graphik, numGrids, gridSize) + environment, start_time, end_time = create_environment(graphik, num_grids, grid_size) environments[env_key] = { "environment_id": environment.getEnvironmentId(), - "grid_size": gridSize, - "num_grids": numGrids, + "grid_size": grid_size, + "num_grids": num_grids, "creation_time_seconds": end_time - start_time } with open(env_file, "w") as f: @@ -103,8 +103,8 @@ def main(): if exit_after_create: log("Exiting after environment creation.") - environmentRenderer = EnvironmentRenderer(graphik, url, port) - environmentRenderer.draw(environment) + environment_renderer = EnvironmentRenderer(graphik, url, port) + environment_renderer.draw(environment) pygame.display.update() time.sleep(2) pygame.quit() @@ -112,7 +112,7 @@ def main(): running = True - environmentRenderer = EnvironmentRenderer(graphik, url, port) + environment_renderer = EnvironmentRenderer(graphik, url, port) while running: for event in pygame.event.get(): @@ -120,8 +120,8 @@ def main(): pygame.quit() quit() - gameDisplay.fill(white) - environmentRenderer.draw(environment) + game_display.fill(white) + environment_renderer.draw(environment) pygame.display.update() main() \ No newline at end of file From 7030d56bcbaeb5fd06c70ae689d949e7467b9b34 Mon Sep 17 00:00:00 2001 From: dmccoystephenson Date: Sun, 1 Jun 2025 16:58:31 -0600 Subject: [PATCH 14/16] refactor: add type hints for parameters in EnvironmentRenderer, GridRenderer, and LocationRenderer classes --- environmentRenderer.py | 5 +++-- gridRenderer.py | 17 +++++++++-------- locationRenderer.py | 7 +++++-- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/environmentRenderer.py b/environmentRenderer.py index da0d78b..60dc3d8 100644 --- a/environmentRenderer.py +++ b/environmentRenderer.py @@ -1,15 +1,16 @@ +from Viron.src.main.python.preponderous.viron.models.environment import Environment from Viron.src.main.python.preponderous.viron.services.gridService import GridService from graphik import Graphik from gridRenderer import GridRenderer class EnvironmentRenderer: - def __init__(self, graphik: Graphik, url, port): + def __init__(self, graphik: Graphik, url: str, port: int): self.graphik = graphik self.grid_service = GridService(url, port) self.grid_renderer = GridRenderer(graphik, url, port) - def draw(self, environment): + def draw(self, environment: Environment): grids = self.grid_service.get_grids_in_environment(environment.getEnvironmentId()) # assume one grid for now, can be extended later if grids: diff --git a/gridRenderer.py b/gridRenderer.py index 8ea5059..da539ef 100644 --- a/gridRenderer.py +++ b/gridRenderer.py @@ -1,21 +1,22 @@ +from Viron.src.main.python.preponderous.viron.models.grid import Grid from Viron.src.main.python.preponderous.viron.services.locationService import LocationService from graphik import Graphik from locationRenderer import LocationRenderer class GridRenderer: - def __init__(self, graphik: Graphik, url, port): + def __init__(self, graphik: Graphik, url: str, port: int): self.graphik = graphik - self.locationService = LocationService(url, port) + self.location_service = LocationService(url, port) self.location_renderer = LocationRenderer(graphik) - self.locationsCache = {} + self.locations_cache = {} - def draw(self, grid): - gridId = grid.get_grid_id() - if gridId not in self.locationsCache: - self.locationsCache[gridId] = self.locationService.get_locations_in_grid(gridId) + def draw(self, grid: Grid): + grid_id = grid.get_grid_id() + if grid_id not in self.locations_cache: + self.locations_cache[grid_id] = self.location_service.get_locations_in_grid(grid_id) - locations = self.locationsCache[gridId] + locations = self.locations_cache[grid_id] width = self.graphik.getGameDisplay().get_width() / grid.get_columns() height = self.graphik.getGameDisplay().get_height() / grid.get_rows() for location in locations: diff --git a/locationRenderer.py b/locationRenderer.py index 0b60fc6..5ec4eaa 100644 --- a/locationRenderer.py +++ b/locationRenderer.py @@ -1,11 +1,14 @@ import random +from Viron.src.main.python.preponderous.viron.models.location import Location +from graphik import Graphik + class LocationRenderer: - def __init__(self, graphik): + def __init__(self, graphik: Graphik): self.graphik = graphik - def draw(self, location, width, height): + def draw(self, location: Location, width: int, height: int): x = location.get_x() * width y = location.get_y() * height color = self.get_random_color() From 6679be6a25be76b4adfabf878b7d40b55cdcd015 Mon Sep 17 00:00:00 2001 From: dmccoystephenson Date: Sun, 1 Jun 2025 16:59:13 -0600 Subject: [PATCH 15/16] fix: add delay in main loop to improve rendering performance --- main.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.py b/main.py index 3b42592..d1c8a0d 100644 --- a/main.py +++ b/main.py @@ -123,5 +123,7 @@ def main(): game_display.fill(white) environment_renderer.draw(environment) pygame.display.update() + + time.sleep(0.1) main() \ No newline at end of file From 27fdf5a2f8e58d375b04e644bbbdd2721adc3b37 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Sun, 21 Sep 2025 09:08:42 -0600 Subject: [PATCH 16/16] Update gridRenderer.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- gridRenderer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gridRenderer.py b/gridRenderer.py index da539ef..64be56b 100644 --- a/gridRenderer.py +++ b/gridRenderer.py @@ -17,7 +17,7 @@ def draw(self, grid: Grid): self.locations_cache[grid_id] = self.location_service.get_locations_in_grid(grid_id) locations = self.locations_cache[grid_id] - width = self.graphik.getGameDisplay().get_width() / grid.get_columns() - height = self.graphik.getGameDisplay().get_height() / grid.get_rows() + width = int(self.graphik.getGameDisplay().get_width() / grid.get_columns()) + height = int(self.graphik.getGameDisplay().get_height() / grid.get_rows()) for location in locations: self.location_renderer.draw(location, width, height)