From: xangelo Date: Thu, 1 Jun 2023 16:09:29 +0000 (-0400) Subject: csv parsing for monsters X-Git-Tag: v0.0.1~93 X-Git-Url: https://git.xangelo.ca/?a=commitdiff_plain;h=9aa8077d2fd117e91114ebbe578095c381f5435d;p=risinglegends.git csv parsing for monsters --- diff --git a/data/monsters.csv b/data/monsters.csv new file mode 100644 index 0000000..42b49d3 --- /dev/null +++ b/data/monsters.csv @@ -0,0 +1,5 @@ +name,level,strength,constitution,intelligence,dexterity,hp,exp,gold,helmAp,chestAp,armsAp,legsAp +Giant Rat,1,3,3,3,3,5,2,1,0,0,0,0 +Slime,2,1,5,2,3,12,3,1,0,0,0,0 +Drunk Thug,3,5,4,2,5,16,5,8,0,5,0,0 +Kobold,4,7,8,4,7,22,9,2,3,3,0,0 diff --git a/seeds/monsters.ts b/seeds/monsters.ts index e2502c5..270d3f0 100644 --- a/seeds/monsters.ts +++ b/seeds/monsters.ts @@ -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 { // 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[] = []; + 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); };