Tracking stats using tables.

Posted by Glodan on Sat 07 Dec 2013 05:24 AM — 3 posts, 14,769 views.

#0
Hi Guys, I'm fairly new to LUA, the help files and other forum posts have helped me for all my previous hurdles, but I'm in need of a little help for my current project. I'm looking at tracking combat stats, damage done, healing done, damage taken, etc. I've got a fairly solid system going at the moment, but it uses a crapload of variables. I'd really like to get it all into tables but I just don't seem to be having any luck setting it up.

Each attack action shows up in this format which is easy enough to filter through to grab all the relevant names/numbers.

<(Combatant)><(Attack Name)> (Target) takes (Amount) damage!

(Taken from http://www.gammon.com.au/forum/?id=6036)
-----
t = { 

  dwarf = { str = 22, dex = 23, wis = 18 },
  human = { str = 20, dex = 20, wis = 20 },
  elf   = { str = 18, dex = 24, wis = 25 },

}
-----

I think a table structured like the one above would be exactly what i need, such as:

-----
t = { 

  player1 = { damagedone = 111, damagehealed = 222, damagetaken = 333 },
  player2 = { damagedone = 111, damagehealed = 222, damagetaken = 333 },
  player3 = { damagedone = 111, damagehealed = 222, damagetaken = 333 },

}
-----

Ideally i'd like to get the list of players to be dynamic, set via looking at a in-game party list, and then having a blank slate of stats for each party member.

Anyone up for giving me a hand on getting a small framework done?
Amended on Sat 07 Dec 2013 05:26 AM by Glodan
Australia Forum Administrator #1
You just add new entries at runtime. eg


player = "foo"  -- for example

if t [player] then
  t [player].damagedone   = t [player].damagedone + 10
  t [player].damagehealed = t [player].damagehealed + 20
  t [player].damagetaken  = t [player].damagetaken + 30
else
  t [player] = { damagedone = 10, damagehealed = 20, damagetaken = 30 }
end -- if 


That code adds to existing player info if that player exists, otherwise it creates a new table entry.
#2
It's been a while but I've finally got back around to working on this. Nick, thanks, that worked perfectly. Here's what I've got so far:

Combat text format example:

<player1><power> player2 is healed for 100 damage!

My trigger:

^\<(.*)\>\<.*\> (.*) is healed for (.*) damage\!


combatant1 = "%1"
combatant2 = "%2"
cstat1 = tonumber (%3)

if cst [combatant1] then

  cst [combatant1].healsout = cst [combatant1].healsout + cstat1
else
  cst [combatant1] = {
      damageoutdealt = 0,
      damageoutraw = 0,
      healsout = cstat1,
      healsin = 0
   }
end

if cst [combatant2] then 
   cst [combatant2].healsin = cst [combatant2].healsin + cstat1
else
   cst [combatant2] = {
      damageoutdealt = 0,
      damageoutraw = 0,
      healsout = 0,
      healsin = cstat1,
   }
end -- if 


The list stats being tracked is much longer and working fine; I've got a series of triggers taking care of each stat. Using the example above, printing my cst table would look like this:

"player1":
 "damageoutdealt"=0
 "damageoutraw"=0
 "healsout"=100
 "healsin"=0
"player2":
 "damageoutdealt"=0
 "damageoutraw"=0
 "healsout"=0
 "healsin"=100


I can pluck individual values out ok, but what I'd really love to do now is generate post-combat reports such as the top 5 damage dealers.

--- edit ---

So, thinking out loud, I need a loop that:
1. goes through each combatant and pulls a particular stat 2. places the scanned stat into a new table
3. sort the new table
4. print the 5 highest entries

2-4 are straight forward, what I need help with is the syntax for loop.

Oh score. just tested:


for k, v in pairs (cst) do
  print (k, v.damageoutdealt)  
end 


It worked perfectly, printing a list of each combatant with the stat number.
I should be ok now.