From 0ac8844f6f49399da9ad2200263703c226c6fa98 Mon Sep 17 00:00:00 2001 From: NewSoupVi <57900059+NewSoupVi@users.noreply.github.com> Date: Tue, 7 May 2024 08:15:09 +0200 Subject: [PATCH] Core: Add "has_all_counts" and "has_any_count" functions to CollectionState (#2933) * Add has_all_counts and has_any_counts * Messenger gave me a red x and I'm mad about it * Update BaseClasses.py Co-authored-by: Exempt-Medic <60412657+Exempt-Medic@users.noreply.github.com> * Update BaseClasses.py Co-authored-by: Exempt-Medic <60412657+Exempt-Medic@users.noreply.github.com> * Mapping instead of Dict --------- Co-authored-by: Exempt-Medic <60412657+Exempt-Medic@users.noreply.github.com> --- BaseClasses.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/BaseClasses.py b/BaseClasses.py index 53a6b3b1..895e4310 100644 --- a/BaseClasses.py +++ b/BaseClasses.py @@ -11,8 +11,8 @@ from argparse import Namespace from collections import Counter, deque from collections.abc import Collection, MutableSequence from enum import IntEnum, IntFlag -from typing import Any, Callable, Dict, Iterable, Iterator, List, NamedTuple, Optional, Set, Tuple, TypedDict, Union, \ - Type, ClassVar +from typing import Any, Callable, Dict, Iterable, Iterator, List, Mapping, NamedTuple, Optional, Set, Tuple, \ + TypedDict, Union, Type, ClassVar import NetUtils import Options @@ -707,6 +707,14 @@ class CollectionState(): """Returns True if at least one item name of items is in state at least once.""" return any(self.prog_items[player][item] for item in items) + def has_all_counts(self, item_counts: Mapping[str, int], player: int) -> bool: + """Returns True if each item name is in the state at least as many times as specified.""" + return all(self.prog_items[player][item] >= count for item, count in item_counts.items()) + + def has_any_count(self, item_counts: Mapping[str, int], player: int) -> bool: + """Returns True if at least one item name is in the state at least as many times as specified.""" + return any(self.prog_items[player][item] >= count for item, count in item_counts.items()) + def count(self, item: str, player: int) -> int: return self.prog_items[player][item]