Added replacement.js
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
//replacement(options)(key [, args...])
|
||||
//options - the json object containing all options
|
||||
//key - one of the keys in options
|
||||
//data (optional) - a number of arguments that are substituted into the resulting string
|
||||
const replacement = (options) => (key, ...data) => {
|
||||
let result;
|
||||
|
||||
//randomize arrays
|
||||
if (Array.isArray(options[key])) {
|
||||
result = options[key][Math.floor(Math.random() * options[key].length)];
|
||||
} else {
|
||||
result = options[key];
|
||||
}
|
||||
|
||||
//handle no result
|
||||
if (result === undefined) {
|
||||
const noResult = options["noResult"];
|
||||
if (noResult === undefined) {
|
||||
return ""; //nothing at all to show
|
||||
}
|
||||
//randomized noResult array
|
||||
if (Array.isArray(noResult)) {
|
||||
result = noResult[Math.floor(Math.random() * noResult.length)];
|
||||
} else {
|
||||
result = noResult;
|
||||
}
|
||||
}
|
||||
|
||||
//replacement engine
|
||||
let counter = 0;
|
||||
data.map((dat) => {
|
||||
counter++;
|
||||
result = result.replace(/\{([1-9][0-9]*)\}/g, a => a === "{" + counter + "}" ? dat : a);
|
||||
});
|
||||
|
||||
//return the final result
|
||||
return result;
|
||||
};
|
||||
|
||||
//templateReplacement(templateString [, args...])
|
||||
//templateString - the string to act as a template
|
||||
//data (optional) - a number of arguments that are substituted into the template string
|
||||
const stringReplacement = (templateString, ...data) => {
|
||||
let result = templateString;
|
||||
|
||||
//replacement engine
|
||||
let counter = 0;
|
||||
data.map((dat) => {
|
||||
counter++;
|
||||
result = result.replace(/\{([1-9][0-9]*)\}/g, a => a === "{" + counter + "}" ? dat : a);
|
||||
});
|
||||
|
||||
//return the final result
|
||||
return result;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
replacement: replacement,
|
||||
stringReplacement: stringReplacement
|
||||
};
|
||||
@@ -7,7 +7,7 @@
|
||||
<link rel="stylesheet" href="/styles/shared.css"/>
|
||||
|
||||
<title>Kingdom Battles!</title>
|
||||
<meta name="description" content="Build Your Kingdom!" />
|
||||
<meta name="description" content="{1}" />
|
||||
<meta name="author" content="Kayne Ruse" />
|
||||
<meta name="keywords" content="gaming, browser game, persistent browser based game, free game" />
|
||||
<!--
|
||||
@@ -19,7 +19,7 @@
|
||||
<meta property="og:image" content="https://kingdombattles.net/img/flag_scaled.png" />
|
||||
|
||||
<meta property="og:title" content="Kingdom Battles!" />
|
||||
<meta property="og:description" content="Build Your Kingdom!" />
|
||||
<meta property="og:description" content="{1}" />
|
||||
|
||||
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
|
||||
<script>
|
||||
+7
-2
@@ -6,10 +6,12 @@ let express = require('express');
|
||||
let app = express();
|
||||
let http = require('http').Server(app);
|
||||
let bodyParser = require('body-parser');
|
||||
let fs = require('fs');
|
||||
let path = require('path');
|
||||
|
||||
//utilities
|
||||
let { log } = require('../common/utilities.js');
|
||||
let { replacement, stringReplacement } = require('../common/replacement.js');
|
||||
|
||||
app.use(bodyParser.json());
|
||||
|
||||
@@ -90,9 +92,12 @@ app.get('/app.bundle.js.map', (req, res) => {
|
||||
res.sendFile(path.resolve(__dirname + `/../public/${req.originalUrl}`));
|
||||
});
|
||||
|
||||
//fallback
|
||||
//fallback to index.html template (with randomization)
|
||||
const indexTemplate = fs.readFileSync(path.resolve(__dirname + '/../public/index.html.t'), 'utf8');
|
||||
const replacementEngine = replacement(require('./website_descriptions.json'));
|
||||
|
||||
app.get('*', (req, res) => {
|
||||
res.sendFile(path.resolve(__dirname + '/../public/index.html'));
|
||||
res.send(stringReplacement(indexTemplate, replacementEngine('description') ));
|
||||
});
|
||||
|
||||
//startup
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"description": [
|
||||
"Build Your Kingdom!",
|
||||
"These Descriptions Are Random!"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user