Skip to content

Commit

Permalink
push to save code so I can work in class
Browse files Browse the repository at this point in the history
  • Loading branch information
akex06 committed Apr 3, 2024
1 parent 19f3e01 commit f7b95b5
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 19 deletions.
93 changes: 87 additions & 6 deletions valorant/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,24 @@ class Agent(msgspec.Struct):


class Version(msgspec.Struct):
manifest_id: str = msgspec.field(name="manifestId")
# TODO: Learn to pass the contents of valorant-api.com data to the struct without converting to json
# manifest_id: str = msgspec.field(name="manifestId")
# branch: str = msgspec.field(name="branch")
# version: str = msgspec.field(name="version")
# build_version: str = msgspec.field(name="buildVersion")
# engine_version: str = msgspec.field(name="engineVersion")
# riot_client_version: str = msgspec.field(name="riotClientVersion")
# riot_client_build: str = msgspec.field(name="riotClientBuild")
# build_date: str = msgspec.field(name="buildDate")

manifestId: str = msgspec.field(name="manifestId")
branch: str = msgspec.field(name="branch")
version: str = msgspec.field(name="version")
build_version: str = msgspec.field(name="buildVersion")
engine_version: str = msgspec.field(name="engineVersion")
riot_client_version: str = msgspec.field(name="riotClientVersion")
riot_client_build: str = msgspec.field(name="riotClientBuild")
build_date: str = msgspec.field(name="buildDate")
buildVersion: str = msgspec.field(name="buildVersion")
engineVersion: str = msgspec.field(name="engineVersion")
riotClientVersion: str = msgspec.field(name="riotClientVersion")
riotClientBuild: str = msgspec.field(name="riotClientBuild")
buildDate: str = msgspec.field(name="buildDate")


class Password(msgspec.Struct):
Expand Down Expand Up @@ -149,3 +159,74 @@ class Loadout(msgspec.Struct):
sprays: list[Spray] = msgspec.field(name="Sprays")
identity: Identity = msgspec.field(name="Identity")
incognito: bool = msgspec.field(name="Incognito")


class MatchInfo(msgspec.Struct):
id: str = msgspec.field(name="matchId")
map_id: str = msgspec.field(name="mapId")
game_pod_id: str = msgspec.field(name="gamePodId")
game_loop_zone: str = msgspec.field(name="gameLoopZone")
server: str = msgspec.field(name="gameServerAddress")
version: str = msgspec.field(name="gameVersion")
duration: int | None = msgspec.field(name="gameLengthMillis")
game_start_millis: str = msgspec.field(name="gameStartMillis")
provisioning_flow_id: Literal["Matchmaking", "CustomGame"] = msgspec.field(
name="provisioningFlowID"
)
has_finished: bool = msgspec.field(name="isCompleted")
custom_game_name: str = msgspec.field(name="customGameName")
force_post_processing: bool = msgspec.field(name="forcePostProcessing")
queue_id: str = msgspec.field(name="queueID")
game_mode: str = msgspec.field(name="gameMode")
is_ranked: bool = msgspec.field(name="isRanked")
is_match_sampled: bool = msgspec.field(name="isMatchSampled")
season_id: str = msgspec.field(name="seasonId")
completion_state: Literal["Surrendered", "Completed", "VoteDraw", ""] = (
msgspec.field(name="completionState")
)
party_penalties: dict = msgspec.field(name="partyRRPenalties")
should_match_disabled_penalties: bool = msgspec.field(
name="shouldMatchDisablePenalties"
)


class AbilityCasts(msgspec.Struct):
grenade: int = msgspec.field(name="grenadeCasts")
ability1: int = msgspec.field(name="ability1Casts")
ability2: int = msgspec.field(name="ability2Casts")
ultimate: int = msgspec.field(name="ultimateCasts")


class PlayerStats(msgspec.Struct):
score: int = msgspec.field(name="score")
roundsPlayed: int = msgspec.field(name="roundsPlayed")
kills: int = msgspec.field(name="kills")
deaths: int = msgspec.field(name="deaths")
assists: int = msgspec.field(name="assists")
playtimeMillis: int = msgspec.field(name="playtimeMillis")
ability_casts: tuple[AbilityCasts] | None = msgspec.field(name="abilityCasts")


class RoundDamage(msgspec.Struct):
round: int = msgspec.field(name="round")
receiver_id: str = msgspec.field(name="receiver")
amount: int = msgspec.field(name="damage")


class Player(msgspec.Struct):
player_id: str = msgspec.field(name="subject")
name: str = msgspec.field(name="gameName")
tagline: str = msgspec.field(name="tagLine")
team: Literal["Blue", "Red"] = msgspec.field(name="teamId")
party_id: str = msgspec.field(name="partyId")
character_id: str = msgspec.field(name="characterId")
stats: PlayerStats = msgspec.field(name="stats")


class Match(msgspec.Struct):
history: MatchInfo = msgspec.field(name="matchInfo")
players: list[Player] = msgspec.field(name="matchInfo")
coaches: list[Coach] = msgspec.field(name="matchInfo")
teams: list[Team] | None = msgspec.field(name="matchInfo")
rounds: list[Round] | None = msgspec.field(name="matchInfo")
kills: list[Kill] = msgspec.field(name="matchInfo")
26 changes: 13 additions & 13 deletions valorant/valorant.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import msgspec.json
import requests

from valorant import Auth, Version, User, AccountXP, Loadout, URLS, API, Region, regions
from valorant.classes import Version, User, AccountXP, Loadout
from valorant.constants import URLS, API, Region, regions
from valorant.auth import Auth


class Valorant:
Expand Down Expand Up @@ -51,13 +53,11 @@ def client_platform(self) -> bytes:

@property
def client_version(self) -> Version:
"""
Get the latest client version
:return: str
"""
return requests.get("https://valorant-api.com/v1/version", timeout=30).json()[
"data"
]
version = requests.get(
"https://valorant-api.com/v1/version", timeout=30
).json()["data"]

return Version(**version)

def get_user(self) -> User:
user_info = self.session.post(
Expand Down Expand Up @@ -86,7 +86,7 @@ def get_content(self) -> dict:
f"{self.pd_server}{API.CONTENT}",
headers={
"X-Riot-ClientPlatform": f"{self.client_platform}",
"X-Riot-ClientVersion": f"{self.client_version.riot_client_version}",
"X-Riot-ClientVersion": f"{self.client_version.riotClientVersion}",
"X-Riot-Entitlements-JWT": f"{self.auth.get_entitlement_token()}",
"Authorization": f"Bearer {self.auth.get_access_token()}",
},
Expand Down Expand Up @@ -130,7 +130,7 @@ def get_player_mmr(self, player_id: str | None = None) -> dict:
f"{self.pd_server}{API.MMR}/{player_id}",
headers={
"X-Riot-ClientPlatform": self.client_platform,
"X-Riot-ClientVersion": self.client_version.riot_client_version,
"X-Riot-ClientVersion": self.client_version.riotClientVersion,
"X-Riot-Entitlements-JWT": self.auth.get_entitlement_token(),
"Authorization": f"Bearer {self.auth.get_access_token()}",
},
Expand Down Expand Up @@ -178,7 +178,7 @@ def get_leaderboard(
else ""
),
headers={
"X-Riot-ClientVersion": self.client_version.riot_client_version,
"X-Riot-ClientVersion": self.client_version.riotClientVersion,
"X-Riot-Entitlements-JWT": self.auth.get_entitlement_token(),
"Authorization": f"Bearer {self.auth.get_access_token()}",
},
Expand Down Expand Up @@ -219,7 +219,7 @@ def get_store(self) -> dict:
"Authorization": f"Bearer {self.auth.get_access_token()}",
"X-Riot-Entitlements-JWT": self.auth.get_entitlement_token(),
"X-Riot-ClientPlatform": self.client_platform,
"X-Riot-ClientVersion": self.client_version.riot_client_version,
"X-Riot-ClientVersion": self.client_version.riotClientVersion,
"Content-Type": "application/json",
},
).json()
Expand Down Expand Up @@ -367,7 +367,7 @@ def get_contracts(self, player_id: str | None = None) -> dict:
return self.session.get(
f"{self.pd_server}{API.CONTRACTS}/{player_id}",
headers={
"X-Riot-ClientVersion": self.client_version.riot_client_version,
"X-Riot-ClientVersion": self.client_version.riotClientVersion,
"X-Riot-Entitlements-JWT": self.auth.get_entitlement_token(),
"Authorization": f"Bearer {self.auth.get_access_token()}",
},
Expand Down

0 comments on commit f7b95b5

Please sign in to comment.