Skip to content

Commit

Permalink
Refactoring to match eslint and adding version
Browse files Browse the repository at this point in the history
  • Loading branch information
drewry committed Jan 15, 2018
1 parent ac3d50d commit 0d6126b
Show file tree
Hide file tree
Showing 17 changed files with 1,569 additions and 176 deletions.
18 changes: 18 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": "airbnb-base",
"plugins": [
"import"
],
"rules": {
"import/no-dynamic-require": 0,
"import/newline-after-import": 0,
"no-extend-native": 0,
"func-names": 0,
"no-bitwise": 0,
"global-require": 0,
"max-len": 0,
"no-param-reassign": 0,
"no-underscore-dangle": 0,
"class-methods-use-this": 0
}
}
3 changes: 3 additions & 0 deletions lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ if (fs.existsSync(userPath)) {
defaults = require(path.join(libDir, 'defaults-default'));
}

// add themes from nomina
defaults.themes = Nomina.getThemes();

module.exports = defaults;
13 changes: 5 additions & 8 deletions lib/demographics.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const defaults = require('./defaults');
const { supportValues, livestockValues } = defaults;

Expand All @@ -9,15 +7,14 @@ class Demographics {

// generate the industries
industries = industries.map((industry) => {

// determine if advantage or not
let applyAdvantage = 1;
const advantages = defaults.advantages[industry];
if (advantages) {
advantages.forEach((advantage) => {
// halves the prosperity value if we have advantage
if (advantages.indexOf(advantage) >= 0) {
applyAdvantage = 2;
applyAdvantage = 2;
}
});
}
Expand All @@ -44,9 +41,9 @@ class Demographics {
industries = industries.reduce((a, b) => {
if (b.count > 0) {
return a.concat(b);
} else {
return a;
}

return a;
}, []);

return industries;
Expand All @@ -71,7 +68,7 @@ class Demographics {
}

static generate(opts = {}) {
const { population, terrain, resources, prosperity } = opts;
const { population, prosperity } = opts;
const prosperityValue = defaults.prosperity.indexOf(prosperity) + 1;
const industries = this.industries(population.count, prosperityValue);
const livestock = this.livestock(population.count);
Expand All @@ -85,4 +82,4 @@ class Demographics {
}
}

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

const Generator = require('./generator');
const defaults = require('./defaults');
const { measurements } = defaults;
Expand All @@ -15,7 +13,7 @@ class Dominia {

this.generator = new Generator();

switch(size) {
switch (size) {
case 'hamlet':
return this.generateHamlet(opts);

Expand Down Expand Up @@ -196,7 +194,6 @@ class Dominia {
generateDemesnes(popCount = 0, demesnes = []) {
const multiplier = 1.3;
let maxPop = popCount * 0.7;
const minDemesne = measurements.getMaxSize('county') * multiplier;
let demesne;

// generate duchy
Expand Down Expand Up @@ -231,4 +228,4 @@ class Dominia {
}
}

module.exports = Dominia;
module.exports = Dominia;
28 changes: 16 additions & 12 deletions lib/generator.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
'use strict';

const Names = require('./names');
const Populations = require('./populations');
const Geography = require('./geography');
const Demographics = require('./demographics');
const Resources = require('./resources');
const defaults = require('./defaults');
const path = require('path');
const rootDir = path.join(__dirname, '..');
const libDir = path.join(rootDir, 'lib');
const pinfo = require(path.join(rootDir, 'package.json'));
const Names = require(path.join(libDir, 'names'));
const Populations = require(path.join(libDir, 'populations'));
const Geography = require(path.join(libDir, 'geography'));
const Demographics = require(path.join(libDir, 'demographics'));
const Resources = require(path.join(libDir, 'resources'));
const defaults = require(path.join(libDir, 'defaults'));

class Generator {
primitive(opts = {}) {
let { scale, size } = opts;
const { version } = pinfo;
const { scale, size } = opts;

// generate name
const nameOpts = Object.assign(opts, {});
const name = Names.generate(nameOpts);

// generate population
const popOpts = Object.assign(opts, {});
const population = Populations.generate(popOpts);
Expand All @@ -30,6 +33,7 @@ class Generator {

// set the primitive
let primitive = {
version,
scale,
size,
name,
Expand All @@ -41,7 +45,7 @@ class Generator {
// generate demographics if locality
if (scale === 'locality') {
const terrain = opts.terrain || defaults.terrains.sample();
const resources = opts.resources || Resources.generate({ terrain, });
const resources = opts.resources || Resources.generate({ terrain });

// demographic opts
const demoOpts = Object.assign(opts, {
Expand All @@ -66,4 +70,4 @@ class Generator {
}
}

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

const randgen = require('randgen');
const { rnorm } = randgen;
const defaults = require('./defaults');
Expand All @@ -9,8 +7,8 @@ const { sqmi } = measurements;
class Geography {
static generate(opts = {}) {
const { population } = opts;
let { count, density } = population;
const { count, density } = population;

const total = Math.ceil(count / density);
const arable = rnorm(0.4, 0.1);
const wilderness = 1.0 - arable;
Expand All @@ -36,4 +34,4 @@ class Geography {
}
}

module.exports = Geography;
module.exports = Geography;
7 changes: 3 additions & 4 deletions lib/names.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';

const Nomina = require('nomina');
const defaults = require('./defaults');

class Names {
static generate(opts = {}) {
let theme = opts.theme || 'medieval';
const theme = opts.theme || defaults.themes.sample();

const name = Nomina.generate({
theme,
Expand All @@ -16,4 +15,4 @@ class Names {
}
}

module.exports = Names;
module.exports = Names;
14 changes: 6 additions & 8 deletions lib/populations.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
'use strict';

const randgen = require('randgen');
const { rnorm } = randgen;
const defaults = require('./defaults');
const { measurements } = defaults;
const { sizes, densities, sqmi } = measurements;
const { densities } = measurements;

class Populations {
static generate(opts = {}) {
const { size, scale } = opts;
const { size } = opts;
const template = this.generateTemplate(opts);
const { min, max, diff, mean, stdev } = template;
const { min, max, mean, stdev } = template;

// calculate population
// prevent max and min
let count = Math.floor(rnorm(mean, stdev));
Expand Down Expand Up @@ -45,7 +43,7 @@ class Populations {
// max
let max = measurements.getMaxSize(size);
if (opts.max !== undefined) max = opts.max;

const diff = max - min;
const stdev = diff / 5;
const mean = (diff / 2) + min;
Expand All @@ -60,4 +58,4 @@ class Populations {
}
}

module.exports = Populations;
module.exports = Populations;
115 changes: 7 additions & 108 deletions lib/program.js
Original file line number Diff line number Diff line change
@@ -1,125 +1,24 @@
const fs = require('fs');
const program = require('commander');
const questions = require('questions');
const numeral = require('numeral');
const colors = require('colors/safe');
const path = require('path');
const rootDir = path.join(__dirname, '..');
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 defaults = require(path.join(libDir, 'defaults'));
const logo = fs.readFileSync(path.join(rootDir, 'logo.txt'), { encoding: 'utf-8' });
const { measurements } = defaults;
const wizard = require(path.join(libDir, 'wizard'));

// program basics
program
.version(pinfo.version, '-v, --version')
.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, renderer } = program;
let dominia;

const themes = ['asian', 'classical', 'medieval', 'nirimsese', 'scottish'];

// check the input
if (input !== undefined) {
dominia = Saver.load(input);
Renderer.render(dominia, renderer);
// load a file or go through the wizard
if (program.input !== undefined) {
const dominia = Saver.load(program.input);
Renderer.output(dominia);
} else {

// output welcome
console.log('\n' + colors.cyan(logo) + '\n');

// define a default output dir
if (output === undefined) output = '.';

// ask a few questions
questions.askMany({
size: {
info: colors.cyan('What size domain do you want to generate?') + colors.white(' (' + Object.keys(measurements.sizes).join(' | ') + ')'),
required: false
},

population: {
info: colors.cyan('Would you like to specify the population?') + colors.white(' ex: input 50,000 (people)'),
required: false
},

theme: {
info: colors.cyan('What theme would you like?') + colors.white(' (' + themes.join(' | ') + ')'),
required: false
},

name: {
info: colors.cyan('Would you like to use an existing name?') + colors.white(' ex: Allaria, leave blank to generate'),
required: false
},

prosperity: {
info: colors.cyan('Would you like to specify the prosperity?') + colors.white(' (' + defaults.prosperity.join(' | ') + ')'),
required: false
},

terrain: {
info: colors.cyan('Would you like to specify the terrain?') + colors.white(' (' + defaults.terrains.join(' | ') + ')'),
required: false
},

resources: {
info: colors.cyan('Would you like to specify the resources?') + colors.white(' separate with comma (' + defaults.resources.join(' | ') + ')'),
required: false
},

density: {
info: colors.cyan('Would you like to specify the density?') + colors.white(' ex: input 75 (people per sqmi)'),
required: false
},

land: {
info: colors.cyan('Would you like to specify the total land area?') + colors.white(' ex: input 1,000,000 (sqmi)'),
required: false
},
}, (opts) => {

// manage the opts
opts.size = (opts.size === '') ? undefined : opts.size.toLowerCase();
opts.name = (opts.name === '') ? undefined : opts.name;
opts.theme = (opts.theme === '') ? undefined : opts.theme.toLowerCase();
opts.population = (opts.population === '') ? undefined : numeral(opts.population).value();
opts.density = (opts.density === '') ? undefined : numeral(opts.density).value();
opts.land = (opts.land === '') ? undefined : numeral(opts.land).value();
opts.terrain = (opts.terrain === '') ? undefined : opts.terrain;
opts.resources = (opts.resources === '') ? undefined : opts.resources.split(', ');

/* Calculations:
* L = 10 sqmi
* P = 500 people
* D = 50 people per sqmi
*
* Formulas:
* L = P / D ex: 10 = 500 / 50
* P = D * L ex: 500 = 50 * 10
* D = P / L ex: 50 = 500 / 10
*/
// calculate land from pop and density
if ((opts.land === undefined) && opts.population && opts.density) opts.land = opts.population / opts.density;

// calculate pop from land and density
if ((opts.population === undefined) && opts.land && opts.density) opts.population = opts.land * opts.density;

// calculate density from land and population
if ((opts.density === undefined) && opts.land && opts.population) opts.density = opts.population / opts.land;

// generate
dominia = new Dominia(opts).generate();
Renderer.render(dominia, renderer);
Saver.finish(output, 'Would you like to save your domain? (y | n)', dominia, dominia.name);
});
}
wizard(program.output);
}
Loading

0 comments on commit 0d6126b

Please sign in to comment.