Page 1 of 1
CSGO: check player is disconnected
Posted: Sun Apr 05, 2015 10:23 am
by nullable
I don't understand why my code doesn't work correct.
Syntax: Select all
@Event
def player_disconnect(game_event):
if len(list(PlayerIter('t'))) == 0 or len(list(PlayerIter('ct'))) == 0:
engine_server.server_command('killserver;')
After disconnected all people from one team len(list(PlayerIter('t'))) != 0 and len(list(PlayerIter('ct'))) != 0. How to resolve this problem?
Posted: Sun Apr 05, 2015 12:48 pm
by stonedegg
Maybe player_disconnect fires before the player is fully disconnected, so try to delay it a little bit.
Posted: Sun Apr 05, 2015 12:51 pm
by Ayuto
What exactly is the problem?
Posted: Sun Apr 05, 2015 1:05 pm
by stonedegg
Ayuto wrote:What exactly is the problem?
As I understood it, when all people disconnected from one team, "len(list(PlayerIter(<team>)))" is not 0 when player_disconnect event fires.
Posted: Sun Apr 05, 2015 2:01 pm
by Ayuto
Ahh, okay. Yes, the event player_disconnect is fired when a player is about to disconnect and not when he already disconnected. The reason behind that is simple: you can still access the player's attributes, functions, etc.
You can use this to work around that problem.
Syntax: Select all
from events import Event
from filters.players import PlayerIter
from players.entity import PlayerEntity
from players.helpers import index_from_userid
@Event
def player_disconnect(event):
player = PlayerEntity(index_from_userid(event.get_int('userid')))
t_count = len(list(PlayerIter('t')))
ct_count = len(list(PlayerIter('ct')))
if player.team == 2:
t_count -= 1
elif player.team == 3:
ct_count -= 1
if t_count == 0 or ct_count == 0:
engine_server.server_command('killserver;')
Posted: Sun Apr 05, 2015 2:09 pm
by L'In20Cible
Or even shorter ^^
Syntax: Select all
@Event
def player_disconnect(game_event):
# Are we the last player on the server?
if len(list(PlayerIter())) == 1:
engine_server.server_command('killserver;')
Posted: Sun Apr 05, 2015 2:11 pm
by Ayuto
Yeah, but you forgot to exclude unassigned players and spectators.

Posted: Sun Apr 05, 2015 2:26 pm
by L'In20Cible
Hm yeah, good point
Syntax: Select all
@Event
def player_disconnect(game_event):
# Are we the last player on the server?
if len(PlayerIter(['t', 'ct']) == 1:
engine_server.server_command('killserver;')
Posted: Sun Apr 05, 2015 2:37 pm
by Ayuto
That won't work. Imagine you have 2 CT players and 1 T player and now the T player leaves. Then the T team has no players and "killserver" should be executed, but your code won't do that.
I didn't thought about that in my previous answer to your code.
Posted: Sun Apr 05, 2015 2:59 pm
by L'In20Cible
I don't think that makes any sense to shutdown the server if there is still players playing. He either wanted to shut it down on empty, I guess. ^^
If no, then he should rather listen player_team so he knows when a team gets empty.
Posted: Sun Apr 05, 2015 6:09 pm
by satoon101
This will iterate over no players, as no player can be both a terrorist and a counter-terrorist. You should instead use:
Syntax: Select all
PlayerIter(not_filters=['un', 'spec'])
Posted: Sun Apr 05, 2015 6:29 pm
by L'In20Cible
Oh, I thought that player matching any of the is_filters were yielded.