2013-05-26 15:20:49 -04:00
|
|
|
|
|
|
|
-- Storage.lua
|
|
|
|
-- Implements the storage access object, shielding the rest of the code away from the DB
|
|
|
|
|
2013-06-04 17:05:33 -04:00
|
|
|
--[[
|
|
|
|
The cStorage class is the interface to the underlying storage, the SQLite database.
|
|
|
|
This class knows how to load player areas from the DB, how to add or remove areas in the DB
|
|
|
|
and other such operations.
|
|
|
|
|
|
|
|
Also, a g_Storage global variable is declared, it holds the single instance of the storage.
|
|
|
|
--]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cStorage = {};
|
|
|
|
|
|
|
|
g_Storage = {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--- Initializes the storage subsystem, creates the g_Storage object
|
2013-06-06 13:10:45 -04:00
|
|
|
-- Returns true if successful, false if not
|
2013-06-04 17:05:33 -04:00
|
|
|
function InitializeStorage()
|
|
|
|
g_Storage = cStorage:new();
|
2013-06-06 13:10:45 -04:00
|
|
|
if (not(g_Storage:OpenDB())) then
|
|
|
|
return false;
|
|
|
|
end
|
|
|
|
|
|
|
|
return true;
|
2013-06-04 17:05:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function cStorage:new(obj)
|
|
|
|
obj = obj or {};
|
|
|
|
setmetatable(obj, self);
|
|
|
|
self.__index = self;
|
|
|
|
return obj;
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-06 13:10:45 -04:00
|
|
|
--- Opens the DB and makes sure it has all the columns needed
|
|
|
|
-- Returns true if successful, false otherwise
|
|
|
|
function cStorage:OpenDB()
|
|
|
|
local ErrCode, ErrMsg;
|
|
|
|
self.DB, ErrCode, ErrMsg = sqlite3.open("ProtectionAreas.sqlite");
|
|
|
|
if (self.DB == nil) then
|
|
|
|
LOGWARNING(PluginPrefix .. "Cannot open ProtectionAreas.sqlite, error " .. ErrCode .. " (" .. ErrMsg ..")");
|
|
|
|
return false;
|
|
|
|
end
|
|
|
|
|
|
|
|
if (
|
2013-06-07 12:28:37 -04:00
|
|
|
not(self:CreateTable("Areas", {"ID INTEGER PRIMARY KEY AUTOINCREMENT", "MinX", "MaxX", "MinZ", "MaxZ", "WorldName", "CreatorUserName"})) or
|
2013-06-06 13:10:45 -04:00
|
|
|
not(self:CreateTable("AllowedUsers", {"AreaID", "UserName"}))
|
|
|
|
) then
|
|
|
|
LOGWARNING(PluginPrefix .. "Cannot create DB tables!");
|
|
|
|
return false;
|
|
|
|
end
|
|
|
|
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-07 12:28:37 -04:00
|
|
|
--- Executes the SQL command given, calling the a_Callback for each result
|
|
|
|
-- If the SQL command fails, prints it out on the server console and returns false
|
|
|
|
-- Returns true on success
|
|
|
|
function cStorage:DBExec(a_SQL, a_Callback, a_CallbackParam)
|
|
|
|
local ErrCode = self.DB:exec(a_SQL, a_Callback, a_CallbackParam);
|
|
|
|
if (ErrCode ~= sqlite3.OK) then
|
|
|
|
LOGWARNING(PluginPrefix .. "Error " .. ErrCode .. " (" .. self.DB:errmsg() ..
|
|
|
|
") while processing SQL command >>" .. a_SQL .. "<<"
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
end
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-06 13:10:45 -04:00
|
|
|
--- Creates the table of the specified name and columns[]
|
|
|
|
-- If the table exists, any columns missing are added; existing data is kept
|
|
|
|
function cStorage:CreateTable(a_TableName, a_Columns)
|
|
|
|
|
|
|
|
local sql = "CREATE TABLE IF NOT EXISTS '" .. a_TableName .. "' (";
|
|
|
|
sql = sql .. table.concat(a_Columns, ", ");
|
|
|
|
sql = sql .. ")";
|
2013-06-07 12:28:37 -04:00
|
|
|
if (not(self:DBExec(sql))) then
|
|
|
|
LOGWARNING(PluginPrefix .. "Cannot create DB Table " .. a_TableName);
|
2013-06-06 13:10:45 -04:00
|
|
|
return false;
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Check each column whether it exists
|
|
|
|
-- Remove all the existing columns from a_Columns:
|
|
|
|
local RemoveExistingColumn = function(UserData, NumCols, Values, Names)
|
|
|
|
-- Remove the received column from a_Columns. Search for column name in the Names[] / Values[] pairs
|
|
|
|
for i = 1, NumCols do
|
|
|
|
if (Names[i] == "name") then
|
|
|
|
local ColumnName = Values[i]:lower();
|
|
|
|
-- Search the a_Columns if they have that column:
|
|
|
|
for j = 1, #a_Columns do
|
|
|
|
-- Cut away all column specifiers (after the first space), if any:
|
|
|
|
local SpaceIdx = string.find(a_Columns[j], " ");
|
|
|
|
if (SpaceIdx ~= nil) then
|
|
|
|
SpaceIdx = SpaceIdx - 1;
|
|
|
|
end
|
|
|
|
local ColumnTemplate = string.lower(string.sub(a_Columns[j], 1, SpaceIdx));
|
|
|
|
-- If it is a match, remove from a_Columns:
|
|
|
|
if (ColumnTemplate == ColumnName) then
|
|
|
|
table.remove(a_Columns, j);
|
|
|
|
break; -- for j
|
|
|
|
end
|
|
|
|
end -- for j - a_Columns[]
|
|
|
|
end
|
|
|
|
end -- for i - Names[] / Values[]
|
|
|
|
return 0;
|
|
|
|
end
|
2013-06-07 12:28:37 -04:00
|
|
|
if (not(self:DBExec("PRAGMA table_info(" .. a_TableName .. ")", RemoveExistingColumn))) then
|
|
|
|
LOGWARNING(PluginPrefix .. "Cannot query DB table structure");
|
2013-06-06 13:10:45 -04:00
|
|
|
return false;
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Create the missing columns
|
|
|
|
-- a_Columns now contains only those columns that are missing in the DB
|
|
|
|
if (#a_Columns > 0) then
|
|
|
|
LOGINFO(PluginPrefix .. "Database table \"" .. a_TableName .. "\" is missing " .. #a_Columns .. " columns, fixing now.");
|
|
|
|
for idx, ColumnName in ipairs(a_Columns) do
|
2013-06-07 12:28:37 -04:00
|
|
|
if (not(self:DBExec("ALTER TABLE '" .. a_TableName .. "' ADD COLUMN " .. ColumnName))) then
|
|
|
|
LOGWARNING(PluginPrefix .. "Cannot add DB table \"" .. a_TableName .. "\" column \"" .. ColumnName .. "\"");
|
2013-06-06 13:10:45 -04:00
|
|
|
return false;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
LOGINFO(PluginPrefix .. "Database table \"" .. a_TableName .. "\" columns fixed.");
|
|
|
|
end
|
|
|
|
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-07 16:19:36 -04:00
|
|
|
--- Returns true if the specified area is allowed for the specified player
|
|
|
|
function cStorage:IsAreaAllowed(a_AreaID, a_PlayerName, a_WorldName)
|
|
|
|
local res = false;
|
|
|
|
local sql = "SELECT COUNT(*) FROM AllowedUsers WHERE (AreaID = " .. a_AreaID ..
|
|
|
|
") AND (UserName ='" .. a_PlayerName .. "')";
|
|
|
|
local function SetResTrue(UserData, NumValues, Values, Names)
|
|
|
|
res = (tonumber(Values[1]) > 0);
|
|
|
|
return 0;
|
|
|
|
end
|
|
|
|
if (not(self:DBExec(sql, SetResTrue))) then
|
|
|
|
LOGWARNING("SQL error while determining area allowance");
|
|
|
|
return false;
|
|
|
|
end
|
|
|
|
return res;
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-07 12:28:37 -04:00
|
|
|
--- Loads cPlayerAreas for the specified player from the DB. Returns a cPlayerAreas object
|
|
|
|
function cStorage:LoadPlayerAreas(a_PlayerName, a_PlayerX, a_PlayerZ, a_WorldName)
|
2013-06-07 16:19:36 -04:00
|
|
|
res = cPlayerAreas:new();
|
2013-06-07 12:28:37 -04:00
|
|
|
|
2013-06-07 16:19:36 -04:00
|
|
|
-- Bounds for which the areas are loaded
|
|
|
|
local BoundsMinX = a_PlayerX - g_AreaBounds;
|
|
|
|
local BoundsMaxX = a_PlayerX + g_AreaBounds;
|
|
|
|
local BoundsMinZ = a_PlayerZ - g_AreaBounds;
|
|
|
|
local BoundsMaxZ = a_PlayerZ + g_AreaBounds;
|
2013-06-07 12:28:37 -04:00
|
|
|
|
2013-06-07 16:19:36 -04:00
|
|
|
-- Load the areas from the DB, based on the player's location
|
|
|
|
local sql =
|
|
|
|
"SELECT ID, MinX, MaxX, MinZ, MaxZ FROM Areas WHERE " ..
|
|
|
|
"MinX < " .. BoundsMaxX .. " AND MaxX > " .. BoundsMinX .. " AND " ..
|
|
|
|
"MinZ < " .. BoundsMaxZ .. " AND MaxZ > " .. BoundsMinZ .. " AND " ..
|
|
|
|
"WorldName='" .. a_WorldName .."'";
|
|
|
|
|
|
|
|
local function AddAreas(UserData, NumValues, Values, Names)
|
|
|
|
if ((NumValues < 5) or ((Values[1] and Values[2] and Values[3] and Values[4] and Values[5]) == nil)) then
|
|
|
|
LOGWARNING("SQL query didn't return all data");
|
|
|
|
return 0;
|
|
|
|
end
|
|
|
|
res:AddArea(cCuboid(Values[2], 0, Values[4], Values[3], 255, Values[5]), self:IsAreaAllowed(Values[1], a_PlayerName, a_WorldName));
|
|
|
|
return 0;
|
|
|
|
end
|
|
|
|
|
|
|
|
if (not(self:DBExec(sql, AddAreas))) then
|
|
|
|
LOGWARNING("SQL error while querying areas");
|
|
|
|
return res;
|
|
|
|
end
|
|
|
|
|
2013-06-07 12:28:37 -04:00
|
|
|
return res;
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-06 16:42:42 -04:00
|
|
|
--- Adds a new area into the DB. a_AllowedNames is a table listing all the players that are allowed in the area
|
2013-06-08 11:06:24 -04:00
|
|
|
-- Returns the ID of the new area, or -1 on failure
|
2013-06-07 12:28:37 -04:00
|
|
|
function cStorage:AddArea(a_Cuboid, a_WorldName, a_CreatorName, a_AllowedNames)
|
2013-06-06 16:42:42 -04:00
|
|
|
-- Store the area in the DB
|
|
|
|
local ID = -1;
|
|
|
|
local function RememberID(UserData, NumCols, Values, Names)
|
|
|
|
for i = 1, NumCols do
|
|
|
|
if (Names[i] == "ID") then
|
|
|
|
ID = Values[i];
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return 0;
|
|
|
|
end
|
|
|
|
local sql =
|
2013-06-07 12:28:37 -04:00
|
|
|
"INSERT INTO Areas (ID, MinX, MaxX, MinZ, MaxZ, WorldName, CreatorUserName) VALUES (NULL, " ..
|
|
|
|
a_Cuboid.p1.x .. ", " .. a_Cuboid.p2.x .. ", " .. a_Cuboid.p1.z .. ", " .. a_Cuboid.p2.z ..
|
|
|
|
", '" .. a_WorldName .. "', '" .. a_CreatorName ..
|
2013-06-07 16:19:36 -04:00
|
|
|
"'); SELECT last_insert_rowid() AS ID";
|
2013-06-06 16:42:42 -04:00
|
|
|
if (not(self:DBExec(sql, RememberID))) then
|
|
|
|
LOGWARNING(PluginPrefix .. "SQL Error while inserting new area");
|
2013-06-08 11:06:24 -04:00
|
|
|
return -1;
|
2013-06-06 16:42:42 -04:00
|
|
|
end
|
|
|
|
if (ID == -1) then
|
|
|
|
LOGWARNING(PluginPrefix .. "SQL Error while retrieving INSERTion ID");
|
2013-06-08 11:06:24 -04:00
|
|
|
return -1;
|
2013-06-06 16:42:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Store each allowed player in the DB
|
|
|
|
for idx, Name in ipairs(a_AllowedNames) do
|
|
|
|
local sql = "INSERT INTO AllowedUsers (AreaID, UserName) VALUES (" .. ID .. ", '" .. Name .. "')";
|
|
|
|
if (not(self:DBExec(sql))) then
|
|
|
|
LOGWARNING(PluginPrefix .. "SQL Error while inserting new area's allowed player " .. Name);
|
2013-06-07 12:28:37 -04:00
|
|
|
end
|
2013-06-06 16:42:42 -04:00
|
|
|
end
|
2013-06-08 11:06:24 -04:00
|
|
|
return ID;
|
2013-06-06 16:42:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-06 13:10:45 -04:00
|
|
|
|
2013-06-07 12:28:37 -04:00
|
|
|
function cStorage:DelArea(a_WorldName, a_AreaID)
|
|
|
|
-- Since all areas are stored in a single DB (for now), the worldname parameter isn't used at all
|
|
|
|
-- Later if we change to a per-world DB, we'll need the world name
|
|
|
|
|
|
|
|
-- Delete from both tables simultaneously
|
|
|
|
local sql =
|
|
|
|
"DELETE FROM Areas WHERE ID=" .. a_AreaID .. ";" ..
|
|
|
|
"DELETE FROM AllowedPlayers WHERE AreaID=" .. a_AreaID;
|
|
|
|
if (not(self:DBExec(sql))) then
|
|
|
|
LOGWARNING(PluginPrefix .. "SQL error while deleting area " .. a_AreaID .. " from world \"" .. a_WorldName .. "\"");
|
2013-06-06 16:42:42 -04:00
|
|
|
return false;
|
|
|
|
end
|
2013-06-07 12:28:37 -04:00
|
|
|
|
2013-06-06 16:42:42 -04:00
|
|
|
return true;
|
2013-06-06 13:10:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-06 16:42:42 -04:00
|
|
|
|
2013-06-07 12:28:37 -04:00
|
|
|
--- Removes the user from the specified area
|
|
|
|
function cStorage:RemoveUser(a_AreaID, a_UserName, a_WorldName)
|
|
|
|
-- TODO
|
|
|
|
LOGWARNING("cStorage:RemoveUser(): Not implemented yet!");
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--- Removes the user from all areas in the specified world
|
|
|
|
function cStorage:RemoveUserAll(a_UserName, a_WorldName)
|
|
|
|
-- TODO
|
|
|
|
LOGWARNING("cStorage:RemoveUserAll(): Not implemented yet!");
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-08 11:06:24 -04:00
|
|
|
|
|
|
|
--- Calls the callback for each area intersecting the specified coords
|
|
|
|
-- Callback signature: function(ID, MinX, MinZ, MaxX, MaxZ, CreatorName)
|
|
|
|
function cStorage:ForEachArea(a_BlockX, a_BlockZ, a_WorldName, a_Callback)
|
|
|
|
|
|
|
|
-- SQL callback that parses the values and calls our callback
|
|
|
|
function CallCallback(UserData, NumValues, Values, Names)
|
|
|
|
if (NumValues ~= 6) then
|
|
|
|
-- Not enough values returned, skip this row
|
|
|
|
return 0;
|
|
|
|
end
|
|
|
|
local ID = Values[1];
|
|
|
|
local MinX = Values[2];
|
|
|
|
local MaxX = Values[3];
|
|
|
|
local MinZ = Values[4];
|
|
|
|
local MaxZ = Values[5];
|
|
|
|
local CreatorName = Values[6];
|
|
|
|
a_Callback(ID, MinX, MinZ, MaxX, MaxZ, CreatorName);
|
|
|
|
return 0;
|
|
|
|
end
|
|
|
|
|
|
|
|
local sql = "SELECT ID, MinX, MinZ, MaxX, MaxZ, CreatorUserName FROM Areas WHERE " ..
|
|
|
|
"MinX <= " .. a_BlockX .. " AND MaxX >= " .. a_BlockX .. " AND " ..
|
|
|
|
"MinZ <= " .. a_BlockZ .. " AND MaxZ >= " .. a_BlockZ;
|
|
|
|
if (not(self:DBExec(sql, CallCallback))) then
|
|
|
|
LOGWARNING("SQL Error while iterating through areas (cStorage:ForEachArea())");
|
|
|
|
return false;
|
|
|
|
end
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|