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


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  How shared variables in different world

How shared variables in different world

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


Posted by Narci   (32 posts)  [Biography] bio
Date Sun 27 Jul 2014 10:54 AM (UTC)
Message
I want shared talbe in different world.
How can I do it?
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Sun 27 Jul 2014 08:40 PM (UTC)
Message
If this is for something like mobs killed or rooms visited, a simple approach might be to use the database functions, and share a database file.

http://gammon.com.au/db

- Nick Gammon

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

Posted by Narci   (32 posts)  [Biography] bio
Date Reply #2 on Wed 30 Jul 2014 12:10 PM (UTC)

Amended on Wed 30 Jul 2014 12:24 PM (UTC) by Narci

Message


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
DatabaseExec ("db", 
  [[
  INSERT INTO weapons (name, damage) VALUES ('sword', 42);
  INSERT INTO weapons (name, damage) VALUES ('mace', 55);
  INSERT INTO weapons (name, damage) VALUES ('staff', 35);
  ]])

-- 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


How to insert variables to database?
a="blade"
b=100
Can not do like this:INSERT INTO weapons (name, damage) VALUES (a, b)
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Wed 30 Jul 2014 08:04 PM (UTC)
Message
Well first you don't want to recreate the database every time. So make the DROP TABLE / CREATE TABLE part happen once only.

Next, you need to define what sort of data you want to hold in the database. This might help:

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


Quote:

Can not do like this:INSERT INTO weapons (name, damage) VALUES (a, b)


Why not? Can you explain what you want to store (eg. mob names, levels)?

There is a different syntax for adding to the database, compared to changing something.

To add new data use INSERT INTO.

To change an existing row (eg. if you levelled up) then you use UPDATE.

- Nick Gammon

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

Posted by Narci   (32 posts)  [Biography] bio
Date Reply #4 on Wed 30 Jul 2014 09:17 PM (UTC)

Amended on Wed 30 Jul 2014 09:22 PM (UTC) by Narci

Message
I do like this:

local aa='staff'
DatabaseExec ("db", 
  [[
  INSERT INTO weapons (name, damage) VALUES ('sword', 42);
  INSERT INTO weapons (name, damage) VALUES ('mace', 55);
  INSERT INTO weapons (name, damage) VALUES (aa, 35);
  ]])

I can find "sword","mace",But I can not find the 'staff' in the database!
[Go to top] top

Posted by Narci   (32 posts)  [Biography] bio
Date Reply #5 on Wed 30 Jul 2014 09:37 PM (UTC)
Message
Do like this also failed:

DatabaseExec ("db", 
  [[
  INSERT INTO weapons (name, damage) VALUES ('sword', 42);
  INSERT INTO weapons (name, damage) VALUES ('mace', 55);
  INSERT INTO weapons (name, damage) VALUES ("blade", 33);
  ]])
aa="staff"
DatabaseExec ("db", "UPDATE weapons SET name=aa WHERE damage=55;")

No update the "mace" to "satff"!

my mushclient version is 4.61.
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #6 on Wed 30 Jul 2014 09:46 PM (UTC)

Amended on Wed 30 Jul 2014 09:50 PM (UTC) by Nick Gammon

Message
In Lua, the use of [[ ... ]] is a string constant (similar to "...").

You need to put the contents of "aa" into the string.

For example:


weaponType = "staff"
damage = 42
DatabaseExec ("db", string.format (
  "INSERT INTO weapons (name, damage) VALUES ('%s', %i);",
  weaponType, damage))


There is a slight problem there, even, because if the weaponType has quotes in it, that will mess up the quotes in the SQL statement. So better is:


-- 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

weaponType =  "Nick's staff"
damage = 42

DatabaseExec ("db", string.format (
  "INSERT INTO weapons (name, damage) VALUES (%s, %i);",
  fixsql (weaponType), damage))


The function fixsql doubles quotes (as required by SQL) and thus it lets you have things with quotes in their names.

And to save headaches later on, check the return code like this:


local MUSHclient_Database_Errors = {
  [-1] = "Database id not found",
  [-2] = "Database not open",
  [-3] = "Already have prepared statement",
  [-4] = "Do not have prepared statement",
  [-5] = "Do not have a valid row",
  [-6] = "Database already exists under a different disk name",
  [-7] = "Column number out of range",
  } -- end of MUSHclient_Database_Errors

-- check for errors on a DatabaseXXXXX call
function dbcheck (code)
 if code == sqlite3.OK or       -- no error
    code == sqlite3.ROW or      -- completed OK with another row of data
    code == sqlite3.DONE then   -- completed OK, no more rows
    return code
  end -- if code OK

  -- DatabaseError won't return the negative errors  
  local err = MUSHclient_Database_Errors [code] or DatabaseError("db")
  DatabaseExec ("db", "ROLLBACK")  -- rollback any transaction to unlock the database
  error (err, 2)                 -- show error in caller's context
end -- dbcheck 


Now your code should look like this:



dbcheck (DatabaseExec ("db", string.format (
  "INSERT INTO weapons (name, damage) VALUES (%s, %i);",
  fixsql (weaponType), damage)))


That extra function call (dbcheck) will raise an error if you get the SQL wrong.

For example, in your original code adding the dbcheck like this:


dbcheck (DatabaseExec ("db", "INSERT INTO weapons (name, damage) VALUES (aa, 35);"))


Gives you this error:


Run-time error
World: smaug2
Immediate execution
[string "Immediate"]:66: no such column: aa
stack traceback:
        [C]: in function 'error'
        [string "Immediate"]:26: in function 'dbcheck'
        [string "Immediate"]:66: in main chunk


Now you can see more clearly that it tried to add "aa" to the database, but it didn't exist.

- 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 #7 on Wed 30 Jul 2014 09:55 PM (UTC)
Message
Narci said:

Do like this also failed:

DatabaseExec ("db", 
  [[
  INSERT INTO weapons (name, damage) VALUES ('sword', 42);
  INSERT INTO weapons (name, damage) VALUES ('mace', 55);
  INSERT INTO weapons (name, damage) VALUES ("blade", 33);
  ]])
aa="staff"
DatabaseExec ("db", "UPDATE weapons SET name=aa WHERE damage=55;")

No update the "mace" to "satff"!


That's a similar problem. Try:


aa="staff"
DatabaseExec ("db", string.format ("UPDATE weapons SET name=%s WHERE damage=55;", fixsql (aa)))


(You need the fixsql function from above).

Quote:

my mushclient version is 4.61.


The current version is 4.94:

http://www.gammon.com.au/forum/?id=12532

- Nick Gammon

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

Posted by Narci   (32 posts)  [Biography] bio
Date Reply #8 on Sun 03 Aug 2014 11:12 PM (UTC)
Message
Nick Gammon said:

If this is for something like mobs killed or rooms visited, a simple approach might be to use the database functions, and share a database file.

http://gammon.com.au/db


I have use it.But it not work well! because it make mushclient very stutter.
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #9 on Mon 04 Aug 2014 05:01 AM (UTC)
Message
You need to post your code so I can help you more. If you open and close the database every time, that would slow it down a lot.

- 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 #10 on Mon 04 Aug 2014 05:02 AM (UTC)
Message
So far I have no real idea of what variables you are trying to share, or when. With a bit more information I could make a more descriptive answer.

- Nick Gammon

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

Posted by Narci   (32 posts)  [Biography] bio
Date Reply #11 on Mon 04 Aug 2014 09:31 AM (UTC)
Message
Nick Gammon said:

So far I have no real idea of what variables you are trying to share, or when. With a bit more information I could make a more descriptive answer.


I would share some information from a world to another world.
example:
quest information.
quest cd time.
……
Data is dynamic,can not be calculateļ¼
I should get the information every second.
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #12 on Mon 04 Aug 2014 07:39 PM (UTC)
Message
I'm not a mind-reader. If you don't post your code I can't help fix it.

Template:copying For advice on how to copy aliases, timers or triggers from within MUSHclient, and paste them into a forum message, please see Copying XML.


Narci said:

I would share some information from a world to another world.


What information?




For small amounts of information, eg. that you just killed a mob, or got a message, you could use the "world" functions to send data from one world to another.

http://www.gammon.com.au/scripts/doc.php?function=GetWorld

For example there is a plugin (Multiple_Send) that sends a message from one world to all other open worlds.

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

- 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 Mon 04 Aug 2014 11:35 PM (UTC)
Message
Here is an example of sending information from one world to another. Say you want to let your second world know that your first world has finished a quest.

In world A we have a trigger that matches:


You have completed the daily quest.


This gets a "world object" by name, which in this case is "my_second_world"



<triggers>
  <trigger
   enabled="y"
   match="You have completed the daily quest."
   send_to="12"
   sequence="100"
  >
  <send>

name = "my_second_world"

otherworld = GetWorld (name)

if otherworld == nil then
  Note ("World " .. name .. " is not open")
  return
end

Execute (otherworld, "NOTIFICATION: %0")

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


That "executes" the word: NOTIFICATION: followed by whatever the message was.

Now in your other word you can detect that with an alias, like this:


<aliases>
  <alias
   match="NOTIFICATION: *"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>Note ("You received message: %1")</send>
  </alias>
</aliases>





If you want to send to multiple worlds, you can do it slightly more simply, like this:


<triggers>
  <trigger
   enabled="y"
   match="You have completed the daily quest."
   send_to="12"
   sequence="100"
  >
  <send>

for k, v in pairs (GetWorldIdList ()) do
  if v ~= GetWorldID() then   -- don't send to ourself
    GetWorldById (v):Execute ("NOTIFICATION: %0")
  end -- if 
end

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


This gets a list of all worlds and then sends the message to every one (except the current one). That way it doesn't matter what the world name is.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[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.


27,993 views.

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]