Skip to content

Commit

Permalink
Merge pull request #628 from EraOSBeta/master
Browse files Browse the repository at this point in the history
Added the reject_recently_left_players plugin into the base game
  • Loading branch information
efroemling authored Oct 27, 2023
2 parents c2f6bf9 + e0052db commit 5cd779f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
### Era0S
- Bug Fixer
- Modder
- Added a feature

### VinniTR
- Fixes

### Rikko
- Created the original "reject_recently_left_players" plugin
42 changes: 42 additions & 0 deletions src/assets/ba_data/python/bascenev1/_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""Defines base session class."""
from __future__ import annotations

import math
import weakref
import logging
from typing import TYPE_CHECKING
Expand All @@ -17,6 +18,8 @@

import bascenev1

TIMEOUT = 10


class Session:
"""Defines a high level series of bascenev1.Activity-es.
Expand Down Expand Up @@ -203,6 +206,10 @@ def __init__(
# Instantiate our session globals node which will apply its settings.
self._sessionglobalsnode = _bascenev1.newnode('sessionglobals')

self._players_on_wait: dict = {}
self._player_requested_identifiers: dict = {}
self._waitlist_timers: dict = {}

@property
def context(self) -> bascenev1.ContextRef:
"""A context-ref pointing at this activity."""
Expand Down Expand Up @@ -253,6 +260,26 @@ def on_player_request(self, player: bascenev1.SessionPlayer) -> bool:
)
return False

identifier = player.get_v1_account_id()
if identifier:
leave_time = self._players_on_wait.get(identifier)
if leave_time:
diff = str(math.ceil(TIMEOUT - babase.apptime() + leave_time))
_bascenev1.broadcastmessage(
babase.Lstr(
translate=(
'serverResponses',
'You can join in ${COUNT} seconds.',
),
subs=[('${COUNT}', diff)],
),
color=(1, 1, 0),
clients=[player.inputdevice.client_id],
transient=True,
)
return False
self._player_requested_identifiers[player.id] = identifier

_bascenev1.getsound('dripity').play()
return True

Expand All @@ -270,6 +297,15 @@ def on_player_leave(self, sessionplayer: bascenev1.SessionPlayer) -> None:

activity = self._activity_weak()

identifier = self._player_requested_identifiers.get(sessionplayer.id)
if identifier:
self._players_on_wait[identifier] = babase.apptime()
with babase.ContextRef.empty():
self._waitlist_timers[identifier] = babase.AppTimer(
TIMEOUT,
babase.Call(self._remove_player_from_waitlist, identifier),
)

if not sessionplayer.in_game:
# Ok, the player is still in the lobby; simply remove them.
with self.context:
Expand Down Expand Up @@ -770,3 +806,9 @@ def _add_chosen_player(
if pass_to_activity:
activity.add_player(sessionplayer)
return sessionplayer

def _remove_player_from_waitlist(self, identifier: str) -> None:
try:
self._players_on_wait.pop(identifier)
except KeyError:
pass

0 comments on commit 5cd779f

Please sign in to comment.