I need to check players in team and get "Total Players" and a single player data like "Player name " - "Player id"
Any ideas ?
How to check how many players in team and get their values
How to check how many players in team and get their values
My English is not perfect, but i try :D
- L'In20Cible
- Project Leader
- Posts: 1536
- Joined: Sat Jul 14, 2012 9:29 pm
- Location: Québec
Re: How to check how many players in team and get their values
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.

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.')
Re: How to check how many players in team and get their values
also
could be useful
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.
Re: How to check how many players in team and get their values
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.
Re: How to check how many players in team and get their values
Speed0x wrote:alsoSyntax: 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
Reason: Original post version
Re: How to check how many players in team and get their values
Sam wrote:from engines.server import server
yeah thank you. edited
Re: How to check how many players in team and get their values
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])
Re: How to check how many players in team and get their values
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]}')
Return to “Plugin Development Support”
Who is online
Users browsing this forum: No registered users and 125 guests