Skip to content

Commit

Permalink
Web: Added the latest claims and edits pages. (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
madmaxoft authored Nov 5, 2016
1 parent 9db0bc6 commit 146f8e3
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 2 deletions.
52 changes: 52 additions & 0 deletions Storage_SQLite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,58 @@ end



--- Loads up to a_NumAreas most recently claimed areas
-- Returns an array-table of area descriptions, in recentness order (most recent first)
function SQLite:LoadLatestClaimedAreas(a_NumAreas)
-- Check params:
assert(self)
assert(tonumber(a_NumAreas))

-- Query the DB:
local res = {}
self:ExecuteStatement(
"SELECT * FROM Areas ORDER BY DateClaimed DESC LIMIT " .. tonumber(a_NumAreas),
{},
function (a_Values)
local area = self:FixupAreaAfterLoad(a_Values)
if (area) then
table.insert(res, area)
end
end
)
return res
end





--- Loads up to a_NumAreas most recently changed areas
-- Returns an array-table of area descriptions, in recentness order (most recent first)
function SQLite:LoadLatestChangedAreas(a_NumAreas)
-- Check params:
assert(self)
assert(tonumber(a_NumAreas))

-- Query the DB:
local res = {}
self:ExecuteStatement(
"SELECT * FROM Areas WHERE ((NumPlacedBlocks > 0) OR (NumBrokenBlocks > 0)) ORDER BY DateLastChanged DESC LIMIT " .. tonumber(a_NumAreas),
{},
function (a_Values)
local area = self:FixupAreaAfterLoad(a_Values)
if (area) then
table.insert(res, area)
end
end
)
return res
end





--- Loads all player allowances in the specified world
-- Returns a table that has both an array of the area objects, as well as a map AreaName -> area object
function SQLite:LoadPlayerAllowancesInWorld(a_WorldName, a_PlayerName)
Expand Down
94 changes: 92 additions & 2 deletions WebList.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

-- WebList.lua

-- Implements the webadmin page listing the gallery areas
-- Implements the webadmin page listing the gallery areas, and the News



Expand Down Expand Up @@ -599,17 +599,107 @@ end



local function HandleWebLatestClaims(a_Request)
-- Schedule the previews for a refresh:
local Areas = g_DB:LoadLatestClaimedAreas(g_Config.NumWebNewsClaims or 20)
if (g_Config.WebPreview) then
RefreshPreviewForAreas(Areas)
end

-- Compose the page:
local res =
{
"<h2>Latest claimed areas</h2>\n",
"<table>\n",
"<tr><th>#</th><th>Claimed</th><th>Player</th><th>Position</th><th>Block changes</th>",
g_Config.WebPreview and "<th colspan=4>Preview</th>" or "",
"</tr>\n"
}
local requestBasePath = a_Request.Path .. "/../"
for idx, area in ipairs(Areas) do
local cells =
{
idx,
string.gsub(area.DateClaimed, "T", " "),
cWebAdmin:GetHTMLEscapedString(area.PlayerName),
cWebAdmin:GetHTMLEscapedString(area.GalleryName) .. " " .. area.GalleryIndex,
"+" .. area.NumPlacedBlocks .. " / -" .. area.NumBrokenBlocks
}
if (g_Config.WebPreview) then
for rot = 0, 3 do
table.insert(cells, string.format("<img src=\"/~%s%s?action=getpreview&galleryname=%s&galleryidx=%d&rot=%d\"/>",
requestBasePath, area.GalleryName, area.GalleryName, area.GalleryIndex, rot)
)
end
end
table.insert(res, "<tr><td valign='top'>" .. table.concat(cells, "</td><td valign='top'>") .. "</td></tr>\n")
end
table.insert(res, "</table>\n")
return table.concat(res)
end





local function HandleWebLatestChanges(a_Request)
-- Schedule the previews for a refresh:
local Areas = g_DB:LoadLatestChangedAreas(g_Config.NumWebNewsChanges or 20)
if (g_Config.WebPreview) then
RefreshPreviewForAreas(Areas)
end

-- Compose the page:
local res =
{
"<h2>Latest edits</h2>\n",
"<table>\n",
"<tr><th>#</th><th>Changed</th><th>Player</th><th>Position</th><th>Total block changes</th>",
g_Config.WebPreview and "<th colspan=4>Preview</th>" or "",
"</tr>\n"
}
local requestBasePath = a_Request.Path .. "/../"
for idx, area in ipairs(Areas) do
local cells =
{
idx,
string.gsub(area.DateLastChanged, "T", " "),
cWebAdmin:GetHTMLEscapedString(area.PlayerName),
cWebAdmin:GetHTMLEscapedString(area.GalleryName) .. " " .. area.GalleryIndex,
"+" .. area.NumPlacedBlocks .. " / -" .. area.NumBrokenBlocks
}
if (g_Config.WebPreview) then
for rot = 0, 3 do
table.insert(cells, string.format("<img src=\"/~%s%s?action=getpreview&galleryname=%s&galleryidx=%d&rot=%d\"/>",
requestBasePath, area.GalleryName, area.GalleryName, area.GalleryIndex, rot)
)
end
end
table.insert(res, "<tr><td valign='top'>" .. table.concat(cells, "</td><td valign='top'>") .. "</td></tr>\n")
end
table.insert(res, "</table>\n")
return table.concat(res)
end





--- Registers the web page in the webadmin and does whatever initialization is needed
function InitWebList()
-- For each gallery, add a webadmin tab of the name, and a custom handler producing HTML for that gallery
for _, gal in ipairs(g_Galleries) do
cPluginManager:Get():GetCurrentPlugin():AddWebTab(gal.Name,
cWebAdmin:AddWebTab(gal.Name, gal.Name,
function (a_Request)
return BuildGalleryPage(gal, a_Request)
end
)
end

-- Add the News tabs:
cWebAdmin:AddWebTab("Latest claims", "latest-claims", HandleWebLatestClaims)
cWebAdmin:AddWebTab("Latest changes", "latest-changes", HandleWebLatestChanges)

if (g_Config.WebPreview) then
InitWebPreview()
end
Expand Down

0 comments on commit 146f8e3

Please sign in to comment.