Add blessing priorities.

This commit is contained in:
Lingjie Mei 2024-03-04 01:49:17 -05:00
parent 18981f4e5b
commit 01190fd1b1
3 changed files with 31 additions and 11 deletions

View File

@ -41,34 +41,48 @@ class Filter:
def is_preset(self, filter): def is_preset(self, filter):
return len(filter) and filter.lower() in self.preset return len(filter) and filter.lower() in self.preset
def apply(self, objs, func=None): def apply(self, objs, sort_fn=None, filter_fn=None):
""" """
Args: Args:
objs (list): List of objects and strings objs (list): List of objects
func (callable): A function to filter object. filter_fn (callable): A function to filter object.
Function should receive an object as arguments, and return a bool. Function should receive an object as arguments, and return a bool.
True means add it to output. True means add it to output.
sort_fn (callable): A function to sort object.
Function should receive an object as arguments, and return a float/int, the larger the better.
Returns: Returns:
list: A list of objects and preset strings, such as [object, object, object, 'reset'] list: A list of objects and preset strings, such as [object, object, object, 'reset']
""" """
out = [] out = []
for raw, filter in zip(self.filter_raw, self.filter): for raw, filter in zip(self.filter_raw, self.filter):
if self.is_preset(raw): if raw.lower() == 'random':
candidates = objs
if sort_fn is not None:
candidates = list(sorted(candidates, key=sort_fn, reverse=True))
for c in candidates:
if c not in out:
out.append(c)
elif self.is_preset(raw):
raw = raw.lower() raw = raw.lower()
if raw not in out: if raw not in out:
out.append(raw) out.append(raw)
else: else:
for index, obj in enumerate(objs): candidates = [o for o in objs if self.apply_filter_to_obj(o, filter)]
if self.apply_filter_to_obj(obj=obj, filter=filter) and obj not in out: if sort_fn is not None:
out.append(obj) candidates = list(sorted(candidates, key=sort_fn, reverse=True))
for c in candidates:
if c not in out:
out.append(c)
if func is not None: if filter_fn is not None:
objs, out = out, [] objs, out = out, []
for obj in objs: for obj in objs:
if isinstance(obj, str): if isinstance(obj, str):
out.append(obj) out.append(obj)
elif func(obj): elif filter_fn(obj):
out.append(obj) out.append(obj)
else: else:
# Drop this object # Drop this object

View File

@ -12,6 +12,7 @@ from tasks.rogue.assets.assets_rogue_blessing import *
from tasks.rogue.assets.assets_rogue_ui import BLESSING_CONFIRM from tasks.rogue.assets.assets_rogue_ui import BLESSING_CONFIRM
from tasks.rogue.blessing.preset import BLESSING_PRESET, RESONANCE_PRESET from tasks.rogue.blessing.preset import BLESSING_PRESET, RESONANCE_PRESET
from tasks.rogue.blessing.selector import RogueSelector from tasks.rogue.blessing.selector import RogueSelector
from tasks.rogue.blessing.ui import RogueUI
from tasks.rogue.blessing.utils import get_regex_from_keyword_name, is_card_selected, parse_name from tasks.rogue.blessing.utils import get_regex_from_keyword_name, is_card_selected, parse_name
from tasks.rogue.keywords import * from tasks.rogue.keywords import *
@ -108,6 +109,10 @@ class RogueBlessingSelector(RogueSelector):
self.recognize_and_select() self.recognize_and_select()
""" """
def __init__(self, main: RogueUI):
super().__init__(main)
self.sort_fn = lambda _: _.rarity
def get_blessing_count(self) -> int: def get_blessing_count(self) -> int:
""" """
Returns: The number of blessing Returns: The number of blessing

View File

@ -15,6 +15,7 @@ class RogueSelector:
self.main = main self.main = main
self.filter_ = None self.filter_ = None
self.ocr_results = [] self.ocr_results = []
self.sort_fn = None
def recognition(self): def recognition(self):
... ...
@ -55,8 +56,8 @@ class RogueSelector:
return None return None
if self.filter_: if self.filter_:
keywords = [result.matched_keyword for result in self.ocr_results] keywords = [result.matched_keyword for result in self.ocr_results if result.matched_keyword is not None]
priority = self.filter_.apply(keywords) priority = self.filter_.apply(keywords, self.sort_fn)
priority = [option if isinstance(option, str) else match_ocr_result(option) for option in priority] priority = [option if isinstance(option, str) else match_ocr_result(option) for option in priority]
else: else:
logger.warning("No filter loaded, use random instead") logger.warning("No filter loaded, use random instead")