Skip to content

Commit

Permalink
config: Add cvar for maximum amount of team changes
Browse files Browse the repository at this point in the history
The amount of team changes for a player are remembered
and checked against the cvar:

* Player team change count > maximum:
        - client command 'jointeam' blocked entirely

* Player team change count < maximum:
        - increment player team change count
	- respawn after respawn delay
	- allow client command 'jointeam'

Player team change counts are cleared each new round or map change.

This commit also fixes the issue with not being able to join a team in
the middle of a round.

Note: The first time a player joins a team on the server is not remembered.

Signed-off-by: BackRaw <[email protected]>
  • Loading branch information
BackRaw committed Nov 21, 2017
1 parent 2f5b5c2 commit 74b9bf6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
6 changes: 6 additions & 0 deletions addons/source-python/plugins/udm/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@
"The minimum distance players have to have between a spawn point for it to be 'safe'."
)

# The maximum amount of times a players are allowed to change their team per round.
cvar_team_changes_per_round = config.cvar(
'team_changes_per_round', 2,
'The maximum amount of times a players are allowed to change their team per round.'
)

config.text('----------------------------------')
config.text(' * Chat Commands')
config.text('----------------------------------')
Expand Down
31 changes: 31 additions & 0 deletions addons/source-python/plugins/udm/players/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# >> IMPORTS
# =============================================================================
# Python Imports
# Collections
from collections import defaultdict
# Random
import random

Expand All @@ -17,6 +19,8 @@
from engines.server import global_vars
# Filters
from filters.players import PlayerIter
# Listeners
from listeners import OnLevelEnd
# Memory
from memory import make_object
# Messages
Expand All @@ -39,6 +43,13 @@
from udm.weapons import weapon_manager


# =============================================================================
# >> TEAM CHANGES
# =============================================================================
# Store team changes count for each player
team_changes = defaultdict(int)


# =============================================================================
# >> ALIVE PLAYERS GENERATOR
# =============================================================================
Expand Down Expand Up @@ -189,6 +200,17 @@ def spawn(self):
"""Always force spawn the player."""
super().spawn(True)

def set_team_changes(self, value):
"""Store `value` as the team change count for the player."""
team_changes[self.uniqueid] = value

def get_team_changes(self):
"""Return the team change count for the player."""
return team_changes[self.uniqueid]

# Set the `team_changes` property for PlayerEntity
team_changes = property(get_team_changes, set_team_changes)

@property
def inventories(self):
"""Return the player's inventories."""
Expand Down Expand Up @@ -239,3 +261,12 @@ def get_random_mode(self):

# Set the `random_mode` property for PlayerEntity
random_mode = property(get_random_mode, set_random_mode)


# =============================================================================
# >> LISTENERS
# =============================================================================
@OnLevelEnd
def on_level_end():
"""Clear the team change counts."""
team_changes.clear()
43 changes: 42 additions & 1 deletion addons/source-python/plugins/udm/udm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# =============================================================================
# Source.Python Imports
# Commands
from commands.client import ClientCommandFilter
from commands.typed import TypedSayCommand
# Core
from core import OutputReturn
Expand Down Expand Up @@ -42,13 +43,15 @@
from udm.config import cvar_saycommand_admin
from udm.config import cvar_saycommand_guns
from udm.config import cvar_spawn_protection_delay
from udm.config import cvar_team_changes_per_round
# Delays
from udm.delays import delay_manager
# Info
from udm.info import info
# Menus
from udm.weapons.menus import primary_menu
# Players
from udm.players import team_changes
from udm.players import PlayerEntity
# Spawn Points
from udm.spawnpoints import spawnpoints
Expand Down Expand Up @@ -184,8 +187,9 @@ def on_player_disconnect(event):

@Event('round_end')
def on_round_end(event):
"""Cancel all pending delays."""
"""Cancel all pending delays and team change counts."""
delay_manager.clear()
team_changes.clear()


@Event('weapon_reload')
Expand Down Expand Up @@ -272,6 +276,43 @@ def on_server_output(severity, msg):
return OutputReturn.CONTINUE


# =============================================================================
# >> CLIENT COMMAND FILTER
# =============================================================================
@ClientCommandFilter
def client_command_filter(command, index):
"""Spawn the player if the round has already started."""
# Allow the client command if it is not `jointeam`
if command[0] != 'jointeam':
return True

# Get a PlayerEntity instance for the player
player = PlayerEntity(index)

# Get the team the player wants to join
team_index = int(command[1])

# Allow spectators
if team_index < 2:
return True

# Allow the team change, if the player hasn't yet exceeded the maximum team change count
if player.team_changes < cvar_team_changes_per_round.get_int() + 1:
player.team = team_index

# Take a note of the team change
player.team_changes += 1

# Respawn the player after the respawn delay
delay_manager(f'respawn_{player.userid}', abs(cvar_respawn_delay.get_float()), player.spawn)

# Allow the client command
return True

# Block the client command, if the player has exceeded the maximum team change count
return False


# =============================================================================
# >> SAY COMMANDS
# =============================================================================
Expand Down

0 comments on commit 74b9bf6

Please sign in to comment.