2023-06-17 08:19:13 +00:00
|
|
|
import re
|
|
|
|
|
2023-09-12 17:43:59 +00:00
|
|
|
import module.config.server as server
|
2023-06-16 19:15:26 +00:00
|
|
|
from module.base.timer import Timer
|
2023-09-26 19:14:12 +00:00
|
|
|
from module.base.utils import color_similar, get_color
|
2023-06-27 15:52:41 +00:00
|
|
|
from module.logger import logger
|
2023-06-16 17:04:17 +00:00
|
|
|
from module.ocr.ocr import Digit, DigitCounter
|
2023-06-15 14:12:47 +00:00
|
|
|
from tasks.base.ui import UI
|
2023-06-16 17:04:17 +00:00
|
|
|
from tasks.combat.assets.assets_combat_prepare import (
|
|
|
|
OCR_TRAILBLAZE_POWER,
|
2023-06-27 15:52:41 +00:00
|
|
|
OCR_WAVE_COST,
|
2023-06-16 17:04:17 +00:00
|
|
|
OCR_WAVE_COUNT,
|
|
|
|
WAVE_MINUS,
|
|
|
|
WAVE_PLUS
|
|
|
|
)
|
2023-06-15 14:12:47 +00:00
|
|
|
|
|
|
|
|
2023-06-17 08:19:13 +00:00
|
|
|
class TrailblazePowerOcr(DigitCounter):
|
|
|
|
def after_process(self, result):
|
|
|
|
result = super().after_process(result)
|
|
|
|
# The trailblaze power icon is recognized as 买
|
|
|
|
# OCR_TRAILBLAZE_POWER includes the icon because the length varies by value
|
|
|
|
result = re.sub(r'[买米装:()]', '', result)
|
2023-09-16 21:43:28 +00:00
|
|
|
# 61240 -> 6/240
|
|
|
|
result = re.sub(r'1240$', '/240', result)
|
2023-06-17 08:19:13 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2023-06-15 14:12:47 +00:00
|
|
|
class CombatPrepare(UI):
|
2023-07-10 14:23:11 +00:00
|
|
|
# Current combat waves,
|
|
|
|
combat_waves = 1
|
|
|
|
# Limit combat runs, 0 means no limit.
|
|
|
|
combat_wave_limit = 0
|
|
|
|
combat_wave_done = 0
|
|
|
|
# E.g. 10, 30, 40
|
|
|
|
combat_wave_cost = 10
|
2023-06-27 15:52:41 +00:00
|
|
|
|
2023-06-15 14:12:47 +00:00
|
|
|
def combat_set_wave(self, count=6):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
count: 1 to 6
|
2023-06-16 17:04:17 +00:00
|
|
|
|
|
|
|
Pages:
|
|
|
|
in: COMBAT_PREPARE
|
2023-06-15 14:12:47 +00:00
|
|
|
"""
|
|
|
|
self.ui_ensure_index(
|
2023-09-12 17:43:59 +00:00
|
|
|
count, letter=Digit(OCR_WAVE_COUNT, lang=server.lang),
|
2023-06-15 14:12:47 +00:00
|
|
|
next_button=WAVE_PLUS, prev_button=WAVE_MINUS,
|
|
|
|
skip_first_screenshot=True
|
|
|
|
)
|
2023-06-16 17:04:17 +00:00
|
|
|
|
2023-06-16 19:15:26 +00:00
|
|
|
def combat_has_multi_wave(self) -> bool:
|
2023-06-16 17:04:17 +00:00
|
|
|
"""
|
2023-06-16 19:15:26 +00:00
|
|
|
If combat has waves to set.
|
|
|
|
Most dungeons can do 6 times at one time while bosses don't.
|
|
|
|
"""
|
|
|
|
return self.appear(WAVE_MINUS) or self.appear(WAVE_PLUS)
|
|
|
|
|
|
|
|
def combat_get_trailblaze_power(self, expect_reduce=False, skip_first_screenshot=True) -> int:
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
expect_reduce: Current value is supposed to be lower than the previous.
|
|
|
|
skip_first_screenshot:
|
|
|
|
|
2023-06-16 17:04:17 +00:00
|
|
|
Pages:
|
|
|
|
in: COMBAT_PREPARE or COMBAT_REPEAT
|
|
|
|
"""
|
2023-06-16 19:15:26 +00:00
|
|
|
timeout = Timer(1, count=2).start()
|
2023-12-18 15:57:33 +00:00
|
|
|
before = self.config.stored.TrailblazePower.value
|
2023-06-16 19:15:26 +00:00
|
|
|
while 1:
|
|
|
|
if skip_first_screenshot:
|
|
|
|
skip_first_screenshot = False
|
|
|
|
else:
|
|
|
|
self.device.screenshot()
|
|
|
|
|
2023-07-05 16:37:21 +00:00
|
|
|
current, _, total = TrailblazePowerOcr(OCR_TRAILBLAZE_POWER).ocr_single_line(self.device.image)
|
|
|
|
# Empty result
|
|
|
|
if total == 0:
|
|
|
|
continue
|
2023-09-26 19:14:12 +00:00
|
|
|
# Confirm if it is > 240, sometimes just OCR errors
|
2023-08-30 11:14:14 +00:00
|
|
|
if current > 240 and timeout.reached():
|
2023-06-16 19:15:26 +00:00
|
|
|
break
|
2023-12-18 15:57:33 +00:00
|
|
|
if expect_reduce and current >= before:
|
2023-06-16 19:15:26 +00:00
|
|
|
continue
|
2023-08-30 11:14:14 +00:00
|
|
|
if current <= 240:
|
2023-06-16 19:15:26 +00:00
|
|
|
break
|
|
|
|
|
2023-08-27 16:41:49 +00:00
|
|
|
self.config.stored.TrailblazePower.value = current
|
2023-06-16 17:04:17 +00:00
|
|
|
return current
|
2023-06-27 15:52:41 +00:00
|
|
|
|
2023-06-28 14:55:19 +00:00
|
|
|
def combat_get_wave_cost(self, skip_first_screenshot=True):
|
2023-06-27 15:52:41 +00:00
|
|
|
"""
|
|
|
|
Get traiblaze power cost and set it to `combat_cost`
|
|
|
|
|
2023-06-28 14:55:19 +00:00
|
|
|
Returns:
|
2023-07-10 14:23:11 +00:00
|
|
|
int: E.g. 10, 30, 40
|
2023-06-27 15:52:41 +00:00
|
|
|
|
|
|
|
Pages:
|
|
|
|
in: COMBAT_PREPARE
|
|
|
|
"""
|
|
|
|
timeout = Timer(1.5, count=6).start()
|
2023-06-28 14:55:19 +00:00
|
|
|
while 1:
|
|
|
|
if skip_first_screenshot:
|
|
|
|
skip_first_screenshot = False
|
|
|
|
else:
|
|
|
|
self.device.screenshot()
|
|
|
|
|
2023-09-26 19:14:12 +00:00
|
|
|
color = get_color(self.device.image, OCR_WAVE_COST.area)
|
|
|
|
if color_similar(color, (229, 231, 223), threshold=30):
|
|
|
|
logger.info(f'Combat is trailblaze power free')
|
|
|
|
self.combat_wave_cost = 0
|
|
|
|
return 0
|
|
|
|
|
2023-06-27 15:52:41 +00:00
|
|
|
cost = Digit(OCR_WAVE_COST).ocr_single_line(self.device.image)
|
|
|
|
if cost == 10:
|
2023-10-04 21:04:10 +00:00
|
|
|
logger.attr('CombatMultiWave', self.combat_has_multi_wave())
|
|
|
|
if self.combat_has_multi_wave():
|
2023-06-27 15:52:41 +00:00
|
|
|
self.combat_wave_cost = cost
|
|
|
|
return cost
|
|
|
|
else:
|
|
|
|
logger.warning(f'Combat wave costs {cost} but does not has multiple waves')
|
|
|
|
self.combat_wave_cost = cost
|
|
|
|
return cost
|
|
|
|
elif cost in [30, 40]:
|
2023-10-04 21:04:10 +00:00
|
|
|
if self.combat_has_multi_wave():
|
2023-06-27 15:52:41 +00:00
|
|
|
logger.warning(f'Combat wave costs {cost} but has multiple waves, '
|
|
|
|
f'probably wave amount is preset')
|
|
|
|
self.combat_set_wave(1)
|
2023-06-28 14:55:19 +00:00
|
|
|
skip_first_screenshot = True
|
2023-06-27 15:52:41 +00:00
|
|
|
timeout.reset()
|
|
|
|
continue
|
|
|
|
else:
|
2023-10-04 21:04:10 +00:00
|
|
|
logger.attr('CombatMultiWave', self.combat_has_multi_wave())
|
2023-06-27 15:52:41 +00:00
|
|
|
self.combat_wave_cost = cost
|
|
|
|
return cost
|
|
|
|
else:
|
|
|
|
logger.warning(f'Unexpected combat wave cost: {cost}')
|
|
|
|
continue
|
|
|
|
|
2023-10-04 21:04:10 +00:00
|
|
|
logger.attr('CombatMultiWave', self.combat_has_multi_wave())
|
|
|
|
if self.combat_has_multi_wave():
|
2023-06-28 14:55:19 +00:00
|
|
|
cost = 10
|
|
|
|
else:
|
|
|
|
cost = 40
|
2023-06-27 15:52:41 +00:00
|
|
|
logger.warning(f'Get combat wave cost timeout, assume it costs {cost}')
|
|
|
|
self.combat_wave_cost = cost
|
|
|
|
return cost
|