2013-05-26 15:20:49 -04:00
|
|
|
|
|
|
|
-- CommandHandlers.lua
|
|
|
|
-- Defines the individual command handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function InitializeCommandHandlers()
|
|
|
|
local PlgMgr = cRoot:Get():GetPluginManager();
|
|
|
|
for idx, Cmd in ipairs(CommandReg()) do
|
|
|
|
PlgMgr:BindCommand(Cmd[2], Cmd[3], Cmd[1], Cmd[4]);
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--- Handles the ProtAdd command
|
|
|
|
function HandleAddArea(a_Split, a_Player)
|
|
|
|
-- Command syntax: ProtAdd username1 [username2] [username3] ...
|
|
|
|
if (#a_Split < 2) then
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.ErrExpectedListOfUsernames);
|
2013-05-26 15:20:49 -04:00
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
2013-06-06 16:42:42 -04:00
|
|
|
-- Get the cuboid that the player had selected
|
|
|
|
local CmdState = GetCommandStateForPlayer(a_Player);
|
|
|
|
if (CmdState == nil) then
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.ErrCmdStateNilAddArea);
|
2013-06-06 16:42:42 -04:00
|
|
|
return true;
|
|
|
|
end
|
|
|
|
local Cuboid = CmdState:GetCurrentCuboid();
|
|
|
|
if (Cuboid == nil) then
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.ErrNoAreaWanded);
|
2013-06-06 16:42:42 -04:00
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Put all allowed players into a table:
|
|
|
|
AllowedNames = {};
|
|
|
|
for i = 2, #a_Split do
|
|
|
|
table.insert(AllowedNames, a_Split[i]);
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Add the area to the storage
|
2013-06-08 11:06:24 -04:00
|
|
|
local AreaID = g_Storage:AddArea(Cuboid, a_Player:GetWorld():GetName(), a_Player:GetName(), AllowedNames);
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(string.format(g_Msgs.AreaAdded, AreaID));
|
2013-06-06 16:42:42 -04:00
|
|
|
|
2013-06-07 12:28:37 -04:00
|
|
|
-- Reload all currently logged in players
|
|
|
|
ReloadAllPlayersInWorld(a_Player:GetWorld():GetName());
|
2013-05-26 15:20:49 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function HandleAddAreaCoords(a_Split, a_Player)
|
2013-06-07 12:28:37 -04:00
|
|
|
-- Command syntax: ProtAddCoords x1 z1 x2 z2 username1 [username2] [username3] ...
|
|
|
|
if (#a_Split < 6) then
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.ErrExpectedCoordsUsernames);
|
2013-06-07 12:28:37 -04:00
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Convert the coords to a cCuboid
|
|
|
|
local x1, z1 = tonumber(a_Split[2]), tonumber(a_Split[3]);
|
|
|
|
local x2, z2 = tonumber(a_Split[4]), tonumber(a_Split[5]);
|
|
|
|
if ((x1 == nil) or (z1 == nil) or (x2 == nil) or (z2 == nil)) then
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.ErrParseCoords);
|
2013-06-07 12:28:37 -04:00
|
|
|
return true;
|
|
|
|
end
|
|
|
|
local Cuboid = cCuboid(x1, 0, z1, x2, 255, z1);
|
|
|
|
Cuboid:Sort();
|
|
|
|
|
|
|
|
-- Put all allowed players into a table:
|
|
|
|
AllowedNames = {};
|
|
|
|
for i = 6, #a_Split do
|
|
|
|
table.insert(AllowedNames, a_Split[i]);
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Add the area to the storage
|
2013-06-08 11:06:24 -04:00
|
|
|
local AreaID = g_Storage:AddArea(Cuboid, a_Player:GetWorld():GetName(), a_Player:GetName(), AllowedNames);
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(string.format(g_Msgs.AreaAdded, AreaID));
|
2013-06-07 12:28:37 -04:00
|
|
|
|
|
|
|
-- Reload all currently logged in players
|
|
|
|
ReloadAllPlayersInWorld(a_Player:GetWorld():GetName());
|
|
|
|
|
|
|
|
return true;
|
2013-05-26 15:20:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function HandleAddAreaUser(a_Split, a_Player)
|
2013-06-07 12:28:37 -04:00
|
|
|
-- Command syntax: ProtAddUser AreaID username1 [username2] [username3] ...
|
|
|
|
if (#a_Split < 3) then
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.ErrExpectedAreaIDUsernames);
|
2013-06-07 12:28:37 -04:00
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Put all allowed players into a table:
|
|
|
|
AllowedNames = {};
|
|
|
|
for i = 3, #a_Split do
|
|
|
|
table.insert(AllowedNames, a_Split[i]);
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Add the area to the storage
|
2013-06-08 15:16:57 -04:00
|
|
|
if (not(g_Storage:AddAreaUsers(
|
|
|
|
tonumber(a_Split[2]), a_Player:GetWorld():GetName(), a_Player:GetName(), AllowedNames))
|
|
|
|
) then
|
|
|
|
LOGWARNING("g_Storage:AddAreaUsers failed");
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.ErrDBFailAddUsers);
|
2013-06-08 15:16:57 -04:00
|
|
|
return true;
|
|
|
|
end
|
|
|
|
if (#AllowedNames == 0) then
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.AllUsersAlreadyAllowed);
|
2013-06-08 15:16:57 -04:00
|
|
|
else
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(string.format(g_Msgs.UsersAdded, table.concat(AllowedNames, ", ")));
|
2013-06-08 15:16:57 -04:00
|
|
|
end
|
2013-06-07 12:28:37 -04:00
|
|
|
|
|
|
|
-- Reload all currently logged in players
|
|
|
|
ReloadAllPlayersInWorld(a_Player:GetWorld():GetName());
|
|
|
|
|
|
|
|
return true;
|
2013-05-26 15:20:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function HandleDelArea(a_Split, a_Player)
|
2013-06-07 12:28:37 -04:00
|
|
|
-- Command syntax: ProtDelArea AreaID
|
|
|
|
if (#a_Split ~= 2) then
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.ErrExpectedAreaID);
|
2013-06-07 12:28:37 -04:00
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Parse the AreaID
|
|
|
|
local AreaID = tonumber(a_Split[2]);
|
|
|
|
if (AreaID == nil) then
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.ErrParseAreaID);
|
2013-06-07 12:28:37 -04:00
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Delete the area
|
|
|
|
g_Storage:DelArea(a_Player:GetWorld():GetName(), AreaID);
|
|
|
|
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(string.format(g_Msgs.AreaDeleted, AreaID));
|
2013-06-07 12:28:37 -04:00
|
|
|
-- Reload all currently logged in players
|
|
|
|
ReloadAllPlayersInWorld(a_Player:GetWorld():GetName());
|
|
|
|
|
|
|
|
return true;
|
2013-05-26 15:20:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function HandleGiveWand(a_Split, a_Player)
|
|
|
|
local NumGiven = a_Player:GetInventory():AddItem(cConfig:GetWandItem());
|
|
|
|
if (NumGiven == 1) then
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.WandGiven);
|
2013-05-26 15:20:49 -04:00
|
|
|
else
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.ErrNoSpaceForWand);
|
2013-05-26 15:20:49 -04:00
|
|
|
end
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function HandleListAreas(a_Split, a_Player)
|
2013-06-07 12:28:37 -04:00
|
|
|
-- Command syntax: ProtListAreas [x, z]
|
|
|
|
|
|
|
|
local x, z;
|
|
|
|
if (#a_Split == 1) then
|
|
|
|
-- Get the last "wanded" coord
|
|
|
|
local CmdState = GetCommandStateForPlayer(a_Player);
|
|
|
|
if (CmdState == nil) then
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.ErrCmdStateNilListAreas);
|
2013-06-07 12:28:37 -04:00
|
|
|
return true;
|
|
|
|
end
|
|
|
|
x, z = CmdState:GetLastCoords();
|
|
|
|
if ((x == nil) or (z == nil)) then
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.ErrListNotWanded);
|
2013-06-07 12:28:37 -04:00
|
|
|
return true;
|
|
|
|
end
|
|
|
|
elseif (#a_Split == 3) then
|
|
|
|
-- Parse the coords from the command params
|
|
|
|
x = tonumber(a_Split[2]);
|
|
|
|
z = tonumber(a_Split[3]);
|
|
|
|
if ((x == nil) or (z == nil)) then
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.ErrParseCoordsListAreas);
|
2013-06-07 12:28:37 -04:00
|
|
|
return true;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- Wrong number of params, report back to the user
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.ErrSyntaxErrorListAreas);
|
2013-06-07 12:28:37 -04:00
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(string.format(g_Msgs.ListAreasHeader, x, z));
|
2013-06-07 12:28:37 -04:00
|
|
|
|
|
|
|
-- List areas intersecting the coords
|
2013-06-08 11:06:24 -04:00
|
|
|
local PlayerName = a_Player:GetName();
|
|
|
|
local WorldName = a_Player:GetWorld():GetName();
|
|
|
|
g_Storage:ForEachArea(x, z, WorldName,
|
|
|
|
function(AreaID, MinX, MinZ, MaxX, MaxZ, CreatorName)
|
2013-06-11 05:55:46 -04:00
|
|
|
local Coords = string.format("%s: {%d, %d} - {%d, %d} ", AreaID, MinX, MinZ, MaxX, MaxZ);
|
2013-06-07 12:28:37 -04:00
|
|
|
local Allowance;
|
2013-06-08 11:06:24 -04:00
|
|
|
if (g_Storage:IsAreaAllowed(AreaID, PlayerName, WorldName)) then
|
2013-06-11 05:55:46 -04:00
|
|
|
Allowance = g_Msgs.AreaAllowed;
|
2013-06-07 12:28:37 -04:00
|
|
|
else
|
2013-06-11 05:55:46 -04:00
|
|
|
Allowance = g_Msgs.AreaNotAllowed;
|
2013-06-07 12:28:37 -04:00
|
|
|
end
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(string.format(g_Msgs.ListAreasRow, Coords, Allowance, CreatorName));
|
2013-06-07 12:28:37 -04:00
|
|
|
end
|
|
|
|
);
|
|
|
|
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.ListAreasFooter);
|
2013-06-07 12:28:37 -04:00
|
|
|
return true;
|
2013-05-26 15:20:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-08 12:47:03 -04:00
|
|
|
--- Lists all allowed users for a particular area
|
|
|
|
function HandleListUsers(a_Split, a_Player)
|
|
|
|
-- Command syntax: ProtListUsers AreaID
|
|
|
|
if (#a_Split ~= 2) then
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.ErrExpectedAreaID);
|
2013-06-08 12:47:03 -04:00
|
|
|
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
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(string.format(g_Msgs.ErrNoSuchArea, AreaID));
|
2013-06-08 12:47:03 -04:00
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Send the header
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(string.format(g_Msgs.ListUsersHeader, AreaID, MinX, MinZ, MaxX, MaxZ, CreatorName));
|
2013-06-08 12:47:03 -04:00
|
|
|
|
|
|
|
-- List and count the allowed users
|
|
|
|
local NumUsers = 0;
|
|
|
|
g_Storage:ForEachUserInArea(AreaID, WorldName,
|
|
|
|
function(UserName)
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(string.format(g_Msgs.ListUsersRow, UserName));
|
2013-06-08 12:47:03 -04:00
|
|
|
NumUsers = NumUsers + 1;
|
|
|
|
end
|
|
|
|
);
|
|
|
|
|
|
|
|
-- Send the footer
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(string.format(g_Msgs.ListUsersFooter, AreaID, NumUsers));
|
2013-06-08 12:47:03 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-26 15:20:49 -04:00
|
|
|
|
|
|
|
function HandleRemoveUser(a_Split, a_Player)
|
2013-06-07 12:28:37 -04:00
|
|
|
-- Command syntax: ProtRemUser AreaID UserName
|
|
|
|
if (#a_Split ~= 3) then
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.ErrExpectedAreaIDUserName);
|
2013-06-07 12:28:37 -04:00
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Parse the AreaID
|
|
|
|
local AreaID = tonumber(a_Split[2]);
|
|
|
|
if (AreaID == nil) then
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.ErrParseAreaID);
|
2013-06-07 12:28:37 -04:00
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Remove the user from the DB
|
2013-06-08 12:47:03 -04:00
|
|
|
local UserName = a_Split[3];
|
|
|
|
g_Storage:RemoveUser(AreaID, UserName, a_Player:GetWorld():GetName());
|
|
|
|
|
|
|
|
-- Send confirmation
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(string.format(g_Msgs.RemovedUser, UserName, AreaID));
|
2013-06-08 12:47:03 -04:00
|
|
|
|
2013-06-10 02:47:03 -04:00
|
|
|
-- Reload all currently logged in players
|
|
|
|
ReloadAllPlayersInWorld(a_Player:GetWorld():GetName());
|
|
|
|
|
2013-06-07 12:28:37 -04:00
|
|
|
return true;
|
2013-05-26 15:20:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function HandleRemoveUserAll(a_Split, a_Player)
|
2013-06-07 12:28:37 -04:00
|
|
|
-- Command syntax: ProtRemUserAll UserName
|
|
|
|
if (#a_Split ~= 2) then
|
2013-06-11 05:55:46 -04:00
|
|
|
a_Player:SendMessage(g_Msgs.ErrExpectedUserName);
|
2013-06-07 12:28:37 -04:00
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Remove the user from the DB
|
2013-06-08 15:49:26 -04:00
|
|
|
g_Storage:RemoveUserAll(a_Split[2], a_Player:GetWorld():GetName());
|
2013-06-10 02:47:03 -04:00
|
|
|
|
2013-06-11 05:55:46 -04:00
|
|
|
-- Send confirmation
|
|
|
|
a_Player:SendMessage(string.format(g_Msgs.RemovedUserAll, UserName));
|
|
|
|
|
2013-06-10 02:47:03 -04:00
|
|
|
-- Reload all currently logged in players
|
|
|
|
ReloadAllPlayersInWorld(a_Player:GetWorld():GetName());
|
|
|
|
|
2013-06-07 12:28:37 -04:00
|
|
|
return true;
|
2013-05-26 15:20:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|