From 146f8e35b8f2d3ec414cbb07bf772b5dd2077984 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sat, 5 Nov 2016 22:57:57 +0100 Subject: [PATCH] Web: Added the latest claims and edits pages. (#56) --- Storage_SQLite.lua | 52 +++++++++++++++++++++++++ WebList.lua | 94 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 144 insertions(+), 2 deletions(-) diff --git a/Storage_SQLite.lua b/Storage_SQLite.lua index 0e133ac..fe18903 100644 --- a/Storage_SQLite.lua +++ b/Storage_SQLite.lua @@ -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) diff --git a/WebList.lua b/WebList.lua index bf1e6c1..d38c7d9 100644 --- a/WebList.lua +++ b/WebList.lua @@ -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 @@ -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 = + { + "

Latest claimed areas

\n", + "\n", + "", + g_Config.WebPreview and "" or "", + "\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("", + requestBasePath, area.GalleryName, area.GalleryName, area.GalleryIndex, rot) + ) + end + end + table.insert(res, "\n") + end + table.insert(res, "
#ClaimedPlayerPositionBlock changesPreview
" .. table.concat(cells, "") .. "
\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 = + { + "

Latest edits

\n", + "\n", + "", + g_Config.WebPreview and "" or "", + "\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("", + requestBasePath, area.GalleryName, area.GalleryName, area.GalleryIndex, rot) + ) + end + end + table.insert(res, "\n") + end + table.insert(res, "
#ChangedPlayerPositionTotal block changesPreview
" .. table.concat(cells, "") .. "
\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