Skip to content

Commit

Permalink
Update 1.0.2
Browse files Browse the repository at this point in the history
- Added Idle Timestamp's
- Added get_client_idle_time(index, timestamp=False)
- Changed dict() to PlayerDictionary()
- Updated README.md
  • Loading branch information
Frag1337 committed May 22, 2017
1 parent e22e827 commit 36a3c6a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 35 deletions.
32 changes: 29 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
```


Expand All @@ -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))
```


Expand All @@ -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)))
```
Original file line number Diff line number Diff line change
@@ -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',
)
73 changes: 42 additions & 31 deletions addons/source-python/packages/custom/idle_manager/idle_manager.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -24,18 +26,18 @@
# >> GLOBAL VARIABLES
# =============================================================================
# Store all timers
_timers = dict()
_timers = PlayerDictionary(lambda index: None)

# Store all players
_players = dict()
_players = PlayerDictionary(lambda index: None)


# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
class OnClientIdle(ListenerManagerDecorator):
"""Register/unregister a ClientIdle listener."""

manager = ListenerManager()


Expand All @@ -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.'
)

Expand All @@ -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)


Expand All @@ -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)
Expand All @@ -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))

0 comments on commit 36a3c6a

Please sign in to comment.