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

Merge branch '100-player-id-in-hooks-and-in-all-relevant-methods' into 'main'

Resolve "player id in hooks and in all relevant methods"

Closes #100

See merge request scs/cocosy/overcooked-simulator!69
parents 10291784 e60d58b0
No related branches found
No related tags found
1 merge request!69Resolve "player id in hooks and in all relevant methods"
Pipeline #47464 passed
......@@ -13,9 +13,8 @@ meals:
# if all: false -> only orders for these meals are generated
# TODO: what if this list is empty?
list:
- Burger
- Chips
- OnionSoup
- Salad
- TomatoSoup
layout_chars:
_: Free
......
......@@ -133,16 +133,17 @@ class Counter:
else:
self.orientation = orientation
def pick_up(self, on_hands: bool = True) -> Item | None:
def pick_up(self, on_hands: bool = True, player: str = "0") -> Item | None:
"""Gets called upon a player performing the pickup action. If the counter can give something to
the player, it does so. In the standard counter this is when an item is on the counter.
Args:
on_hands: Will the item be put on empty hands or on a cooking equipment.
player: The player name that tries to pick up from the counter.
Returns: The item which the counter is occupied by. None if nothing is there.
"""
self.hook(PRE_COUNTER_PICK_UP, counter=self, on_hands=on_hands)
self.hook(PRE_COUNTER_PICK_UP, counter=self, on_hands=on_hands, player=player)
if on_hands:
if self.occupied_by:
occupied_by = self.occupied_by
......@@ -180,18 +181,21 @@ class Counter:
Args:
item: The item for which to check, if it can be placed on the counter.
player: The player name that tries to drop something on the counter.
Returns: True if the item can be placed on the counter, False if not.
"""
return self.occupied_by is None or self.occupied_by.can_combine(item)
def drop_off(self, item: Item) -> Item | None:
def drop_off(self, item: Item, player: str = "0") -> Item | None:
"""Takes the thing dropped of by the player.
Args:
item: The item to be placed on the counter.
Returns:
Item or None what should be put back on the players hand, e.g., the cooking equipment.
"""
......@@ -203,6 +207,7 @@ class Counter:
item=item,
equipment=self.occupied_by,
counter=self,
player=player,
)
return self.occupied_by.combine(item)
return None
......@@ -364,15 +369,21 @@ class ServingWindow(Counter):
"""Reference to get the current env time by calling the `env_time_func`."""
super().__init__(**kwargs)
def drop_off(self, item) -> Item | None:
def drop_off(self, item, player: str = "0") -> Item | None:
env_time = self.env_time_func()
self.hook(PRE_SERVING, counter=self, item=item, env_time=env_time)
if self.order_manager.serve_meal(item=item, env_time=env_time):
self.hook(
PRE_SERVING,
counter=self,
item=item,
env_time=env_time,
player=player,
)
if self.order_manager.serve_meal(item=item, env_time=env_time, player=player):
if self.plate_dispenser is not None:
self.plate_dispenser.update_plate_out_of_kitchen(env_time=env_time)
self.hook(POST_SERVING, counter=self, item=item, env_time=env_time)
return None
self.hook(NO_SERVING, counter=self, item=item, env_time=env_time)
self.hook(NO_SERVING, counter=self, item=item, env_time=env_time, player=player)
return item
def can_drop_off(self, item: Item) -> bool:
......@@ -389,7 +400,7 @@ class ServingWindow(Counter):
or (len(item.content_list) == 1 and item.content_list[0].name in self.meals)
)
def pick_up(self, on_hands: bool = True) -> Item | None:
def pick_up(self, on_hands: bool = True, player: str = "0") -> Item | None:
pass
def add_plate_dispenser(self, plate_dispenser):
......@@ -424,8 +435,8 @@ class Dispenser(Counter):
**kwargs,
)
def pick_up(self, on_hands: bool = True) -> Item | None:
self.hook(PRE_DISPENSER_PICK_UP, counter=self, on_hands=on_hands)
def pick_up(self, on_hands: bool = True, player: str = "0") -> Item | None:
self.hook(PRE_DISPENSER_PICK_UP, counter=self, on_hands=on_hands, player=player)
return_this = self.occupied_by
self.occupied_by = self.create_item()
self.hook(
......@@ -433,10 +444,11 @@ class Dispenser(Counter):
counter=self,
on_hands=on_hands,
return_this=return_this,
player=player,
)
return return_this
def drop_off(self, item: Item) -> Item | None:
def drop_off(self, item: Item, player: str = "0") -> Item | None:
if self.occupied_by.can_combine(item):
return self.occupied_by.combine(item)
......@@ -519,14 +531,14 @@ class PlateDispenser(Counter):
"""Random instance."""
self.setup_plates()
def pick_up(self, on_hands: bool = True) -> Item | None:
def pick_up(self, on_hands: bool = True, player: str = "0") -> Item | None:
if self.occupied_by:
return self.occupied_by.pop()
def can_drop_off(self, item: Item) -> bool:
return not self.occupied_by or self.occupied_by[-1].can_combine(item)
def drop_off(self, item: Item) -> Item | None:
def drop_off(self, item: Item, player: str = "0") -> Item | None:
if not self.occupied_by:
self.occupied_by.append(item)
elif self.occupied_by[-1].can_combine(item):
......@@ -614,10 +626,10 @@ class Trashcan(Counter):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def pick_up(self, on_hands: bool = True) -> Item | None:
def pick_up(self, on_hands: bool = True, player: str = "0") -> Item | None:
pass
def drop_off(self, item: Item) -> Item | None:
def drop_off(self, item: Item, player: str = "0") -> Item | None:
if any(
e.item_info.effect_type == EffectType.Unusable for e in item.active_effects
) or any(
......@@ -628,7 +640,7 @@ class Trashcan(Counter):
item.reset_content()
item.reset()
return item
self.hook(TRASHCAN_USAGE, counter=self, item=item)
self.hook(TRASHCAN_USAGE, counter=self, item=item, player=player)
return None
def can_drop_off(self, item: Item) -> bool:
......@@ -771,12 +783,12 @@ class Sink(Counter):
def can_drop_off(self, item: Item) -> bool:
return isinstance(item, Plate) and not item.clean
def drop_off(self, item: Plate) -> Item | None:
def drop_off(self, item: Plate, player: str = "0") -> Item | None:
self.occupied_by.appendleft(item)
self.hook(ADDED_PLATE_TO_SINK, counter=self, item=item)
self.hook(ADDED_PLATE_TO_SINK, counter=self, item=item, player=player)
return None
def pick_up(self, on_hands: bool = True) -> Item | None:
def pick_up(self, on_hands: bool = True, player: str = "0") -> Item | None:
return None
def set_addon(self, sink_addon: SinkAddon):
......@@ -801,15 +813,15 @@ class SinkAddon(Counter):
def can_drop_off(self, item: Item) -> bool:
return self.occupied_by and self.occupied_by[-1].can_combine(item)
def drop_off(self, item: Item) -> Item | None:
self.hook(DROP_ON_SINK_ADDON, counter=self, item=item)
def drop_off(self, item: Item, player: str = "0") -> Item | None:
self.hook(DROP_ON_SINK_ADDON, counter=self, item=item, player=player)
return self.occupied_by[-1].combine(item)
def add_clean_plate(self, plate: Plate):
"""Called from the `Sink` after a plate is cleaned / the progress is complete."""
self.occupied_by.appendleft(plate)
def pick_up(self, on_hands: bool = True) -> Item | None:
def pick_up(self, on_hands: bool = True, player: str = "0") -> Item | None:
if self.occupied_by:
self.hook(PICK_UP_FROM_SINK_ADDON)
self.hook(PICK_UP_FROM_SINK_ADDON, player=player)
return self.occupied_by.pop()
......@@ -904,7 +904,8 @@ class Environment:
if self.player_view_restricted
else None,
"served_meals": [
("?", str(meal)) for (meal, time) in self.order_manager.served_meals
(player, str(meal))
for (meal, time, player) in self.order_manager.served_meals
],
"info_msg": [
(msg["msg"], msg["level"])
......
......@@ -164,7 +164,7 @@ class OrderManager:
"""Current open orders. This attribute is used for the environment state."""
# TODO log who / which player served which meal -> for split scores
self.served_meals: list[Tuple[Item, datetime]] = []
self.served_meals: list[Tuple[Item, datetime, str]] = []
"""List of served meals. Maybe for the end screen."""
self.last_finished: list[Order] = []
"""Cache last finished orders for `OrderGeneration.get_orders` call. From the served meals."""
......@@ -185,7 +185,7 @@ class OrderManager:
)
self.next_relevant_time = next_relevant_time
def serve_meal(self, item: Item, env_time: datetime) -> bool:
def serve_meal(self, item: Item, env_time: datetime, player: str) -> bool:
"""Is called by the ServingWindow to serve a meal. Returns True if the meal can be served and should be
"deleted" from the hands of the player."""
if isinstance(item, Plate):
......@@ -201,7 +201,7 @@ class OrderManager:
meal_name=meal.name,
)
log.info(f"Serving meal without order {meal.name!r}")
self.served_meals.append((meal, env_time))
self.served_meals.append((meal, env_time, player))
return True
log.info(
f"Do not serve meal {meal.name!r} because it is not ordered"
......@@ -211,7 +211,7 @@ class OrderManager:
log.info(f"Serving meal {meal.name!r} with order")
self.last_finished.append(order)
del self.open_orders[index]
self.served_meals.append((meal, env_time))
self.served_meals.append((meal, env_time, player))
self.hook(
COMPLETED_ORDER,
order=order,
......
......@@ -144,15 +144,15 @@ class Player:
"""
if self.holding is None:
self.holding = counter.pick_up()
self.holding = counter.pick_up(player=self.name)
elif counter.can_drop_off(self.holding):
self.holding = counter.drop_off(self.holding)
self.holding = counter.drop_off(self.holding, player=self.name)
elif not isinstance(
counter.occupied_by, (list, deque)
) and self.holding.can_combine(counter.occupied_by):
returned_by_counter = counter.pick_up(on_hands=False)
returned_by_counter = counter.pick_up(on_hands=False, player=self.name)
self.holding.combine(returned_by_counter)
log.debug(
......
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