From c8c7a773285a55b81e615068f4bef9d5b8073d31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Schr=C3=B6der?= <fschroeder@techfak.uni-bielefeld.de> Date: Fri, 2 Feb 2024 16:56:16 +0100 Subject: [PATCH] Add callback hooks for game events in overcooked simulator This commit introduces specific event hooks for key game moments within the overcooked simulator. These include events like serving a non-ordered meal, starting to use the sink, completing an order, and using the trashcan. Callbacks were registered for these hooks to provide real-time feedback such as messages. Also, a typo in POST_PERFORM_ACTION parameter was corrected. --- overcooked_simulator/hooks.py | 6 ++- .../overcooked_environment.py | 41 ++++++++++++++++++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/overcooked_simulator/hooks.py b/overcooked_simulator/hooks.py index e147cfd3..bcdfbd7a 100644 --- a/overcooked_simulator/hooks.py +++ b/overcooked_simulator/hooks.py @@ -16,7 +16,7 @@ ENV_INITIALIZED = "env_initialized" PRE_PERFORM_ACTION = "pre_perform_action" """Before an action is performed / entered into the environment. `action` kwarg with the entered action.""" -POST_PERFORM_ACTION = "post_perfom_action" +POST_PERFORM_ACTION = "post_perform_action" """After an action is performed / entered into the environment. `action` kwarg with the entered action.""" # TODO Pre and Post Perform Movement @@ -105,3 +105,7 @@ class Hooks: self.hooks[ref].append(callback) else: self.hooks[hook_ref].append(callback) + + +def print_hook_callback(text, env, **kwargs): + print(env.env_time, text) diff --git a/overcooked_simulator/overcooked_environment.py b/overcooked_simulator/overcooked_environment.py index f7880728..326dcd56 100644 --- a/overcooked_simulator/overcooked_environment.py +++ b/overcooked_simulator/overcooked_environment.py @@ -8,6 +8,7 @@ import random import sys from datetime import timedelta, datetime from enum import Enum +from functools import partial from pathlib import Path from typing import Literal, TypedDict, Callable @@ -41,6 +42,11 @@ from overcooked_simulator.hooks import ( ACTION_ON_NOT_REACHABLE_COUNTER, ACTION_PUT, ACTION_INTERACT_START, + SERVE_NOT_ORDERED_MEAL, + print_hook_callback, + SINK_START_INTERACT, + COMPLETED_ORDER, + TRASHCAN_USAGE, ) from overcooked_simulator.order import ( OrderAndScoreManager, @@ -127,6 +133,37 @@ class Environment: self.hook: Hooks = Hooks(self) """Hook manager. Register callbacks and create hook points with additional kwargs.""" + # init callbacks here from config + # test: + self.register_callback_for_hook( + SERVE_NOT_ORDERED_MEAL, + partial( + print_hook_callback, + text="You tried to served a meal that was not ordered!", + ), + ) + self.register_callback_for_hook( + SINK_START_INTERACT, + partial( + print_hook_callback, + text="You started to use the Sink!", + ), + ) + self.register_callback_for_hook( + COMPLETED_ORDER, + partial( + print_hook_callback, + text="You completed an order!", + ), + ) + self.register_callback_for_hook( + TRASHCAN_USAGE, + partial( + print_hook_callback, + text="You used the trashcan!", + ), + ) + self.players: dict[str, Player] = {} """the player, keyed by their id/name.""" @@ -678,9 +715,9 @@ class Environment: self.env_time_end - self.env_time, timedelta(0) ).total_seconds(), } - self.hook(STATE_DICT, state=state) + self.hook(STATE_DICT, state=state, player_id=player_id) json_data = json.dumps(state) - self.hook(JSON_STATE, json_data=json_data) + self.hook(JSON_STATE, json_data=json_data, player_id=player_id) assert StateRepresentation.model_validate_json(json_data=json_data) return json_data -- GitLab