1"""Guild rank roles: which Discord rank role a member's in-game guild rank calls for.
2
3The API reports the in-game rank names a member's characters hold on the Battle.net guild roster
4(``MemberLink.guild_ranks``, best first). Each name maps to one Discord rank role:
5
6* the role with the same name (case-insensitive): ``Adventurer`` -> **Adventurer**;
7* else the one role whose name starts with it, since the game cuts rank names at 15 characters:
8 ``Expedition Lead`` -> **Expedition Leader**;
9* else, for a rank ending in ``Alt`` whose stem is a rank (``Quest MasterAlt``), **Alternate Path**: an alt's rank says
10 whose alt it is, not what the player is, and their main's rank wins anyway.
11
12A name that matches nothing is ignored. The member gets the highest rank role any of their ranks maps to, and loses
13every other rank role. The three officer roles carry moderator permissions, so they're only granted from
14``battlenet_guild_ranks``: characters the member's own Battle.net account owns, which no addon upload or site link
15can fake. When a member's highest rank is an officer rank that only a site-linked character holds, the sync leaves
16their rank roles alone rather than grant it, or demote someone who really is an officer.
17"""
18
19from typing import TYPE_CHECKING
20
21if TYPE_CHECKING:
22 from collections.abc import Sequence
23
24RANK_ROLE_NAMES: tuple[str, ...] = (
25 "Quest Master",
26 "Quest Guide",
27 "Expedition Leader",
28 "Seasoned Adventurer",
29 "Adventurer",
30 "New Arrival",
31 "Alternate Path",
32)
33"""Every Discord guild rank role, highest first."""
34
35OFFICER_RANK_ROLE_NAMES = frozenset({"Quest Master", "Quest Guide", "Expedition Leader"})
36"""Rank roles with moderator permissions: granted only from Battle.net-owned characters."""
37
38ALT_RANK_ROLE_NAME = "Alternate Path"
39
40_ALT_SUFFIX = "alt"
41
42
43def _match(rank: str) -> str | None:
44 """The rank role ``rank`` names exactly or as a cut-off prefix, or None."""
45 key = rank.strip().casefold()
46 if not key:
47 return None
48 for name in RANK_ROLE_NAMES:
49 if name.casefold() == key:
50 return name
51 prefixed = [name for name in RANK_ROLE_NAMES if name.casefold().startswith(key)]
52 return prefixed[0] if len(prefixed) == 1 else None
53
54
[docs]
55def rank_role_name(rank: str) -> str | None:
56 """The Discord rank role for the in-game rank ``rank``, or None if it maps to none (see the module docstring)."""
57 if (name := _match(rank)) is not None:
58 return name
59 stem = rank.strip()
60 if stem.casefold().endswith(_ALT_SUFFIX) and _match(stem[: -len(_ALT_SUFFIX)]) is not None:
61 return ALT_RANK_ROLE_NAME
62 return None
63
64
[docs]
65def wanted_rank_role(ranks: Sequence[str], battlenet_ranks: Sequence[str]) -> str | None:
66 """The one rank role a member should hold, or None to leave their rank roles as they are.
67
68 ``ranks`` are all their characters' in-game ranks, ``battlenet_ranks`` those of the characters their Battle.net
69 account owns. None when no rank maps to a rank role, or when the highest is an officer role no Battle.net-owned
70 character's rank calls for.
71 """
72 candidates = {name for rank in ranks if (name := rank_role_name(rank)) is not None}
73 best = next((name for name in RANK_ROLE_NAMES if name in candidates), None)
74 if best in OFFICER_RANK_ROLE_NAMES and best not in {rank_role_name(rank) for rank in battlenet_ranks}:
75 return None
76 return best