CSGO: check player is disconnected

Please post any questions about developing your plugin here. Please use the search function before posting!
nullable
Senior Member
Posts: 137
Joined: Sat Nov 08, 2014 7:22 pm

CSGO: check player is disconnected

Postby nullable » Sun Apr 05, 2015 10:23 am

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?
stonedegg
Senior Member
Posts: 141
Joined: Sun Aug 12, 2012 11:45 am

Postby stonedegg » Sun Apr 05, 2015 12:48 pm

Maybe player_disconnect fires before the player is fully disconnected, so try to delay it a little bit.
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Sun Apr 05, 2015 12:51 pm

What exactly is the problem?
stonedegg
Senior Member
Posts: 141
Joined: Sun Aug 12, 2012 11:45 am

Postby stonedegg » Sun Apr 05, 2015 1:05 pm

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.
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Sun Apr 05, 2015 2:01 pm

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;')
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Sun Apr 05, 2015 2:09 pm

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;')
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Sun Apr 05, 2015 2:11 pm

Yeah, but you forgot to exclude unassigned players and spectators. :p
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Sun Apr 05, 2015 2:26 pm

Hm yeah, good point :smile:

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;')
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Sun Apr 05, 2015 2:37 pm

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.
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Sun Apr 05, 2015 2:59 pm

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.
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Sun Apr 05, 2015 6:09 pm

Syntax: Select all

PlayerIter(['t', 'ct'])

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'])
Image
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Sun Apr 05, 2015 6:29 pm

Oh, I thought that player matching any of the is_filters were yielded.

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 51 guests