[SOLVED] Databases / Dealing with lots of data

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
PiFace
Prole
Posts: 28
Joined: Mon Sep 05, 2016 5:35 pm
Location: Brazil

[SOLVED] Databases / Dealing with lots of data

Post by PiFace »

Hi everyone, this is almost my first post here, and I know this matter has been discussed around here before but the respective threads are long dead, so I thought it would be more efficient to create another one and ask what I need anyway. What I'm mostly looking for is some opinion/advice on what solution to use for my project, cause I'm a bit lost :P

What I'm trying to achieve is a pc version of a card game, and I'll focus on making at least a menu to edit the decks, and an online or LAN mode to play against other people (I would later try to make an AI mode for offilne play, IF I suceeded doing the rest, lol) and then I got to the main problem about making this: there are about 3.2k cards in the game, what is the best way to keep their data accessible for the code? What is the most efficient way to ease the task of whoever's gonna type all that data? (actually a lot of copy-pasting, but you get the ideia)

Yes, I'm trying the SQLite3 library available on the wiki, but there are no references about how to allow LÖVE to read the data. (at least I haven't seen them. If somebody knows how, please tell me, it would solve most of the problems :awesome: ) So maybe I should get to know other methods, including the ones I've read around the forums, like simply making the database in .lua. The problem is typing in the data in a plain lua file takes a lot more time, although it doesn't need any library, plugin or anything, just require the file and it is ready to use. This is plan B if other solutions don't work.

Another problem is about checking if both players that are connected have no differences in their databases. If the database were purely online, there couldn't be any offline mode, so the database should probably be local... but I'm not sure, do you guys have any ideas? Any idea on the general approach would be highly appreciated, thank you! :nyu:

(by the way, is local networking a thing in Löve? Because as I know it has online support built in, I supposed LAN is just as simple, but correct me if I'm wrong.)
Last edited by PiFace on Wed Sep 14, 2016 5:30 pm, edited 1 time in total.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Databases / Dealing with lots of data

Post by raidho36 »

Well 3.2k is not that many and you can simply load them all to a single table, using card name as key and table describing the card as value. As opposed to only loading the necessary data once it's needed and discarding it once it's no longer needed.

Code: Select all

cards = { "foo" = { bar = 123, etc } }
Of course exactly how you will do it depends on how your data is stored already, to avoid converting it back and forth. If it's already in JSON or XML format it should be trivial to load it. But if it's just a text file with standardized card descriptions, that is easily loadable as well. If it's in the database, you simply SELECT * WHERE 1 = 1 and that returns list of all cards, you simply read it row by row to load into your game. I have no clue what library you work with but they all fairly similar for any language and any database. Some of them have to work with cursor but I'm sure the one you're using just returns a full table of results.

One way to check if same database file is used is to compare its hash sum. MD-5 is a common method but you can use SHA-2 for which no collisions have been reported to date, i.e. it's not practically possible to make a different file produce same SHA-2 sum. Otherwise you might just send data about specific cards being used and compare them to some original. Note that forcing players to use some standard database will prevent modding - just in case this wasn't immediately obvious.
User avatar
PiFace
Prole
Posts: 28
Joined: Mon Sep 05, 2016 5:35 pm
Location: Brazil

Re: Databases / Dealing with lots of data

Post by PiFace »

Yeah, using that SQLite3 library I tried to mimic the example given by the creator, doing the first steps, requiring the library and opening the database. (the database is called cards, there are two tables, one for text and the other for the numeric data)

Code: Select all

local sqlite3 = require "sqlite3"
local cards = sqlite3.open("cards.db")
local test

if cards then
        local sQuery = "SELECT name FROM text WHERE id = 10;"  --this 10 could be any ID, just a random entry
        test = cards:execute(sQuery)
        print(test)
end
Here is CentauriSoldier's example on github:

Code: Select all

--create/open database
    local hDB = sqlite3.open("mydatabase.db");

      if hDB then
      local sQuery = "CREATE TABLE test (id INTEGER PRIMARY KEY   AUTOINCREMENT, name CHAR(20));";
      hDB:execute(sQuery);

      hDB:close();
      end
The problem is that when I run and check the console (or love.graphics.print, I tried with it too) the value printed is not the string, it's just a zero. For any value selected, it only returns the number zero. I understand the basics of sqlite commands, it's very simple, I'm just confused about how I should use it inside this example. If it worked like this at least once, I could figure out how to access the rest of the data, but it didn't .-. I'm pretty sure it's a stupid mistake, happens all the time haha.

but about the database check... yeah I was still considering including modding or not, but I was mostly worried about cheating and stuff. Probably only me and my friends are gonna play this anyway, so that actually shouldn't be a problem... but I'll keep your tip in mind.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Databases / Dealing with lots of data

Post by Positive07 »

Well he just wrapped Josefnpat's builds of the Lua SQLlite3 wrappers library so you may as well use that and look at the documentation. Note that the builds on that thread Josefnpat made are for LuaSQLlite3 0.9.1, there have been two minor releases since then and it is now at 0.9.3
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Databases / Dealing with lots of data

Post by Beelz »

If it were me, I would go with the XML route just to avoid all that copy and pasting... You can google 'convert database to xml' and then load that file into your game with something like this. Although a google search may find a way to convert a database to Lua. Not sure, havn't tried...

EDIT:
Didn't see Positive07's above post, that's probably a better idea :P

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Databases / Dealing with lots of data

Post by raidho36 »

I guess the 0 is an error code. It means success. Now the database controller object stores result of your query, you should get the rows using appropriate function.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Databases / Dealing with lots of data

Post by airstruck »

PiFace wrote:For any value selected, it only returns the number zero.
LuaSQLite3 API is asynchronous, you need to pass a callback to db:exec. Take another look at the docs.
User avatar
Evine
Citizen
Posts: 72
Joined: Wed May 28, 2014 11:46 pm

Re: Databases / Dealing with lots of data

Post by Evine »

raidho36 wrote:Well 3.2k is not that many and you can simply load them all to a single table, using card name as key and table describing the card as value. As opposed to only loading the necessary data once it's needed and discarding it once it's no longer needed.
This. Even if you have a massive amount of properties on each card the total dataset will only be a few megabytes. So I think you should focus on how to create the card data in the first place.

I'd suggest either making a simple formatted file that you parse. Or make a minimal editor in love2d that writes .lua files. Both of these methods can help you avoid doing simple copy-paste mistakes.
Artal, A .PSD loader: https://github.com/EvineDev/Artal
User avatar
PiFace
Prole
Posts: 28
Joined: Mon Sep 05, 2016 5:35 pm
Location: Brazil

Re: Databases / Dealing with lots of data

Post by PiFace »

@Positive07, @raidho36 and @airstruck - Thank you a lot! I really had to check the docs again, and what you guys said helped me understand how the luasqlite works. I can loop through data in the callbakc function for the exec, store it inside a table or something and use the retrieved data elsewhere if needed.

@Evine - yeah, I'm aware the file size is not a big deal. What you and raidho36 suggested is retrieving all the data on load and keeping it in a table, right? As you said, 3.2k is not that much so it wouldn't be hard for the system to manage it at runtime. (or would it?)

and again, thank you guys! :awesome: you helped me a lot!
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Databases / Dealing with lots of data

Post by raidho36 »

Depending on how much data there is per card, that should be between under a megabyte and few tens of megabytes. You, of course, shouldn't load graphics for all 3.2k cards all at once - a measly 256x256 image is full 256 kilobytes of data, times 3.2k that becomes 800 megs of graphics memory. Only load it when it has to show up on the screen. Nowdays there's a solid chance that host machine will handle it anyway but that's still a poor idea.
Post Reply

Who is online

Users browsing this forum: No registered users and 30 guests