Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Any, Dict, List
from typing import Any, Dict, List, Optional

"""
Example:
Expand Down Expand Up @@ -73,3 +73,34 @@ def from_dict(cls, data: Dict[str, Any]):
teams = [RankedTeam.from_dict(team) for team in data.get("teams", [])]

return cls(match_live_id, round_position, results, teams) # type: ignore

def get_rank(self, participant_id: str) -> Optional[int]:
"""Returns a player's rank for a match, defaulting to their team rank if it was a teams match.

Args:
participant_id (str): The player's tm account ID.

Returns:
Optional[int]: The player's rank, if they had results in the match.
"""
# If not teams match, get player's individual rank
if self.teams == []:
for result in self.results:
if result.participant == participant_id:
return result.rank

# If teams match, get player's team's rank
else:
player_team = None
for result in self.results:
if result.participant == participant_id:
player_team = result.team

if player_team is None:
return None

for team in self.teams:
if team.team == player_team:
return team.rank

return None
Empty file.
Empty file.
85 changes: 85 additions & 0 deletions nadeo_event_api/test/objects/inbound/test_match_results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import unittest

from src.nadeo_event_api.objects.inbound.match_results import MatchResults, RankedParticipant, RankedTeam


class TestMatchResults(unittest.TestCase):
def test_get_position(self):
ranked_participant_1 = RankedParticipant(
"tm_acc_1",
rank=1,
score=1,
zone=None,
team=None,
)
ranked_participant_2 = RankedParticipant(
"tm_acc_2",
rank=2,
score=0,
zone=None,
team=None,
)

solo_match_results = MatchResults(
match_live_id="test_match_1",
round_position=0,
results=[ranked_participant_1, ranked_participant_2],
teams=[]
)

self.assertEqual(solo_match_results.get_rank("tm_acc_1"), 1)
self.assertEqual(solo_match_results.get_rank("tm_acc_2"), 2)
self.assertEqual(solo_match_results.get_rank("tm_acc_3"), None)

ranked_participant_3 = RankedParticipant(
"tm_acc_3",
rank=3,
score=0,
zone=None,
team="Blue",
)
ranked_participant_4 = RankedParticipant(
"tm_acc_4",
rank=4,
score=0,
zone=None,
team="Blue",
)
ranked_participant_5 = RankedParticipant(
"tm_acc_5",
rank=5,
score=0,
zone=None,
team="Red",
)
ranked_participant_6 = RankedParticipant(
"tm_acc_6",
rank=6,
score=0,
zone=None,
team="Red",
)
ranked_team_blue = RankedTeam(
position=0,
team="Blue",
rank=1,
score=1,
)
ranked_team_red = RankedTeam(
position=1,
team="Red",
rank=2,
score=0,
)

team_match_results = MatchResults(
match_live_id="test_match_2",
round_position=0,
results=[ranked_participant_3, ranked_participant_4, ranked_participant_5, ranked_participant_6],
teams=[ranked_team_blue, ranked_team_red]
)

self.assertEqual(team_match_results.get_rank("tm_acc_3"), 1)
self.assertEqual(team_match_results.get_rank("tm_acc_4"), 1)
self.assertEqual(team_match_results.get_rank("tm_acc_5"), 2)
self.assertEqual(team_match_results.get_rank("tm_acc_6"), 2)