Thread Tools Display Modes
07-22-11, 04:16 PM   #1
htordeux
A Murloc Raider
Join Date: Apr 2007
Posts: 5
Table for health status raid member

hello
I've written a working table for health status raid member.
the table was tested many times in raid with no problems.

I've never learn any programming language... just read some codes of lua
I just submit here in case someone could tell me if we can improve the code to avoid "garbage"

Code:
function jps_RaidStatus(self)

	table.remove(jps.RaidStatus)
	local subgroup = nil
	
	local group_type
	group_type="raid";
	nps=1;
	npe=GetNumRaidMembers();
	if npe==0 then
	group_type="party"
	nps=0;
	npe=GetNumPartyMembers();
	end;

	for i=nps,npe do
		if i==0 then
		unit="player"
		else
		unit=group_type..i
		end
		
--name, rank, subgroup, level, class, fileName, zone, online, isDead, role, isML = GetRaidRosterInfo(raidIndex);
--Index of raid member between 1 and MAX_RAID_MEMBERS (40). If you specify an index that is out of bounds, the function returns nil.
--subgroup - Integer - The raid party this character is currently a member of. Raid subgroups are numbered as on the standard raid window.
--GetNumRaidMembers() returns number of players in your raid group, including yourself; or 0 if you are not in a raid group

		if GetNumRaidMembers()==0 then subgroup = unit else subgroup = select(3,GetRaidRosterInfo(i)) end
		jps.RaidStatus[unit] = {["name"] = UnitName(unit),["hp"] = UnitHealthMax(unit) - UnitHealth(unit),["hpct"] = UnitHealth(unit) / UnitHealthMax(unit),["index"] = subgroup}
	end
	
	local function sortMyTable(a,b) return jps.RaidStatus[a]["hp"] > jps.RaidStatus[b]["hp"] end 
   	
   	--for k,v in pairs(jps.RaidStatus) do 
	--	print(k,v.name,v.hp)
	--end
	
	local sortedKeys = {}
	for k,v in pairs(jps.RaidStatus) do table.insert(sortedKeys, k) end
	table.sort(sortedKeys, sortMyTable)

	--for i,k in ipairs(sortedKeys) do
    --	print(jps.RaidStatus[k].hp,jps.RaidStatus[k].name)
	--end
	
	TargetTable1 = sortedKeys[1]
	TargetTable2 = sortedKeys[2]
	TargetTable3 = sortedKeys[3]
	TargetTable4 = sortedKeys[4]
	TargetTable5 = sortedKeys[5]
	if (#sortedKeys)==1 then 
	TargetTable2=TargetTable1
	TargetTable3=TargetTable1
	TargetTable4=TargetTable1
	TargetTable5=TargetTable1
	end
	
if IsShiftKeyDown() then
print(jps.RaidStatus[TargetTable1].hp, jps.RaidStatus[TargetTable1].name, "Index =",jps.RaidStatus[TargetTable1].index)
print(jps.RaidStatus[TargetTable2].hp, jps.RaidStatus[TargetTable2].name, "Index =",jps.RaidStatus[TargetTable2].index)
print(jps.RaidStatus[TargetTable3].hp, jps.RaidStatus[TargetTable3].name, "Index =",jps.RaidStatus[TargetTable3].index)
print(jps.RaidStatus[TargetTable4].hp, jps.RaidStatus[TargetTable4].name, "Index =",jps.RaidStatus[TargetTable4].index)
else
print("Shift down please")
end
end
the function jps_RaidStatus is updated with event == "UNIT_HEALTH"
every 0.1 sec with OnUpdate(self,elapsed)

Last edited by htordeux : 07-22-11 at 04:23 PM.
  Reply With Quote
07-28-11, 05:03 AM   #2
Aftermathhqt
A Molten Giant
 
Aftermathhqt's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 784
Originally Posted by htordeux View Post
hello
I've written a working table for health status raid member.
the table was tested many times in raid with no problems.

I've never learn any programming language... just read some codes of lua
I just submit here in case someone could tell me if we can improve the code to avoid "garbage"

Code:
function jps_RaidStatus(self)

	table.remove(jps.RaidStatus)
	local subgroup = nil
	
	local group_type
	group_type="raid";
	nps=1;
	npe=GetNumRaidMembers();
	if npe==0 then
	group_type="party"
	nps=0;
	npe=GetNumPartyMembers();
	end;

	for i=nps,npe do
		if i==0 then
		unit="player"
		else
		unit=group_type..i
		end
		
--name, rank, subgroup, level, class, fileName, zone, online, isDead, role, isML = GetRaidRosterInfo(raidIndex);
--Index of raid member between 1 and MAX_RAID_MEMBERS (40). If you specify an index that is out of bounds, the function returns nil.
--subgroup - Integer - The raid party this character is currently a member of. Raid subgroups are numbered as on the standard raid window.
--GetNumRaidMembers() returns number of players in your raid group, including yourself; or 0 if you are not in a raid group

		if GetNumRaidMembers()==0 then subgroup = unit else subgroup = select(3,GetRaidRosterInfo(i)) end
		jps.RaidStatus[unit] = {["name"] = UnitName(unit),["hp"] = UnitHealthMax(unit) - UnitHealth(unit),["hpct"] = UnitHealth(unit) / UnitHealthMax(unit),["index"] = subgroup}
	end
	
	local function sortMyTable(a,b) return jps.RaidStatus[a]["hp"] > jps.RaidStatus[b]["hp"] end 
   	
   	--for k,v in pairs(jps.RaidStatus) do 
	--	print(k,v.name,v.hp)
	--end
	
	local sortedKeys = {}
	for k,v in pairs(jps.RaidStatus) do table.insert(sortedKeys, k) end
	table.sort(sortedKeys, sortMyTable)

	--for i,k in ipairs(sortedKeys) do
    --	print(jps.RaidStatus[k].hp,jps.RaidStatus[k].name)
	--end
	
	TargetTable1 = sortedKeys[1]
	TargetTable2 = sortedKeys[2]
	TargetTable3 = sortedKeys[3]
	TargetTable4 = sortedKeys[4]
	TargetTable5 = sortedKeys[5]
	if (#sortedKeys)==1 then 
	TargetTable2=TargetTable1
	TargetTable3=TargetTable1
	TargetTable4=TargetTable1
	TargetTable5=TargetTable1
	end
	
if IsShiftKeyDown() then
print(jps.RaidStatus[TargetTable1].hp, jps.RaidStatus[TargetTable1].name, "Index =",jps.RaidStatus[TargetTable1].index)
print(jps.RaidStatus[TargetTable2].hp, jps.RaidStatus[TargetTable2].name, "Index =",jps.RaidStatus[TargetTable2].index)
print(jps.RaidStatus[TargetTable3].hp, jps.RaidStatus[TargetTable3].name, "Index =",jps.RaidStatus[TargetTable3].index)
print(jps.RaidStatus[TargetTable4].hp, jps.RaidStatus[TargetTable4].name, "Index =",jps.RaidStatus[TargetTable4].index)
else
print("Shift down please")
end
end
the function jps_RaidStatus is updated with event == "UNIT_HEALTH"
every 0.1 sec with OnUpdate(self,elapsed)

Have you tried to do an own oUF layout? You won't be disspointed.
  Reply With Quote
07-28-11, 07:49 AM   #3
Ketho
A Pyroguard Emberseer
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,026
Originally Posted by htordeux View Post
I've written a working table for health status raid member.
the table was tested many times in raid with no problems.

I've never learn any programming language... just read some codes of lua
I just submit here in case someone could tell me if we can improve the code to avoid "garbage"
I'm not sure what exactly is happening in your code, but this is what I tried to make from it. You were also leaking some globals and not reusing tables

Hopefully other people will come up with more optimized code than this
Code:
local sortedKeys = {}
local TargetTable = {}

local function sortMyTable(a, b)
	return jps.RaidStatus[a]["hp"] > jps.RaidStatus[b]["hp"]
end 

-- what's actually getting passed as "self"?
-- it's not being used either way
function jps_RaidStatus(self)

	tremove(jps.RaidStatus) -- ?

	local numParty = GetNumPartyMembers()
	local numRaid = GetNumRaidMembers()
	local unit, subgroup

	for i = numRaid > 0 and 1 or 0, numRaid > 0 and numRaid or numParty do
		if i == 0 then
			unit = "player"
		else
			unit = (numRaid > 0 and "raid" or "party")..i
		end

		jps.RaidStatus[unit] = {
			["name"] = UnitName(unit),
			["hp"] = UnitHealthMax(unit) - UnitHealth(unit),
			["hpct"] = UnitHealth(unit) / UnitHealthMax(unit),
			["index"] = numRaid == 0 and unit or select(3, GetRaidRosterInfo(i)),
		}
	end

	wipe(sortedKeys)
	for k, v in pairs(jps.RaidStatus) do
		tinsert(sortedKeys, k)
	end
	sort(sortedKeys, sortMyTable)

	for i = 1, 5 do
		TargetTable[i] = #sortedKeys == 1 and sortedKeys[1] or sortedKeys[i]
	end	

	if IsShiftKeyDown() then
		for i = 1, 4 do
			local tt = jps.RaidStatus.TargetTable[i]
			print(tt.hp, tt.name, "Index = "..tt.index)
		end
	else
		print("Shift down please")
	end
end
Originally Posted by htordeux View Post
the function jps_RaidStatus is updated with event == "UNIT_HEALTH"
every 0.1 sec with OnUpdate(self,elapsed)
Can you explain a bit more on this?
Are you saying you are doing either one of the following, or combined?
  1. Registering for UNIT_HEALTH (without a trottle)
  2. Calling your function from a 0.1 sec throttled OnUpdate
Anyway, you might want to post that part of your code too, so other people can have a look at it ..

Last edited by Ketho : 07-28-11 at 09:33 AM.
  Reply With Quote
07-28-11, 01:44 PM   #4
Mischback
A Cobalt Mageweaver
 
Mischback's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 221
Originally Posted by Ketho View Post
Can you explain a bit more on this?
Are you saying you are doing either one of the following, or combined?
  1. Registering for UNIT_HEALTH (without a trottle)
  2. Calling your function from a 0.1 sec throttled OnUpdate
Anyway, you might want to post that part of your code too, so other people can have a look at it ..
Thanks Ketho, I was thinking about that, too...
I read through the API to get, what he was talking about...

Do you register your frame to an event (UNIT_HEALTH) or are you using an OnUpdate-mechanism. Both together just made no sense to me...
__________________
  Reply With Quote
07-31-11, 01:29 PM   #5
htordeux
A Murloc Raider
Join Date: Apr 2007
Posts: 5
Thanks for yours answers, I'll do some change , check if it works and submit it again
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Table for health status raid member


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off