Lua Script: number of connected players


(acQu) #1

Hey,

i have worked today on an exploit fix in Lua. During this i came across a task to count number of connected players at the server. I found no direct field to do this in Lua. Also, my task went somewhat a different road, namely to count the number of players on each team.

Here is my solution:


function getTeamInfo()
	local temp = et.trap_GetConfigstring( 0 )
	temp = et.Info_ValueForKey( temp, "P" )

	local team_free_cnt, team_ax_cnt, team_al_cnt, team_spec_cnt = 0, 0, 0, 0

	for i = 1, #temp do
		if (string.sub( temp, i, i ) == "0" ) then
			team_free_cnt = team_free_cnt + 1
		end
		if (string.sub( temp, i, i ) == "1" ) then
			team_ax_cnt = team_ax_cnt + 1
		end
		if (string.sub( temp, i, i ) == "2" ) then
			team_al_cnt = team_al_cnt + 1
		end
		if (string.sub( temp, i, i ) == "3" ) then
			team_spec_cnt = team_spec_cnt + 1
		end
	end

	return team_free_cnt, team_ax_cnt, team_al_cnt, team_spec_cnt
end

As you can see, i use the P info var from the server. Do you happen to know a better way?

Otherwise, feel free to use this script. It counts the number of players on each team. In ET, there are 4 teams actually:

typedef enum {
TEAM_FREE,
TEAM_AXIS,
TEAM_ALLIES,
TEAM_SPECTATOR,

TEAM_NUM_TEAMS

} team_t;

The script above does count the number of players in each team. You can rewrite it to something like this

function CheckActivePlayers()
	local temp = et.trap_GetConfigstring( 0 )
	temp = et.Info_ValueForKey( temp, "P" )

	local team_ax_cnt, team_al_cnt = 0, 0

	for i = 1, #temp do
		if (string.sub( temp, i, i ) == "1" ) then
			team_ax_cnt = team_ax_cnt + 1
		end
		if (string.sub( temp, i, i ) == "2" ) then
			team_al_cnt = team_al_cnt + 1
		end
	end

	if team_ax_cnt > 0 or team_al_cnt > 0 then
		return 0
	end

	return 1
end

… to check if there is a player currently in team allies or in team axis.

So yeah, both a giveaway and a question in one thread. Hope someone has a workaround to not work with the serverinfo P stuff …


(Micha) #2

You could use something like the following. I think there are more ways to do it.
You could make a counter on clientconnect and clientdisconnect.


function countPlayers()
for i=0, mclients-1, 1 do
	local teamnum = et.gentity_get(i, "sess.sessionTeam")
		if teamnum == 1 or teamnum == 2 then
			if et.gentity_get(i,"inuse") then		
			playercountready = playercountready + 1
			end
		end
   	end
end

or



function et_RunFrame( levelTime )
countallies = 0
countaxis = 0
countspecs = 0
    for i = 0, tonumber( et.trap_Cvar_Get( "sv_maxClients" ) ) - 1 do
        if checkteam(i) == 1 then
            countaxis = countaxis + 1
        elseif checkteam(i) == 2 then    
            countallies =  countallies + 1
        elseif checkteam(i) == 3 then    
            countspecs =  countspecs + 1
        end
   end
end

et.CS_PLAYERS = 689

function checkteam(client)
   local cs = et.trap_GetConfigstring(et.CS_PLAYERS + client)
    return tonumber(et.Info_ValueForKey(cs, "t"))
end


(Mateos) #3

Is there a switch instruction in LUA? It could shorten the code, and as in some languages it is more optimized than if/else if, it would be maybe better to use, even if there’s not a lot of possibilities here


(Radegast) #4

Acqu, may I ask which exploit it is that you are trying to fix?


(acQu) #5

PM sent.

@Micha: yo, many ways to do it. Still, i wonder if there is a much much better way.

@Mateos: interesting question. I would need to read some manuals to give a proper answer, but too lazy currently :stuck_out_tongue: And i have not read a switch statement in Lua so far, so i think it is not in. After all, the code lands in Lua itself, which may decide what to do.

==============================
Consolidating quatuple-post:

Done.

@Micha: i ended up not being able to make use of any of the scripts above, because i discovered something which makes counting the number of active players pretty much useless. Anyway, thanks for the replies.

I filed a couple of PMs about this exploit, including a Lua fix for it and explanations. Of course i am not going to tell how it is done Sooner or later there will probably be some updated combinedfixes.lua or something, don’t really know yet, or how it is done. Will see, will see. Until then one can use the Lua script, until mods catch up in fixing it in the C code.

Regards and thanks again
acqu

Oh, and thanks to gaoesa for telling about this. I think there is already a fix out there in mod code (instead of Lua) by the silent team.

Greetings
acqu


(acQu) #6

Done.

@Micha: i ended up not being able to make use of any of the scripts above, because i discovered something which makes counting the number of active players pretty much useless. Anyway, thanks for the replies.

I filed a couple of PMs about this exploit, including a Lua fix for it and explanations. Of course i am not going to tell how it is done :slight_smile: Sooner or later there will probably be some updated combinedfixes.lua or something, don’t really know yet, or how it is done. Will see, will see. Until then one can use the Lua script, until mods catch up in fixing it in the C code.

Regards and thanks again
acqu


(acQu) #7

Oh, and thanks to gaoesa for telling about this. I think there is already a fix out there in mod code (instead of Lua) by the silent team.

Greetings
acqu


(acQu) #8

I deleted some posts and consolidated it in the above post.

So Micha made a better script which obscures the fact how to reproduce this exploit and which fixes it. So i redirect to his script here then, and all the receivers of PMs i sent with my old script should use Michas script (still my script can also be used privately, it still works :)).

Regards
acqu


(Radegast) #9

Lua doesn’t have switches as such. You can mimic them using associative tables, but it looks horrible.

Thanks to acQu, Adawolf and Gaoesa for investigating this exploit. Current and older mod versions can use acQu/Micha’s lua script to protect themselves and there is a patch available for the forthcoming mod versions. And they say ET community is dying, hehe.


(Dragonji) #10

Why keeping it secret?


(acQu) #11

Because it’s fairly easy to reproduce. Also, mods need a bit more time to catch up. Mods without Lua and without update are still problematic.

FYI the symptoms: if your server is empty (overnight maybe) and you come back next morning and find it crashed for literally no reason, then it might this. If your mod at server supports Lua, then you can protect yourself until mods update. Without Lua support, and without any upcoming mod update (Jaymod) it looks pretty grim. Maybe a QMM plugin can be made, but i have no clue at all about that kind of stuff.

If you want more information, then you may ask Radegast in IRC, or Micha and gaoesa here via PM: they know everything. I have literally spammed this topic too much already, want to get off it lol

Cheers
acqu


(zbzero) #12

Can you please pm me with the some info about this exploit? im afraid to have problems in my server due to i use etpro.


(ETJump-Zero) #13

[QUOTE=acQu;487859]Because it’s fairly easy to reproduce. Also, mods need a bit more time to catch up. Mods without Lua and without update are still problematic.

FYI the symptoms: if your server is empty (overnight maybe) and you come back next morning and find it crashed for literally no reason, then it might this. If your mod at server supports Lua, then you can protect yourself until mods update. Without Lua support, and without any upcoming mod update (Jaymod) it looks pretty grim. Maybe a QMM plugin can be made, but i have no clue at all about that kind of stuff.

If you want more information, then you may ask Radegast in IRC, or Micha and gaoesa here via PM: they know everything. I have literally spammed this topic too much already, want to get off it lol

Cheers
acqu[/QUOTE]
Well if you keep it a secret it’ll be rather hard to fix. :stuck_out_tongue: But yeah I get your point.


(Micha) #14

It’s simple to fix but crashs server. Will be fixed with new globalcombined.lua