2024-06-11 19:20:57 +00:00
|
|
|
import math
|
2024-05-15 18:59:04 +00:00
|
|
|
import typing as t
|
|
|
|
from datetime import datetime
|
2024-06-11 19:20:57 +00:00
|
|
|
from functools import cached_property as functools_cached_property, partial
|
2024-05-15 18:59:04 +00:00
|
|
|
|
2024-06-11 19:20:57 +00:00
|
|
|
from pydantic import BaseModel, ValidationError, WrapValidator, computed_field, field_validator, model_validator
|
2024-05-15 18:59:04 +00:00
|
|
|
|
|
|
|
from module.base.decorator import cached_property, del_cached_property
|
|
|
|
from module.config.stored.classes import now
|
|
|
|
from module.config.utils import DEFAULT_TIME
|
|
|
|
from module.exception import ScriptError
|
|
|
|
from module.logger import logger
|
|
|
|
from tasks.base.ui import UI
|
|
|
|
from tasks.dungeon.keywords import DungeonList
|
|
|
|
from tasks.planner.keywords import ITEM_TYPES
|
|
|
|
from tasks.planner.keywords.classes import ItemBase
|
|
|
|
|
|
|
|
|
|
|
|
class PlannerResultRow(BaseModel):
|
|
|
|
"""
|
|
|
|
A row of data from planner result page
|
|
|
|
"""
|
|
|
|
item: ITEM_TYPES
|
|
|
|
total: int
|
|
|
|
synthesize: int
|
|
|
|
demand: int
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.item == other.item
|
|
|
|
|
|
|
|
|
|
|
|
class ObtainedAmmount(BaseModel):
|
|
|
|
"""
|
|
|
|
A row of data from DungeonObtain detection
|
|
|
|
"""
|
|
|
|
item: ITEM_TYPES
|
|
|
|
value: int
|
|
|
|
|
|
|
|
|
|
|
|
def _fallback_to_default_validator(
|
|
|
|
get_default: t.Callable[[], t.Any],
|
|
|
|
v: t.Any,
|
|
|
|
next_: t.Callable[[t.Any], t.Any],
|
|
|
|
) -> t.Any:
|
|
|
|
try:
|
|
|
|
return next_(v)
|
|
|
|
except ValueError as e:
|
|
|
|
logger.error(e)
|
|
|
|
return get_default()
|
|
|
|
|
|
|
|
|
|
|
|
class BaseModelWithFallback(BaseModel):
|
|
|
|
"""
|
|
|
|
Pydantic model that fallbacks to default on error
|
|
|
|
https://github.com/pydantic/pydantic/discussions/8579
|
|
|
|
"""
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __pydantic_init_subclass__(cls, **kwargs: t.Any) -> None:
|
|
|
|
for field in cls.model_fields.values():
|
|
|
|
if not field.is_required():
|
|
|
|
validator = WrapValidator(partial(_fallback_to_default_validator, field.get_default))
|
|
|
|
field.metadata.append(validator)
|
|
|
|
|
|
|
|
cls.model_rebuild(force=True)
|
|
|
|
|
|
|
|
|
|
|
|
class MultiValue(BaseModelWithFallback):
|
|
|
|
purple: int = 0
|
2024-05-30 13:30:33 +00:00
|
|
|
blue: int = 0
|
|
|
|
green: int = 0
|
2024-05-15 18:59:04 +00:00
|
|
|
|
2024-05-19 11:26:20 +00:00
|
|
|
def add(self, other: "MultiValue"):
|
|
|
|
self.green += other.green
|
|
|
|
self.blue += other.blue
|
|
|
|
self.purple += other.purple
|
|
|
|
|
2024-06-11 19:20:57 +00:00
|
|
|
def __sub__(self, other):
|
|
|
|
green = max(self.green - other.green, 0)
|
|
|
|
blue = max(self.blue - other.blue, 0)
|
|
|
|
purple = max(self.purple - other.purple, 0)
|
|
|
|
return MultiValue(green=green, blue=blue, purple=purple)
|
|
|
|
|
2024-05-20 20:44:42 +00:00
|
|
|
def equivalent_green(self):
|
|
|
|
return self.green + self.blue * 3 + self.purple * 9
|
|
|
|
|
2024-06-11 19:20:57 +00:00
|
|
|
def clear(self):
|
|
|
|
self.green = 0
|
|
|
|
self.blue = 0
|
|
|
|
self.purple = 0
|
|
|
|
|
|
|
|
|
|
|
|
SET_ROW_EXCLUDE = {
|
|
|
|
'drop_equivalent_green',
|
|
|
|
'combat_cost',
|
|
|
|
'progress_remain',
|
|
|
|
'progress_total',
|
|
|
|
'progress_current',
|
|
|
|
}
|
|
|
|
|
2024-05-15 18:59:04 +00:00
|
|
|
|
|
|
|
class StoredPlannerProxy(BaseModelWithFallback):
|
|
|
|
item: ITEM_TYPES
|
|
|
|
value: int | MultiValue = 0
|
|
|
|
total: int | MultiValue = 0
|
|
|
|
synthesize: int | MultiValue = 0
|
2024-06-11 19:20:57 +00:00
|
|
|
# progress: float = 0.
|
|
|
|
# eta: float = 0.
|
2024-05-15 18:59:04 +00:00
|
|
|
time: datetime = DEFAULT_TIME
|
|
|
|
|
|
|
|
@field_validator('item', mode='before')
|
|
|
|
def val_item(cls, v, info):
|
|
|
|
if isinstance(v, str):
|
|
|
|
v = ItemBase.find_name(v)
|
|
|
|
return v
|
|
|
|
|
|
|
|
@model_validator(mode='after')
|
|
|
|
def val_value(self):
|
|
|
|
if self.item.has_group_base:
|
|
|
|
if not isinstance(self.value, MultiValue):
|
2024-06-14 13:50:59 +00:00
|
|
|
logger.warning(f'Planner item {self.item} has_group_base '
|
|
|
|
f'but given value={self.value} is not a MultiValue')
|
2024-05-15 18:59:04 +00:00
|
|
|
self.value = MultiValue()
|
|
|
|
if not isinstance(self.total, MultiValue):
|
2024-06-14 13:50:59 +00:00
|
|
|
logger.warning(f'Planner item {self.item} has_group_base '
|
|
|
|
f'but given total={self.total} is not a MultiValue')
|
2024-05-15 18:59:04 +00:00
|
|
|
self.total = MultiValue()
|
|
|
|
if not isinstance(self.synthesize, MultiValue):
|
2024-06-14 13:50:59 +00:00
|
|
|
logger.warning(f'Planner item {self.item} has_group_base '
|
|
|
|
f'but given synthesize={self.synthesize} is not a MultiValue')
|
2024-05-15 18:59:04 +00:00
|
|
|
self.synthesize = MultiValue()
|
|
|
|
else:
|
|
|
|
if not isinstance(self.value, int):
|
2024-06-14 13:50:59 +00:00
|
|
|
logger.warning(f'Planner item {self.item} has no group base '
|
|
|
|
f'but given value={self.value} is not an int')
|
2024-05-15 18:59:04 +00:00
|
|
|
self.value = 0
|
|
|
|
if not isinstance(self.total, int):
|
2024-06-14 13:50:59 +00:00
|
|
|
logger.warning(f'Planner item {self.item} has no group base '
|
|
|
|
f'but given total={self.total} is not an int')
|
2024-05-15 18:59:04 +00:00
|
|
|
self.total = 0
|
|
|
|
if not isinstance(self.synthesize, int):
|
2024-06-14 13:50:59 +00:00
|
|
|
logger.warning(f'Planner item {self.item} has no group base '
|
|
|
|
f'but given synthesize={self.synthesize} is not an int')
|
2024-05-15 18:59:04 +00:00
|
|
|
self.synthesize = 0
|
2024-06-14 17:04:08 +00:00
|
|
|
if self.synthesize != 0:
|
|
|
|
logger.warning(f'Planner item {self.item} has no group base '
|
|
|
|
f'its synthesize={self.synthesize} should be 0')
|
|
|
|
self.synthesize = 0
|
2024-05-15 18:59:04 +00:00
|
|
|
return self
|
|
|
|
|
2024-06-11 19:20:57 +00:00
|
|
|
def clear(self):
|
|
|
|
if self.item.has_group_base:
|
|
|
|
self.value.clear()
|
|
|
|
else:
|
|
|
|
self.value = 0
|
|
|
|
|
2024-05-15 18:59:04 +00:00
|
|
|
def update_synthesize(self):
|
|
|
|
if self.item.has_group_base:
|
|
|
|
green = self.value.green - self.total.green
|
|
|
|
blue = self.value.blue - self.total.blue
|
|
|
|
purple = self.value.purple - self.total.purple
|
|
|
|
syn_blue = 0
|
|
|
|
syn_purple = 0
|
|
|
|
if green >= 3 and blue < 0:
|
|
|
|
syn = min(green // 3, -blue)
|
|
|
|
syn_blue += syn
|
|
|
|
green -= syn * 3
|
|
|
|
# blue += syn
|
|
|
|
if blue >= 3 and purple < 0:
|
|
|
|
syn = min(blue // 3, -purple)
|
|
|
|
syn_purple += syn
|
|
|
|
blue -= syn * 3
|
|
|
|
purple += syn
|
|
|
|
if green >= 9 and purple < 0:
|
|
|
|
syn = min(green // 9, -purple)
|
|
|
|
syn_purple += syn
|
|
|
|
syn_blue += syn * 3
|
|
|
|
green -= syn * 9
|
|
|
|
# purple += syn
|
|
|
|
self.synthesize.green = 0
|
|
|
|
self.synthesize.blue = syn_blue
|
|
|
|
self.synthesize.purple = syn_purple
|
|
|
|
else:
|
|
|
|
self.synthesize = 0
|
|
|
|
|
|
|
|
def revert_synthesize(self):
|
|
|
|
if self.item.has_group_base:
|
|
|
|
self.value.green += self.synthesize.blue * 3
|
|
|
|
if self.synthesize.blue > 0:
|
|
|
|
self.value.green += self.synthesize.purple * 9
|
|
|
|
else:
|
2024-05-17 18:58:17 +00:00
|
|
|
self.value.blue += self.synthesize.purple * 3
|
2024-05-15 18:59:04 +00:00
|
|
|
|
2024-06-11 19:20:57 +00:00
|
|
|
@computed_field(repr=False)
|
|
|
|
@functools_cached_property
|
|
|
|
def drop_equivalent_green(self) -> float:
|
|
|
|
# Tracks_of_Destiny
|
|
|
|
if self.item.dungeon is None:
|
|
|
|
return 1
|
2024-05-20 20:44:42 +00:00
|
|
|
if self.item.dungeon.is_Calyx_Golden_Treasures:
|
2024-06-11 19:20:57 +00:00
|
|
|
return 24000
|
2024-05-20 20:44:42 +00:00
|
|
|
if self.item.dungeon.is_Calyx_Golden_Memories:
|
|
|
|
# purple, blue, green = 5, 1, 0
|
2024-06-11 19:20:57 +00:00
|
|
|
return 48
|
2024-05-27 19:56:30 +00:00
|
|
|
if self.item.dungeon.is_Calyx_Golden_Aether:
|
2024-05-20 20:44:42 +00:00
|
|
|
# purple, blue, green = 1, 2, 2.5
|
2024-06-11 19:20:57 +00:00
|
|
|
return 17.5
|
2024-05-20 20:44:42 +00:00
|
|
|
if self.item.is_ItemAscension:
|
2024-06-23 18:28:34 +00:00
|
|
|
return 5
|
2024-05-20 20:44:42 +00:00
|
|
|
if self.item.is_ItemTrace:
|
|
|
|
# purple, blue, green = 0.155, 1, 1.25
|
2024-06-11 19:20:57 +00:00
|
|
|
return 5.645
|
2024-05-20 20:44:42 +00:00
|
|
|
if self.item.is_ItemWeekly:
|
2024-06-11 19:20:57 +00:00
|
|
|
return 3
|
|
|
|
raise ScriptError(f'{self} has no drop_equivalent_green defined')
|
|
|
|
|
|
|
|
@computed_field(repr=False)
|
|
|
|
@functools_cached_property
|
|
|
|
def combat_cost(self) -> int:
|
|
|
|
# Tracks_of_Destiny
|
|
|
|
if self.item.dungeon is None:
|
|
|
|
return 30
|
|
|
|
if self.item.dungeon.is_Calyx_Golden:
|
|
|
|
return 10
|
|
|
|
if self.item.is_ItemAscension:
|
|
|
|
return 30
|
|
|
|
if self.item.is_ItemTrace:
|
|
|
|
return 10
|
|
|
|
if self.item.is_ItemWeekly:
|
|
|
|
return 30
|
|
|
|
raise ScriptError(f'{self} has no stamina_pre_combat defined')
|
|
|
|
|
|
|
|
@computed_field(repr=False)
|
|
|
|
@functools_cached_property
|
|
|
|
def progress_remain(self) -> float:
|
|
|
|
if self.item.has_group_base:
|
|
|
|
remain = self.total - self.value - self.synthesize
|
|
|
|
return remain.equivalent_green()
|
|
|
|
else:
|
|
|
|
remain = max(self.total - self.value, 0)
|
|
|
|
return remain
|
|
|
|
|
|
|
|
@computed_field(repr=False)
|
|
|
|
@functools_cached_property
|
|
|
|
def progress_total(self) -> float:
|
|
|
|
if self.item.has_group_base:
|
|
|
|
return self.total.equivalent_green()
|
|
|
|
else:
|
|
|
|
return self.total
|
2024-05-20 20:44:42 +00:00
|
|
|
|
2024-06-11 19:20:57 +00:00
|
|
|
@computed_field(repr=False)
|
|
|
|
@functools_cached_property
|
|
|
|
def progress_current(self) -> float:
|
2024-05-15 18:59:04 +00:00
|
|
|
if self.item.has_group_base:
|
2024-06-11 19:20:57 +00:00
|
|
|
current = self.progress_total - self.progress_remain
|
|
|
|
current = min(max(current, 0), self.progress_total)
|
|
|
|
return current
|
|
|
|
else:
|
|
|
|
current = self.value
|
|
|
|
current = min(max(current, 0), self.total)
|
|
|
|
return current
|
|
|
|
|
|
|
|
@computed_field
|
|
|
|
@functools_cached_property
|
|
|
|
def progress(self) -> float:
|
2024-06-13 18:01:23 +00:00
|
|
|
try:
|
|
|
|
progress = self.progress_current / self.progress_total * 100
|
|
|
|
return round(min(max(progress, 0), 100), 2)
|
|
|
|
except ZeroDivisionError:
|
|
|
|
return 100.
|
2024-06-11 19:20:57 +00:00
|
|
|
|
|
|
|
def is_approaching_total(self, wave_done: int = 0):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
wave_done:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: True if the future value may >= total after next combat
|
|
|
|
"""
|
|
|
|
wave_done = max(wave_done, 0)
|
|
|
|
# Items with a static drop rate will have `AVG * (wave_done + 1)
|
|
|
|
remain = self.progress_remain
|
|
|
|
cost = self.combat_cost
|
|
|
|
drop = self.drop_equivalent_green
|
|
|
|
if cost == 10:
|
|
|
|
return remain <= drop * (wave_done + 12)
|
2024-05-15 18:59:04 +00:00
|
|
|
else:
|
2024-06-11 19:20:57 +00:00
|
|
|
return remain <= drop * (wave_done + 1)
|
|
|
|
|
|
|
|
@computed_field
|
|
|
|
@functools_cached_property
|
|
|
|
def eta(self) -> float:
|
|
|
|
"""
|
|
|
|
Estimate remaining days to farm
|
|
|
|
"""
|
2024-06-22 15:13:25 +00:00
|
|
|
if not self.can_daily_farm():
|
2024-06-11 19:20:57 +00:00
|
|
|
return 0.
|
|
|
|
if self.item.dungeon is None:
|
|
|
|
return 0.
|
2024-06-18 04:41:52 +00:00
|
|
|
if self.item.is_ItemWeekly:
|
|
|
|
return 0.
|
2024-06-11 19:20:57 +00:00
|
|
|
|
|
|
|
remain = self.progress_remain
|
|
|
|
cost = self.combat_cost
|
|
|
|
drop = self.drop_equivalent_green
|
|
|
|
|
2024-06-22 15:13:25 +00:00
|
|
|
# if self.item.is_ItemWeekly:
|
|
|
|
# weeks = math.ceil(remain / drop / 3)
|
|
|
|
# return weeks * 7
|
|
|
|
stamina = math.ceil(remain / drop) * cost
|
|
|
|
return round(stamina / 240, 1)
|
2024-06-11 19:20:57 +00:00
|
|
|
|
|
|
|
def update(self, time=False):
|
|
|
|
for attr in SET_ROW_EXCLUDE:
|
|
|
|
del_cached_property(self, attr)
|
|
|
|
del_cached_property(self, 'progress')
|
|
|
|
del_cached_property(self, 'eta')
|
2024-05-15 18:59:04 +00:00
|
|
|
|
|
|
|
self.update_synthesize()
|
2024-06-11 19:20:57 +00:00
|
|
|
_ = self.progress
|
|
|
|
_ = self.eta
|
|
|
|
if time:
|
|
|
|
self.time = now()
|
2024-05-15 18:59:04 +00:00
|
|
|
|
|
|
|
def load_value_total(self, item: ItemBase, value=None, total=None, synthesize=None):
|
|
|
|
"""
|
|
|
|
Update data from PlannerResultRow to self
|
|
|
|
"""
|
|
|
|
if self.item.has_group_base:
|
|
|
|
if item.group_base != self.item:
|
|
|
|
raise ScriptError(
|
|
|
|
f'load_value_total: Trying to load {item} into {self} but they are different items')
|
|
|
|
else:
|
|
|
|
if item != self.item:
|
|
|
|
raise ScriptError(
|
|
|
|
f'load_value_total: Trying to load {item} into {self} but they are different items')
|
|
|
|
if self.item.has_group_base:
|
|
|
|
if not self.item.is_rarity_purple:
|
|
|
|
raise ScriptError(
|
|
|
|
f'load_value_total: Trying to load {item} into {self} but self is not in rarity purple')
|
|
|
|
if item.is_rarity_green:
|
|
|
|
if value is not None:
|
|
|
|
self.value.green = value
|
|
|
|
if total is not None:
|
|
|
|
self.total.green = total
|
2024-05-19 11:26:20 +00:00
|
|
|
# Cannot synthesize green
|
2024-05-15 18:59:04 +00:00
|
|
|
# if synthesize is not None:
|
|
|
|
# self.synthesize.green = synthesize
|
2024-05-19 11:26:20 +00:00
|
|
|
self.synthesize.green = 0
|
2024-05-15 18:59:04 +00:00
|
|
|
elif item.is_rarity_blue:
|
|
|
|
if value is not None:
|
|
|
|
self.value.blue = value
|
|
|
|
if total is not None:
|
|
|
|
self.total.blue = total
|
|
|
|
if synthesize is not None:
|
|
|
|
self.synthesize.blue = synthesize
|
|
|
|
elif item.is_rarity_purple:
|
|
|
|
if value is not None:
|
|
|
|
self.value.purple = value
|
|
|
|
if total is not None:
|
|
|
|
self.total.purple = total
|
|
|
|
if synthesize is not None:
|
|
|
|
self.synthesize.purple = synthesize
|
|
|
|
else:
|
|
|
|
raise ScriptError(
|
|
|
|
f'load_value_total: Trying to load {item} in to {self} but item is in invalid rarity')
|
|
|
|
else:
|
2024-05-19 11:26:20 +00:00
|
|
|
# Cannot synthesize if item doesn't have multiple rarity
|
|
|
|
self.synthesize = 0
|
2024-05-15 18:59:04 +00:00
|
|
|
if value is not None:
|
|
|
|
self.value = value
|
|
|
|
if total is not None:
|
|
|
|
self.total = total
|
|
|
|
|
2024-05-19 11:26:20 +00:00
|
|
|
def add_planner_result(self, row: "StoredPlannerProxy"):
|
|
|
|
"""
|
|
|
|
Add data from another StoredPlannerProxy to self
|
|
|
|
"""
|
|
|
|
item = row.item
|
|
|
|
if self.item.has_group_base:
|
|
|
|
if item.group_base != self.item:
|
|
|
|
raise ScriptError(
|
|
|
|
f'load_value_total: Trying to load {item} into {self} but they are different items')
|
|
|
|
else:
|
|
|
|
if item != self.item:
|
|
|
|
raise ScriptError(
|
|
|
|
f'load_value_total: Trying to load {item} into {self} but they are different items')
|
|
|
|
if self.item.has_group_base:
|
|
|
|
if not self.item.is_rarity_purple:
|
|
|
|
raise ScriptError(
|
|
|
|
f'load_value_total: Trying to load {item} into {self} but self is not in rarity purple')
|
|
|
|
# Add `total` only
|
|
|
|
# `synthesize` will be updated later
|
|
|
|
# `value` remains unchanged since you still having that many items
|
|
|
|
self.total.add(row.total)
|
|
|
|
else:
|
|
|
|
self.value += row.value
|
|
|
|
self.total += row.total
|
|
|
|
self.synthesize += row.synthesize
|
|
|
|
|
2024-06-14 17:52:50 +00:00
|
|
|
def can_daily_farm(self):
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
bool: True if item can be farmed in daily
|
|
|
|
False if item.is_ItemWeekly or Tracks_of_Destiny
|
|
|
|
"""
|
|
|
|
if self.item.is_ItemAscension:
|
|
|
|
return True
|
|
|
|
if self.item.is_ItemTrace:
|
|
|
|
return True
|
|
|
|
if self.item.is_ItemExp:
|
|
|
|
return True
|
|
|
|
if self.item.is_ItemCurrency:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2024-05-15 18:59:04 +00:00
|
|
|
def need_farm(self):
|
|
|
|
return self.progress < 100
|
|
|
|
|
|
|
|
def need_synthesize(self):
|
2024-06-15 04:43:44 +00:00
|
|
|
if self.item.is_ItemCalyx or self.item.is_ItemTrace:
|
2024-05-15 18:59:04 +00:00
|
|
|
return self.synthesize.green > 0 or self.synthesize.blue > 0 or self.synthesize.purple > 0
|
|
|
|
else:
|
2024-06-14 17:04:08 +00:00
|
|
|
return False
|
2024-05-15 18:59:04 +00:00
|
|
|
|
|
|
|
def load_planner_result(self, row: PlannerResultRow):
|
|
|
|
"""
|
|
|
|
Update data from PlannerResultRow to self
|
|
|
|
"""
|
|
|
|
# Approximate value, accurate value can be update in DungeonObtain
|
|
|
|
value = row.total - row.synthesize - row.demand
|
|
|
|
self.load_value_total(item=row.item, value=value, total=row.total, synthesize=row.synthesize)
|
|
|
|
|
|
|
|
def load_item_amount(self, row: ObtainedAmmount):
|
|
|
|
"""
|
|
|
|
Update data from ObtainedAmmount to self
|
|
|
|
"""
|
|
|
|
value = row.value
|
|
|
|
self.load_value_total(item=row.item, value=value)
|
|
|
|
|
|
|
|
|
|
|
|
class PlannerProgressParser:
|
|
|
|
def __init__(self):
|
|
|
|
self.rows: dict[str, StoredPlannerProxy] = {}
|
|
|
|
|
|
|
|
def from_planner_results(self, results: list[PlannerResultRow]):
|
|
|
|
self.rows = {}
|
|
|
|
# Create objects of base items first
|
|
|
|
# them load value and total
|
|
|
|
for row in results:
|
|
|
|
base = row.item.group_base
|
|
|
|
if base.name not in self.rows:
|
|
|
|
try:
|
2024-06-14 13:50:59 +00:00
|
|
|
if row.item.has_group_base:
|
|
|
|
obj = StoredPlannerProxy(
|
|
|
|
item=base, value=MultiValue(), total=MultiValue(), synthesize=MultiValue())
|
|
|
|
else:
|
|
|
|
obj = StoredPlannerProxy(item=base, value=0, total=0, synthesize=0)
|
2024-05-15 18:59:04 +00:00
|
|
|
except ScriptError as e:
|
|
|
|
logger.error(e)
|
|
|
|
continue
|
|
|
|
self.rows[base.name] = obj
|
|
|
|
else:
|
|
|
|
obj = self.rows[base.name]
|
|
|
|
obj.load_planner_result(row)
|
|
|
|
|
|
|
|
rows = {}
|
|
|
|
for name, row in self.rows.items():
|
|
|
|
row.revert_synthesize()
|
|
|
|
row.update()
|
|
|
|
if row.need_farm() or row.need_synthesize():
|
|
|
|
rows[name] = row
|
|
|
|
self.rows = rows
|
|
|
|
return self
|
|
|
|
|
|
|
|
def load_obtained_amount(self, results: list[ObtainedAmmount]):
|
|
|
|
for row in results:
|
|
|
|
base = row.item.group_base
|
|
|
|
try:
|
|
|
|
obj = self.rows[base.name]
|
|
|
|
except KeyError:
|
|
|
|
logger.warning(
|
|
|
|
f'load_obtained_amount() drops {row} because no need to farm')
|
|
|
|
continue
|
|
|
|
obj.load_item_amount(row)
|
|
|
|
obj.update()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def from_config(self, data):
|
|
|
|
self.rows = {}
|
2024-06-11 19:20:57 +00:00
|
|
|
for name, row in data.items():
|
2024-05-15 18:59:04 +00:00
|
|
|
if not row:
|
|
|
|
continue
|
2024-06-11 19:20:57 +00:00
|
|
|
if name == 'PlannerOverall':
|
|
|
|
continue
|
2024-05-15 18:59:04 +00:00
|
|
|
try:
|
|
|
|
row = StoredPlannerProxy(**row)
|
2024-06-15 04:43:44 +00:00
|
|
|
except (ScriptError, ValidationError) as e:
|
2024-05-15 18:59:04 +00:00
|
|
|
logger.error(e)
|
|
|
|
continue
|
|
|
|
if not row.item.is_group_base:
|
|
|
|
logger.error(f'from_config: item is not group base {row}')
|
|
|
|
continue
|
2024-06-15 04:43:44 +00:00
|
|
|
if row.item.has_group_base:
|
|
|
|
if row.total.equivalent_green() <= 0:
|
|
|
|
logger.error(f'Planner item {row.item} has invalid total={row.total}, drop')
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
if row.total <= 0:
|
|
|
|
logger.error(f'Planner item {row.item} has invalid total={row.total}, drop')
|
|
|
|
continue
|
2024-06-11 19:20:57 +00:00
|
|
|
row.update(time=False)
|
2024-05-15 18:59:04 +00:00
|
|
|
self.rows[row.item.name] = row
|
|
|
|
return self
|
|
|
|
|
2024-05-19 11:26:20 +00:00
|
|
|
def add_planner_result(self, planner: "PlannerProgressParser"):
|
|
|
|
"""
|
|
|
|
Add another planner result to self
|
|
|
|
"""
|
|
|
|
for name, row in planner.rows.items():
|
|
|
|
if name in self.rows:
|
|
|
|
self_row = self.rows[name]
|
|
|
|
self_row.add_planner_result(row)
|
|
|
|
else:
|
|
|
|
self.rows[name] = row
|
|
|
|
|
|
|
|
for row in self.rows.values():
|
2024-06-11 19:20:57 +00:00
|
|
|
row.update(time=True)
|
2024-05-19 11:26:20 +00:00
|
|
|
|
2024-05-15 18:59:04 +00:00
|
|
|
def to_config(self) -> dict:
|
|
|
|
data = {}
|
|
|
|
for row in self.rows.values():
|
|
|
|
name = f'Item_{row.item.name}'
|
2024-06-11 19:20:57 +00:00
|
|
|
dic = row.model_dump(exclude=SET_ROW_EXCLUDE)
|
2024-05-15 18:59:04 +00:00
|
|
|
dic['item'] = row.item.name
|
2024-06-22 15:13:25 +00:00
|
|
|
dic['time'] = str(row.time)
|
2024-05-15 18:59:04 +00:00
|
|
|
data[name] = dic
|
|
|
|
return data
|
|
|
|
|
2024-06-11 19:20:57 +00:00
|
|
|
def get_overall(self):
|
|
|
|
"""
|
|
|
|
Calculate overall progress
|
|
|
|
Note that this method will clear all values
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
float: Progress percentage
|
|
|
|
float: ETA in days
|
|
|
|
"""
|
|
|
|
eta = 0.
|
|
|
|
progress_current = 0.
|
|
|
|
progress_total = 0.
|
|
|
|
for row in self.rows.values():
|
2024-06-14 17:52:50 +00:00
|
|
|
if not row.can_daily_farm():
|
|
|
|
continue
|
2024-06-11 19:20:57 +00:00
|
|
|
eta += row.eta
|
|
|
|
progress_current += row.progress_current
|
|
|
|
progress_total += row.progress_total
|
|
|
|
|
2024-06-13 18:01:23 +00:00
|
|
|
try:
|
|
|
|
progress = round(progress_current / progress_total * 100, 2)
|
|
|
|
except ZeroDivisionError:
|
|
|
|
progress = 100.
|
2024-06-11 19:20:57 +00:00
|
|
|
return progress, eta
|
|
|
|
|
2024-05-17 18:35:44 +00:00
|
|
|
def iter_row_to_farm(self, need_farm=True) -> t.Iterable[StoredPlannerProxy]:
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
need_farm: True if filter rows that need farm
|
|
|
|
|
|
|
|
Yields:
|
|
|
|
|
|
|
|
"""
|
|
|
|
if need_farm:
|
|
|
|
rows = [row for row in self.rows.values() if row.need_farm()]
|
|
|
|
else:
|
|
|
|
rows = self.rows.values()
|
|
|
|
|
2024-05-15 18:59:04 +00:00
|
|
|
for row in rows:
|
|
|
|
if row.item.is_ItemWeekly:
|
2024-05-17 18:35:44 +00:00
|
|
|
yield row
|
2024-05-15 18:59:04 +00:00
|
|
|
for row in rows:
|
|
|
|
if row.item.is_ItemAscension:
|
2024-05-17 18:35:44 +00:00
|
|
|
yield row
|
2024-05-15 18:59:04 +00:00
|
|
|
for row in rows:
|
|
|
|
if row.item.is_ItemTrace:
|
2024-05-17 18:35:44 +00:00
|
|
|
yield row
|
2024-05-15 18:59:04 +00:00
|
|
|
for row in rows:
|
|
|
|
if row.item.is_ItemExp:
|
2024-05-17 18:35:44 +00:00
|
|
|
yield row
|
2024-05-15 18:59:04 +00:00
|
|
|
for row in rows:
|
|
|
|
if row.item.is_ItemCurrency:
|
2024-05-17 18:35:44 +00:00
|
|
|
yield row
|
2024-05-15 18:59:04 +00:00
|
|
|
|
2024-05-17 18:35:44 +00:00
|
|
|
def get_dungeon(self, double_calyx=False) -> DungeonList | None:
|
2024-05-15 18:59:04 +00:00
|
|
|
"""
|
|
|
|
Get dungeon to farm, or None if planner finished or the remaining items cannot be farmed
|
|
|
|
"""
|
2024-05-17 18:35:44 +00:00
|
|
|
for row in self.iter_row_to_farm():
|
|
|
|
item = row.item
|
2024-05-15 18:59:04 +00:00
|
|
|
if item.is_ItemWeekly:
|
|
|
|
continue
|
|
|
|
dungeon = item.dungeon
|
|
|
|
if dungeon is None:
|
|
|
|
logger.error(f'Item {item} has nowhere to be farmed')
|
|
|
|
continue
|
2024-05-17 18:35:44 +00:00
|
|
|
if double_calyx:
|
|
|
|
if dungeon.is_Calyx:
|
|
|
|
logger.info(f'Planner farm (double_calyx): {dungeon}')
|
|
|
|
return dungeon
|
|
|
|
else:
|
|
|
|
logger.info(f'Planner farm: {dungeon}')
|
|
|
|
return dungeon
|
2024-05-15 18:59:04 +00:00
|
|
|
|
|
|
|
logger.info('Planner farm empty')
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_weekly(self) -> DungeonList | None:
|
2024-05-17 18:35:44 +00:00
|
|
|
for row in self.iter_row_to_farm():
|
|
|
|
item = row.item
|
2024-05-15 18:59:04 +00:00
|
|
|
if not item.is_ItemWeekly:
|
|
|
|
continue
|
|
|
|
dungeon = item.dungeon
|
|
|
|
if dungeon is None:
|
|
|
|
logger.error(f'Item {item} has nowhere to be farmed')
|
|
|
|
continue
|
2024-05-26 19:03:11 +00:00
|
|
|
logger.info(f'Planner weekly farm: {dungeon}')
|
2024-05-15 18:59:04 +00:00
|
|
|
return dungeon
|
|
|
|
|
2024-05-26 19:03:11 +00:00
|
|
|
logger.info('Planner weekly farm empty')
|
2024-05-15 18:59:04 +00:00
|
|
|
return None
|
|
|
|
|
2024-05-17 18:35:44 +00:00
|
|
|
def row_come_from_dungeon(self, dungeon: DungeonList | None) -> StoredPlannerProxy | None:
|
|
|
|
"""
|
|
|
|
If any items in planner is able to be farmed from given dungeon
|
|
|
|
"""
|
|
|
|
if dungeon is None:
|
|
|
|
return None
|
|
|
|
for row in self.iter_row_to_farm(need_farm=False):
|
|
|
|
if row.item.dungeon == dungeon:
|
|
|
|
logger.info(f'Planner {row} come from {dungeon}')
|
|
|
|
return row
|
|
|
|
return None
|
2024-05-15 18:59:04 +00:00
|
|
|
|
2024-05-17 18:35:13 +00:00
|
|
|
|
|
|
|
class PlannerMixin(UI):
|
2024-05-15 18:59:04 +00:00
|
|
|
def planner_write_results(self, results: list[PlannerResultRow]):
|
|
|
|
"""
|
|
|
|
Write planner detection results info user config
|
|
|
|
"""
|
2024-05-19 11:26:20 +00:00
|
|
|
add = self.config.PlannerScan_ResultAdd
|
|
|
|
logger.attr('ResultAdd', add)
|
|
|
|
|
2024-05-17 18:47:33 +00:00
|
|
|
planner = PlannerProgressParser().from_planner_results(results)
|
2024-05-19 11:26:20 +00:00
|
|
|
if add:
|
|
|
|
planner.add_planner_result(self.planner)
|
|
|
|
|
2024-06-17 18:19:59 +00:00
|
|
|
# Load from dashboard
|
|
|
|
try:
|
|
|
|
row = planner.rows['Credit']
|
|
|
|
value = self.config.stored.Credit.value
|
|
|
|
if value:
|
|
|
|
row.value = value
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2024-05-17 18:47:33 +00:00
|
|
|
self.planner_write(planner)
|
2024-05-15 18:59:04 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def planner(self) -> PlannerProgressParser:
|
|
|
|
logger.hr('Planner')
|
2024-06-13 18:01:23 +00:00
|
|
|
data = self.config.cross_get('Dungeon.Planner', default={})
|
|
|
|
model = PlannerProgressParser()
|
|
|
|
model.from_config(data)
|
2024-05-15 18:59:04 +00:00
|
|
|
for row in model.rows.values():
|
|
|
|
logger.info(row)
|
|
|
|
return model
|
|
|
|
|
2024-05-17 18:47:33 +00:00
|
|
|
def planner_write(self, planner=None):
|
2024-05-15 18:59:04 +00:00
|
|
|
"""
|
|
|
|
Write planner into user config, delete planner object
|
|
|
|
"""
|
2024-05-17 18:47:33 +00:00
|
|
|
if planner is None:
|
|
|
|
planner = self.planner
|
|
|
|
|
|
|
|
data = planner.to_config()
|
2024-06-11 19:20:57 +00:00
|
|
|
progress, eta = planner.get_overall()
|
2024-05-17 18:47:33 +00:00
|
|
|
|
|
|
|
with self.config.multi_set():
|
2024-05-19 11:26:20 +00:00
|
|
|
# Set value
|
2024-05-17 18:47:33 +00:00
|
|
|
for key, value in data.items():
|
2024-06-18 04:35:39 +00:00
|
|
|
current = self.config.cross_get(f'Dungeon.Planner.{key}', default={})
|
|
|
|
if value != current:
|
|
|
|
self.config.cross_set(f'Dungeon.Planner.{key}', value)
|
2024-05-19 11:26:20 +00:00
|
|
|
# Remove other value
|
|
|
|
remove = []
|
|
|
|
for key, value in self.config.cross_get('Dungeon.Planner', default={}).items():
|
|
|
|
if value != {} and key not in data:
|
|
|
|
remove.append(key)
|
|
|
|
for key in remove:
|
|
|
|
self.config.cross_set(f'Dungeon.Planner.{key}', {})
|
2024-06-12 17:40:43 +00:00
|
|
|
# print(progress, eta)
|
2024-06-11 19:20:57 +00:00
|
|
|
# Set overall
|
|
|
|
self.config.stored.PlannerOverall.value = f'{progress:.2f}%'
|
|
|
|
self.config.stored.PlannerOverall.comment = f'<{eta:.1f}d'
|
2024-05-19 11:26:20 +00:00
|
|
|
|
2024-05-15 18:59:04 +00:00
|
|
|
del_cached_property(self, 'planner')
|
2024-06-11 19:20:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
self = PlannerMixin('src')
|
|
|
|
self.planner_write(self.planner)
|