Page 1 of 1

Conversion from "Userid" (3) to "Index" failed on server exit

Posted: Thu May 07, 2020 6:37 pm
by Pudge90
My plugin (for CS:S):

Syntax: Select all

from events import Event
from entities.entity import Entity
from players.entity import Player
from players.helpers import index_from_userid
from filters.entities import EntityIter
from messages import SayText2




def load():
SayText2('\x07Debug loaded!').send()

def unload():
SayText2('\x07Debug unloaded!').send()



@Event('player_disconnect')
def on_player_disconnect(game_event):
userid = game_event['userid']
print("UserID is: ", userid)
print("index for userid is: ", index_from_userid(userid))

When I disconnect "normally" then the code runs fine, no problems.
But If I enter 'exit' in the server console then the server disconnects the players to shut itself down.
So the 'player_diconnect' event gets triggered but an error is thrown:

Code: Select all

[SP] Caught an Exception:
Traceback (most recent call last):
  File "../addons/source-python/packages/source-python/events/listener.py", line 92, in fire_game_event
    callback(game_event)
  File "../addons/source-python/plugins/test/test.py", line 23, in on_player_disconnect
    print("index for userid is: ", index_from_userid(userid))

ValueError: Conversion from "Userid" (3) to "Index" failed.


My guess is that there are no player-objects anymore. Why is it working when disconnecting manually and not when the server exits ?

Also I cannot find where index_from_userid() is defined originally. i looked in here https://github.com/Source-Python-Dev-Team/Source.Python/blob/92b3adfaaaf1bb9ecd2be1ddad55bc68589b4152/addons/source-python/packages/source-python/players/helpers.py but it just links to _players._helpers and I don't know where to find that. if anyone knows where to find it, I'd appreciate it.

Re: Conversion from "Userid" (3) to "Index" failed on server exit

Posted: Thu May 07, 2020 6:45 pm
by L'In20Cible
That event is fired too late for you to grab an index out of that userid. Use OnClientDisconnect instead that is called with an index slightly before that event.

Pudge90 wrote:Also I cannot find where index_from_userid() is defined originally. i looked in here https://github.com/Source-Python-Dev-Te ... helpers.py but it just links to _players._helpers and I don't know where to find that. if anyone knows where to find it, I'd appreciate it.


https://github.com/Source-Python-Dev-Te ... ap.cpp#L58

Re: Conversion from "Userid" (3) to "Index" failed on server exit

Posted: Thu May 07, 2020 7:19 pm
by Pudge90
L'In20Cible wrote:That event is fired too late for you to grab an index out of that userid. Use OnClientDisconnect instead that is called with an index slightly before that event.


thanks, I tried that. When disconnecting manualy then this is the output:

Code: Select all

@@@@@Disconnecting Players Index is:  1
Game will not start until both teams have players.
Dropped Pudge90 from server (Disconnect by user.)
Server is hibernating


When entering 'exit' in the server console:

Code: Select all

exit
Dropped Pudge90 from server (Server shutting down)
[Source.Python] Unloading...
[Source.Python] Unloaded successfully.
Do 7. Mai 19:09:01 UTC 2020: Server Quit


plugin:

Syntax: Select all

from events import Event
from entities.entity import Entity
from players.entity import Player
from players.helpers import index_from_userid
from filters.entities import EntityIter
from messages import SayText2

from listeners import OnClientDisconnect


def load():
SayText2('\x07Debug loaded!').send()

def unload():
SayText2('\x07Debug unloaded!').send()



#@Event('player_disconnect')
#def on_player_disconnect(game_event):
# userid = game_event['userid']
# print("UserID is: ", userid)
# print("index for userid is: ", index_from_userid(userid))





@OnClientDisconnect
def on_client_disconnect(index):
print("@@@@@Disconnecting Players Index is: ", index)

So it looks like when the server exits then on_client_disconnect(index) is not called

Re: Conversion from "Userid" (3) to "Index" failed on server exit

Posted: Thu May 07, 2020 7:30 pm
by L'In20Cible
Then is is probably too late there as well and skipped here. Perhaps you could workaround by calling the listener yourself:

Syntax: Select all

from commands.server import ServerCommand
from filters.players import PlayerIter
from listeners import OnClientDisconnect
from listeners import on_client_disconnect_listener_manager
from players.entity import Player

@ServerCommand('quit', 'exit')
def exit(command):
for player in PlayerIter():
on_client_disconnect_listener_manager.notify(player.index)

@OnClientDisconnect
def on_client_disconnect(index):
print(f'{Player(index).name} left the server.')

Re: Conversion from "Userid" (3) to "Index" failed on server exit

Posted: Sat May 09, 2020 12:41 pm
by Pudge90
Thanks, adding a new listener with

Syntax: Select all

@ServerCommand('quit', 'exit')
works fine for me.

I also found viewtopic.php?f=20&t=2188 which is describing a similar problem, in case somebody else runs across the same problem.