Skip to content

Commit

Permalink
Version 1.4.4 (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVG207 authored Apr 30, 2023
2 parents f191bf3 + 4b584cc commit cf158dd
Show file tree
Hide file tree
Showing 15 changed files with 412 additions and 210 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ venv/*
_dump/*
.vscode/*
*__pycache__*
*.log
*.log*
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ charset-normalizer==3.0.1
colorama==0.4.6
contourpy==1.0.7
cycler==0.11.0
elevate==0.1.3
exceptiongroup==1.1.0
fonttools==4.38.0
h11==0.14.0
Expand Down
97 changes: 66 additions & 31 deletions umalauncher/carrotjuicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.common.exceptions import NoSuchWindowException
from screenstate import ScreenState, Location
import screenstate_utils
import util
import constants
import mdb
import helper_table
import training_tracker
Expand All @@ -34,6 +35,8 @@ class CarrotJuicer():
training_tracker = None
previous_request = None
last_helper_data = None
previous_race_program_id = None
last_data = None

_browser_list = None

Expand Down Expand Up @@ -67,7 +70,12 @@ def restart_time(self):
def load_request(self, msg_path):
try:
with open(msg_path, "rb") as in_file:
return msgpack.unpackb(in_file.read()[170:], strict_map_key=False)
unpacked = msgpack.unpackb(in_file.read()[170:], strict_map_key=False)
# Remove keys that are not needed
for key in constants.REQUEST_KEYS_TO_BE_REMOVED:
if key in unpacked:
del unpacked[key]
return unpacked
except PermissionError:
logger.warning("Could not load request because it is already in use!")
time.sleep(0.1)
Expand All @@ -94,7 +102,7 @@ def create_gametora_helper_url_from_start(self, packet_data):


def to_json(self, packet, out_name="packet.json"):
with open(out_name, 'w', encoding='utf-8') as f:
with open(util.get_relative(out_name), 'w', encoding='utf-8') as f:
f.write(json.dumps(packet, indent=4, ensure_ascii=False))

# def to_python_dict_file(self, packet, out_name="packet.py"):
Expand Down Expand Up @@ -346,6 +354,33 @@ def add_response_to_tracker(self, data):
if should_track:
self.training_tracker.add_response(data)


EVENT_ID_TO_POS_STRING = {
7005: '(1st)',
7006: '(2nd-5th)',
7007: '(6th or worse)'
}

def get_after_race_event_title(self, event_id):
if not self.previous_race_program_id:
return "PREVIOUS RACE UNKNOWN"

race_grade = mdb.get_program_id_grade(self.previous_race_program_id)

if not race_grade:
logger.error(f"Race grade not found for program id {self.previous_race_program_id}")
return "RACE GRADE NOT FOUND"

grade_text = ""
if race_grade > 300:
grade_text = "OP/Pre-OP"
elif race_grade > 100:
grade_text = "G2/G3"
else:
grade_text = "G1"

return f"{grade_text} {self.EVENT_ID_TO_POS_STRING[event_id]}"

def handle_response(self, message):
data = self.load_response(message)

Expand Down Expand Up @@ -377,22 +412,28 @@ def handle_response(self, message):
# Concert Theater
if "live_theater_save_info_array" in data:
if self.screen_state_handler:
new_state = ScreenState(self.threader.screenstate)
new_state.location = Location.THEATER
new_state = screenstate_utils.ss.ScreenState(self.threader.screenstate)
new_state.location = screenstate_utils.ss.Location.THEATER
new_state.main = "Concert Theater"
new_state.sub = "Vibing"

self.screen_state_handler.carrotjuicer_state = new_state
return

# Race starts.
if 'race_scenario' in data and 'race_start_info' in data and data['race_scenario']:
if self.training_tracker and 'race_scenario' in data and 'race_start_info' in data and data['race_scenario']:
self.previous_race_program_id = data['race_start_info']['program_id']
# Currently starting a race. Add packet to training tracker.
logger.debug("Race packet received.")
self.add_response_to_tracker(data)
return


# Update history
if 'race_history' in data and data['race_history']:
self.previous_race_program_id = data['race_history'][-1]['program_id']


# Gametora
if 'chara_info' in data:
# Inside training run.
Expand All @@ -406,27 +447,15 @@ def handle_response(self, message):

# Training info
outfit_id = data['chara_info']['card_id']
chara_id = int(str(outfit_id)[:-2])
supports = [card_data['support_card_id'] for card_data in data['chara_info']['support_card_array']]
scenario_id = data['chara_info']['scenario_id']

# Training stats
if self.screen_state_handler:
new_state = ScreenState(self.threader.screenstate)

new_state.location = Location.TRAINING

new_state.main = f"Training - {util.turn_to_string(data['chara_info']['turn'])}"
new_state.sub = f"{data['chara_info']['speed']} {data['chara_info']['stamina']} {data['chara_info']['power']} {data['chara_info']['guts']} {data['chara_info']['wiz']} | {data['chara_info']['skill_point']}"

scenario_id = data['chara_info']['scenario_id']
scenario_name = util.SCENARIO_DICT.get(scenario_id, None)
if not scenario_name:
logger.error(f"Scenario ID not found in scenario dict: {scenario_id}")
scenario_name = "You are now breathing manually."
new_state.set_chara(chara_id, outfit_id=outfit_id, small_text=scenario_name)

self.screen_state_handler.carrotjuicer_state = new_state
if data.get('race_start_info', None):
self.screen_state_handler.carrotjuicer_state = screenstate_utils.make_training_race_state(data, self.threader.screenstate)
else:
self.screen_state_handler.carrotjuicer_state = screenstate_utils.make_training_state(data, self.threader.screenstate)

if not self.browser or not self.browser.current_url.startswith("https://gametora.com/umamusume/training-event-helper"):
logger.info("GT tab not open, opening tab")
Expand Down Expand Up @@ -467,6 +496,11 @@ def handle_response(self, message):
else:
logger.debug("Trained character or support card detected")

# Check for after-race event.
if event_data['event_id'] in (7005, 7006, 7007):
logger.debug("After-race event detected.")
event_title = self.get_after_race_event_title(event_data['event_id'])

# Activate and scroll to the outcome.
self.previous_element = self.browser.execute_script(
"""a = document.querySelectorAll("[class^='compatibility_viewer_item_']");
Expand All @@ -485,7 +519,7 @@ def handle_response(self, message):
event_title
)
if not self.previous_element:
logger.debug("Could not find event on GT page.")
logger.debug(f"Could not find event on GT page: {event_title} {event_data['story_id']}")
self.browser.execute_script("""
if (arguments[0]) {
// document.querySelector(".tippy-box").scrollIntoView({behavior:"smooth", block:"center"});
Expand All @@ -495,11 +529,14 @@ def handle_response(self, message):
""",
self.previous_element
)

self.last_data = data
except Exception:
logger.error("ERROR IN HANDLING RESPONSE MSGPACK")
logger.error(data)
logger.error(traceback.format_exc())
util.show_warning_box("Uma Launcher: Error in response msgpack.", "This should not happen. You may contact the developer about this issue.")
exception_string = traceback.format_exc()
logger.error(exception_string)
util.show_warning_box("Uma Launcher: Error in response msgpack.", f"This should not happen. You may contact the developer about this issue.\n\n{exception_string}")
# self.close_browser()

def check_browser(self):
Expand All @@ -517,10 +554,7 @@ def check_browser(self):

def start_concert(self, music_id):
logger.debug("Starting concert")
new_state = ScreenState(self.threader.screenstate)
new_state.location = Location.THEATER
new_state.set_music(music_id)
self.screen_state_handler.carrotjuicer_state = new_state
self.screen_state_handler.carrotjuicer_state = screenstate_utils.make_concert_state(music_id, self.threader.screenstate)
return

def handle_request(self, message):
Expand Down Expand Up @@ -559,8 +593,9 @@ def handle_request(self, message):
except Exception:
logger.error("ERROR IN HANDLING REQUEST MSGPACK")
logger.error(data)
logger.error(traceback.format_exc())
util.show_warning_box("Uma Launcher: Error in request msgpack.", "This should not happen. You may contact the developer about this issue.")
exception_string = traceback.format_exc()
logger.error(exception_string)
util.show_warning_box("Uma Launcher: Error in request msgpack.", f"This should not happen. You may contact the developer about this issue.\n\n{exception_string}")
# self.close_browser()


Expand Down
118 changes: 118 additions & 0 deletions umalauncher/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
SCENARIO_DICT = {
1: "URA Finals",
2: "Aoharu Cup",
3: "Grand Live",
4: "Make a New Track",
5: "Grand Masters",
}

MOTIVATION_DICT = {
5: "Very High",
4: "High",
3: "Normal",
2: "Low",
1: "Very Low"
}

SUPPORT_CARD_RARITY_DICT = {
1: "R",
2: "SR",
3: "SSR"
}

SUPPORT_CARD_TYPE_DICT = {
(101, 1): "speed",
(105, 1): "stamina",
(102, 1): "power",
(103, 1): "guts",
(106, 1): "wiz",
(0, 2): "friend",
(0, 3): "group"
}

SUPPORT_CARD_TYPE_DISPLAY_DICT = {
"speed": "Speed",
"stamina": "Stamina",
"power": "Power",
"guts": "Guts",
"wiz": "Wisdom",
"friend": "Friend",
"group": "Group"
}

SUPPORT_TYPE_TO_COMMAND_IDS = {
"speed": [101, 601],
"stamina": [105, 602],
"power": [102, 603],
"guts": [103, 604],
"wiz": [106, 605],
"friend": [],
"group": []
}

COMMAND_ID_TO_KEY = {
101: "speed",
105: "stamina",
102: "power",
103: "guts",
106: "wiz",
601: "speed",
602: "stamina",
603: "power",
604: "guts",
605: "wiz"
}

TARGET_TYPE_TO_KEY = {
1: "speed",
2: "stamina",
3: "power",
4: "guts",
5: "wiz"
}

MONTH_DICT = {
1: 'January',
2: 'February',
3: 'March',
4: 'April',
5: 'May',
6: 'June',
7: 'July',
8: 'August',
9: 'September',
10: 'October',
11: 'November',
12: 'December'
}

GL_TOKEN_LIST = [
'dance',
'passion',
'vocal',
'visual',
'mental'
]

ORIENTATION_DICT = {
True: 'portrait',
False: 'landscape',
'portrait': True,
'landscape': False,
}

# Request packets contain keys that should not be kept for privacy reasons.
REQUEST_KEYS_TO_BE_REMOVED = [
"device",
"device_id",
"device_name",
"graphics_device_name",
"ip_address",
"platform_os_version",
"carrier",
"keychain",
"locale",
"button_info",
"dmm_viewer_id",
"dmm_onetime_token",
]
Loading

0 comments on commit cf158dd

Please sign in to comment.