Skip to content

Commit

Permalink
Merge pull request #5 from batterylake/chowington-frontend-decoupling
Browse files Browse the repository at this point in the history
Chowington frontend decoupling
  • Loading branch information
chowington authored Dec 21, 2023
2 parents b9b55a8 + 3cfd904 commit e8b6240
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 53 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ environment/frontend_server/storage/*
!environment/frontend_server/storage/base_the_ville_n25/
!environment/frontend_server/storage/July1_the_ville_isabella_maria_klaus-step-3-*/
!environment/frontend_server/storage/test-22/
!environment/frontend_server/storage/test-23/

bin/
include/
Expand Down
29 changes: 29 additions & 0 deletions monitor_simulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os
import time
import json
import glob

frontend_path = "environment/frontend_server"

# Extract curr_sim_code from JSON file
with open(f"{frontend_path}/temp_storage/curr_sim_code.json", "r") as f:
data = json.load(f)
curr_sim_code = data["sim_code"]

movement_path = f"{frontend_path}/storage/{curr_sim_code}/movement"
length = len(movement_path) + 1
last_file = None

while True:
list_of_files = glob.glob(f"{movement_path}/*.json")
latest_file = sorted(list_of_files, key=lambda s: int(s[length:-5]))[-1]

if latest_file != last_file:
os.system("clear")
with open(latest_file, "r") as f:
print(f.read())
print(latest_file)

last_file = latest_file

time.sleep(0.2)
77 changes: 24 additions & 53 deletions reverie/backend_server/reverie.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def _print_tree(tree, depth):
time.sleep(self.server_sleep * 10)


def start_server(self, int_counter):
def start_server(self, int_counter, headless=False):
"""
The main backend server of Reverie.
This function retrieves the environment file from the frontend to
Expand Down Expand Up @@ -376,6 +376,9 @@ def start_server(self, int_counter):
# This is where the core brains of the personas are invoked.
movements = {"persona": dict(),
"meta": dict()}
# This will only be used in headless mode
next_env = {}

for persona_name, persona in self.personas.items():
# <next_tile> is a x,y coordinate. e.g., (58, 9)
# <pronunciatio> is an emoji. e.g., "\ud83d\udca4"
Expand All @@ -391,6 +394,13 @@ def start_server(self, int_counter):
movements["persona"][persona_name]["description"] = description
movements["persona"][persona_name]["chat"] = (persona
.scratch.chat)

if headless:
next_env[persona_name] = {
"x": next_tile[0],
"y": next_tile[1],
"maze": self.maze.maze_name,
}

# Include the meta information about the current stage in the
# movements dictionary.
Expand All @@ -410,6 +420,12 @@ def start_server(self, int_counter):
with open(curr_move_file, "w") as outfile:
outfile.write(json.dumps(movements, indent=2))

# If we're running in headless mode, also create the environment file
# to immediately trigger the next simulation step
if headless:
with open(f"{sim_folder}/environment/{self.step + 1}.json", "w") as outfile:
outfile.write(json.dumps(next_env, indent=2))

# After this cycle, the world takes one step forward, and the
# current time moves by <sec_per_step> amount.
self.step += 1
Expand Down Expand Up @@ -477,6 +493,13 @@ def open_server(self):
int_count = int(sim_command.split()[-1])
rs.start_server(int_count)

elif sim_command[:8].lower() == "headless":
# Runs the simulation in headless mode, which means that it will
# run without the frontend server.
# Example: headless 1000
int_count = int(sim_command.split()[-1])
self.start_server(int_count, headless=True)

elif ("print persona schedule"
in sim_command[:22].lower()):
# Print the decomposed schedule of the persona specified in the
Expand Down Expand Up @@ -639,55 +662,3 @@ def open_server(self):
outfile.write(f"{origin_prompt}{origin}\n{target_prompt}{target}\n")

rs.open_server()




















































0 comments on commit e8b6240

Please # to comment.