Skip to content

Commit

Permalink
Return a dict for get_slots
Browse files Browse the repository at this point in the history
  • Loading branch information
cemathey committed May 29, 2024
1 parent d436388 commit 3b66c4e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
11 changes: 7 additions & 4 deletions rcon/rcon.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
ParsedLogsType,
PlayerIdsType,
ServerInfoType,
SlotsType,
StatusType,
StructuredLogLineType,
StructuredLogLineWithMetaData,
Expand Down Expand Up @@ -1002,19 +1003,21 @@ def set_broadcast(self, message: str) -> str:
return prev.decode() if prev else ""

@ttl_cache(ttl=5)
def get_slots(self) -> tuple[int, int]:
def get_slots(self) -> SlotsType:
"""Return the current number of connected players and max players allowed"""
res = super().get_slots()
if not self.slots_regexp.match(res):
raise CommandFailedError("Server returned crap")

current, max = tuple(map(int, res.split("/", maxsplit=1)))
return current, max
current_players, max_players = tuple(map(int, res.split("/", maxsplit=1)))
return {"current_players": current_players, "max_players": max_players}

@ttl_cache(ttl=5, cache_falsy=False)
def get_status(self) -> StatusType:
config = RconServerSettingsUserConfig.load_from_db()
current_players, max_players = self.get_slots()
slots = self.get_slots()
current_players = slots["current_players"]
max_players = slots["max_players"]
return {
"name": self.get_name(),
"map": self.current_map.model_dump(),
Expand Down
3 changes: 2 additions & 1 deletion rcon/stats_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def snapshot(self, rcon):
current_players, max_players = rcon.get_slots()
slots = rcon.get_slots()
current_players = slots["current_players"]

self.client.ts().add(self.NAME, "*", float(current_players))

Expand Down
5 changes: 5 additions & 0 deletions rcon/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,3 +694,8 @@ class PublicInfoType(TypedDict):
time_remaining: float
vote_status: VoteMapResultType | None
name: PublicInfoNameType


class SlotsType(TypedDict):
current_players: int
max_players: int
6 changes: 4 additions & 2 deletions rconweb/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ def get_public_info(request):

config = RconServerSettingsUserConfig.load_from_db()
gamestate = rcon_api.get_gamestate()
curr_players, max_players = rcon_api.get_slots()
slots = rcon_api.get_slots()
current_players = slots["current_players"]
max_players = slots["max_players"]

current_map: PublicInfoMapType = {
"map": gamestate["current_map"],
Expand Down Expand Up @@ -132,7 +134,7 @@ def get_public_info(request):
res: PublicInfoType = {
"current_map": current_map,
"next_map": next_map,
"player_count": curr_players,
"player_count": current_players,
"max_player_count": max_players,
"player_count_by_team": players,
"score": score,
Expand Down

0 comments on commit 3b66c4e

Please sign in to comment.