Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Tracking stats using tables.

Tracking stats using tables.

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Glodan   (4 posts)  Bio
Date Sat 07 Dec 2013 05:24 AM (UTC)

Amended on Sat 07 Dec 2013 05:26 AM (UTC) by Glodan

Message
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?
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #1 on Sat 07 Dec 2013 10:16 PM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Glodan   (4 posts)  Bio
Date Reply #2 on Sat 15 Feb 2014 12:54 PM (UTC)

Amended on Sat 15 Feb 2014 10:05 PM (UTC) by Glodan

Message
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.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


13,960 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.