Jump to content

Module:ImportRaces

From Utopia Game
Revision as of 13:24, 6 November 2025 by Sonja says (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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