Jump to content

Need help with gang manager


jkub

Recommended Posts

I just put my server up a couple of days ago and have been trying to work out all the bugs and implement scripts into my server. I have players in my server who want to be on a gang/team and I want to make an efficient system that handles creating the teams and setting those players on their teams according to their own account names.

I store the team names and account names of the members in an XML file.

I can do the scripting part where it gets the team names and creates the teams based on the XML.

I can't seem to do the part that puts a player on his right team though. Can you help me please.

Here is my LUA

teams = {}
 
function resStartHandler()
gangDatabase = xmlLoadFile ( "gangList.xml" )
if gangDatabase then
local teamsXml = xmlNodeGetChildren ( gangDatabase )
for index, gangNode in ipairs ( teamsXml ) do
		teamElement = xmlNodeGetValue ( gangNode )
if teamElement then
		    teams[index] = createTeam ( ""..teamElement.."", 255, 255, 255 )
else
outputChatBox ( "*Failed to get value of a specific node" )
end
end
else
outputChatBox ( "*Failed to load the ganglist database" )
end
end	
 
addEventHandler ( "onResourceStart", getResourceRootElement ( getThisResource() ), resStartHandler )

This is only the part that gets the names from the xml and it DOES seem to work.

For managing the player's teams I have tried looping through the teams table and checking a player's account name vs the account name of an attribute inside my xml but I seemed to fail. I don't have that code for that part anymore. I want to post my XML contents here but I don't know what tags to put it in?

Link to comment

I got a little bit further. I looked at the wiki long and hard and seemed to squeeze out a little bit.

The code below only loops through the first gang I have in my xml file. So therefore it only works partially. However I have 3 gangs in the xml file and I need it to loop through all 3.

teams = {}
 
function resStartHandler()
gangDatabase = xmlLoadFile ( "gangList.xml" )
if gangDatabase then
local teamsXml = xmlNodeGetChildren ( gangDatabase )
for index, gangNode in ipairs ( teamsXml ) do
		teamElement = xmlNodeGetValue ( gangNode )
if teamElement then
		    teams[index] = createTeam ( ""..teamElement.."", 255, 255, 255 )
else
outputChatBox ( "*Failed to get value of a specific node" )
end
end
if gangDatabase then xmlUnloadFile ( gangDatabase ) end
else
outputChatBox ( "*Failed to load the ganglist database" )
end
end	
 
function playerSpawnHandler()
gangDatabase = xmlLoadFile ( "gangList.xml" )
if gangDatabase then
local teamsXml = xmlNodeGetChildren ( gangDatabase )
for index, gangNode in ipairs ( teamsXml ) do
		teamElement = xmlNodeGetValue ( gangNode )
if teamElement then
local account = getPlayerAccount ( source )
if isGuestAccount ( account ) == false then
local accountName = getAccountName ( account )
outputChatBox ( teamElement )
				hax = xmlFindChild ( gangDatabase, "members", 0 )
if hax then
local attr = xmlNodeGetAttributes ( hax )
for name, value in pairs ( attr ) do
if accountName == value then setPlayerTeam ( source, getTeamFromName ( teamElement ) ) end
end
end
end
end
end
end
end
 
addEventHandler ( "onResourceStart", getResourceRootElement ( getThisResource() ), resStartHandler ) 
addEventHandler ( "onPlayerSpawn", getRootElement(), playerSpawnHandler )

Here is my XML. I don't really know what to put it in so I will just try some lua tags

<gangs>
 
<gang>Yakuzagang>
<members member1="noobz" member2="1233321" />
<skins skin1="169" skin2="186" skin3="187" skin4="150" />
<vehicles vehicle1="429" vehicle2="461" vehicle3="402" />
<weapons weapon1="29" weapon2="24" weapon3="5" weapon4="31" weapon5="1" />
 
<gang>Deadlinegang>
<members member1="[TMS]n3m3s1s" />
<skins skin1="114" skin2="115" skin3="116" />
<vehicles vehicle1="475" vehicle2="522" vehicle3="541" vehicle4="602" vehicle5="520"  />
<weapons weapon1="24" weapon2="30" weapon3="28" weapon4="34" />
 
<gang>Policegang>
<members member1="phil.jus91" member2="zeus" />
<skins skin1="280" skin2="281" skin3="282" skin4="283" skin5="284" skin6="285" skin7="288" />
<vehicles vehicle1="596" vehicle2="597" vehicle3="598" vehicle4="523" vehicle5="528" vehicle6="425" />
<weapons weapon1="24" weapon2="31" weapon3="3" weapon4="17" />
 
-- The team element goes in the gang node
<members member1="ACCOUNTNAME" member2="ACCOUNTNAME" member3="ACCOUNTNAME" member4="ACCOUNTNAME" /> The names of the accounts go here
<skins skin1="SKINID" skin2="SKINID" skin3="SKINID" skin4="SKINID" /> Skins for the gang go here
<vehicles vehicle1="VEHICLEID" vehicle2="VEHICLEID" vehicle3="VEHICLEID" vehicle4="VEHICLEID" /> Vehicle IDs for the gang go here
<weapons weapon1="WEAPONID" weapon2="WEAPONID" weapon3="WEAPONID" weapon4="WEAPONID" /> Weapon IDs for the gang go here -->
 
gangs>

Link to comment

What message do you see? "Failed to get.." or "Failed to load..." or maybe you get some warning/error messages in debug window?

I don't know who designed your gangs xml file but I know it looks like members, skins, vehicles, weapons and gang are all child of gangs. Your gangs node should have gang children only. Your gang nodes should have their own children nodes which would be members, skins, vehicles and weapons. This will be easier to read and easier to parse.

If you load a file, don't forget to unload it!

Link to comment

Thanks for the help n3m3s1s But I already explained on my server's forum that I made those outputChatBox functions when a player spawns so I could see that the gangs are actually found correctly. That is sort of how I debug sometimes so I could check and see if things actually exist.

But from those pictures you posted above you can see that it obviously only loops through the first gang's members.

==============================

@50p:

For my first function that creates the team I get no errors. It loops through and creates the team.

I made the XML myself and I of course am a noob at XML. I figured that it would go something like that. I'm not quite getting what your saying though. Can you show me how I should rewrite the XML or maybe a little example?

Link to comment

<gangs>
   <!-- GANG 1 -->
   <gang name="Gang name 1" >
       <weapons ids="1,2,3,4,5,6" />
       <skins ids="0,1,3,5,62,123" />
       <vehicles ids="461,429,402" />
       <members names="piglet,chicken" />
       <!-- for members you could do this but i'd recommend different children because player names may have commas (,) - do something like: -->
       <members>
           <member name="piglet" />
           <member name="chicken" />
       </members>
   </gang>
 
   <!-- GANG 2 -->
   <gang name="Gang name 2">
       <!-- GANG 2 DATA -->
   </gang>
</gangs>

g_Teams = { }; -- a global table for teams and their data
 
function initTeams( )
local gangsRoot = xmlLoadFile( "gangList.xml" );
for i, gangNode in ipairs( xmlNodeGetChildren( gangsRoot ) ) do -- loop all the  nodes
local team = { createTeam( xmlNodeGetAttribute( gangNode, "name" ), 255, 255, 255 ) }; -- create a team and store it in a table
for j, child in ipairs( xmlNodeGetChildren( gangNode ) ) do -- loop all the gang children: weapons, skins, etc.
local nodeName = xmlNodeGetName( child );
if nodeName == "weapons" then -- check if the node name is "weapons" if so, get the weapon ids from its "ids" attribute
local weaponsStr = xmlNodeGetAttribute( child, "ids" ); -- this will return ids as a string from "ids", like:  "1,2,3,4,5,6"
team.weapons = split( weaponsStr, string.byte( "," ) ); -- returns a table with all weapons in separate index of the table... check split func on wiki for more info
elseif nodeName == "skins" then
-- do exactly the same as for weapons
end
end
table.insert( g_Teams, team ); -- insert team table to the global teams table
end
end

I hope it will give you the information you asked for. If you still don't understand something, just say what and I'll try to explain it in more detail.

Link to comment

From what you provided I somewhat understand and was able to come up with a rather ludicrous way to loop through all members.

function playerSpawnCheck()
local account = getPlayerAccount ( source )
if isGuestAccount ( account ) == false then
local gangsRoot = xmlLoadFile ( "gangList.xml" )
for i, gangNode in ipairs ( xmlNodeGetChildren ( gangsRoot ) ) do
for j, child in ipairs ( xmlNodeGetChildren ( gangNode ) ) do
local nodeName = xmlNodeGetName( child );
--outputChatBox ( "hax1" )
if nodeName == "members" then
--outputChatBox ( "hax" )
				thisChild = xmlFindChild ( child, "member", 0 )
				thisChild2 = xmlFindChild ( child, "member", 1 )
				thisChild3 = xmlFindChild ( child, "member", 2 )
				thisChild4 = xmlFindChild ( child, "member", 3 )
if thisChild or thisChild2 or thisChild3 or thisChild4 then
local name = xmlNodeGetAttribute ( thisChild, "name" )
local name2 = xmlNodeGetAttribute ( thisChild2, "name" )
local name3 = xmlNodeGetAttribute ( thisChild3, "name" )
local name4 = xmlNodeGetAttribute ( thisChild4, "name" )
if name or name2 or name3 or name4 then
local myAccountName = getAccountName ( account )
if myAccountName == name then
setPlayerTeam ( source, getTeamFromName ( xmlNodeGetAttribute ( gangNode, "name" ) ) )
elseif myAccountName == name2 then
setPlayerTeam ( source, getTeamFromName ( xmlNodeGetAttribute ( gangNode, "name" ) ) )
elseif myAccountName == name3 then
setPlayerTeam ( source, getTeamFromName ( xmlNodeGetAttribute ( gangNode, "name" ) ) )
elseif myAccountName == name4 then
setPlayerTeam ( source, getTeamFromName ( xmlNodeGetAttribute ( gangNode, "name" ) ) )
else
--
end
 
else
outputChatBox ( "hax denied" )
end
else
outputChatBox ( "no hax" )
end
end
end
end
xmlUnloadFile ( gangsRoot )
end
end

You notice I have to manually ad the member1, member2 in. Is there another shorter more efficient way I can loop through any possible indexes?

Link to comment

Of course lol... Well I got a new code through a loop like you said. It seems to work perfectly :D

Here it is:

g_Teams = { }; -- a global table for teams and their data
 
function initTeams( )
local gangsRoot = xmlLoadFile( "gangList.xml" );
for i, gangNode in ipairs( xmlNodeGetChildren( gangsRoot ) ) do -- loop all the  nodes
team = { createTeam( xmlNodeGetAttribute( gangNode, "name" ), 255, 255, 255 ) }; -- create a team and store it in a table
for j, child in ipairs( xmlNodeGetChildren( gangNode ) ) do -- loop all the gang children: weapons, skins, etc.
local nodeName = xmlNodeGetName( child );
if nodeName == "weapons" then -- check if the node name is "weapons" if so, get the weapon ids from its "ids" attribute
local weaponsStr = xmlNodeGetAttribute( child, "ids" ); -- this will return ids as a string from "ids", like:  "1,2,3,4,5,6"
team.weapons = split( weaponsStr, string.byte( "," ) ); -- returns a table with all weapons in separate index of the table... check split func on wiki for more info
elseif nodeName == "skins" then
-- do exactly the same as for weapons
end
end
table.insert( g_Teams, team ); -- insert team table to the global teams table
end
end
 
function playerSpawnCheck()
local account = getPlayerAccount ( source )
if isGuestAccount ( account ) == false then
local gangsRoot = xmlLoadFile ( "gangList.xml" )
for i, gangNode in ipairs ( xmlNodeGetChildren ( gangsRoot ) ) do
for j, child in ipairs ( xmlNodeGetChildren ( gangNode ) ) do
for k, childNode in ipairs ( xmlNodeGetChildren ( child ) ) do
local memberName = xmlNodeGetAttribute ( childNode, "name" )
if memberName == false then
outputChatBox ( "*Error in finding name attribute in child node: Check the ganglist for mistakes" )
else
local myAccountName = getAccountName ( account )
if myAccountName == memberName then setPlayerTeam ( source, getTeamFromName ( xmlNodeGetAttribute ( gangNode, "name" ) ) ) end
end
end
end
end
xmlUnloadFile ( gangsRoot )
end
end
 
addEventHandler ( "onResourceStart", getResourceRootElement ( getThisResource()), initTeams )
addEventHandler ( "onPlayerSpawn", getRootElement(), playerSpawnCheck )

Thank you so much 50p!

Link to comment
  • 1 month later...

BUMP:

TBH after weeks of staring and referencing myself to MTAs wiki and the lua manual I didn't understand much of what you did 50p.

I was never really able to manipulate weapons, skins etc with it. I changed up the structure of the gangList a little bit and have snipped and rewrote a bit of the script.

With the current state of the system I am able to give the weapons to their gangs but if I try to spawn the player at their base I get a "c stack overflow" error in debug. I have researched this and I understand I am getting this error because it is causing an infinite loops aka a player first spawns and then the function spawns him again on and on and on...

I tried changing up the script a bit to where the person is spawned, given weapons and skins as soon as they log in but that failed.

You know of any other ways to do this without changing xml structure?

Here is my current gangList.xml

    
        
        
         
        
            
            
            
            
        
    
     
    
        
        
         
        
            
            
            
            
            
        
     
    
    

I grayed out skin and vehicles because I wanted to make sure the spawns and weapons worked first (which they do not at the same time due to the loop).

Here is my current lua

g_Teams = { }; -- a global table for teams and their data 
  
function initTeams( ) 
local gangsRoot = xmlLoadFile( "gangList.xml" ); 
    for i, gangNode in ipairs( xmlNodeGetChildren( gangsRoot ) ) do -- loop all the  nodes 
        team = { createTeam( xmlNodeGetAttribute( gangNode, "name" ), 255, 255, 255 ) }; -- create a team and store it in a table 
        table.insert( g_Teams, team ); -- insert team table to the global teams table 
    end 
end 
  
function playerSpawnCheck() 
    local account = getPlayerAccount ( source ) 
    if isGuestAccount ( account ) == false then 
        local gangsRoot = xmlLoadFile ( "gangList.xml" ) 
        for i, gangNode in ipairs ( xmlNodeGetChildren ( gangsRoot ) ) do 
            for j, child in ipairs ( xmlNodeGetChildren ( gangNode ) ) do 
                for k, childNode in ipairs ( xmlNodeGetChildren ( child ) ) do 
                    local memberName = xmlNodeGetAttribute ( childNode, "name" ) 
                    if memberName == false then 
                        outputChatBox ( "*Error in finding name attribute in child node: Check the ganglist for mistakes" ) 
                    else 
                        local myAccountName = getAccountName ( account ) 
                        if myAccountName == memberName then  
                            setPlayerTeam ( source, getTeamFromName ( xmlNodeGetAttribute ( gangNode, "name" ) ) ) 
                            ---- 
                            local gangName = xmlNodeGetAttribute ( gangNode, "name" ) 
                            local weaponNode = xmlFindChild ( gangNode, "weapons", 0 ) 
                            local spawnNode = xmlFindChild ( gangNode, "spawn", 0 ) 
                            local gangWeapons = xmlNodeGetAttributes ( weaponNode ) 
                            spawnX = xmlNodeGetAttribute ( spawnNode, "posX" ) 
                            spawnY = xmlNodeGetAttribute ( spawnNode, "posY" ) 
                            spawnZ = xmlNodeGetAttribute ( spawnNode, "posZ" ) 
                            rotation = xmlNodeGetAttribute ( spawnNode, "rot" ) 
                            ---- 
                            for k, v in pairs ( gangWeapons ) do 
                                addWeaponToInventory ( source, tonumber(v) )  
                            end 
                        end 
                    end 
                end 
            end 
        end 
        xmlUnloadFile ( gangsRoot ) 
    end 
    --spawnPlayer ( source, spawnX, spawnY, spawnZ, rotation ) 
end 
  
function addWeaponToInventory ( player, weapon ) 
    if weapon >= 0 and weapon <= 9 or weapon >= 10 and weapon <= 15 then 
        ammo = 0 
    elseif weapon == 22 or weapon == 23 then--colt45,silenced 
        ammo = 68--4 mags 
    elseif weapon == 24 then--deagle 
        ammo = 35--5 mags 
    elseif weapon == 25 then--shotgun 
        ammo = 24--24 shells 
    elseif weapon == 26 then--sawed off 
        ammo = 32--32 shells 
    elseif weapon == 27 then--combat shotty 
        ammo = 49--7 clips(49 shells) 
    elseif weapon == 28 or weapon == 32 then--Uzi,tec-9 
        ammo = 250 
    elseif weapon == 29 then--mp5 
        ammo = 150 
    elseif weapon == 30 then--ak 
        ammo = 320 
    elseif weapon == 31 then--m4 
        ammo = 250 
    elseif weapon == 33 then--rifle 
        ammo = 18 
    elseif weapon == 34 then--sniper 
        ammo = 16 
    elseif weapon == 16 or weapon == 17 or weapon == 18 or weapon == 39 then--projectiles (nades, molotovs, tear gas, satchels) 
        ammo = 6 
    end 
    giveWeapon ( player, weapon, ammo ) 
end 
  
addEventHandler ( "onResourceStart", getResourceRootElement ( getThisResource()), initTeams ) 
addEventHandler ( "onPlayerSpawn", getRootElement(), playerSpawnCheck ) 

I appreciate any help given.

Thanks

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...