Module:Sandbox/SkillTree: Difference between revisions

wsg
(def not chatgpt)
(wsg)
Line 42: Line 42:
     local args = frame.args ~= nil and frame.args or frame
     local args = frame.args ~= nil and frame.args or frame
     local skillName = args[1]
     local skillName = args[1]
   
 
     if not skillName then
     if not skillName then
         return "Invalid skillName"
         return "Invalid skillName"
     end
     end
   
 
     local skillNodes = p.getSkillTreeNodesFromSkillName(skillName)
     local skillNodes = p.getSkillTreeNodesFromSkillName(skillName)
   
 
     if not skillNodes or #skillNodes == 0 then
     if not skillNodes or #skillNodes == 0 then
         return "No skill tree found for: " .. skillName
         return "No skill tree found for: " .. skillName
     end
     end
    -- Helper function to calculate the level of each node
    local function calculateNodeLevels(nodes)
        local levels = {}
        local function findLevel(node, currentLevel)
            -- Set level for the node if it's not already set
            if not levels[node.name] then
                levels[node.name] = currentLevel
            else
                levels[node.name] = math.max(levels[node.name], currentLevel)
            end
            -- Recursively set level for child nodes
            if node.children then
                for _, child in ipairs(node.children) do
                    findLevel(child, currentLevel + 1)
                end
            end
        end
        -- Start with nodes that have no parents (root nodes)
        for _, node in ipairs(nodes) do
            if not node.parents or #node.parents == 0 then
                findLevel(node, 0) -- Root nodes start at level 0
            end
        end
        return levels
    end
    -- Calculate levels for each node
    local nodeLevels = calculateNodeLevels(skillNodes)


     -- Constants for layout
     -- Constants for layout
     local baseX = 500  -- Start X position (middle of the screen)
     local baseX = 500  -- Start X position (center of the screen)
     local baseY = 50  -- Start Y position (top of the screen)
     local baseY = 50  -- Start Y position (top of the screen)
     local verticalSpacing = 200  -- Distance between levels (Y-axis)
     local verticalSpacing = 200  -- Distance between levels (Y-axis)
     local horizontalSpacing = 300  -- Distance between nodes at the same level (X-axis)
     local horizontalSpacing = 300  -- Distance between nodes at the same level (X-axis)


     -- Find the maximum level (depth) of the skill tree
     -- Count nodes per level for horizontal positioning
    local maxLevel = 0
    for _, node in ipairs(skillNodes) do
        if node.level > maxLevel then
            maxLevel = node.level
        end
    end
 
    -- Calculate the number of nodes per level
     local nodesPerLevel = {}
     local nodesPerLevel = {}
     for _, node in ipairs(skillNodes) do
     for nodeName, level in pairs(nodeLevels) do
        local level = node.level
         if not nodesPerLevel[level] then
         if not nodesPerLevel[level] then
             nodesPerLevel[level] = 0
             nodesPerLevel[level] = 0
Line 89: Line 112:
         :attr('preserveAspectRatio', 'none')
         :attr('preserveAspectRatio', 'none')
         :attr('viewBox', '0 0 1000 ' .. (maxLevel + 1) * verticalSpacing)
         :attr('viewBox', '0 0 1000 ' .. (maxLevel + 1) * verticalSpacing)
   
 
     -- Keep track of node positions for connections
     -- Track positions for each node
     local nodePositions = {}
     local nodePositions = {}


     -- Render each node and calculate its position
     -- Position each node dynamically
     for _, node in ipairs(skillNodes) do
     for _, node in ipairs(skillNodes) do
         local level = node.level
         local level = nodeLevels[node.name]
         local indexInLevel = nodesPerLevel[level]
         local nodesAtThisLevel = nodesPerLevel[level]
        local indexInLevel = nodesAtThisLevel


         -- Calculate X and Y positions for this node
         -- Calculate X and Y positions for this node
         local xPos = baseX + (indexInLevel - nodesPerLevel[level] / 2) * horizontalSpacing
         local xPos = baseX + (indexInLevel - nodesAtThisLevel / 2) * horizontalSpacing
         local yPos = baseY + level * verticalSpacing
         local yPos = baseY + level * verticalSpacing


         -- Store the node's position for later use in drawing connections
         -- Store node position for connections
         nodePositions[node.name] = { x = xPos, y = yPos }
         nodePositions[node.name] = { x = xPos, y = yPos }


Line 178: Line 202:
     return tostring(html)
     return tostring(html)
end
end


return p
return p
393

edits