Fixed csv tools

This commit is contained in:
Sam 2021-03-28 23:48:51 -04:00
parent cbca2a66b6
commit bdbe6134f2
2 changed files with 13 additions and 8 deletions

View File

@ -219,7 +219,7 @@ fn all_items() -> Result<(), Box<dyn Error>> {
fn loot_table(loot_table: &str) -> Result<(), Box<dyn Error>> {
let mut wtr = csv::Writer::from_path("loot_table.csv")?;
wtr.write_record(&["Kind", "Item", "Relative Chance"])?;
wtr.write_record(&["Relative Chance", "Kind", "Item", "Lower Amount", "Upper Amount"])?;
let loot_table = "common.loot_tables.".to_owned() + loot_table;
@ -236,12 +236,12 @@ fn loot_table(loot_table: &str) -> Result<(), Box<dyn Error>> {
.div(10_f32.powi(5))
.to_string();
let (kind, item) = match item {
LootSpec::Item(item) => ("Item", item),
LootSpec::LootTable(table) => ("LootTable", table),
};
wtr.write_record(&[kind, item, &chance])?;
match item {
LootSpec::Item(item) => wtr.write_record(&[&chance, "Item", item, "", ""])?,
LootSpec::ItemQuantity(item, lower, upper) => wtr.write_record(&[&chance, "Item", item, &lower.to_string(), &upper.to_string()])?,
LootSpec::LootTable(table) => wtr.write_record(&[&chance, "LootTable", table, "", ""])?,
LootSpec::CreatureMaterial => wtr.write_record(&[&chance, "CreatureMaterial", "", "", ""])?,
}
}
wtr.flush()?;

View File

@ -383,8 +383,13 @@ fn loot_table(loot_table: &str) -> Result<(), Box<dyn Error>> {
for record in rdr.records() {
if let Ok(ref record) = record {
let item = match record.get(headers["Kind"]).expect("No loot specifier") {
"Item" => LootSpec::Item(record.get(headers["Item"]).expect("No item").to_string()),
"Item" => if let (Some(Ok(lower)), Some(Ok(upper))) = (record.get(headers["Lower Amount"]).map(|a| a.parse()), record.get(headers["Upper Amount"]).map(|a| a.parse())) {
LootSpec::ItemQuantity(record.get(headers["Item"]).expect("No item").to_string(), lower, upper)
} else {
LootSpec::Item(record.get(headers["Item"]).expect("No item").to_string())
},
"LootTable" => LootSpec::LootTable(record.get(headers["Item"]).expect("No loot table").to_string()),
"CreatureMaterial" => LootSpec::CreatureMaterial,
_ => panic!("Loot specifier kind must be either \"Item\" or \"LootTable\""),
};
let chance: f32 = record