summaryrefslogtreecommitdiffstats
path: root/MCServer/Plugins/ProtectionAreas/Storage.lua
diff options
context:
space:
mode:
authormadmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6>2013-06-07 18:28:37 +0200
committermadmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6>2013-06-07 18:28:37 +0200
commit9790a6817cb4f1b4821de9af2630558cb5df882f (patch)
treeed9fd86bef0a74f0fcdc9b9bed12bb49c274b146 /MCServer/Plugins/ProtectionAreas/Storage.lua
parentProtectionAreas: Newly added areas are stored in the DB (diff)
downloadcuberite-9790a6817cb4f1b4821de9af2630558cb5df882f.tar
cuberite-9790a6817cb4f1b4821de9af2630558cb5df882f.tar.gz
cuberite-9790a6817cb4f1b4821de9af2630558cb5df882f.tar.bz2
cuberite-9790a6817cb4f1b4821de9af2630558cb5df882f.tar.lz
cuberite-9790a6817cb4f1b4821de9af2630558cb5df882f.tar.xz
cuberite-9790a6817cb4f1b4821de9af2630558cb5df882f.tar.zst
cuberite-9790a6817cb4f1b4821de9af2630558cb5df882f.zip
Diffstat (limited to '')
-rw-r--r--MCServer/Plugins/ProtectionAreas/Storage.lua116
1 files changed, 79 insertions, 37 deletions
diff --git a/MCServer/Plugins/ProtectionAreas/Storage.lua b/MCServer/Plugins/ProtectionAreas/Storage.lua
index 307233eac..effdbd029 100644
--- a/MCServer/Plugins/ProtectionAreas/Storage.lua
+++ b/MCServer/Plugins/ProtectionAreas/Storage.lua
@@ -47,20 +47,6 @@ end
---- Loads cPlayerAreas for the specified player from the DB. Returns a cPlayerAreas object
-function cStorage:LoadPlayerAreas(PlayerName)
- local res = cPlayerAreas:new();
- -- TODO: Load the areas from the DB, based on the player's location
-
- -- DEBUG: Insert a dummy area for testing purposes:
- res:AddArea(cCuboid(10, 0, 10, 20, 255, 20), false);
- return res;
-end
-
-
-
-
-
--- Opens the DB and makes sure it has all the columns needed
-- Returns true if successful, false otherwise
function cStorage:OpenDB()
@@ -72,7 +58,7 @@ function cStorage:OpenDB()
end
if (
- not(self:CreateTable("Areas", {"ID INTEGER PRIMARY KEY AUTOINCREMENT", "MinX", "MaxX", "MinZ", "MaxZ", "CreatorUserName"})) or
+ not(self:CreateTable("Areas", {"ID INTEGER PRIMARY KEY AUTOINCREMENT", "MinX", "MaxX", "MinZ", "MaxZ", "WorldName", "CreatorUserName"})) or
not(self:CreateTable("AllowedUsers", {"AreaID", "UserName"}))
) then
LOGWARNING(PluginPrefix .. "Cannot create DB tables!");
@@ -86,6 +72,24 @@ end
+--- 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
+
+
+
+
+
--- 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)
@@ -93,9 +97,8 @@ function cStorage:CreateTable(a_TableName, a_Columns)
local sql = "CREATE TABLE IF NOT EXISTS '" .. a_TableName .. "' (";
sql = sql .. table.concat(a_Columns, ", ");
sql = sql .. ")";
- local ErrCode = self.DB:exec(sql);
- if (ErrCode ~= sqlite3.OK) then
- LOGWARNING(PluginPrefix .. "Cannot create DB Table, error " .. ErrCode .. " (" .. self.DB:errmsg() .. ")");
+ if (not(self:DBExec(sql))) then
+ LOGWARNING(PluginPrefix .. "Cannot create DB Table " .. a_TableName);
return false;
end
@@ -124,9 +127,8 @@ function cStorage:CreateTable(a_TableName, a_Columns)
end -- for i - Names[] / Values[]
return 0;
end
- local ErrCode = self.DB:exec("PRAGMA table_info(" .. a_TableName .. ")", RemoveExistingColumn);
- if (ErrCode ~= sqlite3.OK) then
- LOGWARNING(PluginPrefix .. "Cannot query DB table structure, error " .. ErrCode .. " (" .. self.DB:errmsg() ..")");
+ if (not(self:DBExec("PRAGMA table_info(" .. a_TableName .. ")", RemoveExistingColumn))) then
+ LOGWARNING(PluginPrefix .. "Cannot query DB table structure");
return false;
end
@@ -135,9 +137,8 @@ function cStorage:CreateTable(a_TableName, a_Columns)
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
- local ErrCode = self.DB:exec("ALTER TABLE '" .. a_TableName .. "' ADD COLUMN " .. ColumnName);
- if (ErrCode ~= sqlite3.OK) then
- LOGWARNING(PluginPrefix .. "Cannot add DB table \"" .. a_TableName .. "\" column \"" .. ColumnName .. "\", error " .. ErrCode .. " (" .. self.DB:errmsg() ..")");
+ if (not(self:DBExec("ALTER TABLE '" .. a_TableName .. "' ADD COLUMN " .. ColumnName))) then
+ LOGWARNING(PluginPrefix .. "Cannot add DB table \"" .. a_TableName .. "\" column \"" .. ColumnName .. "\"");
return false;
end
end
@@ -151,8 +152,26 @@ end
+--- Loads cPlayerAreas for the specified player from the DB. Returns a cPlayerAreas object
+function cStorage:LoadPlayerAreas(a_PlayerName, a_PlayerX, a_PlayerZ, a_WorldName)
+ local res = cPlayerAreas:new();
+
+ -- TODO: Load the areas from the DB, based on the player's location
+ -- local sql = "SELECT MinX, MaxX, MinZ, MaxZ FROM Areas WHERE";
+
+ LOGWARNING("cStorage:LoadPlayerAreas(): Not implemented yet!");
+
+ -- DEBUG: Insert a dummy area for testing purposes:
+ res:AddArea(cCuboid(10, 0, 10, 20, 255, 20), false);
+ return res;
+end
+
+
+
+
+
--- Adds a new area into the DB. a_AllowedNames is a table listing all the players that are allowed in the area
-function cStorage:AddArea(a_Cuboid, a_CreatorName, a_AllowedNames)
+function cStorage:AddArea(a_Cuboid, a_WorldName, a_CreatorName, a_AllowedNames)
-- Store the area in the DB
local ID = -1;
local function RememberID(UserData, NumCols, Values, Names)
@@ -164,8 +183,9 @@ function cStorage:AddArea(a_Cuboid, a_CreatorName, a_AllowedNames)
return 0;
end
local sql =
- "INSERT INTO Areas (ID, MinX, MaxX, MinZ, MaxZ, CreatorUserName) VALUES (NULL, " ..
- a_Cuboid.p1.x .. ", " .. a_Cuboid.p2.x .. ", " .. a_Cuboid.p1.z .. ", " .. a_Cuboid.p2.z .. ", '" .. a_CreatorName ..
+ "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 ..
"'); SELECT last_insert_rowid() as ID";
if (not(self:DBExec(sql, RememberID))) then
LOGWARNING(PluginPrefix .. "SQL Error while inserting new area");
@@ -181,25 +201,28 @@ function cStorage:AddArea(a_Cuboid, a_CreatorName, a_AllowedNames)
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);
- end;
+ end
end
+ return true;
end
---- 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 .. "<<"
- );
+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 .. "\"");
return false;
end
+
return true;
end
@@ -207,3 +230,22 @@ end
+--- 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
+
+
+
+