[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Keeping track of items.

Keeping track of items.

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


Pages: 1 2  3  

Posted by Panaku   (34 posts)  [Biography] bio
Date Sun 24 Aug 2014 05:58 AM (UTC)
Message
Was gone for a while handing personal things, but I'm back and ready to get this working. I'm at the same point I was when I left off. There is an issue in the MUD I'm playing and guild/faction housing has been removed for the time being which means I have what is best described as 'bank' characters. I however have thousands of items spread across about a hundred characters with no easy way to keep track of who has what. So I'm trying to get a script working that will record what item is picked up or dropped and on which characters. Anyone have an suggestion?
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Sun 24 Aug 2014 06:10 AM (UTC)
Message
Are these logged on from the same "world" file?

Anyway, a SQLite3 database is probably the safest. See the pages about databases and SQL:

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

Depending on how "good" you want your data to be you could have one simple table, or a number of tables.

A simple table (effectively like a spreadsheet) could record something like:


  • Character name (that owns the item)
  • Item description
  • Quantity


That could be fine if you have an automated way of keeping track of things, otherwise you might record it once as "sword, bronze" and another time as "bronze sword". This may or may not matter.

Then a simple query could locate instances of (say) swords, and list who is holding them.

You would obviously want to keep track of when you got an item (eg. a mob drop), if you sold it, or if you transferred it.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Panaku   (34 posts)  [Biography] bio
Date Reply #2 on Sun 24 Aug 2014 06:24 AM (UTC)
Message
They're all from the same world file, there is about 50 characters on my account, each of them with a host of items being held for when needed. I don't care to much if its an overall table or individual ones for each character just as long as I don't have to log into each character in sequence and look over what they have every time I need something specific. I'll take a look at the links and see what I can come up with but I've never worked with databases before.
[Go to top] top

Posted by Panaku   (34 posts)  [Biography] bio
Date Reply #3 on Sun 24 Aug 2014 06:55 AM (UTC)
Message
<triggers>
  <trigger
   enabled="y"
   match="You get *."
   send_to="12"
   sequence="100"
  >
  <send>require "tprint"

DatabaseOpen ("db", GetInfo (66) .. "mytestdb.sqlite", 6)

rc = DatabaseExec ("db", [[
DROP TABLE IF EXISTS weapons;
CREATE TABLE weapons(
        weapon_id INTEGER NOT NULL PRIMARY KEY autoincrement,
        name  TEXT NOT NULL,
        damage INT default 10,
        weight REAL
      );
      ]])
      

-- put some data into the database
rc = DatabaseExec ("db", 
  [[
  INSERT INTO weapons (name, damage) VALUES ('%1', 70);
  ]])

-- prepare a query
DatabasePrepare ("db", "SELECT * from weapons ORDER BY name")

-- find the column names
names = DatabaseColumnNames ("db")
tprint (names)

-- execute to get the first row
rc = DatabaseStep ("db")  -- read first row

-- now loop, displaying each row, and getting the next one
while rc == 100 do
  
  print ("")
  values = DatabaseColumnValues ("db")
  tprint (values)

  rc = DatabaseStep ("db")  -- read next row

end -- while loop

-- finished with the statement
DatabaseFinalize ("db")

DatabaseClose ("db")  -- close it</send>
  </trigger>
</triggers>


Using that I'm having items I get added to the weapons database, and using this alias I'm calling the database
<aliases>
  <alias
   match="database"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>require "tprint"

DatabaseOpen ("db", GetInfo (66) .. "mytestdb.sqlite", 6)    

-- prepare a query
DatabasePrepare ("db", "SELECT * from weapons ORDER BY name")

-- find the column names
names = DatabaseColumnNames ("db")
tprint (names)

-- execute to get the first row
rc = DatabaseStep ("db")  -- read first row

-- now loop, displaying each row, and getting the next one
while rc == 100 do
  
  print ("")
  values = DatabaseColumnValues ("db")
  tprint (values)

  rc = DatabaseStep ("db")  -- read next row

end -- while loop

-- finished with the statement
DatabaseFinalize ("db")

DatabaseClose ("db")  -- close it</send>
  </alias>
</aliases>

Which is working, I've noticed the trigger is removing the table then recreating it, which isn't doing much good. But I'm only taking in so much so fast about how this is all working so I've yet to edit the example too much.
[Go to top] top

Posted by Panaku   (34 posts)  [Biography] bio
Date Reply #4 on Sun 24 Aug 2014 06:57 AM (UTC)
Message
I've completely removed the section --
rc = DatabaseExec ("db", [[
 DROP TABLE IF EXISTS weapons;
 CREATE TABLE weapons(
        weapon_id INTEGER NOT NULL PRIMARY KEY autoincrement,
        name  TEXT NOT NULL,
        damage INT default 10,
        weight REAL
      );
      ]])
      

and it's recording everything to the weapons table now as I wanted, just have to look how to remove weapons from the table when I drop them/sell them/give them away. Then I need to make it log which character an item is on.
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #5 on Sun 24 Aug 2014 07:07 AM (UTC)
Message
In your case only one table, I think, not one per character. I was really thinking of a table for characters, and a table for item names, but that is probably over-complicating it.

Stick with one table.

It might look like this:


CREATE TABLE items(
        item_id INTEGER NOT NULL PRIMARY KEY autoincrement,
        character_name TEXT NOT NULL,
        description  TEXT NOT NULL,
        quantity INT DEFAULT 1
      );


Then add (INSERT) a new item with the character name being the current toon, the description being the item, and the quantity being the number (if relevant).

To remove items use delete.

eg,


DELETE FROM items 
  WHERE character_name = 'Gandalf' 
  AND description = 'yellow wand';


Use this with caution because this would delete all yellow wands owned by Gandalf, not just one of them.

This can all be made to work but you might want to read up on SQL theory a bit, to understand better what you are doing.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Panaku   (34 posts)  [Biography] bio
Date Reply #6 on Sun 24 Aug 2014 07:29 AM (UTC)

Amended on Sun 24 Aug 2014 08:01 AM (UTC) by Panaku

Message
Modified the original trigger to this --
<triggers>
  <trigger
   enabled="y"
   expand_variables="y"
   match="You get *."
   send_to="12"
   sequence="100"
  >
  <send>require "tprint"

DatabaseOpen ("db", GetInfo (66) .. "mytestdb.sqlite", 6)

rc = DatabaseExec ("db", [[
 CREATE TABLE IF NOT EXISTS items(
        item_id INTEGER NOT NULL PRIMARY KEY autoincrement,
        character_name TEXT NOT NULL,
        description  TEXT NOT NULL,
        quantity INT DEFAULT 1
      );
      ]])

-- put some data into the database
rc = DatabaseExec ("db", 
  [[
  INSERT INTO items (character_name, description) VALUES ('@Current_Character', '%1');
  ]])

-- prepare a query
DatabasePrepare ("db", "SELECT * from items ORDER BY character_name")

-- find the column names
names = DatabaseColumnNames ("db")
tprint (names)

-- execute to get the first row
rc = DatabaseStep ("db")  -- read first row

-- now loop, displaying each row, and getting the next one
while rc == 100 do
  
  print ("")
  values = DatabaseColumnValues ("db")
  tprint (values)

  rc = DatabaseStep ("db")  -- read next row

end -- while loop

-- finished with the statement
DatabaseFinalize ("db")

DatabaseClose ("db")  -- close it</send>
  </trigger>
</triggers>


And the testing alias to --
<aliases>
  <alias
   match="database"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>require "tprint"

DatabaseOpen ("db", GetInfo (66) .. "mytestdb.sqlite", 6)

rc = DatabaseExec ("db", [[
 CREATE TABLE IF NOT EXISTS items(
        item_id INTEGER NOT NULL PRIMARY KEY autoincrement,
        character_name TEXT NOT NULL,
        description  TEXT NOT NULL,
        quantity INT DEFAULT 1
      );
      ]])

-- prepare a query
DatabasePrepare ("db", "SELECT * from items ORDER BY character_name")

-- find the column names
names = DatabaseColumnNames ("db")
tprint (names)

-- execute to get the first row
rc = DatabaseStep ("db")  -- read first row

-- now loop, displaying each row, and getting the next one
while rc == 100 do
  
  print ("")
  values = DatabaseColumnValues ("db")
  tprint (values)

  rc = DatabaseStep ("db")  -- read next row

end -- while loop

-- finished with the statement
DatabaseFinalize ("db")

DatabaseClose ("db")  -- close it</send>
  </alias>
</aliases>


This is now listing the database like so --
1=13
2="Artemis"
3="a small brass lantern"
4=1

1=14
2="Flynn"
3="a welding mask"
4=1


Would be able to list it in a easier to read fashion such as --

Character          Item
------------------------------------------
Artemis            a small brass lantern
Flynn              a welding mask
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #7 on Sun 24 Aug 2014 07:50 AM (UTC)
Message
You only have to create the database once, ever. So get rid of the CREATE TABLE.

Quote:

Would be able to list it in a easier to read fashion such as --


Well you have the data there, just display it the way you want to.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Panaku   (34 posts)  [Biography] bio
Date Reply #8 on Sun 24 Aug 2014 08:07 AM (UTC)
Message
I set all instance of the CREATE TABLE to "CREATE TABLE IF NOT EXISTS" for two reasons. One being I plan on giving this inventory tracker to the other players because they're in the same situation I'm in, ton of items and now where in game to store it. And they won't have the table or know how to create it themselves. The other reason being I have an alias set up the delete the table so I can do some debugging while I'm working everything out.

Is there another reason to remove the CREATE TABLE? My brain tells me if I do remove it then the database wouldn't ever work unless you manually made the table
[Go to top] top

Posted by Panaku   (34 posts)  [Biography] bio
Date Reply #9 on Sun 24 Aug 2014 09:15 AM (UTC)
Message
For testing purposes I've been using this alias to check the database --
<aliases>
  <alias
   match="database"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>require "tprint"

DatabaseOpen ("db", GetInfo (66) .. "InventoryDB.sqlite", 6)

rc = DatabaseExec ("db", [[
 CREATE TABLE IF NOT EXISTS items(
        item_id INTEGER NOT NULL PRIMARY KEY autoincrement,
        character_name TEXT NOT NULL,
        description  TEXT NOT NULL,
        quantity INT DEFAULT 1
      );
      ]])

-- prepare a query
DatabasePrepare ("db", "SELECT character_name, description FROM items ORDER BY character_name")

-- find the column names
names = DatabaseColumnNames ("db")
-- ***DROPPED OUT*** tprint (names)

-- execute to get the first row
rc = DatabaseStep ("db")  -- read first row

-- now loop, displaying each row, and getting the next one
while rc == 100 do
  
  print ("")
  values = DatabaseColumnValues ("db")
  tprint (values)
  rc = DatabaseStep ("db")  -- read next row

end -- while loop

-- finished with the statement
DatabaseFinalize ("db")

DatabaseClose ("db")  -- close it</send>
  </alias>
</aliases>


This is displaying

1="Aerin"
2="a copper medallion"

1="Aerin"
2="black muslin wrappings"

1="Artemis"
2="a scorched length of bamboo"

1="Artemis"
2="a small brass lantern"

1="Renald"
2="a bo staff"

1="Renald"
2="a wooden mallet"


Ultimately I was able to remove a bit of useless information but its still not being displayed very smoothly, and reading over both of the links you originally offered didn't seem to help me too much though I was able to change how it was displaying from previously to how it is now which is an improvement. Being able to display it side by side like I mentioned previously would be the best option I think, but even if I can drop out the numbers and quotes that would be good.

Another issue I think I'm going to run into is a how this particular MUD tracks multiples of items. Instead of your traditional "(2) small brass lantern" it displays "two small brass lanterns" Which means with the current DELETE setup it'll either no erase anything if I had two lanterns logged but only dropped one, or if I pick them up separately so they show up as two different items dropping both deletes nothing and dropping one deletes both (this you mentioned). I was looking over http://www.gammon.com.au/forum/?id=9650 and came across the section about events and was wondering if those could be incorporated to tack on an ID number to each item so that when I delete it can delete only one item accordingly.

Also, thank you for your quick replies and all the help you've given so far.
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #10 on Sun 24 Aug 2014 10:55 AM (UTC)
Message
Panaku said:

Is there another reason to remove the CREATE TABLE? My brain tells me if I do remove it then the database wouldn't ever work unless you manually made the table


Just trying to save time, as otherwise every time you do something you are trying to create a table which probably exists. You could put the create table "if not exists" somewhere like in the world "on connect" part rather than for every trigger.

Quote:

... but its still not being displayed very smoothly ...


Read up a bit on string.format. You just need to take the data (from the "values" table) and display it in a single line. That is pretty straightforward. I could tell you but you will learn more by attempting it for yourself.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Panaku   (34 posts)  [Biography] bio
Date Reply #11 on Sun 24 Aug 2014 11:26 AM (UTC)
Message
These are the new triggers and aliases I have in use and its coming along pretty smoothly.

Deleter
<triggers>
  <trigger
   enabled="y"
   expand_variables="y"
   match="You drop *."
   send_to="12"
   sequence="100"
  >
  <send>require "tprint"

DatabaseOpen ("db", GetInfo (66) .. "InventoryDB.sqlite", 6)

rc = DatabaseExec ("db", [[
 CREATE TABLE IF NOT EXISTS items(
        item_id INTEGER NOT NULL PRIMARY KEY autoincrement,
        character_name TEXT NOT NULL,
        description  TEXT NOT NULL,
        quantity INT DEFAULT 1
      );
      ]])

-- drop some data into the database
rc = DatabaseExec ("db", 
  [[
  DELETE FROM items 
  WHERE character_name = '@Current_Character' 
  AND description = '%1';
  ]])

-- finished with the statement
DatabaseFinalize ("db")

DatabaseClose ("db")  -- close it</send>
  </trigger>
</triggers>


Inserter
<triggers>
  <trigger
   enabled="y"
   expand_variables="y"
   match="You get *."
   send_to="12"
   sequence="100"
  >
  <send>require "tprint"

DatabaseOpen ("db", GetInfo (66) .. "InventoryDB.sqlite", 6)

rc = DatabaseExec ("db", [[
 CREATE TABLE IF NOT EXISTS items(
        item_id INTEGER NOT NULL PRIMARY KEY autoincrement,
        character_name TEXT NOT NULL,
        description  TEXT NOT NULL,
        quantity INT DEFAULT 1
      );
      ]])

-- put some data into the database
rc = DatabaseExec ("db", 
  [[
  INSERT INTO items (character_name, description) VALUES ('@Current_Character', '%1');
  ]])

-- finished with the statement
DatabaseFinalize ("db")

DatabaseClose ("db")  -- close it</send>
  </trigger>
</triggers>


Database Viewer
<aliases>
  <alias
   match="database"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>require "tprint"

DatabaseOpen ("db", GetInfo (66) .. "InventoryDB.sqlite", 6)

rc = DatabaseExec ("db", [[
 CREATE TABLE IF NOT EXISTS items(
        item_id INTEGER NOT NULL PRIMARY KEY autoincrement,
        character_name TEXT NOT NULL,
        description  TEXT NOT NULL,
        quantity INT DEFAULT 1
      );
      ]])

-- prepare a query
DatabasePrepare ("db", "SELECT character_name, description FROM items ORDER BY character_name")

-- find the column names
names = DatabaseColumnNames ("db")
-- ***DROPPED OUT*** tprint (names)

-- prints the header
print ("  Character                Item  ")
print ("+--------------------------------------+")

-- execute to get the first row
rc = DatabaseStep ("db")  -- read first row

-- now loop, displaying each row, and getting the next one
while rc == 100 do
  
  print ("")
  names = DatabaseColumnText ("db", 1)
  items = DatabaseColumnText ("db", 2)
  print (" ", names, "          ", items)
  rc = DatabaseStep ("db")  -- read next row

end -- while loop

-- finished with the statement
DatabaseFinalize ("db")

DatabaseClose ("db")  -- close it</send>
  </alias>
</aliases>


Database Output
  Character                Item  
+--------------------------------------+

  Aerin            a copper medallion

  Aerin            black muslin wrappings

  Artemis            a scorched length of bamboo

  Artemis            a small brass lantern

  Ibauthwyn            a lump of indigo

  Renald            a bo staff

  Renald            a wooden mallet


As far as the output goes there are only a few personal changes I'd like to make so I'm going to put those on the back burner and focus on the functionality.

First off, I have a trigger running already on world connect to open up my second account and can add the CREATE TABLE to that trigger, do I need to run the database open/finalize/close on that trigger?

The second issue is deleting only one instance of an item of certain characters. I thought about adding in triggers for when "You get two small brass lanterns." it would do a loop 2x and insert "a small brass lantern" each time but that would require a lot of work to differentiate between items that will be listed with a/an.

Let's say my inventory is "You are carrying a thorned staff, a small brass lantern, a thorned staff, a scorched length of bamboo, a small brass lantern, a steel-shod
quarterstaff, a thorned staff, two steel-shod quarterstaves, and a small brass lantern." and I drop a thorned staff (just one of them), is there a way to delete only one of the 3 instead of all 3?
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #12 on Sun 24 Aug 2014 07:50 PM (UTC)

Amended on Sun 24 Aug 2014 08:04 PM (UTC) by Nick Gammon

Message
First, you can make your list line up neatly by using a length specifier.

eg.


print (string.format ("%-20s %-20s", "foo", "bar"))


Output:


foo                  bar                 


Quote:

... is there a way to delete only one of the 3 instead of all 3?


Yes, update the quantity. *


UPDATE items 
  SET quantity = quantity - 1
  WHERE character_name = 'Gandalf' 
  AND description = 'yellow wand';


Then you might delete it if the quantity is zero. Or do that occasionally later (just don't show items with <= 0 quantity in your listing).

* And if you get a new one, add one to the quantity rather than subtracting one.





Quote:
do I need to run the database open/finalize/close on that trigger?


You should open the database once - this is a slow operation. You can omit closing it, as MUSHclient will close it eventually.

Or do this:

* World connect: open database and create tables if necessary

* World disconnect: close database


The finalize is part of Prepare/Finalize pair. If you do a DatabasePrepare you should do a DatabaseFinalize.

I still think you should move all those opens/creates into a "on connect" world handler like I mention above. They just clutter things up and hide what you are doing.

If you use a script file just make a world connect and world disconnect handler. Their names go into the scripting configuration tab.

eg.


function OnWorldConnect ()
  DatabaseOpen ("db", GetInfo (66) .. "InventoryDB.sqlite", 6)

  rc = DatabaseExec ("db", [[
   CREATE TABLE IF NOT EXISTS items(
        item_id INTEGER NOT NULL PRIMARY KEY autoincrement,
        character_name TEXT NOT NULL,
        description  TEXT NOT NULL,
        quantity INT DEFAULT 1
      );
      ]])
end -- OnWorldConnect

function OnWorldDisconnect ()
  DatabaseClose ("db")  -- close it
end -- OnWorldDisconnect


Then take those lines out of your triggers and aliases.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #13 on Sun 24 Aug 2014 09:02 PM (UTC)

Amended on Sun 24 Aug 2014 09:12 PM (UTC) by Nick Gammon

Message
Quote:

  INSERT INTO items (character_name, description) VALUES ('@Current_Character', '%1');



Be cautious here of character names or descriptions that contain quotes (eg. "Nick's sword"). That will throw an error here (and you are not detecting errors).

Better is to "fix" the SQL by replacing quotes with two quotes. An example is on one of those linked pages:


-- Quote the argument, replacing single quotes with two lots of single quotes.
-- If nil supplied, return NULL (not quoted).
function fixsql (s)
  if s then
    return "'" .. (string.gsub (s, "'", "''")) .. "'"
  else
    return "NULL"
  end -- if
end -- fixsql


Now you can replace:


rc = DatabaseExec ("db", 
  [[
  INSERT INTO items (character_name, description) VALUES ('@Current_Character', '%1');
  ]])


With:


rc = DatabaseExec ("db", string.format (
  [[
  INSERT INTO items (character_name, description) VALUES (%%s, %%s);
  ]], 
  fixsql (GetVariable ("Current_Character")), 
  fixsql ("%1"))
)

if rc ~= sqlite3.OK then
  ColourNote ("red", "", "Error adding item %1: " .. DatabaseError("db"))
end -- if

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Panaku   (34 posts)  [Biography] bio
Date Reply #14 on Sun 24 Aug 2014 09:32 PM (UTC)
Message
I had actually updated everything a few hours after my last post and came up with something very similar to these --

Table Creator(Current)
<triggers>
  <trigger
   enabled="y"
   group="Inventory"
   keep_evaluating="y"
   match="[* logged in.]"
   name="Table_Creation"
   send_to="12"
   sequence="99"
  >
  <send>DatabaseOpen ("db", GetInfo (66) .. "InventoryDB.sqlite", 6)

rc = DatabaseExec ("db", [[
 CREATE TABLE IF NOT EXISTS items(
        item_id INTEGER NOT NULL PRIMARY KEY autoincrement,
        character_name TEXT NOT NULL,
        description  TEXT NOT NULL,
        quantity INT DEFAULT 0
      );
      ]])

DatabaseClose ("db")  -- close it

</send>
  </trigger>
</triggers>


Inserter(Current)
<triggers>
  <trigger
   enabled="y"
   expand_variables="y"
   group="Inventory"
   match="You get *."
   name="Inserter"
   send_to="12"
   sequence="100"
  >
  <send>require "tprint"

DatabaseOpen ("db", GetInfo (66) .. "InventoryDB.sqlite", 6)

-- put some data into the database
rc = DatabaseExec ("db", 
  [[
  INSERT INTO items (character_name, description, quantity) VALUES ('@Current_Character', '%1', +1);
  ]])

-- finished with the statement
DatabaseFinalize ("db")

DatabaseClose ("db")  -- close it</send>
  </trigger>
</triggers>


Deleter(Not Current)
<triggers>
  <trigger
   enabled="y"
   expand_variables="y"
   group="Inventory"
   match="You drop *."
   name="Deleter"
   send_to="12"
   sequence="100"
  >
  <send>require "tprint"

DatabaseOpen ("db", GetInfo (66) .. "InventoryDB.sqlite", 6)

-- drop some data into the database
rc = DatabaseExec ("db", 
  [[
  DELETE FROM items 
  WHERE character_name = '@Current_Character' 
  AND description = '%1';
  ]])

-- finished with the statement
DatabaseFinalize ("db")

DatabaseClose ("db")  -- close it</send>
  </trigger>
</triggers>


Display
<aliases>
  <alias
   name="Inventory_Display"
   match="database"
   enabled="y"
   group="Inventory"
   send_to="12"
   sequence="100"
  >
  <send>require "tprint"

DatabaseOpen ("db", GetInfo (66) .. "InventoryDB.sqlite", 6)

-- prepare a query
DatabasePrepare ("db", "SELECT character_name, description, quantity FROM items ORDER BY character_name")

-- prints the header
print (string.format ("%-15s %-65s %-30s", "  Character", " Item", "Quantity"))
print ("+------------------------------------------------------------------------------------------+")

-- execute to get the first row
rc = DatabaseStep ("db")  -- read first row

-- now loop, displaying each row, and getting the next one
while rc == 100 do
  
  print ("")
  names = DatabaseColumnText ("db", 1)
  items = DatabaseColumnText ("db", 2)
  quant = DatabaseColumnText ("db", 3)
  --print (" ", names, "          ", items)
 print (string.format ("%-1s %-14s %-64s %-29s", " ", names, items, quant))
  rc = DatabaseStep ("db")  -- read next row

end -- while loop

-- finished with the statement
DatabaseFinalize ("db")

DatabaseClose ("db")  -- close it</send>
  </alias>
</aliases>


I dropped out the create table from each individual trigger/alias and left it only to check when you first connect to the world and log in to your account.

As of right now everything is running as individual triggers/aliases and are not part of a single script file. When I tried to omit the open and close from each trigger/alias nothing was working.

I have since modified my output with the help of your string .format example and have something much pleasing to the eyes. I also adding in tracking of quantity when I get an item. I ran into a issue where using --
<triggers>
  <trigger
   enabled="y"
   expand_variables="y"
   group="Inventory"
   match="You get *."
   name="Inserter"
   send_to="12"
   sequence="100"
  >
  <send>require "tprint"

DatabaseOpen ("db", GetInfo (66) .. "InventoryDB.sqlite", 6)

-- put some data into the database
rc = DatabaseExec ("db", 
  [[
    UPDATE items 
    SET quantity = quantity + 1
    WHERE character_name = '@Current_Character' 
    AND description = '%1';
  ]])

-- finished with the statement
DatabaseFinalize ("db")

DatabaseClose ("db")  -- close it</send>
  </trigger>
</triggers>

If the item isn't already in the table it can't be updated, but if I have the item added to the table everytime, it'll update the quantity and make listing for the same item. Is there a way to do "INSERT INTO IF NOT EXIST" or something like that. Also using the code --
UPDATE items 
  SET quantity = quantity - 1
  WHERE character_name = '@Current_Character' 
  AND description = '%1';

Wasn't changing any of the information that was being displayed.

There are only a few select items in the game that use single quotes, player names can't have them at all so I'll look into that fix after a bit.
[Go to top] 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.


63,858 views.

This is page 1, subject is 3 pages long: 1 2  3  [Next page]

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]