csv parsing for monsters
authorxangelo <git@xangelo.ca>
Thu, 1 Jun 2023 16:09:29 +0000 (12:09 -0400)
committerxangelo <git@xangelo.ca>
Thu, 1 Jun 2023 16:09:29 +0000 (12:09 -0400)
data/monsters.csv [new file with mode: 0644]
seeds/monsters.ts

diff --git a/data/monsters.csv b/data/monsters.csv
new file mode 100644 (file)
index 0000000..42b49d3
--- /dev/null
@@ -0,0 +1,5 @@
+name,level,strength,constitution,intelligence,dexterity,hp,exp,gold,helmAp,chestAp,armsAp,legsAp\r
+Giant Rat,1,3,3,3,3,5,2,1,0,0,0,0\r
+Slime,2,1,5,2,3,12,3,1,0,0,0,0\r
+Drunk Thug,3,5,4,2,5,16,5,8,0,5,0,0\r
+Kobold,4,7,8,4,7,22,9,2,3,3,0,0\r
index e2502c575079ccf5073d335706ad519a3551a2ce..270d3f082422eff7e568407269c6ed668da0409e 100644 (file)
@@ -1,34 +1,46 @@
 import { Knex } from "knex";
+import { join } from 'path';
+import { readFile } from 'fs';
+import { promisify } from 'util';
+import {Monster} from "@shared/monsters";
+
+const read = promisify(readFile);
 
 export async function seed(knex: Knex): Promise<void> {
     // Deletes ALL existing entries
     await knex("monsters").del();
+    await knex("fight").del();
+
+    const data = await read(join(__dirname, '..', 'data', 'monsters.csv'), 'utf8');
+
+
+    const monsters: Omit<Monster, 'id'>[] = [];
+    data.split("\r\n").slice(1).forEach(line => {
+      let pieces = line.split(',');
+      if(pieces.length === 13) {
+        console.log('Creating', pieces[0]);
+        monsters.push({
+          name: pieces[0],
+          level: parseInt(pieces[1]),
+          strength: parseInt(pieces[2]),
+          constitution: parseInt(pieces[3]),
+          intelligence: parseInt(pieces[4]),
+          dexterity: parseInt(pieces[5]),
+          hp: parseInt(pieces[6]),
+          maxHp: parseInt(pieces[6]),
+          exp: parseInt(pieces[7]),
+          gold: parseInt(pieces[8]),
+          helmAp: parseInt(pieces[9]),
+          chestAp: parseInt(pieces[10]),
+          armsAp: parseInt(pieces[11]),
+          legsAp: parseInt(pieces[12])
+        })
+      }
+      else {
+        console.log(`Skipping`, line);
+      }
+    })
 
     // Inserts seed entries
-    await knex("monsters").insert([
-      {
-        name: 'Rat',
-        strength: 1,
-        dexterity: 1,
-        intelligence: 1,
-        constitution: 1,
-        level: 1,
-        gold: 1,
-        exp: 1,
-        hp: 5,
-        maxHp: 5
-      },
-      {
-        name: 'Skulking Bandit',
-        strength: 3,
-        dexterity: 2,
-        intelligence: 1,
-        constitution: 3,
-        level: 2,
-        gold: 5,
-        exp: 3,
-        hp: 10,
-        maxHp: 10
-      },
-    ]);
+    await knex("monsters").insert(monsters);
 };