Go Back   EQEmulator Home > EQEmulator Forums > Quests > Quests::LUA

Quests::LUA Section regarding LUA scripts, help, questions, answers

Reply
 
Thread Tools Display Modes
  #1  
Old 06-17-2016, 11:56 PM
demonstar55
Demi-God
 
Join Date: Apr 2008
Location: MA
Posts: 1,164
Default LuaRocks support

I added support to auto set up lua's path and cpath to be able to use the lua_modules folder for a Rocks tree. This should simplify the process of those who wish to extend lua with modules that are not provided by their package manager on linux and greatly simply for windows.

LuaRocks is a package management system for Lua similar to CPAN for PERL.

Lets get some examples.

Let's say we wanted to use inspect module help us with some logging of our quests.

Now I have my server set up with the server in ~/server and I use the default lua_modules path for my lua_models, so to install inspect I do

Code:
cd ~/server
luarocks --tree=lua_modules install inspect
This will set up some folders which the change will auto add to our path, so it's simple to set up.

Now lets look at a simple quest (lots of details left out)

Code:
local inspect = require("inspect")
local trade_ins = {} -- a table that keeps track of some trade ins

...

function event_trade(e)
if e.trade.item1 ~= nil then
    table.insert(trade_ins, e.trade.item1:GetID())
end

if e.trade.item2 ~= nil then
    table.insert(trade_ins, e.trade.item2:GetID())
end

if e.trade.item3 ~= nil then
    table.insert(trade_ins, e.trade.item3:GetID())
end

if e.trade.item4 ~= nil then
    table.insert(trade_ins, e.trade.item4:GetID())
end

eq.debug(e.self:GetName() .. " has been given " .. inspect(trade_ins) .. " this session", 3)

...

end

...
Now this example may not be very useful, but it's simple to show how we can use LuaRocks. Now why lua inspect is useful here is an example from my lua terminal

Code:
> inspect = require("inspect")
> t = {1,2,3}
> print(t)
table: 0xa86260
> print(inspect(t))
{ 1, 2, 3 }
Now, some modules actually make use of the lua's C API so might be a bit more complicated to install like luasql for example. (Some distros actually package this, some don't so ...)

Usually they will print helpful (or not) error messages like luasql-mysql

Code:
luarocks --local --tree=lua_modules install luasql-mysql
Installing https://rocks.moonscript.org/luasql-mysql-2.3.0-1.src.rock...
Using https://rocks.moonscript.org/luasql-mysql-2.3.0-1.src.rock... switching to 'build' mode

Error: Could not find expected file mysql.h, or mysql.h for MYSQL -- you may have to install MYSQL in your system and/or pass MYSQL_DIR or MYSQL_INCDIR to the luarocks command. Example: luarocks install luasql-mysql MYSQL_DIR=/usr/local
To install this on my Linux box I had to use (what you need to do for Windows might be different)

Code:
luarocks --tree=lua_modules install luasql-mysql MYSQL_INCDIR=/usr/include
Here is a simple (missing error checking etc) test quest command

Code:
function command_itemname(e)
    local driver = require("luasql.mysql")
    env = driver.mysql()
    con = env:connect("db", "user", "pass")
    cur = con:execute("select Name from items where id = " .. e.args[1])
    e.self:Message(335, "ID " .. e.args[1] .. " is " .. cur:fetch())
end
Reply With Quote
  #2  
Old 06-21-2016, 06:14 PM
rhyotte
Hill Giant
 
Join Date: Jul 2012
Location: Oklahoma
Posts: 222
Default

Nice!

/10char
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

   

All times are GMT -4. The time now is 10:25 PM.


 

Everquest is a registered trademark of Daybreak Game Company LLC.
EQEmulator is not associated or affiliated in any way with Daybreak Game Company LLC.
Except where otherwise noted, this site is licensed under a Creative Commons License.
       
Powered by vBulletin®, Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Template by Bluepearl Design and vBulletin Templates - Ver3.3