Page 1 of 1

OnClientConnect getting Player problem

Posted: Thu Dec 01, 2016 10:32 pm
by nullable
Hi,

After update new version of sourcepython, we have a problem with getting player.

Syntax: Select all

@OnClientConnect
def on_client_connect(allow_connect_ptr, player_edict, player_name, ip_port, reject_msg_ptr, max_reject_length):
player = Player(index_from_edict(player_edict))



Traceback:

Syntax: Select all

Traceback (most recent call last):
File "../addons/source-python/plugins/automation/automation.py", line 246, in on_client_connect
player = Player(index_from_edict(edict))
File "../addons/source-python/packages/source-python/players/_base.py", line 86, in __init__
super().__init__(index)
File "../addons/source-python/packages/source-python/entities/entity.py", line 108, in __init__
super().__init__(index)

ValueError: Conversion from "Index" (2) to "BaseEntity" failed.


Please help us to fix this problem.

Re: OnClientConnect getting Player problem

Posted: Thu Dec 01, 2016 10:49 pm
by L'In20Cible
OnClientConnect is called before the instances are created and linked so getting a Player instance from there is not possible. Use OnClientActive instead.

Re: OnClientConnect getting Player problem

Posted: Thu Dec 01, 2016 11:39 pm
by sl0
tnx, but it worked when second arg was `index`, before https://github.com/Source-Python-Dev-Te ... 63bf9987fd
OnClientActive is not what we need, as i understand we can't drop connection from OnClientActive
We using `@OnClientConnect` listener for checking is player.steamid is allowed for connect to the server, if not - dropping
something like:

Syntax: Select all

@OnClientConnect
def on_client_connect(allow_connect_ptr, edict, player_name, ip_port, reject_msg_ptr, max_reject_length):
player = Player(index_from_edict(edict)) # before update it was Player(index_from_userid(index))
if player.steamid not in users:
allow_connect_ptr.set_bool(False)
reject_msg_ptr.set_string_array('Connect was blocked')

Re: OnClientConnect getting Player problem

Posted: Fri Dec 02, 2016 2:50 am
by L'In20Cible
sl0 wrote:tnx, but it worked when second arg was `index`, before https://github.com/Source-Python-Dev-Te ... 63bf9987fd

Well, it was simply because the listeners were not called if it was unable to get an index from the edict. As you can see there: https://github.com/Source-Python-Dev-Te ... n.cpp#L416

In short, it was not "working" but ignored the raised exceptions. You can do it yourself:

Syntax: Select all

@OnClientConnect
def on_client_connect(allow_connect_ptr, edict, player_name, ip_port, reject_msg_ptr, max_reject_length):
try:
index = index_from_edict(edict)
except ValueError:
return
player = Player(index) # before update it was Player(index_from_userid(index))
if player.steamid not in users:
allow_connect_ptr.set_bool(False)
reject_msg_ptr.set_string_array('Connect was blocked')


But it won't kick anyone as it will always exit the function. As you can see in the traceback you posted above, the exception is raised by the BaseEntity class initialization that cannot find the instance matching the given index (being not created at this point). Even tho you was able to get the Player instance, the steamid attribute would returns STEAM_ID_PENDING.

sl0 wrote:OnClientActive is not what we need, as i understand we can't drop connection from OnClientActive
We using `@OnClientConnect` listener for checking is player.steamid is allowed for connect to the server, if not - dropping
something like:

Syntax: Select all

@OnClientConnect
def on_client_connect(allow_connect_ptr, edict, player_name, ip_port, reject_msg_ptr, max_reject_length):
player = Player(index_from_edict(edict)) # before update it was Player(index_from_userid(index))
if player.steamid not in users:
allow_connect_ptr.set_bool(False)
reject_msg_ptr.set_string_array('Connect was blocked')
Ayuto posted a nice snippet couple months ago: viewtopic.php?p=7955#p7955

Re: OnClientConnect getting Player problem

Posted: Fri Dec 02, 2016 9:41 am
by sl0
tnx, your right
i forget that we have also @Event('player_connect') in our code for do same thing - kick user if it is not allowed