Skip to content

Commit

Permalink
Merge pull request #20 from Its-Haze/DEV-9
Browse files Browse the repository at this point in the history
Use Ddragon to gather champion & skin information to support for different languages of league.

Japanese/korean languages on league did not properly work before in game.. the art was not found.
  • Loading branch information
Its-Haze authored Jan 7, 2024
2 parents ea3a396 + 9ceecc9 commit e8475df
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 31 deletions.
13 changes: 11 additions & 2 deletions league_rpc_linux/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from league_rpc_linux.colors import Colors
from league_rpc_linux.const import (
ALL_GAME_DATA_URL,
CHAMPION_NAME_CONVERT_MAP,
DEFAULT_CLIENT_ID,
DISCORD_PROCESS_NAMES,
LEAGUE_OF_LEGENDS_LOGO,
Expand Down Expand Up @@ -108,7 +109,11 @@ def main(cli_args: argparse.Namespace):
while player_state() == "InGame":
rpc.update( # type:ignore
large_image=skin_asset,
large_text=skin_name if skin_name else champ_name,
large_text=skin_name
if skin_name
else CHAMPION_NAME_CONVERT_MAP.get(
champ_name, champ_name
),
details=gamemode,
state=f"In Game {f'· {get_kda()} · lvl: {get_level()} · gold: {get_gold()}' if not cli_args.no_stats else ''}",
small_image=LEAGUE_OF_LEGENDS_LOGO,
Expand All @@ -131,7 +136,11 @@ def main(cli_args: argparse.Namespace):
break
rpc.update( # type:ignore
large_image=skin_asset,
large_text=skin_name if skin_name else champ_name,
large_text=skin_name
if skin_name
else CHAMPION_NAME_CONVERT_MAP.get(
champ_name, champ_name
),
details=gamemode,
state=f"In Game {f'· {get_kda()} · {get_creepscore()}' if not cli_args.no_stats else ''}",
small_image=LEAGUE_OF_LEGENDS_LOGO,
Expand Down
43 changes: 35 additions & 8 deletions league_rpc_linux/champion.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,27 @@
ALL_GAME_DATA_URL,
BASE_SKIN_URL,
CHAMPION_NAME_CONVERT_MAP,
DDRAGON_CHAMPION_DATA,
GAME_MODE_CONVERT_MAP,
)
from league_rpc_linux.kda import get_gold, get_level
from league_rpc_linux.latest_version import get_latest_version
from league_rpc_linux.polling import wait_until_exists
from league_rpc_linux.username import get_summoner_name

urllib3.disable_warnings()


def get_specific_champion_data(name: str) -> dict[str, Any]:
response = requests.get(
url=DDRAGON_CHAMPION_DATA.format_map(
{"version": get_latest_version(), "name": name}
),
timeout=15,
)
return response.json()


def gather_ingame_information() -> tuple[str, str, int, str, int, int]:
"""
Get the current playing champion name.
Expand Down Expand Up @@ -55,7 +67,7 @@ def gather_ingame_information() -> tuple[str, str, int, str, int, int]:
print("-" * 50)
if champion_name:
print(
f"{Colors.yellow}Champion name found {Colors.green}({champion_name}),{Colors.yellow} continuing..{Colors.reset}"
f"{Colors.yellow}Champion name found {Colors.green}({CHAMPION_NAME_CONVERT_MAP.get(champion_name, champion_name)}),{Colors.yellow} continuing..{Colors.reset}"
)
if skin_name:
print(
Expand All @@ -81,22 +93,37 @@ def gather_ingame_information() -> tuple[str, str, int, str, int, int]:
def gather_league_data(
parsed_data: dict[str, Any],
summoners_name: str,
) -> tuple[Optional[str], Optional[int], Optional[str]]:
) -> tuple[Optional[str], int, Optional[str]]:
"""
If the gamemode is LEAGUE, gather the relevant information and return it to RPC.
"""
champion_name: Optional[str] = None
skin_id: Optional[int] = None
skin_id: int = 0
skin_name: Optional[str] = None

skin_ids = []

for player in parsed_data["allPlayers"]:
if player["summonerName"] == summoners_name:
champion_name = CHAMPION_NAME_CONVERT_MAP.get(
player["championName"],
player["championName"],
)
raw_champion_name = player["rawChampionName"].split("_")[-1]
champion_data = get_specific_champion_data(name=raw_champion_name)

champion_name = champion_data["data"][raw_champion_name]["id"]

skin_id = player["skinID"]
skin_name = player.get("skinName")

skin_ids = [
i["num"] for i in champion_data["data"][raw_champion_name]["skins"]
]

if skin_id:
while skin_id not in skin_ids:
skin_id -= 1

for i in champion_data["data"][raw_champion_name]["skins"]:
if skin_id == i["num"]:
skin_name = i["name"]
break
break
continue
return champion_name, skin_id, skin_name
Expand Down
56 changes: 36 additions & 20 deletions league_rpc_linux/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
"https://raw.communitydragon.org/latest/game/assets/ux/summonericons/profileicon"
)

DDRAGON_CHAMPION_DATA = (
"https://ddragon.leagueoflegends.com/cdn/{version}/data/en_US/champion/{name}.json"
)

DDRAGON_API_VERSIONS = "https://ddragon.leagueoflegends.com/api/versions.json"

MAP_ICON_CONVERT_MAP = {
11: "classic_sru",
12: "aram",
Expand All @@ -37,27 +43,37 @@
}

CHAMPION_NAME_CONVERT_MAP = {
"Aurelion Sol": "AurelionSol",
"Bel'Veth": "Belveth",
"Cho'Gath": "Chogath",
"Dr. Mundo": "DrMundo",
"Jarvan IV": "JarvanIV",
"Kai'Sa": "Kaisa",
"Kha'Zix": "Khazix",
"Kog'Maw": "KogMaw",
"K'Sante": "KSante",
"Lee Sin": "LeeSin",
"Master Yi": "MasterYi",
"Miss Fortune": "MissFortune",
"Nunu & Willump": "Nunu",
"Rek'Sai": "RekSai",
"Renata Glasc": "Renata",
"Tahm Kench": "TahmKench",
"Twisted Fate": "TwistedFate",
"Vel'Koz": "Velkoz",
"Wukong": "MonkeyKing",
"Xin Zhao": "XinZhao",
"AurelionSol": "Aurelion Sol",
"Belveth": "Bel'Veth",
"Chogath": "Cho'Gath",
"DrMundo": "Dr. Mundo",
"JarvanIV": "Jarvan IV",
"Kaisa": "Kai'Sa",
"Khazix": "Kha'Zix",
"KogMaw": "Kog'Maw",
"KSante": "K'Sante",
"LeeSin": "Lee Sin",
"MasterYi": "Master Yi",
"MissFortune": "Miss Fortune",
"Nunu": "Nunu & Willump",
"RekSai": "Rek'Sai",
"Renata": "Renata Glasc",
"TahmKench": "Tahm Kench",
"TwistedFate": "Twisted Fate",
"Velkoz": "Vel'Koz",
"MonkeyKing": "Wukong",
"XinZhao": "Xin Zhao",
}

LEAGUE_RANKED_EMBLEM = "https://github.com/Its-Haze/league-assets/blob/master/ranked_emblems/{tier}.png?raw=true"
LEAGUE_CHERRY_RANKED_EMBLEM = "https://github.com/Its-Haze/league-assets/blob/master/cherry_rated_medallions/{tier}.png?raw=true"


RANKED_TYPE_MAPPER = {
"RANKED_SOLO_5x5": "Ranked Solo/Duo",
"RANKED_FLEX_SR": "Ranked Flex",
"RANKED_TFT": "Teamfight Tactics (Ranked)",
"RANKED_TFT_DOUBLE_UP": "Teamfight Tactics (Double Up Workshop)",
"RANKED_TFT_TURBO": "Teamfight Tactics (Hyper Roll)",
"CHERRY": "Arena",
}
11 changes: 11 additions & 0 deletions league_rpc_linux/latest_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import requests

from league_rpc_linux.const import DDRAGON_API_VERSIONS


def get_latest_version() -> str:
response = requests.get(url=DDRAGON_API_VERSIONS, timeout=15)

data = response.json()
latest_version = data[0]
return latest_version
1 change: 1 addition & 0 deletions league_rpc_linux/lcu_api/base_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ async def gather_queue_data(connection: Connection, data: ClientData):
)
lobby_queue_info = await lobby_queue_info_raw.json()
data.queue = lobby_queue_info[LolGameQueuesQueue.NAME]
data.queue_type = lobby_queue_info[LolGameQueuesQueue.TYPE]
data.max_players = int(
lobby_queue_info[LolGameQueuesQueue.MAXIMUM_PARTICIPANT_LIST_SIZE]
)
Expand Down
1 change: 1 addition & 0 deletions league_rpc_linux/lcu_api/lcu_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ async def in_lobby(connection: Connection, event: WebsocketEventResponse) -> Non
lobby_queue_info = await lobby_queue_info_raw.json()

data.queue = lobby_queue_info[LolGameQueuesQueue.NAME]
data.queue_type = lobby_queue_info[LolGameQueuesQueue.TYPE]
data.queue_is_ranked = lobby_queue_info[LolGameQueuesQueue.IS_RANKED]

rpc_updater.delay_update(module_data)
Expand Down
1 change: 1 addition & 0 deletions league_rpc_linux/models/client_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ClientData:
max_players: int = 0 # max players in lobby
players: int = 0 # players in lobby
queue: str = ""
queue_type = ""
queue_id: int = -1
queue_is_ranked: bool = False
summoner_icon: int = 0
Expand Down
3 changes: 2 additions & 1 deletion league_rpc_linux/models/rpc_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
LEAGUE_OF_LEGENDS_LOGO,
MAP_ICON_CONVERT_MAP,
PROFILE_ICON_BASE_URL,
RANKED_TYPE_MAPPER,
SMALL_TEXT,
)
from league_rpc_linux.lcu_api.lcu_connector import ModuleData
Expand Down Expand Up @@ -124,7 +125,7 @@ def show_ranked_data(
) -> tuple[str, ...]:
large_text = small_text = small_image = ""

match module_data.client_data.queue:
match RANKED_TYPE_MAPPER.get(module_data.client_data.queue_type):
case "Ranked Solo/Duo":
summoner_rank = module_data.client_data.summoner_rank
if summoner_rank.tier:
Expand Down

0 comments on commit e8475df

Please sign in to comment.