Skip to content

Commit

Permalink
Merge pull request #52 from Its-Haze/add-chroma-name
Browse files Browse the repository at this point in the history
Add chroma names to LargeImage text on Discord
  • Loading branch information
Its-Haze authored May 25, 2024
2 parents 9c8e3ed + 4f4469c commit e176fb5
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 46 deletions.
91 changes: 60 additions & 31 deletions league_rpc/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def main(cli_args: argparse.Namespace) -> None:
(
champ_name,
skin_name,
chroma_name,
skin_id,
gamemode,
_,
Expand All @@ -88,16 +89,22 @@ def main(cli_args: argparse.Namespace) -> None:
if gamemode == "TFT":
# TFT RPC
while player_state() == "InGame":
rpc.update( # type:ignore
large_image="https://wallpapercave.com/wp/wp7413493.jpg",
large_text="Playing TFT",
details="Teamfight Tactics",
state=f"In Game · lvl: {get_level()}",
small_image=LEAGUE_OF_LEGENDS_LOGO,
small_text=SMALL_TEXT,
start=int(time.time())
- get_current_ingame_time(default_time=start_time),
)
try:
rpc.update( # type:ignore
large_image="https://wallpapercave.com/wp/wp7413493.jpg",
large_text="Playing TFT",
details="Teamfight Tactics",
state=f"In Game · lvl: {get_level()}",
small_image=LEAGUE_OF_LEGENDS_LOGO,
small_text=SMALL_TEXT,
start=int(time.time())
- get_current_ingame_time(default_time=start_time),
)
except RuntimeError:
print(
f"{Color.red}Discord seems to be closed, will attempt to reconnect!{Color.reset}"
)
discord_reconnect_attempt(rpc=rpc)
time.sleep(10)
elif gamemode == "Arena":
# ARENA RPC
Expand All @@ -109,22 +116,33 @@ def main(cli_args: argparse.Namespace) -> None:
f"{Color.green}Successfully gathered all data. Updating your Presence now!{Color.reset}"
)
while player_state() == "InGame":
rpc.update( # type:ignore
large_image=skin_asset,
large_text=(
large_text = (
f"{skin_name} ({chroma_name})"
if chroma_name
else (
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,
small_text=SMALL_TEXT,
start=int(time.time())
- get_current_ingame_time(default_time=start_time),
)
)
try:
rpc.update( # type:ignore
large_image=skin_asset,
large_text=large_text,
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,
small_text=SMALL_TEXT,
start=int(time.time())
- get_current_ingame_time(default_time=start_time),
)
except RuntimeError:
print(
f"{Color.red}Discord seems to be closed, will attempt to reconnect!{Color.reset}"
)
discord_reconnect_attempt(rpc=rpc)
time.sleep(10)
else:
# LEAGUE RPC
Expand All @@ -138,22 +156,33 @@ def main(cli_args: argparse.Namespace) -> None:
while player_state() == "InGame":
if not champ_name or not gamemode:
break
rpc.update( # type:ignore
large_image=skin_asset,
large_text=(
large_text = (
f"{skin_name} ({chroma_name})"
if chroma_name
else (
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,
small_text=SMALL_TEXT,
start=int(time.time())
- get_current_ingame_time(default_time=start_time),
)
)
try:
rpc.update( # type:ignore
large_image=skin_asset,
large_text=large_text,
details=gamemode,
state=f"In Game {f'· {get_kda()} · {get_creepscore()}' if not cli_args.no_stats else ''}",
small_image=LEAGUE_OF_LEGENDS_LOGO,
small_text=SMALL_TEXT,
start=int(time.time())
- get_current_ingame_time(default_time=start_time),
)
except RuntimeError:
print(
f"{Color.red}Discord seems to be closed, will attempt to reconnect!{Color.reset}"
)
discord_reconnect_attempt(rpc=rpc)
time.sleep(10)

case "InLobby":
Expand Down Expand Up @@ -223,7 +252,7 @@ def main(cli_args: argparse.Namespace) -> None:
default=-1,
help="Time in seconds to wait for the Discord client to start. -1 for infinite waiting, Good when you want to start this script before you've had time to start Discord.",
)
action = parser.add_argument(
parser.add_argument(
"--launch-league",
type=str,
default=DEFAULT_LEAGUE_CLIENT_EXE_PATH,
Expand Down
2 changes: 1 addition & 1 deletion league_rpc/__version__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Set version number of package."""

__version__ = "v2.1.4"
__version__ = "v2.2.0"
import requests

RELEASES_PAGE = "https://github.com/Its-Haze/league-rpc/releases"
Expand Down
66 changes: 52 additions & 14 deletions league_rpc/champion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import requests
import urllib3

from league_rpc.disable_native_rpc.disable import find_game_locale
from league_rpc.kda import get_gold, get_level
from league_rpc.latest_version import get_latest_version
Expand All @@ -15,6 +14,7 @@
CHAMPION_NAME_CONVERT_MAP,
DDRAGON_CHAMPION_DATA,
GAME_MODE_CONVERT_MAP,
MERAKIANALYTICS_CHAMPION_DATA,
)
from league_rpc.utils.polling import wait_until_exists

Expand All @@ -38,24 +38,40 @@ def get_specific_champion_data(name: str, locale: str) -> dict[str, Any]:
return response.json()


def gather_ingame_information() -> tuple[str, str, int, str, int, int]:
def get_specific_chroma_data(name: str, locale: str) -> dict[str, Any]:
"""
Get the specific chroma champion data for the champion name.
"""
url = MERAKIANALYTICS_CHAMPION_DATA.format_map(
{
"locale": locale.replace("_", "-"),
}
)
response: requests.Response = requests.get(
url=url,
timeout=15,
)
return response.json()[name]


def gather_ingame_information() -> tuple[str, str, str, int, str, int, int]:
"""
Get the current playing champion name.
"""
all_game_data_url = ALL_GAME_DATA_URL
your_summoner_name: str = get_riot_id()

champion_name: str | None = None
skin_id: int | None = None
skin_name: str | None = None
chroma_name: str | None = None
game_mode: str | None = (
None # Set if the game mode was never found.. Maybe you are playing something new?
)
level: int | None = None
gold: int | None = None

if response := wait_until_exists(
url=all_game_data_url,
url=ALL_GAME_DATA_URL,
custom_message="Did not find game data.. Will try again in 5 seconds",
):
parsed_data = response.json()
Expand All @@ -69,7 +85,7 @@ def gather_ingame_information() -> tuple[str, str, int, str, int, int]:
level = get_level()
else:
# If the gamemode is LEAGUE gather the relevant information.
champion_name, skin_id, skin_name = gather_league_data(
champion_name, skin_id, skin_name, chroma_name = gather_league_data(
parsed_data=parsed_data, summoners_name=your_summoner_name
)
if game_mode == "Arena":
Expand All @@ -83,6 +99,10 @@ def gather_ingame_information() -> tuple[str, str, int, str, int, int]:
print(
f"{Color.yellow}Skin detected: {Color.green}{skin_name},{Color.yellow} continuing..{Color.reset}"
)
if chroma_name:
print(
f"{Color.yellow}Chroma detected: {Color.green}{chroma_name},{Color.yellow} continuing..{Color.reset}"
)
if game_mode:
print(
f"{Color.yellow}Game mode detected: {Color.green}{game_mode},{Color.yellow} continuing..{Color.reset}"
Expand All @@ -93,6 +113,7 @@ def gather_ingame_information() -> tuple[str, str, int, str, int, int]:
return (
(champion_name or ""),
(skin_name or ""),
(chroma_name or ""),
(skin_id or 0),
(game_mode or ""),
(level or 0),
Expand All @@ -103,39 +124,56 @@ 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], int, Optional[str]]:
) -> tuple[Optional[str], int, Optional[str], Optional[str]]:
"""
If the gamemode is LEAGUE, gather the relevant information and return it to RPC.
"""
champion_name: Optional[str] = None
skin_id: int = 0
base_skin_id: int = 0
skin_name: Optional[str] = None
chroma_name: Optional[str] = None
locale = find_game_locale(
league_processes=["LeagueClient.exe", "LeagueClientUx.exe"]
)

for player in parsed_data["allPlayers"]:
if player["riotId"] == summoners_name:
raw_champion_name: str = player["rawChampionName"].split("_")[-1]
champion_data: dict[str, Any] = get_specific_champion_data(
name=raw_champion_name,
locale=find_game_locale(
league_processes=[
"LeagueClient.exe",
"LeagueClientUx.exe",
]
),
locale=locale,
)
champion_name = champion_data["data"][raw_champion_name]["id"]
skin_name = player.get("skinName", None)
skin_id = player.get("skinID", None)

if skin_name:
skin_id = [
base_skin_id = [
x["num"]
for x in champion_data["data"][raw_champion_name]["skins"]
if x["name"] == skin_name
][0]
if skin_id != base_skin_id:
# Chroma detected: Get the name of the chroma:
chroma_data = get_specific_chroma_data(
name=raw_champion_name,
locale="en-US",
)
_skin_data: dict[str, Any] = [
x
for x in chroma_data["skins"]
if str(x["id"]).endswith(str(base_skin_id))
][0]
chroma_name = [
x["name"]
for x in _skin_data["chromas"]
if str(x["id"]).endswith(str(skin_id))
][0]

break
continue
return champion_name, skin_id, skin_name
return champion_name, base_skin_id, skin_name, chroma_name


def get_skin_asset(
Expand Down
4 changes: 4 additions & 0 deletions league_rpc/utils/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@

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

MERAKIANALYTICS_CHAMPION_DATA = (
"https://cdn.merakianalytics.com/riot/lol/resources/latest/{locale}/champions.json"
)

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

MAP_ICON_CONVERT_MAP: dict[int, str] = {
Expand Down

0 comments on commit e176fb5

Please sign in to comment.