Repository navigation
Expand file tree
/
Copy pathmodels.py
More file actions
111 lines (90 loc) · 3.36 KB
/
Copy pathmodels.py
File metadata and controls
111 lines (90 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""Structures de données typées construites à partir des réponses de l'API Riot."""
from __future__ import annotations
from dataclasses import dataclass
from typing import Any
# Une partie de moins de 4 minutes est considérée comme un remake.
REMAKE_MAX_DURATION = 240
QUEUE_RANKED_SOLO = 420
@dataclass(frozen=True)
class PlayerRef:
"""Un joueur identifié par son Riot ID et son PUUID."""
riot_id: str
tag: str
puuid: str
@property
def display(self) -> str:
return f"{self.riot_id}#{self.tag}"
@dataclass(frozen=True)
class Participant:
puuid: str
name: str
champion: str
team_id: int
win: bool
kills: int
deaths: int
assists: int
cs: int
damage: int
vision: int
items: tuple[int, ...]
spells: tuple[int, int]
early_surrender: bool
@property
def kda(self) -> float:
return (self.kills + self.assists) / max(self.deaths, 1)
@classmethod
def from_api(cls, p: dict[str, Any]) -> Participant:
return cls(
puuid=p["puuid"],
name=p.get("riotIdGameName") or p.get("summonerName") or "Inconnu",
champion=p.get("championName", "Inconnu"),
team_id=p.get("teamId", 0),
win=bool(p.get("win", False)),
kills=p.get("kills", 0),
deaths=p.get("deaths", 0),
assists=p.get("assists", 0),
cs=p.get("totalMinionsKilled", 0) + p.get("neutralMinionsKilled", 0),
damage=p.get("totalDamageDealtToChampions", 0),
vision=p.get("visionScore", 0),
items=tuple(p[f"item{i}"] for i in range(7) if p.get(f"item{i}")),
spells=(p.get("summoner1Id", 0), p.get("summoner2Id", 0)),
early_surrender=bool(p.get("gameEndedInEarlySurrender", False)),
)
@dataclass(frozen=True)
class Match:
match_id: str
queue_id: int
duration: int # secondes
participants: tuple[Participant, ...]
ended_at: int | None = None # horodatage Unix (secondes) de la fin de partie
@classmethod
def from_api(cls, data: dict[str, Any]) -> Match:
info = data["info"]
return cls(
match_id=data.get("metadata", {}).get("matchId", ""),
queue_id=info.get("queueId", 0),
duration=info.get("gameDuration", 0),
participants=tuple(Participant.from_api(p) for p in info.get("participants", [])),
ended_at=_end_timestamp(info),
)
def participant(self, puuid: str) -> Participant | None:
return next((p for p in self.participants if p.puuid == puuid), None)
def is_remake(self, participant: Participant) -> bool:
return participant.early_surrender or self.duration < REMAKE_MAX_DURATION
@property
def is_ranked_solo(self) -> bool:
return self.queue_id == QUEUE_RANKED_SOLO
def _end_timestamp(info: dict[str, Any]) -> int | None:
"""Fin de partie en secondes Unix, à partir des horodatages Riot (millisecondes)."""
if end := info.get("gameEndTimestamp"):
return end // 1000
if start := info.get("gameStartTimestamp"):
return start // 1000 + info.get("gameDuration", 0)
return None
def match_sort_key(match_id: str) -> int:
"""Clé de tri chronologique d'un match ID (format REGION_NUMERO)."""
try:
return int(match_id.rsplit("_", 1)[1])
except (IndexError, ValueError):
return 0