From 0a3964827c0e3f380b4d58343b79f2cf7a4758bc Mon Sep 17 00:00:00 2001 From: Nixotica Date: Thu, 24 Apr 2025 22:23:55 -0700 Subject: [PATCH] add get_rank to MatchResults --- .../objects/inbound/match_results.py | 33 ++++++- nadeo_event_api/test/objects/__init__.py | 0 .../test/objects/inbound/__init__.py | 0 .../objects/inbound/test_match_results.py | 85 +++++++++++++++++++ 4 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 nadeo_event_api/test/objects/__init__.py create mode 100644 nadeo_event_api/test/objects/inbound/__init__.py create mode 100644 nadeo_event_api/test/objects/inbound/test_match_results.py diff --git a/nadeo_event_api/src/nadeo_event_api/objects/inbound/match_results.py b/nadeo_event_api/src/nadeo_event_api/objects/inbound/match_results.py index c59f1be..0780d87 100644 --- a/nadeo_event_api/src/nadeo_event_api/objects/inbound/match_results.py +++ b/nadeo_event_api/src/nadeo_event_api/objects/inbound/match_results.py @@ -1,5 +1,5 @@ from dataclasses import dataclass -from typing import Any, Dict, List +from typing import Any, Dict, List, Optional """ Example: @@ -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 \ No newline at end of file diff --git a/nadeo_event_api/test/objects/__init__.py b/nadeo_event_api/test/objects/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nadeo_event_api/test/objects/inbound/__init__.py b/nadeo_event_api/test/objects/inbound/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nadeo_event_api/test/objects/inbound/test_match_results.py b/nadeo_event_api/test/objects/inbound/test_match_results.py new file mode 100644 index 0000000..da337de --- /dev/null +++ b/nadeo_event_api/test/objects/inbound/test_match_results.py @@ -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) \ No newline at end of file