Module:GameLanding/Latest news

From TwogPedia
< Module:GameLanding
Revision as of 11:30, 29 September 2023 by Couchor (talk | contribs) (Created page with "local cargo = mw.ext.cargo local p = {} local prizeToString = require('Module:Functions').prizeToString function p.news(game) local tables = 'News' local fields = '_pageName, date, game' local cargoArgs = { where = 'game HOLDS "' .. game .. '"', orderBy = 'date DESC', limit = 10 } local results = cargo.query(tables, fields, cargoArgs) if #results > 0 then local list = '' for i = 1, #results do local result = results[i] local dateString = mw.u...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:GameLanding/Latest news/doc

local cargo = mw.ext.cargo
local p = {}
local prizeToString = require('Module:Functions').prizeToString

function p.news(game)
	local tables = 'News'
	local fields = '_pageName, date, game'
	
	local cargoArgs = {
		where = 'game HOLDS "' .. game .. '"',
		orderBy = 'date DESC',
		limit = 10
	}
	local results = cargo.query(tables, fields, cargoArgs)
	
	if #results > 0 then
		local list = ''
	
		for i = 1, #results do
			local result = results[i]
			local dateString = mw.ustring.gsub(result.date, "%s(AM|PM)$", "")

			-- Use os.date to convert the string to a table of date and time components
			local dateTimeTable = os.date("*t", os.time({year=string.sub(dateString, 1, 4), month=string.sub(dateString, 6, 7), day=string.sub(dateString, 9, 10), hour=tonumber(string.sub(dateString, 12, 13)) + ((string.sub(dateString, 22, 22) == "PM") and 12 or 0), min=string.sub(dateString, 15, 16), sec=string.sub(dateString, 18, 19)}))
			
			-- Use os.date again to format the date and time components into a desired format
			local formattedDate = os.date("%d %b %Y %H:%M", os.time(dateTimeTable))

			local news = mw.html.create('div'):wikitext(formattedDate .. ' - [[' .. result._pageName .. ']]')

			list = list .. tostring(news)
		end
		
		local latestNews = mw.html.create('div'):addClass('box__container'):node(mw.html.create('div'):addClass('box__title'):wikitext('Latest news')):node(mw.html.create('div'):addClass('tc'):node(list))
		return latestNews
	end
	return nil
end

return p