From 6f16f87cec403b2e04dcb32981aac4442439847f 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:59:31 +0100 Subject: [PATCH] Refactor callback registration in OvercookedEnvironment The refactoring moves the redundant callback registrations from `overcooked_environment.py` to a dedicated function, `add_dummy_callbacks`, in `hooks.py`. This restructure streamlines the code and improves readability by reducing clutter in the Overcooked Environment initialization method. --- overcooked_simulator/hooks.py | 32 +++++++++++++++++ .../overcooked_environment.py | 36 ++----------------- tests/test_start.py | 2 +- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/overcooked_simulator/hooks.py b/overcooked_simulator/hooks.py index bcdfbd7a..93391eda 100644 --- a/overcooked_simulator/hooks.py +++ b/overcooked_simulator/hooks.py @@ -1,4 +1,5 @@ from collections import defaultdict +from functools import partial from typing import Callable # TODO add player_id as kwarg to all hooks -> pass player id to all methods @@ -109,3 +110,34 @@ class Hooks: def print_hook_callback(text, env, **kwargs): print(env.env_time, text) + + +def add_dummy_callbacks(env): + env.register_callback_for_hook( + SERVE_NOT_ORDERED_MEAL, + partial( + print_hook_callback, + text="You tried to served a meal that was not ordered!", + ), + ) + env.register_callback_for_hook( + SINK_START_INTERACT, + partial( + print_hook_callback, + text="You started to use the Sink!", + ), + ) + env.register_callback_for_hook( + COMPLETED_ORDER, + partial( + print_hook_callback, + text="You completed an order!", + ), + ) + env.register_callback_for_hook( + TRASHCAN_USAGE, + partial( + print_hook_callback, + text="You used the trashcan!", + ), + ) diff --git a/overcooked_simulator/overcooked_environment.py b/overcooked_simulator/overcooked_environment.py index 326dcd56..719c79f7 100644 --- a/overcooked_simulator/overcooked_environment.py +++ b/overcooked_simulator/overcooked_environment.py @@ -8,7 +8,6 @@ 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 @@ -42,11 +41,7 @@ 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, + add_dummy_callbacks, ) from overcooked_simulator.order import ( OrderAndScoreManager, @@ -135,34 +130,7 @@ class Environment: # 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!", - ), - ) + add_dummy_callbacks(self) self.players: dict[str, Player] = {} """the player, keyed by their id/name.""" diff --git a/tests/test_start.py b/tests/test_start.py index cd4bb7ab..b3f0b7ea 100644 --- a/tests/test_start.py +++ b/tests/test_start.py @@ -2,11 +2,11 @@ from datetime import timedelta import numpy as np import pytest -from overcooked_simulator.hook import Hooks from overcooked_simulator import ROOT_DIR from overcooked_simulator.counters import Counter, CuttingBoard from overcooked_simulator.game_items import Item, ItemInfo, ItemType +from overcooked_simulator.hooks import Hooks from overcooked_simulator.overcooked_environment import ( Action, Environment, -- GitLab