From 01260be5721003a95664c31885fdecaed873024a Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Sat, 8 Jun 2013 16:47:03 +0000 Subject: [PATCH] ProtectionAreas: Added ProtListUsers, implemented ProtRemoveUser, fixed ProtDelID commands git-svn-id: http://mc-server.googlecode.com/svn/trunk@1565 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- .../ProtectionAreas/CommandHandlers.lua | 49 +++++++++- .../Plugins/ProtectionAreas/CurrentLng.lua | 1 + MCServer/Plugins/ProtectionAreas/Storage.lua | 89 +++++++++++++++++-- 3 files changed, 132 insertions(+), 7 deletions(-) diff --git a/MCServer/Plugins/ProtectionAreas/CommandHandlers.lua b/MCServer/Plugins/ProtectionAreas/CommandHandlers.lua index eec1f8042..4ce0b2f35 100644 --- a/MCServer/Plugins/ProtectionAreas/CommandHandlers.lua +++ b/MCServer/Plugins/ProtectionAreas/CommandHandlers.lua @@ -219,6 +219,48 @@ end +--- Lists all allowed users for a particular area +function HandleListUsers(a_Split, a_Player) + -- Command syntax: ProtListUsers AreaID + if (#a_Split ~= 2) then + a_Player:SendMessage("Expected AreaID as a parameter"); + end + + -- Get the general info about the area + local AreaID = a_Split[2]; + local WorldName = a_Player:GetWorld():GetName(); + local MinX, MinZ, MaxX, MaxZ, CreatorName = g_Storage:GetArea(AreaID, WorldName); + if (MinX == nil) then + a_Player:SendMessage("No such area: " .. AreaID); + return true; + end + + -- Send the header + a_Player:SendMessage( + "Area ID " .. AreaID .. ": {" .. + MinX .. ", " .. MinZ .. "} - {" .. + MaxX .. ", " .. MaxZ .. "} " .. + "Created by " .. CreatorName .. "; allowed users:" + ); + + -- List and count the allowed users + local NumUsers = 0; + g_Storage:ForEachUserInArea(AreaID, WorldName, + function(UserName) + a_Player:SendMessage(" " .. UserName); + NumUsers = NumUsers + 1; + end + ); + + -- Send the footer + a_Player:SendMessage("End of area " .. AreaID .. " user list, total " .. NumUsers .. " users"); + + return true; +end + + + + function HandleRemoveUser(a_Split, a_Player) -- Command syntax: ProtRemUser AreaID UserName @@ -235,7 +277,12 @@ function HandleRemoveUser(a_Split, a_Player) end -- Remove the user from the DB - g_Storage:RemoveUser(AreaID, a_Split[3], a_Player:GetWorld():GetName()); + local UserName = a_Split[3]; + g_Storage:RemoveUser(AreaID, UserName, a_Player:GetWorld():GetName()); + + -- Send confirmation + a_Player:SendMessage("Removed " .. UserName .. " from area " .. AreaID); + return true; end diff --git a/MCServer/Plugins/ProtectionAreas/CurrentLng.lua b/MCServer/Plugins/ProtectionAreas/CurrentLng.lua index 0c5fc636b..8fda353ae 100644 --- a/MCServer/Plugins/ProtectionAreas/CurrentLng.lua +++ b/MCServer/Plugins/ProtectionAreas/CurrentLng.lua @@ -18,6 +18,7 @@ function CommandReg() {HandleDelArea, "/ProtDelID", "Prot.Del", "Deletes a protected area by ID"}, {HandleGiveWand, "/ProtWand", "Prot.Wand", "Gives you the wand used for protection"}, {HandleListAreas, "/ProtList", "Prot.List", "Lists all areas for the marked block"}, + {HandleListUsers, "/ProtUsers", "Prot.List", "Lists all allowed users for a given area ID"}, {HandleRemoveUser, "/ProtRemUser", "Prot.RemUser", "Removes a user from the protected area"}, {HandleRemoveUserAll, "/ProtRemUserAll", "Prot.RemUser", "Removes a user from all protected areas"}, }; diff --git a/MCServer/Plugins/ProtectionAreas/Storage.lua b/MCServer/Plugins/ProtectionAreas/Storage.lua index b84415c1d..118ab0434 100644 --- a/MCServer/Plugins/ProtectionAreas/Storage.lua +++ b/MCServer/Plugins/ProtectionAreas/Storage.lua @@ -258,7 +258,7 @@ function cStorage:DelArea(a_WorldName, a_AreaID) -- Delete from both tables simultaneously local sql = "DELETE FROM Areas WHERE ID=" .. a_AreaID .. ";" .. - "DELETE FROM AllowedPlayers WHERE AreaID=" .. a_AreaID; + "DELETE FROM AllowedUsers 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; @@ -273,8 +273,14 @@ end --- Removes the user from the specified area function cStorage:RemoveUser(a_AreaID, a_UserName, a_WorldName) - -- TODO - LOGWARNING("cStorage:RemoveUser(): Not implemented yet!"); + -- WorldName is not used yet, because all the worlds share the same DB in this version + local sql = "DELETE FROM AllowedUsers WHERE " .. + "AreaID = " .. a_AreaID .. " AND UserName = '" .. a_UserName .. "'"; + if (not(self:DBExec(sql))) then + LOGWARNING("SQL error while removing user " .. a_UserName .. " from area ID " .. a_AreaID); + return false; + end + return true; end @@ -303,8 +309,8 @@ function cStorage:ForEachArea(a_BlockX, a_BlockZ, a_WorldName, a_Callback) end local ID = Values[1]; local MinX = Values[2]; - local MaxX = Values[3]; - local MinZ = Values[4]; + local MinZ = Values[3]; + local MaxX = Values[4]; local MaxZ = Values[5]; local CreatorName = Values[6]; a_Callback(ID, MinX, MinZ, MaxX, MaxZ, CreatorName); @@ -313,7 +319,8 @@ function cStorage:ForEachArea(a_BlockX, a_BlockZ, a_WorldName, a_Callback) 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; + "MinZ <= " .. a_BlockZ .. " AND MaxZ >= " .. a_BlockZ .. " AND " .. + "WorldName = '" .. a_WorldName .. "'"; if (not(self:DBExec(sql, CallCallback))) then LOGWARNING("SQL Error while iterating through areas (cStorage:ForEachArea())"); return false; @@ -325,3 +332,73 @@ end +--- Returns the info on the specified area +-- Returns MinX, MinZ, MaxX, MaxZ, CreatorName on success, or nothing on failure +function cStorage:GetArea(a_AreaID, a_WorldName) + + local MinX, MinZ, MaxX, MaxZ, CreatorName; + local HasValues = false; + + -- SQL callback that parses the values and remembers them in variables + function RememberValues(UserData, NumValues, Values, Names) + if (NumValues ~= 5) then + -- Not enough values returned, skip this row + return 0; + end + MinX = Values[1]; + MinZ = Values[2]; + MaxX = Values[3]; + MaxZ = Values[4]; + CreatorName = Values[5]; + HasValues = true; + return 0; + end + + local sql = "SELECT MinX, MinZ, MaxX, MaxZ, CreatorUserName FROM Areas WHERE " .. + "ID = " .. a_AreaID .. " AND WorldName = '" .. a_WorldName .. "'"; + if (not(self:DBExec(sql, RememberValues))) then + LOGWARNING("SQL Error while getting area info (cStorage:ForEachArea())"); + return; + end + + -- If no data has been retrieved, return nothing + if (not(HasValues)) then + return; + end + + return MinX, MinZ, MaxX, MaxZ, CreatorName; +end + + + + + +--- Calls the callback for each allowed user for the specified area +-- Callback signature: function(UserName) +function cStorage:ForEachUserInArea(a_AreaID, a_WorldName, a_Callback) + assert(a_AreaID); + assert(a_WorldName); + assert(a_Callback); + + -- Since in this version all the worlds share a single DB, the a_WorldName parameter is not actually used + -- But this may change in the future, when we have a per-world DB + + local function CallCallback(UserData, NumValues, Values) + if (NumValues ~= 1) then + return 0; + end + a_Callback(Values[1]); + return 0; + end + local sql = "SELECT UserName FROM AllowedUsers WHERE AreaID = " .. a_AreaID; + if (not(self:DBExec(sql, CallCallback))) then + LOGWARNING("SQL error while iterating area users for AreaID" .. a_AreaID); + return false; + end + return true; +end + + + + +