SGT_BlueHawk
Member
+5|5575|U.S.A.
I'm looking to find someone who's managed a BF2 server. I want to create a quick modified version of the autobalance.py script. Is anyone familiar with this type of script?
EA4never
Member
+2|6246
Yes, but it might be helpful to know what you're trying to achieve.
SGT_BlueHawk
Member
+5|5575|U.S.A.
Sure, to make it very simple, autobalance will be to 1 (on). Now, I'd like to create a script that says "If this player connects (onPlayerConnect) then, move them to this team, and enFORCE it throughout the entire round." I have some bits and pieces from this stie, but it's not enough. I need someone to play with the code and give me some stuff to test. I don't know python that well to manage it and I only have a few days to get this working right. I need a light-bulb moment basically, lol.
FFLink
There is.
+1,380|6701|Devon, England
You want to force people on to the rounds they're given at the start? That's horrible.

I know when me and my mates join a server, we like to be on the same team, you know?

Why are you trying to implement that idea?
SGT_BlueHawk
Member
+5|5575|U.S.A.

FFLink13 wrote:

You want to force people on to the rounds they're given at the start? That's horrible.
Don't think you're quite understanding the thought here. This is for a tournament. Players can choose a force (a.k.a. team) to join, afterwhich they will be forced to stick with the same team. The tournament is not designed to find out who can kill the most (I'm anticipating your next question), but to decide which team has the best teamwork to overcome the opposing team. Therefore, I need some way of enforcing this either by rules or by force. I'd rather implement it by force. Again, this is not for "normal" play. If it was, yes I agree it would be horrible (for most players).
EA4never
Member
+2|6246
I think the PlayerChangeTeams(playerObject, humanHasSpawned) event should work. Just get the player's team and move him to the other team again. Will require some testing as I don't know if a forced switch fires up the event again. If it doesn't, the stuff below should be sufficient.

if playerObject.getTeam() == 1: playerObject.setTeam(2)
if playerObject.getTeam() == 2: playerObject.setTeam(1)

Or were the teams 0 and 1? Nah I think the version above is correct.
SGT_BlueHawk
Member
+5|5575|U.S.A.
Your code above looks like it's headed in the right direction, but what kind of statement am I going to need to say "if this player name starts with x put him on team 1" Take a look at this bit of code which is the from the BF2 Tactical Information Wiki. This particular script is checking a clantag and then connecting players to the appropriate team based on whether or not they are in that clan. Anyhow, let's not get confused. All I'm saying is I see the elements of what I need in this script. See if you can pick out what I mean. Here's a quote . . .

Remember: We need to enforce the rule, not just put the player on a team on connect like this script does. Just an fyi. Ok, the quote . . .
Auto TeamSet on Connect:

Open autobalance.py add after:

Code:

if team2 > team1:
                p.setTeam(1)
        else:
                p.setTeam(2)
this code part for only One Clan:

Code:

#change Team to 1 on Connect if Clanmember
name=p.getName()
if name.find(clantag)>-1: p.setTeam(1)
or this code part for multiple Clans:

Code:

#change Team to 1 on Connect if Clanmember
name=p.getName()
for clantag in clantags:
 if name.find(clantag)>-1: p.setTeam(1)
So, having seen this bit of code, are we looking at simply modifying the autobalance script? That's my guess. One last thought. When you sign in to BF2 with a clan prefix which can be set a logon, does the "clantag" check or just a check of the playerName look a the prefox as part of the whole name. In other words, are we just checking one variable, or is there some sort of "playerPrefix" variable we need to incorporate or concatinate to the playerName? Forgive the spelling right now, I want to get the answers first. I'll do some editing later. Thanks guys.

Last edited by SGT_BlueHawk (2009-02-28 14:16:56)

EA4never
Member
+2|6246
Why not just combine my code with the code you quoted? Put the player into a certain team on connect and then switch him back whenever he tries to switch. I'll go test some, will report back in a few mins.
SGT_BlueHawk
Member
+5|5575|U.S.A.

EA4never wrote:

Why not just combine my code with the code you quoted? Put the player into a certain team on connect and then switch him back whenever he tries to switch. I'll go test some, will report back in a few mins.
Sounds like a plan. Only problem is -- again, I'm not familiar enough with the code to slice and dice it. I know other languages well like PHP, JavaScript, etc. Therefore, I can pin-point what I need and stuff, but not familiar enough with Python yet to do that, so your testing will help a lot. Will wait for your reply.
EA4never
Member
+2|6246
OK, I think I got the alpha script ready.

1. Make sure to add the constants (change the string to whatever you need):

Code:

import bf2
import host
from bf2 import g_debug


USTAG="US"
MECTAG="MEC"
2. Move player to team depending on his name (clantag is part of the .getName; simply seperated by a space)

Code:

        if team2 > team1:
                p.setTeam(1)
        else:
                p.setTeam(2)
        name = p.getName()
        if name.find(USTAG)!=-1: p.setTeam(2)
        elif name.find(MECTAG)!=-1: p.setTeam(1)
        #else: kick player for having no tag?
3. Switch the player back whenever he tries to get into the other team.

Code:

                # checking to see if player is allowed to change teams
                team = p.getTeam()
                if team == 1: p.setTeam(2)
                elif team == 2: p.setTeam(1)
                else: print "error"
        
##        team1 = 0
##        team2 = 0
##        for tp in bf2.playerManager.getPlayers():    
##            if tp.getTeam() == 1: team1 += 1
##            else: team2 += 1
##        if abs(team1 - team2) > 1:
##            if p.getTeam() == 1: p.setTeam(2)
##            else: p.setTeam(1)
The script still lacks some fine-tuning as it checks both playername and clantag for the specific army related tag. Players also can disconnect, change their clantag and rejoin a running round.
SGT_BlueHawk
Member
+5|5575|U.S.A.
A few things to keep in mind: You do not need to worry about kicking any players for not having a tag name. I already have a separate script specifically designed to do this. Here's my question about your script. When are you checking to see whether a player is trying to move over to the other team? I don't see any events like "playerDies" or something of the sort. Cause players can do that, like switch when they die or suicide or something of th ssort. Anyhow, thanks for putting in the effort into this. A few more posts we should have this goood to go. This isn't that complicated of a deal, just need to ensure we cover any potential holes. I"m not at home right now, so I can't test it . But, I will in a few hours. Let me know if you decide to change it in light of my comments here. Again, I will go back over this thread and sick the spelling gremlins on all my posts. , Chhers!
EA4never
Member
+2|6246
The PlayerChangeTeams event occurs whenever a player manually moves to the other team, i.e. that case is covered by step 3 and the player is instantly moved back to the other team.
SGT_BlueHawk
Member
+5|5575|U.S.A.
Hmm, someting isn't quite right. I believe the server is moving me over to the correct server upon connection, but other than that, I'm easilly able to switch teams after dying. I have autobalacne = 1. Forgive me for posting the whole autobalance.py file, but to ensure I have it right, here it is. The two places I've edited are around line 7, and around line 40. You should be able to copy & paste it into a good text editor and take a look. See if you can find out a mistake I made. I did not include the text within the "##" as I assumed this was purely commented out? The reason I have "SGT" for the US team is I'm trying to test it using my name (just fyi).

Code:

# team autobalance system

import bf2
import host
from bf2 import g_debug

USTAG="SGT"
MECTAG="MEC"

def init():
    if g_debug: print 'initializing autobalance script'
    
    host.registerHandler('PlayerConnect', onPlayerConnect, 1)    
    host.registerHandler('PlayerDeath', onPlayerDeath)
    host.registerHandler('PlayerChangeTeams', onPlayerChangeTeams)



def onPlayerConnect(p):
    
    # dont teamswitch alive players, or they will have the wrong teams kit 
    if p.isAlive(): return
    
    # place player on the team with least players
    team1 = 0
    team2 = 0
    for tp in bf2.playerManager.getPlayers():
        if tp != p:
            if tp.getTeam() == 1: team1 += 1
            else: team2 += 1    
            
    team2 = team2 * bf2.serverSettings.getTeamRatioPercent() / 100.0        
    if team2 > team1:
        p.setTeam(1)
    else:
        p.setTeam(2)

        name = p.getName()
        if name.find(USTAG)!=-1: p.setTeam(2)
        elif name.find(MECTAG)!=-1: p.setTeam(1)

        # checking to see if player is allowed to change teams
        team = p.getTeam()
        if team == 1: p.setTeam(2)
        elif team == 2: p.setTeam(1)
        else: print "error"

def onPlayerDeath(p, vehicle):        
    
    if not bf2.serverSettings.getAutoBalanceTeam(): return
    
    if p.isSquadLeader(): return        
    if p.isCommander(): return    
    
    if (host.ss_getParam('gameMode') == "gpm_coop") and p.isAIPlayer():
        return
    
    # dont use autobalance when its suicide/changes team
    if p.getSuicide(): 
        p.setSuicide(0)
        return
    
    aiPlayerBalance = 0
    team1 = 0
    team2 = 0
    for tp in bf2.playerManager.getPlayers():    
        if tp.getTeam() == 1:
            team1 += 1
        else:
            team2 += 1
        if tp.isAIPlayer():
            aiPlayerBalance += 1
        else:
            aiPlayerBalance -= 1                    
    
    if host.sgl_getIsAIGame():
        if not (host.ss_getParam('gameMode') == "gpm_coop"):
            if not (aiPlayerBalance < 0):        
                if not p.isAIPlayer():
                    return
    
    team2 = team2 * bf2.serverSettings.getTeamRatioPercent() / 100.0

    if (p.getTeam() == 1):
        if (team2+1) < team1:
            p.setTeam(2)            
    elif (p.getTeam() == 2):
        if (team1+1) < team2:
            p.setTeam(1)            


        
def onPlayerChangeTeams(p, humanHasSpawned):
    
    if not bf2.serverSettings.getAutoBalanceTeam(): return

    # dont teamswitch alive players, or they will have the wrong teams kit 
    if p.isAlive(): return
    
    if host.sgl_getIsAIGame() and not (host.ss_getParam('gameMode') == "gpm_coop"):
        if humanHasSpawned: return
        if p.isAIPlayer(): return
            
        # handling aiplayer team change autobalance when round not started
        team = p.getTeam()    
        aiplayer = 0    
    
        for tp in bf2.playerManager.getPlayers():        
            if aiplayer == 0 and tp.getTeam() == team and tp.isAIPlayer(): 
                aiplayer = tp
                break
        
        if aiplayer: 
            if p.getTeam() == 1: aiplayer.setTeam(2)
            else: aiplayer.setTeam(1)
                                    
    else:
        # checking to see if player is allowed to change teams
        team1 = 0
        team2 = 0
        for tp in bf2.playerManager.getPlayers():    
            if tp.getTeam() == 1: team1 += 1
            else: team2 += 1
        if abs(team1 - team2) > 1:
            if p.getTeam() == 1: p.setTeam(2)
            else: p.setTeam(1)

Last edited by SGT_BlueHawk (2009-02-28 20:14:41)

EA4never
Member
+2|6246
OK my bad, posting just pieces won't work with python being so tab sensitive. Below is what I meant to say:

Code:

# team autobalance system

import bf2
import host
from bf2 import g_debug


USTAG="SGT"
MECTAG="MEC"


def init():
    if g_debug: print 'initializing autobalance script'
    
    host.registerHandler('PlayerConnect', onPlayerConnect, 1)    
    host.registerHandler('PlayerDeath', onPlayerDeath)
    host.registerHandler('PlayerChangeTeams', onPlayerChangeTeams)



def onPlayerConnect(p):
    
    # dont teamswitch alive players, or they will have the wrong teams kit 
    if p.isAlive(): return
    
    # place player on the team with least players
    team1 = 0
    team2 = 0
    for tp in bf2.playerManager.getPlayers():
        if tp != p:
            if tp.getTeam() == 1: team1 += 1
            else: team2 += 1    
            
    team2 = team2 * bf2.serverSettings.getTeamRatioPercent() / 100.0        
    if team2 > team1:
        p.setTeam(1)
    else:
        p.setTeam(2)
        
    name = p.getName()
    if name.find(USTAG)!=-1: p.setTeam(2)
    elif name.find(MECTAG)!=-1: p.setTeam(1)


        
def onPlayerDeath(p, vehicle):        
    
    if not bf2.serverSettings.getAutoBalanceTeam(): return
    
    if p.isSquadLeader(): return        
    if p.isCommander(): return    
    
    if (host.ss_getParam('gameMode') == "gpm_coop") and p.isAIPlayer():
        return
    
    # dont use autobalance when its suicide/changes team
    if p.getSuicide(): 
        p.setSuicide(0)
        return
    
    aiPlayerBalance = 0
    team1 = 0
    team2 = 0
    for tp in bf2.playerManager.getPlayers():    
        if tp.getTeam() == 1:
            team1 += 1
        else:
            team2 += 1
        if tp.isAIPlayer():
            aiPlayerBalance += 1
        else:
            aiPlayerBalance -= 1                    
    
    if host.sgl_getIsAIGame():
        if not (host.ss_getParam('gameMode') == "gpm_coop"):
            if not (aiPlayerBalance < 0):        
                if not p.isAIPlayer():
                    return
    
    team2 = team2 * bf2.serverSettings.getTeamRatioPercent() / 100.0

    if (p.getTeam() == 1):
        if (team2+1) < team1:
            p.setTeam(2)            
    elif (p.getTeam() == 2):
        if (team1+1) < team2:
            p.setTeam(1)            


        
def onPlayerChangeTeams(p, humanHasSpawned):
    
    if not bf2.serverSettings.getAutoBalanceTeam(): return

    # dont teamswitch alive players, or they will have the wrong teams kit 
    if p.isAlive(): return
    
    if host.sgl_getIsAIGame() and not (host.ss_getParam('gameMode') == "gpm_coop"):
        if humanHasSpawned: return
        if p.isAIPlayer(): return
            
        # handling aiplayer team change autobalance when round not started
        team = p.getTeam()    
        aiplayer = 0    
    
        for tp in bf2.playerManager.getPlayers():        
            if aiplayer == 0 and tp.getTeam() == team and tp.isAIPlayer(): 
                aiplayer = tp
                break
        
        if aiplayer: 
            if p.getTeam() == 1: aiplayer.setTeam(2)
            else: aiplayer.setTeam(1)
                                    
    else:
        # checking to see if player is allowed to change teams
        team = p.getTeam()
        if team == 1: p.setTeam(2)
        elif team == 2: p.setTeam(1)
SGT_BlueHawk
Member
+5|5575|U.S.A.
Hmm. Somehow the dots are not connecting, I have ab = 1 in my serversettings.con, copy & pasted the autobalance.py and still no go. I can freely change teams at will. This is so frustrating because I see this on other servers all the time . . . "You've been moved over by the server" or similar.

Did this actually work on a test run? Does the script only check for a prefix? Or just the beginning of the nametag? You can test it on my server (208.53.170.190), but I'll need to know your nametag first so I can pop it into the us or mec tags.
EA4never
Member
+2|6246
Well it worked in singleplayer for me with a prototype of the actual script, I'd be switched back instantly. The script checks the whole name (name = prefix + " " + actual name). I'll try to join using "MEC" as clantag.
SGT_BlueHawk
Member
+5|5575|U.S.A.
Wait a second... You're saying that the script checks the entire name? So, it will check the entire name for "USA" or "MEC"? Hmm, that sounds like it could be a problem. What if someone has "USAMEC" as a name? Let me do an example. So if let's say a nametag was . . .

USA_DudeMEC

Where would he get placed?
EA4never
Member
+2|6246
He'd be placed in the US team because the script first checks for the USTAG and the elif isn't run at all. However we're still in beta phase, I will fix it later, it's not so important as of now.

edit: I can't join your server, it says I was kicked by an admin or for tks.

Last edited by EA4never (2009-03-01 05:29:48)

SGT_BlueHawk
Member
+5|5575|U.S.A.
Totally, lol. Just curious. I see no messaging names in your profile, do you IRC, AIM, etc? If so, IM so, much faster. I plan on posting the good results here. Plus, we can utilize SSH, etc, much faster paced.

Update: Ahh, yes, my genius only members script doing it's duty smile. IM me.

Last edited by SGT_BlueHawk (2009-03-01 05:33:24)

Board footer

Privacy Policy - © 2024 Jeff Minard