Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Event datatable

From Sekaipedia
Revision as of 14:57, 7 December 2022 by ChaoticShadow (talk | contribs)

Documentation for this module may be created at Module:Event datatable/doc

local getArgs          = require('Module:Arguments').getArgs
local DatatableBuilder = require('Module:DatatableBuilder')
local QueryBuilder     = require('Module:QueryBuilder')
local aicon     = require('Module:Attribute icon')._main
local cicon     = require('Module:Character icon')._main
local icons     = require('Module:Icons')._main

local p = {}

local function splitStringWithDelim(delim)
	return function(str)
		if str == nil then
			return nil
		end
		
		local list = mw.text.split(str, delim)
		for i=1,#list do
			list[i] = mw.text.trim(list[i])
		end
		
		return list
	end
end

local function formatLogo(logo)
	if logo then
		return string.format('[[File:%s|150px]]', logo)
	end
	
	return nil
end

local function formatName(pageName, eventName)
	return string.format('[[%s|%s]]', pageName, eventName)
end

local function formatUnit(unit)
	if unit and unit ~= 'Mixed' then
		return string.format(
			'%s %s',
			icons({ unit, size = '25px' }),
			unit
		)
	end
	
	return unit
end

local function formatCharacters(characters)
	local characters = splitStringWithDelim(';')(characters)
	local icon_tbl = {}

	for _,x in ipairs(characters) do
		local _icon = cicon({ mw.text.trim(x), size = '40px' })
		if _icon then
			table.insert(icon_tbl, _icon)
		end
	end
	
	return table.concat(icon_tbl, '')
				
	-- local res = {}
	-- local chunkSize = 5
	
	-- for a=0,math.floor(#icon_tbl/chunkSize),1 do
	-- 	local section = {}
	-- 	for b=0,chunkSize-1,1 do
	-- 		table.insert(section, icon_tbl[a * chunkSize + b + 1])
	-- 	end
	-- 	table.insert(res, table.concat(section, ' '))
	-- end
				
	-- return table.concat(res, '<br>')
end

local function formatAttribute(attribute)
	if attribute then
		return string.format(
			'%s %s',
			aicon({ attribute, size = '25px' }),
			attribute
		)
	end
end


function p.main(frame)
	local args = getArgs(frame)
	
	local qb = QueryBuilder.new()
	qb
		:setTables('events')
		:setFields([[
			event_id,
			_pageName,
			image,
			event_name,
			DATE_FORMAT(DATE_ADD(start, INTERVAL 9 HOUR), "%Y/%m/%d, %H:%i JST") = start,
			DATE_FORMAT(DATE_ADD(end, INTERVAL 9 HOUR), "%Y/%m/%d, %H:%i JST") =  end,
			unit_focus,
			type,
			characters,
			attribute
		]])
		:addWhere('event_id', '>', 0)
		:setOrderBy('event_id ASC')
		:setGroupBy('_pageID')
        :setLimit(200)
	qb
		:addWhere('type', '=', args['type'])
		:addWhere('start', '<', args['start before'])
		:addWhere('start', '>', args['start after'])
		:addWhere('end', '<', args['end before'])
		:addWhere('end', '>', args['end after'])
	
	local datatable = DatatableBuilder.new()
	datatable
		:setColumns{
			{
				name = 'id',
				header = 'ID',
				dataFields = { 'event_id' }
			},
			{
				name = 'logo',
				header = 'Logo',
				dataFields = { 'image' },
				func = formatLogo,
				sortable = false
			},
			{
				name = 'name',
				header = 'Event',
				dataFields = { '_pageName', 'event_name' },
				func = formatName,
				css = { ['min-width'] = '150px' },
			},
			{
				name = 'unit',
				header = 'Unit focus',
				dataFields = { 'unit_focus' },
				func = formatUnit,
				css = { ['min-width'] = '200px' },
			},
			{
				name = 'start',
				header = 'Start date',
				dataFields = { 'start' }
			},
			{
				name = 'end',
				header = 'End date',
				dataFields = { 'end' }
			},
			{
				name = 'characters',
				header = 'Bonus characters',
				dataFields = { 'characters' },
				func = formatCharacters,
				css = { ['min-width'] = '150px' },
			},
			{
				name = 'attribute',
				header = 'Bonus attribute',
				dataFields = { 'attribute' },
				func = formatAttribute,
				css = { ['min-width'] = '150px' },
			},
			{
				name = 'type',
				header = 'Type',
				dataFields = { 'type' }
			},
		}
		:setData(qb:query())
	
	return datatable:tostring()
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.