Page 1 of 1

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

Posted: Sat Jul 11, 2020 6:45 am
by v1k1r
I need to check players in team and get "Total Players" and a single player data like "Player name " - "Player id"
Any ideas ?

delete

Posted: Thu Jul 16, 2020 8:15 pm
by hugo5511
delete

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

Posted: Fri Jul 17, 2020 11:05 am
by L'In20Cible
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.')

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

Posted: Sat Jul 18, 2020 10:40 am
by Speed0x
also

Syntax: Select all

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

could be useful

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

Posted: Sat Jul 18, 2020 11:16 am
by Sam

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

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

Posted: Sat Jul 18, 2020 11:33 am
by Sam
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?)

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

Posted: Sat Jul 18, 2020 11:36 am
by Speed0x
Sam wrote:from engines.server import server

yeah thank you. edited

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

Posted: Thu Jul 23, 2020 5:41 pm
by Ayuto
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

Posted: Thu Jul 23, 2020 8:17 pm
by satoon101
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]}')