Module:Area

From IdleOn MMO Wiki
Revision as of 23:13, 15 October 2024 by BHY4A (talk | contribs)
local p = {}
local cargo = mw.ext.cargo

local TEMPLATE_OUTTER = [=[[[Worlds#{{{Area_str}}}|<span class="simple-tooltip simple-tooltip-inline tooltipstered" data-simple-tooltip="{{{html_root}}}">{{{Area}}}</span>]]]=]

local TEMPLATE_ROOT_DIV = [=[&lt;div class='Infobox Detailbox'&gt;{{{html_map}}}{{{html_list_enemy}}}&lt;/div&gt;]=]

local TEMPLATE_MAP = [=[&lt;b class='Header'&gt;{{{Area}}}&lt;/b&gt;{{{html_map_svg}}}]=]
local TEMPLATE_MAP_SVG = [=[&lt;svg width=&quot;100%&quot; height=&quot;100%&quot; viewBox=&quot;0 0 808 428&quot; version=&quot;1.1&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;use id=&quot;MapIMG&quot; xlink:href=&quot;#_MapIMG&quot; x=&quot;0&quot; y=&quot;0&quot; width=&quot;808px&quot; height=&quot;428px&quot; /&gt;
{{{html_list_ref_area}}}
{{{html_area}}}
&lt;defs&gt;&lt;image id=&quot;_MapIMG&quot; width=&quot;808px&quot; height=&quot;428px&quot; xlink:href=&quot;{{filepath:{{{WorldImage}}}}}&quot; /&gt;&lt;/defs&gt;
&lt;/svg&gt;]=]
local TEMPLATE_MAP_RECORD_AREA = [=[&lt;circle cx=&quot;{{{x}}}&quot; cy=&quot;{{{y}}}&quot; r=&quot;15&quot; style=&quot;fill:#f0f;stroke:#000;stroke-width:2px;&quot; /&gt;]=]
local TEMPLATE_MAP_RECORD_REF  = [=[&lt;circle cx=&quot;{{{x}}}&quot; cy=&quot;{{{y}}}&quot; r=&quot;7.5&quot; style=&quot;fill:#ff0;stroke:#000;stroke-width:1px;&quot; /&gt;]=]

local TEMPLATE_ENEMY = [=[&lt;b class='SubHeader'&gt;The {{{Enemy}}} map&lt;/b&gt;&lt;i class='HeaderImage'&gt;&lt;img src='{{filepath:{{{EnemyImage}}}}}'&gt;&lt;/i&gt;]=]

local TEMPLATE_ERROR1 = [=[&lt;span style=&quot;color: red; font-weight: bold; font-size: larger;&quot;&gt;Table `AreaData` contains {{{record_count}}} records for `{{{Area}}}`. Check for typo.&lt;/span&gt;]=]

local WorldImageData = mw.loadData('Module:AreaMapDisplay')

local function fillTemplate(template, args)
    return (template:gsub("({{{.-}}})", function(placeholder)
        local name = placeholder:sub(4, -4)
        return args[name] or placeholder
    end))
end

local function prepare_map(Area, WorldID, Area_x, Area_y)
    local html_map_svg = ""
    local WorldImage = WorldImageData[WorldID] and WorldImageData[WorldID].image

    if WorldImage and Area_x and Area_y then
        local results2 = cargo.query(
            'AreaData',
            'Area, World, x, y, ID',
            {where = 'World = "' .. WorldID .. '"'}
        )
        local html_area = fillTemplate(TEMPLATE_MAP_RECORD_AREA, {x = Area_x, y = Area_y})

        local html_list_ref_area = ""
        for _, record in ipairs(results2) do
            html_list_ref_area = html_list_ref_area .. fillTemplate(TEMPLATE_MAP_RECORD_REF, {x = record.x, y = record.y})
        end

        html_map_svg = fillTemplate(TEMPLATE_MAP_SVG, {WorldImage = WorldImage, html_area = html_area, html_list_ref_area = html_list_ref_area})
    end

    local html_map = fillTemplate(TEMPLATE_MAP, {Area = Area, html_map_svg = html_map_svg})

    return html_map
end


local function prepare_enemy(Area)
    local html_enemy = ""
    local results3 = cargo.query(
        'Enemies',
        '_pageName=Enemy, Image=EnemyImage',
        {where = 'Areas HOLDS LIKE "' .. mw.text.nowiki(Area) .. '"'}
    )
    for _, record in ipairs(results3) do
        html_enemy = html_enemy .. fillTemplate(TEMPLATE_ENEMY, {Enemy = record.Enemy, EnemyImage = record.EnemyImage})
    end

    return html_enemy
end

function p.run(frame)
    local args = frame.args
    local Area = args.Area

    local results1 = cargo.query(
        'AreaData',
        'Area, World, x, y, ID',
        {where = 'Area = "' .. mw.text.nowiki(Area) .. '"'}
    )

    local html_root = ""
    local output = ""
    if #results1 == 1 then
        local ID = tostring(results1[1].ID) or ""
        local html_map = prepare_map(Area, results1[1].World, results1[1].x, results1[1].y)
        local html_list_enemy = prepare_enemy(Area)
        html_root = fillTemplate(TEMPLATE_ROOT_DIV, {html_map = html_map, html_list_enemy = html_list_enemy})
        output = fillTemplate(TEMPLATE_OUTTER, {Area_str = Area, Area = Area, html_root = html_root})
    else
        html_root = fillTemplate(TEMPLATE_ERROR1, {record_count = #results1, Area = Area})
        output = fillTemplate(TEMPLATE_OUTTER, {Area_str = Area, Area = Area, html_root = html_root}) .. [=[[[Category:Error Template Area]]]=]
    end

    return output
end




return p