diff --git a/README.md b/README.md index 3091183..4b5fa7a 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ from idle_manager import OnClientIdle @OnClientIdle def on_client_idle(index): - print("Index '{:d}' is now marked as Idle.".format(index)) + print('Index "{:d}" is now marked as Idle.'.format(index)) ``` @@ -28,7 +28,7 @@ from idle_manager import OnClientBack @OnClientBack def on_client_back(index): - print("Index '{:d}' is no longer marked as Idle.".format(index)) + print('Index "{:d}" is no longer marked as Idle.'.format(index)) ``` @@ -46,5 +46,31 @@ from players.helpers import index_from_userid @Event('player_say') def player_say(game_event): index = index_from_userid(game_event.get_int('userid')) - print("Idle Status for Index '{:d}': {}".format(index, is_client_idle(index))) + print('Idle Status for Index "{:d}": {}'.format(index, is_client_idle(index))) +``` + + +get_client_idle_time(index, timestamp=False) +-------------- + +Returns the Client Idle Time or Timestamp for the given Client Index. + +Returns None if the Client is not Idle. + + +```python +from idle_manager import get_client_idle_time + +from events import Event +from players.helpers import index_from_userid + +@Event('player_say') +def player_say(game_event): + index = index_from_userid(game_event.get_int('userid')) + print('Idle Time for Index "{:d}": {:d}'.format(index, get_client_idle_time(index))) + +@Event('player_jump') +def player_jump(game_event): + index = index_from_userid(game_event.get_int('userid')) + print('Idle Timestamp for Index "{:d}": {:d}'.format(index, get_client_idle_time(index, True))) ``` diff --git a/addons/source-python/packages/custom/idle_manager/__init__.py b/addons/source-python/packages/custom/idle_manager/__init__.py index 48dc3a1..6c45024 100644 --- a/addons/source-python/packages/custom/idle_manager/__init__.py +++ b/addons/source-python/packages/custom/idle_manager/__init__.py @@ -1,14 +1,16 @@ # ../idle_manager/__init__.py -"""Package that marks idle players.""" +"""Custom Package that detects idle players.""" # Custom Package from .idle_manager import OnClientIdle from .idle_manager import OnClientBack from .idle_manager import is_client_idle +from .idle_manager import get_client_idle_time __all__ = ( 'OnClientIdle', 'OnClientBack', 'is_client_idle', + 'get_client_idle_time', ) diff --git a/addons/source-python/packages/custom/idle_manager/idle_manager.py b/addons/source-python/packages/custom/idle_manager/idle_manager.py index 1acf17f..b83fafd 100644 --- a/addons/source-python/packages/custom/idle_manager/idle_manager.py +++ b/addons/source-python/packages/custom/idle_manager/idle_manager.py @@ -1,21 +1,23 @@ # ../idle_manager/idle_manager.py -"""Package that marks idle players.""" +"""Custom Package that detects idle players.""" # ============================================================================= # >> IMPORTS # ============================================================================= +# Python +from time import time as timestamp + # Source.Python +from players.dictionary import PlayerDictionary from config.manager import ConfigManager from listeners import OnClientActive -from listeners import OnClientDisconnect from listeners import OnButtonStateChanged -from listeners import OnClientSettingsChanged from commands.say import SayFilter from commands.client import ClientCommandFilter from commands.server import ServerCommand from filters.players import PlayerIter -from listeners.tick import Delay +from players.entity import Player from listeners import ListenerManager from listeners import ListenerManagerDecorator @@ -24,10 +26,10 @@ # >> GLOBAL VARIABLES # ============================================================================= # Store all timers -_timers = dict() +_timers = PlayerDictionary(lambda index: None) # Store all players -_players = dict() +_players = PlayerDictionary(lambda index: None) # ============================================================================= @@ -35,7 +37,7 @@ # ============================================================================= class OnClientIdle(ListenerManagerDecorator): """Register/unregister a ClientIdle listener.""" - + manager = ListenerManager() @@ -52,8 +54,8 @@ class OnClientBack(ListenerManagerDecorator): with ConfigManager('idle_manager', 'idle_') as config: # Create the time convar - idle_time = config.cvar(name='time', - default=60.0, + idle_time = config.cvar(name='time', + default=60.0, description='The Time before a Client is marked as Idle.' ) @@ -69,39 +71,28 @@ def load(): @OnClientActive def on_client_active(index): - _players[index] = False + # Initiate Player + _players[index] = {'status': False, 'timestamp': None} + # Set Idle Timer reset_client_idle_timer(index) -@OnClientDisconnect -def on_client_disconnect(index): - if index in _timers: - if _timers[index].running: - _timers[index].cancel() - del _timers[index] - - if index in _players: - del _players[index] - - @OnButtonStateChanged def on_button_state_changed(player, old_buttons, new_buttons): + # Reset Idle Timer reset_client_idle_timer(player.index) -@OnClientSettingsChanged -def on_client_settings_changed(index): - reset_client_idle_timer(index) - - @SayFilter def say_filter(command, index, team_only): + # Reset Idle Timer reset_client_idle_timer(index) @ClientCommandFilter def client_command_filter(command, index): + # Reset Idle Timer reset_client_idle_timer(index) @@ -110,19 +101,19 @@ def reset_client_idle_timer(index): if index in _timers and _timers[index].running: _timers[index].cancel() - if _players[index]: + if index in _players and _players[index]['status']: # Client is back from being Idle - _players[index] = False + _players[index] = {'status': False, 'timestamp': None} # Notify OnClientBack.manager.notify(index) - _timers[index] = Delay(idle_time.get_int(), set_client_idle, (index,)) + _timers[index] = Player(index).delay(idle_time.get_int(), set_client_idle, (index,)) def set_client_idle(index): # Client is now Idle - _players[index] = True + _players[index] = {'status': True, 'timestamp': timestamp()} # Notify OnClientIdle.manager.notify(index) @@ -136,6 +127,26 @@ def is_client_idle(index): :rtype: bool """ if index in _players: - return bool(_players[index]) + return bool(_players[index]['status']) + else: + raise ValueError('Invalid Client Index. (:d)'.format(index)) + + +def get_client_idle_time(index, timestamp=False): + """ + Returns the Client Idle Time for the given Client Index. + + :param int index: The Client Index to check for. + :param bool timestamp: Bool if the Idle Timestamp should be returned. + :rtype: float + """ + if index in _players: + if _players[index]['status']: + if timestamp: + return float(_players[index]['timestamp']) + else: + return float(timestamp() - _players[index]['timestamp']) + else: + return None else: raise ValueError('Invalid Client Index. (:d)'.format(index))