Module:ImportRaces: Difference between revisions
Appearance
Sonja says (talk | contribs) No edit summary |
Sonja says (talk | contribs) No edit summary |
||
| Line 3: | Line 3: | ||
function p.import() | function p.import() | ||
-- Load the CSV file from the specified folder | -- Load the CSV file from the specified folder | ||
local csvFile = mw.title.new("File: | 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 | 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 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 | for i, line in ipairs(lines) do -- Loop through each line in the CSV | ||
if i > 1 then -- Skip header line (the first line) | if i > 1 then -- Skip header line (the first line) | ||
local fields = mw.text.split(line, ",") -- Split the line into fields | local fields = mw.text.split(line, ",") -- Split the line into fields | ||
-- Here, you would use Cargo's #cargo_insert_data to insert the data | -- 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 | ||
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