Using Tables to Create a Player database

Posted by Dpw3868 on Fri 04 Dec 2009 06:48 AM — 7 posts, 25,188 views.

#0
Hi, I am new to mushclient and using lua.

Not sure exactly where to start but i want to make an alias that allows me to add a players Name, class, and clan to a table.

I've been looking around and i guess it would look something like this

Alias playeradd * * *

-- playeradd Joe Mage BIGCLAN
player = player or {}

x = %1
player = %1 -- Joe ?

player[x].class = %2 -- Mage ?
player[x].clan = %3 -- BIGCLAN

Not sure if this is right, where i would put this in the program or anything like that.
Australia Forum Administrator #1
You need to decide if you want to "key" on the player name or not (probably you do, so you don't get two of the same name in your table). You really want a table of tables. eg.


players = players or {}  -- make players table if not already there

local name = "%1"

players [name:lower ()] = { class = "%2", clan = "%3" }


Note you need to quote the wildcards, as they are strings.

I have forced the name to lowercase to make sure that Joe and joe are stored in the same place.

The players table is the table of all players, and each entry has the player name as the key. For each player there is another table, which has two entries, class and clan.
#2
Thank you Nick for the helpful response.

I used the script you wrote and it works great.

I wrote another basic script using your tprint function to output the table.

I would to know use this information to highlight peoples names on the mud by the clan affiliation.

Is this possible and what should i read up on?
Australia Forum Administrator #3
This looks similar:

Template:post=9909
Please see the forum thread: http://gammon.com.au/forum/?id=9909.


or

Template:post=9903
Please see the forum thread: http://gammon.com.au/forum/?id=9903.
Amended on Sat 12 Dec 2009 12:08 AM by Nick Gammon
#4
I am now able to make the table, but How do i save the table from 1 session to the next?

Thanks

Dan
Australia Forum Administrator #5
Template:post=4960
Please see the forum thread: http://gammon.com.au/forum/?id=4960.
Australia Forum Administrator #6
Another approach, which might be simpler and possibly safer in case of program crashes, is to use SQLite (ie. a database). For some details and examples see:

http://www.gammon.com.au/db

and

http://www.gammon.com.au/sql

You could set up a database containing a table of players, clans etc., and add to that as required. Then you can use a SQL query to find players in a particular clan, or meeting certain requirements (eg. a level range).