Revised SQL setup script, and the script function

This commit is contained in:
Kayne Ruse
2014-04-20 23:11:46 +10:00
parent f315f4bf35
commit f56cb58dfb
3 changed files with 65 additions and 46 deletions
+48 -39
View File
@@ -1,11 +1,13 @@
--TODO: The SQL startup script needs revising
------------------------- -------------------------
--Server --Server
------------------------- -------------------------
CREATE TABLE IF NOT EXISTS UserAccounts ( CREATE TABLE IF NOT EXISTS UserAccounts (
userAccountID INTEGER PRIMARY KEY AUTOINCREMENT, uid INTEGER PRIMARY KEY AUTOINCREMENT,
username varchar(30) UNIQUE, username varchar(100) UNIQUE,
password varchar(30), password varchar(100), --NOTE: DO NOT DO THIS!!
blacklisted BIT DEFAULT 0, blacklisted BIT DEFAULT 0,
whitelisted BIT DEFAULT 1 whitelisted BIT DEFAULT 1
); );
@@ -14,31 +16,30 @@ CREATE TABLE IF NOT EXISTS UserAccounts (
--Items --Items
------------------------- -------------------------
CREATE TABLE IF NOT EXISTS GlobalItemList (
globalItemListID INTEGER PRIMARY KEY AUTOINCREMENT,
itemName varchar(30) UNIQUE,
itemImage varchar(30),
type varchar(15), --{'mundane', 'consumable', 'equipment'}
maxStackSize INTEGER, --{1-max; 0 for non-stackable}
maxUniqueCopies INTEGER --{1-max; 0 for unlimited}
);
CREATE TABLE IF NOT EXISTS MundaneItems ( CREATE TABLE IF NOT EXISTS MundaneItems (
mundaneItemID INTEGER PRIMARY KEY AUTOINCREMENT, --metadata
globalItemListID INTEGER REFERENCES GlobalItemList(globalItemListID) uid INTEGER PRIMARY KEY AUTOINCREMENT,
--holds whatever itemID INTEGER,
stackSize INTEGER DEFAULT 0,
owner INTEGER REFERENCES PlayerCharacters(uid)
); );
CREATE TABLE IF NOT EXISTS Consumables ( CREATE TABLE IF NOT EXISTS Consumables (
consumableID INTEGER PRIMARY KEY AUTOINCREMENT, --metadata
globalItemListID INTEGER REFERENCES GlobalItemList(globalItemListID) uid INTEGER PRIMARY KEY AUTOINCREMENT,
itemID INTEGER,
stackSize INTEGER DEFAULT 0,
owner INTEGER REFERENCES PlayerCharacters(uid)
--holds all consumable items info (food, potions, etc.) --holds all consumable items info (food, potions, etc.)
); );
CREATE TABLE IF NOT EXISTS Equipment ( CREATE TABLE IF NOT EXISTS Equipment (
equipmentID INTEGER PRIMARY KEY AUTOINCREMENT, --metadata
globalItemListID INTEGER REFERENCES GlobalItemList(globalItemListID) uid INTEGER PRIMARY KEY AUTOINCREMENT,
itemID INTEGER,
owner INTEGER REFERENCES PlayerCharacters(uid)
--hold all equipment info --hold all equipment info
--stat mods, special effects, etc.
); );
------------------------- -------------------------
@@ -46,28 +47,36 @@ CREATE TABLE IF NOT EXISTS Equipment (
------------------------- -------------------------
CREATE TABLE IF NOT EXISTS PlayerCharacters ( CREATE TABLE IF NOT EXISTS PlayerCharacters (
playerCharacterID INTEGER PRIMARY KEY AUTOINCREMENT, uid INTEGER PRIMARY KEY AUTOINCREMENT,
name varchar(30) UNIQUE,
--stats --metadata
currentLevel INTEGER DEFAULT 0, handle varchar(100) UNIQUE,
currentExperience INTEGER DEFAULT 0, avatar varchar(100),
maxHealth INTEGER DEFAULT 0, birth timestamp NOT NULL DEFAULT (datetime()),
maxMana INTEGER DEFAULT 0,
currentHealth INTEGER DEFAULT 0, --position
currentMana INTEGER DEFAULT 0, mapIndex INTEGER DEFAULT 0,
attack INTEGER DEFAULT 0, positionX INTEGER DEFAULT 0,
defence INTEGER DEFAULT 0, positionY INTEGER DEFAULT 0,
--etc.
--statistics
level INTEGER DEFAULT 0,
exp INTEGER DEFAULT 0,
maxHP INTEGER DEFAULT 0,
health INTEGER DEFAULT 0,
maxMP INTEGER DEFAULT 0,
mana INTEGER DEFAULT 0,
attack INTEGER DEFAULT 0,
defence INTEGER DEFAULT 0,
intelligence INTEGER DEFAULT 0,
resistance INTEGER DEFAULT 0,
accuracy REAL DEFAULT 0.0,
evasion REAL DEFAULT 0.0,
luck REAL DEFAULT 0.0,
--equipment --equipment
weapon INTEGER REFERENCES Equipment(equipmentID), weapon INTEGER REFERENCES Equipment(uid),
helmet INTEGER REFERENCES Equipment(equipmentID), helmet INTEGER REFERENCES Equipment(uid),
armour INTEGER REFERENCES Equipment(equipmentID) armour INTEGER REFERENCES Equipment(uid)
--etc. --etc.
); );
CREATE TABLE IF NOT EXISTS PlayerInventoryItems (
characterID INTEGER REFERENCES PlayerCharacters(characterID),
globalItemListID INTEGER REFERENCES GlobalItemList(globalItemListID)
);
+16 -6
View File
@@ -21,10 +21,14 @@
*/ */
#include "server_utility.hpp" #include "server_utility.hpp"
#include "utility.hpp"
#include <stdexcept>
#include <fstream> #include <fstream>
#include <cstdlib>
int runSQLScript(sqlite3* db, std::string fname, int (*callback)(void*,int,char**,char**), void* argPtr) {
int runSQLScript(sqlite3* db, std::string fname) { //load the file into a string
std::ifstream is(fname); std::ifstream is(fname);
if (!is.is_open()) { if (!is.is_open()) {
return -1; return -1;
@@ -32,9 +36,15 @@ int runSQLScript(sqlite3* db, std::string fname) {
std::string script; std::string script;
getline(is, script, '\0'); getline(is, script, '\0');
is.close(); is.close();
//NOTE: flesh out this error if needed
if (sqlite3_exec(db, script.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { //run the SQL loaded from the file
return -2; char* errmsg = nullptr;
int ret = sqlite3_exec(db, script.c_str(), callback, argPtr, &errmsg);
if (ret != SQLITE_OK) {
//handle any errors received from the SQL
std::runtime_error e(std::string() + "SQL Script Error " + to_string_custom(ret) + ": " + errmsg);
free(errmsg);
throw(e);
} }
return 0; return ret;
} }
+1 -1
View File
@@ -26,6 +26,6 @@
#include <string> #include <string>
int runSQLScript(sqlite3* db, std::string fname); int runSQLScript(sqlite3* db, std::string fname, int (*callback)(void*,int,char**,char**) = nullptr, void* argPtr = nullptr);
#endif #endif