diff --git a/overcooked_simulator/hooks.py b/overcooked_simulator/hooks.py index e147cfd3d45fbffc75c4982a7bd10c08d83fabe8..bcdfbd7a0fa55ede58924940bc0edf118ae33233 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 f7880728e98448d12f26539a8ba3612b1406bd86..326dcd5641a1ecf1087e33eb1fec667bc804fa38 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