Re: [HL2:DM] admin joinsound
Posted: Mon Jun 01, 2020 9:47 pm
Painkiller wrote:L'In20Cible wrote:- A message, the admin comes online and leaves again.
It is no longer in the chat.
The admin intro should only sound when joining, not after every mapchange.
It is defined in the admin.ini.
Untested:
Syntax: Select all
# ../addons/source-python/plugins/admin_sounds/admin_sounds.py
# ============================================================================
# >> IMPORTS
# ============================================================================
# Site-Packages Imports
# ConfigObj
from configobj import ConfigObj
# Path
from path import Path
# Source.Python Imports
# Engines
from engines.sound import Sound
# Listeners
from listeners import OnClientActive
from listeners import OnClientDisconnect
from listeners import OnLevelInit
from listeners.tick import Delay
# Messages
from messages import SayText2
# Players
from players.entity import Player
from players.helpers import index_from_userid
from players.helpers import index_from_steamid
from players.helpers import userid_from_index
# Translations
from translations.strings import LangStrings
# ============================================================================
# >> CONFIGURATION
# ============================================================================
# Delay in seconds before playing the sounds
DELAY = 35
# ============================================================================
# >> GLOBAL VARIABLES
# ============================================================================
# Get a set so that we don't handle events again on map changes
handled = set()
# ============================================================================
# >> CLASSES
# ============================================================================
class Admins(ConfigObj):
"""Class used to parse the admins.ini file."""
def _load(self, infile, configspec):
"""Loads the content of the admins.ini file."""
super()._load(infile, configspec)
# Loop through all the admins and their respective data
for admin, data in self.items():
# Loop through all events
for event in ['join', 'disconnect']:
# Overwrite the sound as a downloadable Sound instance
sound = data.get(event + '_sound')
if sound is not None:
data[event + '_sound'] = Sound(sound, download=True)
# Overwrite the message as a SayText2 instance
message = data.get(event + '_message')
if message is None:
continue
# Use dict.__setitem__ to bypass ConfigObj's override.
# Otherwise the assigned object would be a Section, because
# SayText2 is a subclass of dict.
dict.__setitem__(data, event + '_message',
SayText2(
LangStrings._replace_escaped_sequences(message)
)
)
admins = Admins(Path(__file__).parent / 'admins/admins.ini')
# ============================================================================
# >> FUNCTIONS
# ============================================================================
def handle_event(index, event):
"""Play the sound associated to this player."""
try:
player = Player(index)
except ValueError:
return
# Get the data associated to this player
data = admins.get(player.steamid.lstrip('[').rstrip(']'))
if data is None:
return
# Play the sound associated to this event
sound = data.get(event + '_sound')
if sound is not None:
if event != 'disconnect':
Delay(DELAY, sound.play)
else:
Delay(DELAY, delayed_disconnect,
(player.userid, player.steamid, sound.play)
)
# Send the message associated to this event
message = data.get(event + '_message')
if message is None:
return
# Send the message after the given delay
if event != 'disconnect':
Delay(DELAY, message.send)
else:
Delay(DELAY, delayed_disconnect,
(player.userid, player.steamid, message.send)
)
def delayed_disconnect(userid, steamid, function):
"""Handle disconnection delays."""
try:
# If this succeed, the player didn't really disconnect.
index_from_userid(userid)
except ValueError:
try:
# If this succeed, the player joined back (e.g. retry, etc.) so
# forget about it since we will be handling his connection.
index_from_steamid(steamid)
except ValueError:
function()
# ============================================================================
# >> LISTENERS
# ============================================================================
@OnLevelInit
def on_level_init(map_name):
"""Called when a new map starts."""
admins.reload()
@OnClientActive
def on_client_active(index):
"""Called when a client is active."""
# Get the player's userid
userid = userid_from_index(index)
# Exit the call if we already handled this player
if userid in handled:
return
# Handle the connection event for this player
handle_event(index, 'join')
# Mark the player as handled
handled.add(userid)
@OnClientDisconnect
def on_client_disconnect(index):
"""Called when a client is disconnecting."""
# Handle the disconnection event for this player
handle_event(index, 'disconnect')
Your admins.ini file must be in the following format:
Syntax: Select all
[U:1:23456789]
join_sound = "join.mp3"
join_message = "\x01Someone \x05joined the \x03server!"
disconnect_sound = "leave.mp3"
disconnect_message = "\x05Someone \x03left the \x01server!"