Jump to content

Module:ImportRaces: Difference between revisions

From Utopia Game
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:


function p.import()
function p.import()
     local csvFile = mw.title.new("File:utopia_race_details.csv")
    -- Load the CSV file from the specified folder
     local content = csvFile:getContent()
     local csvFile = mw.title.new("File:utopia_race_details.csv") -- Corrected path; ensure the file is in the correct location
    local lines = mw.text.split(content, "\n")
     local content = csvFile:getContent() -- Get the content of the file


     for i, line in ipairs(lines) do
    if not content then
         if i > 1 then -- Skip header line
        return "Error: Unable to load CSV file."  -- Handle case where file is not found
             local fields = mw.text.split(line, ",")
    end
            local race = fields[1]
 
            local bonuses = fields[2]
    local lines = mw.text.split(content, "\n")  -- Split the content into lines
            local penalties = fields[3]
    local output = ""  -- Initialize output string to collect results
            local unique = fields[4]
 
            local spells = fields[5]
     for i, line in ipairs(lines) do -- Loop through each line in the CSV
         if i > 1 then -- Skip header line (the first line)
             local fields = mw.text.split(line, ",") -- Split the line into fields


             {{#cargo_insert_data:
             -- Ensure we have the right number of fields
            | table = races
            if #fields >= 5 then
            | race = race
                local race = fields[1]  -- First field: race name
            | bonuses = bonuses
                local bonuses = fields[2]  -- Second field: bonuses
            | penalties = penalties
                local penalties = fields[3]  -- Third field: penalties
            | unique = unique
                local unique = fields[4]  -- Fourth field: unique features
            | spells = spells
                local spells = fields[5]  -- Fifth field: spells
             }}
 
                -- Here, you would use Cargo's #cargo_insert_data to insert the data
                output = output .. "Inserting race: " .. race .. "\n"  -- Collect output for visibility
 
                -- Use a Cargo function to insert data
                local insertResult = mw.ext.cargo.insertData({
                    table = "races",
                    race = race,
                    bonuses = bonuses,
                    penalties = penalties,
                    unique = unique,
                    spells = spells,
                })
 
                if insertResult then
                    output = output .. "Successfully inserted: " .. race .. "\n"
                else
                    output = output .. "Failed to insert: " .. race .. "\n"
                end
            else
                output = output .. "Error: Incorrect field count in line " .. i .. "\n"
             end
         end
         end
     end
     end
    return output  -- Return the collected output for display
end
end


return p
return p

Latest revision as of 13:24, 6 November 2025

local p = {}

function p.import()

   -- Load the CSV file from the specified folder
   local csvFile = mw.title.new("File:utopia_race_details.csv")  -- Corrected path; ensure the file is in the correct location
   local content = csvFile:getContent()  -- Get the content of the file
   if not content then
       return "Error: Unable to load CSV file."  -- Handle case where file is not found
   end
   local lines = mw.text.split(content, "\n")  -- Split the content into lines
   local output = ""  -- Initialize output string to collect results
   for i, line in ipairs(lines) do  -- Loop through each line in the CSV
       if i > 1 then  -- Skip header line (the first line)
           local fields = mw.text.split(line, ",")  -- Split the line into fields
           -- Ensure we have the right number of fields
           if #fields >= 5 then
               local race = fields[1]  -- First field: race name
               local bonuses = fields[2]  -- Second field: bonuses
               local penalties = fields[3]  -- Third field: penalties
               local unique = fields[4]  -- Fourth field: unique features
               local spells = fields[5]  -- Fifth field: spells
               -- Here, you would use Cargo's #cargo_insert_data to insert the data
               output = output .. "Inserting race: " .. race .. "\n"  -- Collect output for visibility
               -- Use a Cargo function to insert data
               local insertResult = mw.ext.cargo.insertData({
                   table = "races",
                   race = race,
                   bonuses = bonuses,
                   penalties = penalties,
                   unique = unique,
                   spells = spells,
               })
               if insertResult then
                   output = output .. "Successfully inserted: " .. race .. "\n"
               else
                   output = output .. "Failed to insert: " .. race .. "\n"
               end
           else
               output = output .. "Error: Incorrect field count in line " .. i .. "\n"
           end
       end
   end
   return output  -- Return the collected output for display

end

return p