Skip to content

Commit

Permalink
Working on rendering and outputting dominia files
Browse files Browse the repository at this point in the history
  • Loading branch information
drewry committed Sep 17, 2017
1 parent 6f73694 commit 23c9727
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 9 deletions.
12 changes: 6 additions & 6 deletions lib/geography.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ class Geography {

const geography = {
arable: {
'squaremiles': arableSqmi,
'acreage': Math.ceil(arableSqmi * sqmi),
squaremiles: arableSqmi,
acreage: Math.ceil(arableSqmi * sqmi),
},
wilderness: {
'squaremiles': wildernessSqmi,
'acreage': Math.ceil(wildernessSqmi * sqmi),
squaremiles: wildernessSqmi,
acreage: Math.ceil(wildernessSqmi * sqmi),
},
total: {
'squaremiles': total,
'acreage': Math.ceil(total * sqmi),
squaremiles: total,
acreage: Math.ceil(total * sqmi),
},
};

Expand Down
8 changes: 5 additions & 3 deletions lib/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const libDir = path.join(rootDir, 'lib');
const pinfo = require(path.join(rootDir, 'package.json'));
const Dominia = require(path.join(libDir, 'dominia'));
const Saver = require(path.join(libDir, 'saver'));
const Renderer = require(path.join(libDir, 'renderer'));
const measurements = require(path.join(libDir, 'measurements'));
const logo = fs.readFileSync(path.join(rootDir, 'logo.txt'), { encoding: 'utf-8' });

Expand All @@ -17,15 +18,16 @@ program
.description(pinfo.description)
.option('-i, --input <file>', 'input *.dom file')
.option('-o, --output <dir>', 'output directory')
.option('-r, --renderer <renderer>', 'which render to use default or json')
.parse(process.argv);

let { input, output } = program;
let { input, output, renderer } = program;
let dominia;

// check the input
if (input !== undefined) {
dominia = Saver.load(input);
console.log(JSON.stringify(dominia));
Renderer.render(renderer, dominia);
} else {

// output welcome
Expand All @@ -47,7 +49,7 @@ if (input !== undefined) {
}
}, (opts) => {
dominia = new Dominia(opts).generate();
console.log(JSON.stringify(dominia));
Renderer.render(renderer, dominia);

// save y/n?
questions.askOne({ info: colors.cyan('Would you like to save your demesne? (y | n)') }, (res) => {
Expand Down
89 changes: 89 additions & 0 deletions lib/renderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
'use strict';

const colors = require('colors/safe');
const numeral = require('numeral');
const measurements = require('./measurements');
const { sqmi } = measurements;

class Renderer {
static capitalize(s) {
return s.charAt(0).toUpperCase() + s.slice(1);
}

static formatNumber(n, decimal = false) {
let format = '0,0';

if (decimal) format = '0,0.00';

return numeral(n).format(format);
}

static render(renderer = '', dominia) {
if (renderer === 'json') {
console.log(JSON.stringify(dominia));
return;
}

// get vars and do some math
const { name, scale, size, population, geography, demographics, localities, demesnes } = dominia;

// start
let output = colors.white('\n------------------------------------------------------------------------------------------------------------------------------------------\n');

// quick stats
output += colors.white('Type: \t\t');
output += this.capitalize(size);
output += colors.white('\nName: \t\t');
output += this.capitalize(name);
output += colors.white('\nPopulation: \t');
output += this.formatNumber(population.count);

// desc
output += colors.white('\nDescription: \t');

// intro
output += `The ${this.capitalize(size)} of ${this.capitalize(name)} has a population of ${this.formatNumber(population.count)}`;
output += ` and a population density of ${this.formatNumber(population.density)} people per sqmi.\n\t\t`;
output += `The ${size} sits on ${this.formatNumber(geography.total.squaremiles)} sqmi or ${this.formatNumber(geography.total.acreage)} acres of land `;
output += `of which ${this.formatNumber(geography.arable.squaremiles, true)} sqmi or ${this.formatNumber(geography.arable.acreage)} acres is arable.\n\t\t`
output += `The remaining ${this.formatNumber(geography.wilderness.squaremiles, true)} sqmi or ${this.formatNumber(geography.wilderness.acreage)} acres is wilderness.\n\t\t`

// body
if (scale === 'locality') {
const { industries, livestock } = demographics;

// industries
output += '\n\t\tThe locality contains the following industries: \n\t\t';
industries.forEach((industry) => {
output += ` - ${industry.count} ${industry.industry}\n\t\t`;
});

// livestock
output += '\n\t\tThe locality also has livestock of: \n\t\t';
livestock.forEach((animal) => {
output += ` - ${animal.count} ${animal.animal}\n\t\t`;
});
} else if (scale === 'county') {
output += '\n\t\tThe county has the following localities: \n\t\t';
localities.forEach((locality) => {
output += ` - ${this.capitalize(locality.size)} of ${locality.name}, popluation: ${this.formatNumber(locality.population.count)}\n\t\t`;
});
} else if (scale === 'duchy') {
output += '\n\t\tThe duchy has the following counties: \n\t\t';
demesnes.forEach((demesne) => {
output += ` - ${this.capitalize(demesne.size)} of ${demesne.name}, popluation: ${this.formatNumber(demesne.population.count)}\n\t\t`;
});
} else if (scale === 'kingdom') {
output += '\n\t\tThe kingdom has the following demesnes: \n\t\t';
demesnes.forEach((demesne) => {
output += ` - ${this.capitalize(demesne.size)} of ${demesne.name}, popluation: ${this.formatNumber(demesne.population.count)}\n\t\t`;
});
}

// end
output += colors.white('\n------------------------------------------------------------------------------------------------------------------------------------------\n');
console.log(output);
}
}

module.exports = Renderer;
2 changes: 2 additions & 0 deletions lib/saver.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

const fs = require('fs');
const colors = require('colors/safe');

Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"commander": "^2.11.0",
"node-zip": "^1.1.1",
"nomina": "^0.2.0",
"numeral": "^2.0.6",
"questions": "0.0.6",
"randgen": "^0.1.0",
"roll": "^1.2.0"
Expand Down

0 comments on commit 23c9727

Please sign in to comment.