Change-Character-Attack protection

If chooser changes character too quickly, server just breaks down with
some error (but it not restarts, just does not let anyone in)

This protections kicks players what changes characters too quickly and
too much)
This commit is contained in:
Roman Trapeznikov 2020-05-07 22:35:53 +03:00
parent 233a4a4f78
commit dfb2ba4779

View File

@ -35,6 +35,10 @@ if TYPE_CHECKING:
import ba import ba
MAX_QUICK_CHANGE_COUNT = 30
QUICK_CHANGE_INTERVAL = 0.05
# Hmm should we move this to actors?.. # Hmm should we move this to actors?..
class JoinInfo: class JoinInfo:
"""Display useful info for joiners.""" """Display useful info for joiners."""
@ -169,6 +173,7 @@ class Chooser:
self._profilenames: List[str] = [] self._profilenames: List[str] = []
self._ready: bool = False self._ready: bool = False
self.character_names: List[str] = [] self.character_names: List[str] = []
self.last_change: Sequence[Union[float, count]] = (0, 0)
# Hmm does this need to be public? # Hmm does this need to be public?
self.profiles: Dict[str, Dict[str, Any]] = {} self.profiles: Dict[str, Dict[str, Any]] = {}
@ -663,6 +668,13 @@ class Chooser:
def handlemessage(self, msg: Any) -> Any: def handlemessage(self, msg: Any) -> Any:
"""Standard generic message handler.""" """Standard generic message handler."""
if isinstance(msg, ChangeMessage): if isinstance(msg, ChangeMessage):
now = ba.time()
count = self.last_change[1] + 1
if now - self.last_change[0] < QUICK_CHANGE_INTERVAL and count > MAX_QUICK_CHANGE_COUNT:
# Hmm maybe we should notify client?
_ba.disconnect_client(self._player.get_input_device().client_id)
self.last_change = (now, count)
0
# If we've been removed from the lobby, ignore this stuff. # If we've been removed from the lobby, ignore this stuff.
if self._dead: if self._dead: