Created
January 23, 2022 17:03
-
-
Save jmbeach/d0e32d9fe9018fb41a18c7509296581d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { readFileSync } from 'fs'; | |
export interface WorldResource { | |
name: string; | |
weight: number; | |
notes: string; | |
} | |
export const parseResourceFile = (fileName: string): { [key: string]: WorldResource } => { | |
const result: { [key: string]: WorldResource } = {}; | |
const fileText = readFileSync(fileName, { encoding: 'utf-8' }); | |
const lines = fileText.split('\n').map(x => x.trim()); | |
for (const line of lines) { | |
// skip the header | |
if (line === 'Resource,Weight,Notes') continue; | |
const [name, weight, notes] = line.split(','); | |
result[name] = { | |
name, | |
weight: parseFloat(weight), | |
notes | |
} | |
} | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { readFileSync } from "fs"; | |
export const parseStateFile = (stateFile: string): { [country: string]: { [resource: string]: number } } => { | |
const result: { [country: string]: { [resource: string]: number } } = {}; | |
const fileText = readFileSync(stateFile, { encoding: 'utf-8' }) | |
const lines = fileText.split('\n').map(x => x.trim()); | |
const header = lines[0]; | |
// first entry is the word "Country", everything else is resource names | |
const resourceNames = header.split(',').slice(1); | |
for (const line of lines.slice(1)) { | |
const lineParts = line.split(','); | |
const countryName = lineParts[0]; | |
const resourceAmounts = lineParts.slice(1); | |
result[countryName] = {}; | |
for (let i = 0; i < resourceNames.length; i++) { | |
result[countryName][resourceNames[i]] = parseInt(resourceAmounts[i]); | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment