1
0

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
This commit is contained in:
madmaxoft@gmail.com 2013-06-07 20:19:36 +00:00
parent 018adc3153
commit b244f206d5
3 changed files with 58 additions and 14 deletions

View File

@ -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);

View File

@ -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

View File

@ -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";
-- 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;
-- 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
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
@ -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;