81 lines
2.6 KiB
JavaScript
81 lines
2.6 KiB
JavaScript
const fs = require('fs');
|
|
const _ = require('underscore');
|
|
|
|
let storylines = {};
|
|
fs.readdirSync('./game/scenario/storylines').forEach(file => {
|
|
require(`./storylines/${file}`)(storylines);
|
|
});
|
|
|
|
function l(context) {
|
|
return _.keys(storylines);
|
|
}
|
|
|
|
function v(context) {
|
|
return new Promise((resolve, reject) => {
|
|
let ok = true;
|
|
console.info(`[${context.id}] Verifying map integrity...`);
|
|
let uniques = _.chain(context.scenario.map.locations)
|
|
.map(location => location.id)
|
|
.uniq().value();
|
|
if(uniques.length !== context.scenario.map.locations.length) {
|
|
console.error(`[${context.id}] ID collision detected`);
|
|
reject(new Error("ID Collision in map data"));
|
|
}
|
|
_.each(context.scenario.map.connections, connection => {
|
|
if(!_.contains(uniques, connection.locations[0])) {
|
|
console.error(`[${context.id}] Invalid connection detected [${connection.locations[0]}]`);
|
|
ok = false;
|
|
} else if(!_.contains(uniques, connection.locations[1])) {
|
|
console.error(`[${context.id}] Invalid connection detected [${connection.locations[1]}]`);
|
|
ok = false;
|
|
}
|
|
});
|
|
if(ok) {
|
|
console.info(`[${context.id}] ... Verified map integrity`);
|
|
resolve(context);
|
|
} else {
|
|
reject(new Error("Invalid map data"));
|
|
}
|
|
});
|
|
}
|
|
|
|
function i(context) {
|
|
return new Promise((resolve, reject) => {
|
|
if(!context.scenario) {
|
|
if(context.storyline) {
|
|
if(_.has(storylines, context.storyline)) {
|
|
storylines[context.storyline].initialize(context)
|
|
.then(context => {
|
|
context.state.scenario = context.scenario;
|
|
v(context)
|
|
.then(context => {
|
|
context.message.reply(`Initiated scenario \`[${context.storyline}]\``)
|
|
.then(() => {
|
|
context.stateChanged = true;
|
|
resolve(context);
|
|
})
|
|
.catch(err => reject(err))
|
|
})
|
|
.catch(err => reject(err));
|
|
})
|
|
.catch(err => reject(err));
|
|
} else {
|
|
console.warn(`[${context.id}] No such storyline named ${context.storyline}.`)
|
|
reject(new Error(`No such storyline named ${context.storyline}.`));
|
|
}
|
|
} else {
|
|
console.error(`[${context.id}] Missing storyline context`);
|
|
reject(new Error(`Invalid context`));
|
|
}
|
|
} else {
|
|
console.warn(`[${context.id}] Scenario already initiated.`);
|
|
reject(new Error(`Scenario already initiated.`));
|
|
}
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
init: i,
|
|
list: l
|
|
}
|