From b244f206d5c9c22cdae6bdbef273faaa858af5d2 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Fri, 7 Jun 2013 20:19:36 +0000 Subject: ProtectionAreas: The areas are now read from the DB (once) git-svn-id: http://mc-server.googlecode.com/svn/trunk@1563 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- MCServer/Plugins/ProtectionAreas/HookHandlers.lua | 4 +- .../Plugins/ProtectionAreas/ProtectionAreas.lua | 12 +++-- MCServer/Plugins/ProtectionAreas/Storage.lua | 56 ++++++++++++++++++---- 3 files changed, 58 insertions(+), 14 deletions(-) diff --git a/MCServer/Plugins/ProtectionAreas/HookHandlers.lua b/MCServer/Plugins/ProtectionAreas/HookHandlers.lua index 00cbcf3b3..1b22d0ceb 100644 --- a/MCServer/Plugins/ProtectionAreas/HookHandlers.lua +++ b/MCServer/Plugins/ProtectionAreas/HookHandlers.lua @@ -9,9 +9,9 @@ function InitializeHooks(a_Plugin) local PlgMgr = cRoot:Get():GetPluginManager(); PlgMgr:AddHook(a_Plugin, cPluginManager.HOOK_DISCONNECT); - PlgMgr:AddHook(a_Plugin, cPluginManager.HOOK_PLAYER_JOINED); PlgMgr:AddHook(a_Plugin, cPluginManager.HOOK_PLAYER_LEFT_CLICK); PlgMgr:AddHook(a_Plugin, cPluginManager.HOOK_PLAYER_RIGHT_CLICK); + PlgMgr:AddHook(a_Plugin, cPluginManager.HOOK_PLAYER_SPAWNED); end @@ -33,7 +33,7 @@ end; -function OnPlayerJoined(a_Player) +function OnPlayerSpawned(a_Player) -- Create a new cPlayerAreas object for this player if (g_PlayerAreas[a_Player:GetUniqueID()] == nil) then LoadPlayerAreas(a_Player); diff --git a/MCServer/Plugins/ProtectionAreas/ProtectionAreas.lua b/MCServer/Plugins/ProtectionAreas/ProtectionAreas.lua index 744ea7e9c..a8219edf7 100644 --- a/MCServer/Plugins/ProtectionAreas/ProtectionAreas.lua +++ b/MCServer/Plugins/ProtectionAreas/ProtectionAreas.lua @@ -9,6 +9,9 @@ --- Prefix for all messages logged to the server console PluginPrefix = "ProtectionAreas: "; +--- Bounds for the area loading. Areas less this far in any direction from the player will be loaded into cPlayerAreas +g_AreaBounds = 48; + @@ -27,10 +30,11 @@ function Initialize(a_Plugin) InitializeCommandHandlers(); -- We might be reloading, so there may be players already present in the server; reload all of them - local function ReloadPlayers(a_World) - ReloadAllPlayersInWorld(a_World:GetName()); - end - cRoot:Get():ForEachWorld(ReloadPlayers); + cRoot:Get():ForEachWorld( + function(a_World) + ReloadAllPlayersInWorld(a_World:GetName()); + end + ); return true; end diff --git a/MCServer/Plugins/ProtectionAreas/Storage.lua b/MCServer/Plugins/ProtectionAreas/Storage.lua index effdbd029..3c7a09b4e 100644 --- a/MCServer/Plugins/ProtectionAreas/Storage.lua +++ b/MCServer/Plugins/ProtectionAreas/Storage.lua @@ -152,17 +152,57 @@ end +--- 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 + + + + + --- 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(); + 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!"); + -- 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; - -- DEBUG: Insert a dummy area for testing purposes: - res:AddArea(cCuboid(10, 0, 10, 20, 255, 20), false); + -- 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 + return res; end @@ -186,7 +226,7 @@ function cStorage:AddArea(a_Cuboid, a_WorldName, a_CreatorName, a_AllowedNames) "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"; + "'); SELECT last_insert_rowid() AS ID"; if (not(self:DBExec(sql, RememberID))) then LOGWARNING(PluginPrefix .. "SQL Error while inserting new area"); return false; -- cgit v1.2.3