How to check how many players in team and get their values

Please post any questions about developing your plugin here. Please use the search function before posting!
v1k1r
Junior Member
Posts: 6
Joined: Sat Jul 04, 2020 5:16 pm

How to check how many players in team and get their values

Postby v1k1r » Sat Jul 11, 2020 6:45 am

I need to check players in team and get "Total Players" and a single player data like "Player name " - "Player id"
Any ideas ?
My English is not perfect, but i try :D
User avatar
hugo5511
Junior Member
Posts: 25
Joined: Mon Jun 29, 2015 1:20 am

delete

Postby hugo5511 » Thu Jul 16, 2020 8:15 pm

delete
Last edited by hugo5511 on Fri Jul 17, 2020 1:59 pm, edited 1 time in total.
User avatar
L'In20Cible
Project Leader
Posts: 1366
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: How to check how many players in team and get their values

Postby L'In20Cible » Fri Jul 17, 2020 11:05 am

hugo5511 wrote:The command users and status in server console will return a lot of info on players, status, steam id ect if you need more info maybe post question in "plugin requests" with what other info you would like.

The Plugin Development Support is the right section for asking questions about how to achieve certain things with Source.Python. The Plugin Requests section would be to ask for a complete plugin entirely written by someone else. :smile:

v1k1r wrote:I need to check players in team and get "Total Players" and a single player data like "Player name " - "Player id"
Any ideas ?

I'm not entirely sure what you want to achieve, do you want to get the number of players per team? If so, the most efficient way would probably be to iterate over all players and increment counts based on any condition you may want. For example:

Syntax: Select all

from filters.players import PlayerIter

t = 0
ct = 0

# Loop through all players
for player in PlayerIter():

# Get the team of the player
team = player.team

# Is the player a terrorist?
if team == 2:

# Increment the terrorist count
t += 1

# Otherwise, is the player a counter-terrorist?
elif team == 3:

# Increment the counter-terrorist count
ct += 1

print(f'There are {t} terrorists and {ct} counter-terrorists.')
Speed0x
Member
Posts: 65
Joined: Sun Feb 05, 2017 4:55 pm

Re: How to check how many players in team and get their values

Postby Speed0x » Sat Jul 18, 2020 10:40 am

also

Syntax: Select all

engines.server import server
print(server.num_players,"total players")

could be useful
Last edited by Speed0x on Sat Jul 18, 2020 11:35 am, edited 1 time in total.
Sam
Senior Member
Posts: 100
Joined: Tue Jul 03, 2018 3:00 pm
Location: *DELETED*
Contact:

Re: How to check how many players in team and get their values

Postby Sam » Sat Jul 18, 2020 11:16 am

Syntax: Select all

from filters.players import PlayerIter
from commands.typed import TypedServerCommand
from core import console_message
from paths import GAME_PATH

GAME = GAME_PATH.name

@TypedServerCommand('total_players')
def GetTotalPlayers(inf, userid:int=0):
if GAME == 'tf':
total_players = PlayerIter()
if userid >= 0:
console_message('\n##############################################\n')
for player in total_players:
if userid == player.userid:
team = 'BLUE' if player.team == 3 else ('RED' if player.team == 2 else 'SPEC')
console_message(f'> Name: {player.name}\n-> UserID: {player.userid}\n-> Tean: {team}\n')
console_message('##############################################\n')
red_players = PlayerIter('red')
blue_players = PlayerIter('blue')
console_message('\n##############################################')
console_message(f'\nTotal Players: {len(total_players)}\n\n')
console_message(f'Red Players: {len(red_players)}\n')
for player in red_players:
console_message(f'> Name: {player.name}\n-> UserID: {player.userid}\n')
console_message('\n')
console_message(f'Blue Players: {len(blue_players)}\n')
for player in blue_players:
console_message(f'> Name: {player.name}\n-> UserID: {player.userid}\n')
console_message('##############################################\n')
elif GAME == 'cstrike':
total_players = PlayerIter()
ct_players = PlayerIter('ct')
t_players = PlayerIter('t')
console_message('##############################################')
console_message(f'\nTotal Players: {len(total_players)}\n\n')
console_message(f'Counter-Terrorist Players: {len(ct_players)}\n')
for player in ct_players:
console_message(f'> Name: {player.name}\n-> UserID: {player.userid}\n')
console_message('\n')
console_message(f'Terrorist Players: {len(t_players)}\n')
for player in t_players:
console_message(f'> Name: {player.name}\n-> UserID: {player.userid}\n')
console_message('##############################################\n')
else:
console_message(f'\nAm... This game is not support it.\n')
return
Last edited by Sam on Sat Jul 18, 2020 11:28 am, edited 1 time in total.
Sam
Senior Member
Posts: 100
Joined: Tue Jul 03, 2018 3:00 pm
Location: *DELETED*
Contact:

Re: How to check how many players in team and get their values

Postby Sam » Sat Jul 18, 2020 11:33 am

Speed0x wrote:also

Syntax: Select all

engines.server import Server
print(Server.num_players,"total players")

could be useful

Syntax: Select all

from engines.server import Server
print(f'Total players -> {Server.num_players}') # Result: Total players -> <property object at 0x1A50D810>
from engines.server import server
print(f'Total players -> {server.num_players}') # Result: Total players -> 0
print(f'Total players -> {server.num_clients}') # Result: Total players -> 4 (I understand that we also need bots?)
Last edited by Sam on Sat Jul 18, 2020 11:33 am, edited 1 time in total.
Reason: Original post version
Speed0x
Member
Posts: 65
Joined: Sun Feb 05, 2017 4:55 pm

Re: How to check how many players in team and get their values

Postby Speed0x » Sat Jul 18, 2020 11:36 am

Sam wrote:from engines.server import server

yeah thank you. edited
User avatar
Ayuto
Project Leader
Posts: 2102
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: How to check how many players in team and get their values

Postby Ayuto » Thu Jul 23, 2020 5:41 pm

I might be a little bit late to the party, but I would go with a dict to store all possible team counts. This works for every game -- the keys might just be a little bit different in other games.

Syntax: Select all

from filters.players import PlayerIter
from collections import defaultdict

def get_team_counts():
result = defaultdict(int)
for player in PlayerIter():
result[player.team] += 1

return result

# Test
counts = get_team_counts()

print('Un', counts[0])
print('Spec', counts[1])
print('T', counts[2])
print('CT', counts[3])
User avatar
satoon101
Project Leader
Posts: 2638
Joined: Sat Jul 07, 2012 1:59 am

Re: How to check how many players in team and get their values

Postby satoon101 » Thu Jul 23, 2020 8:17 pm

If you want to get the team names dynamically, you can always use the team_managers:

Syntax: Select all

from collections import defaultdict

from filters.entities import EntityIter
from filters.players import PlayerIter
from players.teams import team_managers

team_names = {}

for class_name in team_managers:
for entity in EntityIter(class_name):
team_names[entity.team] = entity.team_name


def get_team_counts():
result = defaultdict(int)
for player in PlayerIter():
result[player.team] += 1
return result

# Test
counts = get_team_counts()

for team_number, team_name in sorted(team_names.items()):
print(f'{team_name} - {counts[team_number]}')
Image

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 12 guests