Page 2 of 2

Re: [HL2:DM] admin joinsound

Posted: Mon Jun 01, 2020 9:47 pm
by L'In20Cible
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!"

Re: [HL2:DM] admin joinsound

Posted: Tue Jun 02, 2020 1:04 pm
by Painkiller
Still the same problem no chat message.
After the mapchange the sound comes again( first the disconect sound after the connect sound.

The first admin is inserted for testing according to your conditions


admin.ini

Code: Select all

[U:1:43907303]
join_sound = "connect/personal.mp3"
join_message = "\x01Someone \x05joined the \x03server!"
disconnect_sound = "connect/see you.mp3"
disconnect_message = "\x05Someone \x03left the \x01server!"

[U:1:52097818]
join_sound = "connect/sparrow.mp3"
disconnect_sound = "connect/servus.mp3"

[U:1:44494671]
join_sound = "connect/ikillyou.mp3"
disconnect_sound = "connect/servus.mp3"

[U:1:2324049]
join_sound = "connect/freaki.mp3"
disconnect_sound = "connect/servus.mp3"

[U:1:10701000]
join_sound = "connect/user.mp3"
disconnect_sound = "connect/servus.mp3"

[U:1:67195663]
join_sound = "connect/nihilanthneu.mp3"
disconnect_sound = "connect/servus.mp3"

[U:1:50814128]
join_sound = "connect/$killi1.mp3"
disconnect_sound = "connect/servus.mp3"

[U:1:118177691]
join_sound = "connect/baxxter.mp3"
disconnect_sound = "connect/servus.mp3"

[U:1:44023098]
join_sound = "connect/error911.mp3"
disconnect_sound = "connect/servus.mp3"



Code: Select all

Traceback (most recent call last):
  File "../addons/source-python/plugins/admin_sounds/admin_sounds.py", line 120, in on_client_active
    handle_event(index, 'join')
  File "../addons/source-python/plugins/admin_sounds/admin_sounds.py", line 100, in handle_event
    Delay(DELAY, message.send)

AttributeError: 'Section' object has no attribute 'send'


2020-06-02 15:02:40 - sp   -   EXCEPTION   
[SP] Caught an Exception:
Traceback (most recent call last):
  File "../addons/source-python/plugins/admin_sounds/admin_sounds.py", line 130, in on_client_disconnect
    handle_event(index, 'disconnect')
  File "../addons/source-python/plugins/admin_sounds/admin_sounds.py", line 100, in handle_event
    Delay(DELAY, message.send)

AttributeError: 'Section' object has no attribute 'send'

Re: [HL2:DM] admin joinsound

Posted: Wed Jun 03, 2020 1:44 am
by L'In20Cible
Painkiller wrote:

Code: Select all

AttributeError: 'Section' object has no attribute 'send'

This happens because SayText2 is a subclass of dict, so ConfigObj parses it as a Section. Passing directly through dict.__setitem__ should do the trick.

Painkiller wrote:Still the same problem no chat message.
After the mapchange the sound comes again( first the disconect sound after the connect sound.

I guess the problem is that the map changes triggers the player to disconnect before the map ends. I believe with some Ingeniousity we can simply check if the player is still on the server at the time of handling the sound/message.

Updated the code above (again, untested).

Re: [HL2:DM] admin joinsound

Posted: Wed Jun 03, 2020 1:54 pm
by Painkiller
It worked but after I adjusted the admins.ini.
Now comes a UTF8 error but I checked it with notepad++ it is UTF8


Code: Select all

[SP] Caught an Exception:
Traceback (most recent call last):
  File "../addons/source-python/packages/source-python/plugins/command.py", line 162, in load_plugin
    plugin = self.manager.load(plugin_name)
  File "../addons/source-python/packages/source-python/plugins/manager.py", line 194, in load
    plugin._load()
  File "../addons/source-python/packages/source-python/plugins/instance.py", line 74, in _load
    self.module = import_module(self.import_name)
  File "../addons/source-python/plugins/admin_sounds/admin_sounds.py", line 80, in <module>
    admins = Admins(Path(__file__).parent / 'admins/admins.ini')
  File "../addons/source-python/packages/site-packages/configobj.py", line 1229, in __init__
    self._load(infile, configspec)
  File "../addons/source-python/plugins/admin_sounds/admin_sounds.py", line 53, in _load
    super()._load(infile, configspec)
  File "../addons/source-python/packages/site-packages/configobj.py", line 1287, in _load
    content = self._handle_bom(content)
  File "../addons/source-python/packages/site-packages/configobj.py", line 1485, in _handle_bom
    return self._decode(infile, 'utf-8')
  File "../addons/source-python/packages/site-packages/configobj.py", line 1517, in _decode
    infile[i] = line.decode(encoding)

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe4 in position 104: invalid continuation byte


:frown:



admins.ini

Code: Select all

[U:1:43907303]
join_sound = "connect/personal.mp3"
join_message = "\x01ACHTUNG !!! \x05ACHTUNG MASTER CHIEF !!! \x03RocKs|Painkiller"
disconnect_sound = "connect/see you.mp3"
disconnect_message = "\x05ACHTUNG !!! \x03ACHTUNG MASTER CHIEF !!! \x01RocKs|Painkiller sagt See you later Aligator"

[U:1:52097818]
join_sound = "connect/sparrow.mp3"
join_message = "\x01ACHTUNG !!! \x05ACHTUNG ADMIN !!! \x03Jack is Back"
disconnect_sound = "connect/servus.mp3"
disconnect_message = "\x05ACHTUNG !!! \x03ACHTUNG ADMIN !!! \x01RocKs|Rumbledoom sagt Tschuess bis zum nächsten mal"

[U:1:44494671]
join_sound = "connect/ikillyou.mp3"
join_message = "\x01ACHTUNG !!! \x05ACHTUNG ADMIN !!! \x03RocKs|I_KILL_YOU"
disconnect_sound = "connect/servus.mp3"
disconnect_message = "\x05ACHTUNG !!! \x03ACHTUNG ADMIN !!! \x01RocKs|I_KILL_YOU sagt Tschuess bis zum nächsten mal"

[U:1:2324049]
join_sound = "connect/freaki.mp3"
join_message = "\x01ACHTUNG !!! \x05ACHTUNG ADMIN !!! \x03RocKs|Freaki"
disconnect_sound = "connect/servus.mp3"
disconnect_message = "\x05ACHTUNG !!! \x03ACHTUNG ADMIN !!! \x01RocKs|Freaki sagt Tschuess bis zum nächsten mal"

[U:1:10701000]
join_sound = "connect/user.mp3"
join_message = "\x01ACHTUNG !!! \x05ACHTUNG ADMIN !!! \x03!!! +++ PARTY ALARM +++ !!!"
disconnect_sound = "connect/servus.mp3"
disconnect_message = "\x05ACHTUNG !!! \x03ACHTUNG ADMIN !!! \x01RocKs|user sagt Tschuess bis zum nächsten mal"

[U:1:67195663]
join_sound = "connect/nihilanthneu.mp3"
join_message = "\x01ACHTUNG !!! \x05ACHTUNG ADMIN !!! \x03RocKs|NiHiLANTH"
disconnect_sound = "connect/servus.mp3"
disconnect_message = "\x05ACHTUNG !!! \x03ACHTUNG ADMIN !!! \x01RocKs|NiHiLANTH sagt Tschuess bis zum nächsten mal"

[U:1:50814128]
join_sound = "connect/$killi1.mp3"
join_message = "\x01ACHTUNG !!! \x05ACHTUNG ADMIN !!! \x03RocKs|$killi"
disconnect_sound = "connect/servus.mp3"
disconnect_message = "\x05ACHTUNG !!! \x03ACHTUNG ADMIN !!! \x01RocKs|$killi sagt Tschuess bis zum nächsten mal"

[U:1:118177691]
join_sound = "connect/baxxter.mp3"
join_message = "\x01ACHTUNG !!! \x05ACHTUNG ADMIN !!! \x03RocKs|Baxxter"
disconnect_sound = "connect/servus.mp3"
disconnect_message = "\x05ACHTUNG !!! \x03ACHTUNG ADMIN !!! \x01RocKs|Baxxter sagt Tschuess bis zum nächsten mal"

[U:1:44023098]
join_sound = "connect/error911.mp3"
join_message = "\x01ACHTUNG !!! \x05ACHTUNG ADMIN !!! \x03RocKs|error911"
disconnect_sound = "connect/servus.mp3"
disconnect_message = "\x05ACHTUNG !!! \x03ACHTUNG ADMIN !!! \x01RocKs|error911 sagt Tschuess bis zum nächsten mal"

Re: [HL2:DM] admin joinsound

Posted: Thu Jun 11, 2020 11:59 am
by L'In20Cible
Painkiller wrote:It worked but after I adjusted the admins.ini.
Now comes a UTF8 error but I checked it with notepad++ it is UTF8

The issue is definitely your file. From that traceback, I assume you saved it with a BOM.