Skip to content
Snippets Groups Projects
Commit c8c7a773 authored by Florian Schröder's avatar Florian Schröder
Browse files

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.
parent 6b2b8c76
No related branches found
No related tags found
1 merge request!46Resolve "Hooks"
Pipeline #45359 failed
......@@ -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)
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment