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
9 changes: 1 addition & 8 deletions src/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,14 +505,7 @@ def _parse_decks(
if winloss and player_id in winloss:
result = winloss[player_id]
else:
if rank == 1:
result = "1st Place"
elif rank == 2:
result = "2nd Place"
elif rank == 3:
result = "3rd Place"
else:
result = f"{rank}th Place"
result = OrderNormalizer.format_place(rank)
rank += 1

deck = Deck(
Expand Down
18 changes: 10 additions & 8 deletions src/normalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ def combine_duplicates(input_items: List[DeckItem]) -> List[DeckItem]:


class OrderNormalizer:
@staticmethod
def format_place(position: int) -> str:
"""Format an integer rank into an ordinal place string (e.g. 1st Place, 32nd Place)."""
if 11 <= (position % 100) <= 13:
suffix = "th"
else:
suffix = {1: "st", 2: "nd", 3: "rd"}.get(position % 10, "th")
return f"{position}{suffix} Place"

@staticmethod
def reorder_decks(
decks: List[Deck],
Expand All @@ -51,14 +60,7 @@ def reorder_decks(
position += 1
continue

rank = f"{position}th Place"
if position == 1:
rank = "1st Place"
elif position == 2:
rank = "2nd Place"
elif position == 3:
rank = "3rd Place"

rank = OrderNormalizer.format_place(position)
position += 1

if update_result:
Expand Down
28 changes: 28 additions & 0 deletions tests/test_normalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,31 @@ def test_order_normalizer_playoff_bracket():

assert reordered[1].player == "Bob"
assert reordered[1].result == "2nd Place"


def test_order_normalizer_format_place():
test_cases = {
1: "1st Place",
2: "2nd Place",
3: "3rd Place",
4: "4th Place",
11: "11th Place",
12: "12th Place",
13: "13th Place",
14: "14th Place",
21: "21st Place",
22: "22nd Place",
23: "23rd Place",
31: "31st Place",
32: "32nd Place",
33: "33rd Place",
40: "40th Place",
101: "101st Place",
111: "111th Place",
112: "112th Place",
113: "113th Place",
122: "122nd Place",
133: "133rd Place",
}
for rank, expected in test_cases.items():
assert OrderNormalizer.format_place(rank) == expected
Loading