Is it possible to grab data from databases in Lua scripts?

  • So, I have quite a few ideas for scripts that Lua coders could create, but they rely on being able to create a database connection and pull data from that database.


    Is this functionality possible in Lua?

  • Rising World gives you functions to access and create a script specific database (SQLite).


    Simple answer, no access to external databases.

  • I am not an expert at lua, but like in any other programming language it should be possible to request some webservices from script. Then you only have to build up a webserver with the database you wish. Constructing an REST-API should provide enough functionality.


    And yeah i know, not all have internet not all use mp but for some data it would be a solution. :)

  • [lua]database = getDatabase();[/lua] will create a database within your script folder called scriptDatabase.


    This is an example of how to create tables:


    [lua]database:queryupdate("CREATE TABLE IF NOT EXISTS 'areas' ('ID' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , 'name' VARCHAR, 'startChunkpositionX' INTEGER, 'startChunkpositionY' INTEGER, 'startChunkpositionZ' INTEGER, 'startBlockpositionX' INTEGER, 'startBlockpositionY' INTEGER, 'startBlockpositionZ' INTEGER,'endChunkpositionX' INTEGER, 'endChunkpositionY' INTEGER, 'endChunkpositionZ' INTEGER, 'endBlockpositionX' INTEGER, 'endBlockpositionY' INTEGER, 'endBlockpositionZ' INTEGER, 'playerID' INTEGER);");[/lua]


    This is an example of how to query the database:


    [lua] result = database:query("SELECT * FROM areas;");
    while result:next() do
    local area = {};
    area["areaID"] = result:getInt("ID");
    area["playerID"] = result:getInt("playerID");
    area["areaName"] = result:getString("name");
    area["startChunkpositionX"] = result:getInt("startChunkpositionX");
    area["startChunkpositionY"] = result:getInt("startChunkpositionY");
    area["startChunkpositionZ"] = result:getInt("startChunkpositionZ");
    area["startBlockpositionX"] = result:getInt("startBlockpositionX");
    area["startBlockpositionY"] = result:getInt("startBlockpositionY");
    area["startBlockpositionZ"] = result:getInt("startBlockpositionZ");


    area["endChunkpositionX"] = result:getInt("endChunkpositionX");
    area["endChunkpositionY"] = result:getInt("endChunkpositionY");
    area["endChunkpositionZ"] = result:getInt("endChunkpositionZ");
    area["endBlockpositionX"] = result:getInt("endBlockpositionX");
    area["endBlockpositionY"] = result:getInt("endBlockpositionY");
    area["endBlockpositionZ"] = result:getInt("endBlockpositionZ");


    area["rights"] = {};


    calculateGlobalAreaPosition(area);
    areas[result:getInt("ID")] = area;
    end[/lua]


    This all comes from the AreaProtection script provided by the devs.

  • The reason I see this coming into play, or one of the reasons, is with server-list websites. To register the votes from a database, or get some nice statistics.


    Another reason could be to store server statistics on an external database, such as the amount of players, to display on a website.


    One other could be to 'sync' users between the server and for example, a forum.

  • Also keep in mind that this game uses LuaJ. LuaJ is a java implementation of Lua and it can be extended. In fact, starting this weekend I am going to add some additional functionality for the script patching and some other file related stuff. i will see about adding functionality to connect to external databases. It doesn't appear that the devs alter LuaJ so my changes shouldn't conflict with theirs.

  • The reason I see this coming into play, or one of the reasons, is with server-list websites. To register the votes from a database, or get some nice statistics.


    Another reason could be to store server statistics on an external database, such as the amount of players, to display on a website.


    One other could be to 'sync' users between the server and for example, a forum.


    All of this can be done without extending LuaJ. The game server already provides enough information about the game server via the http port (the port you specify to run the game on - 1, e.g. game port = 30500, http port = 30499).


    Player list: http://win.thedeadlands.net:30499/playerlist

  • All of this can be done without extending LuaJ. The game server already provides enough information about the game server via the http port (the port you specify to run the game on - 1, e.g. game port = 30500, http port = 30499).


    Player list: http://win.thedeadlands.net:30499/playerlist


    Will be interesting to see what can come out of this game. :)


    Once things are more 'stable', e.g: less modifications, I'll consider getting into the developing side. :)

  • So far:


    http://win.thedeadlands.net:30499/
    http://win.thedeadlands.net:30499/details
    http://win.thedeadlands.net:30499/playerlist


    I don't know of any others yet. That was provided by one of the devs a week ago or so. Definitely enough information to tie player accounts to web accounts. You can also access the player database information from Lua (I forget the syntax) and that contains a lot of info, so if you wanted, through some ingenuity, you could create things like position mapping services, etc.

  • So far:


    http://win.thedeadlands.net:30499/
    http://win.thedeadlands.net:30499/details
    http://win.thedeadlands.net:30499/playerlist


    I don't know of any others yet. That was provided by one of the devs a week ago or so. Definitely enough information to tie player accounts to web accounts.


    Well I can definitely see some useful information in there that could be used with several web-based projects, particularly so on a servers own website. Such as displaying the 'live' version of the server on their website, rather than modifying it manually, as well as showing who's online and how many.

  • Ehm but you are using an SQLlite Database for persitent storage? I just know it from Android but it was a pain to work with large datasets, cause the cursor size bursted the maximum cursorsize of 1 mb, frequently.

  • Maybe I can extend luaj to allow other file based databases, like my favorite, HSQLDB (HyperSQL DB). I am not 100% sure, but I think that size limit issue only affects android. I have never hit an issue with cursor size limits on the PC and I have had some pretty significant result sets.

Participate now!

Don’t have an account yet? Create a new account now and be part of our community!