-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.js
485 lines (407 loc) · 13.8 KB
/
generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
const express = require('express');
const fs = require('fs');
const os = require('os');
const router = express.Router();
const seedrandom = require('seedrandom');
const claims = require('./claims');
const getPortrait = require('./portrait').getPortrait;
root = "/faas";
console.log(os.hostname());
if (os.hostname().indexOf("Omoikane") > -1)
root = "";
console.log(`Root: "${root}"`);
const header = fs.readFileSync('header.html', 'utf8').replaceAll("ROOT", root);
const patterns = JSON.parse(fs.readFileSync('patterns.json', 'utf8'));
const presets = JSON.parse(fs.readFileSync('charpresets.json', 'utf8'));
function pick(fromSet, r)
{
r = r || Math.random;
return Math.floor(r() * fromSet.length)
}
function get(fromSet, r)
{
while (Array.isArray(fromSet))
fromSet = fromSet[pick(fromSet, r)];
return fromSet; //should be a string now
}
function process(startingPattern, r)
{
var thisSet = get(patterns[startingPattern], r);
while (true)
{
var oldSet = thisSet;
thisSet = thisSet.replace(/\[(.+?)\]/g, function(m,p) { return get(patterns[p], r); });
if (oldSet == thisSet)
break;
}
return thisSet;
}
function GetName(req, res)
{
var amount = parseInt(req.query['amount'] || 1);
if (amount > 100) amount = 100;
if (req.query['seed'] != undefined)
seedrandom(req.query['seed'], { global: true });
var results = [];
while (amount--)
{
var result = {};
var startingPattern = '';
if (req.query['male'] != undefined)
startingPattern = 'male';
else if (req.query['female'] != undefined)
startingPattern = 'female';
else
startingPattern = get(['male','female']); //lol
result['name'] = process(startingPattern);
result['gender'] = startingPattern;
if (results.find(e => e['name'] == result['name']))
amount++;
else
results.push(result);
}
return results;
}
function GetPlace(req, res)
{
var amount = parseInt(req.query['amount'] || 1);
if (amount > 100) amount = 100;
if (req.query['name'] != undefined)
{
req.query['name'] = req.query['name'].replace(/[^a-z 0-9]/gi, '');
req.query['name'] = req.query['name'].replace(/\w\S*/g, function(txt) { return txt.charAt(0).toUpperCase() + txt.substr(1); });
amount = 1;
//seedrandom(req.query['name'], { global: true });
}
if (req.query['seed'] != undefined)
seedrandom(req.query['seed'], { global: true });
var results = [];
while (amount--)
{
var result = {};
var thisSet = process('place');
if (req.query['name'] != undefined)
thisSet = req.query['name'];
result['name'] = thisSet;
//Translate
const translations = ['New?','Color','Thing','thing','PlaceSize'];
translations.forEach(p => {
for (var i = 0; i < patterns[p].length; i++)
thisSet = thisSet.replace(patterns[p][i], patterns[p+'2'][i]);
});
result['felinese'] = thisSet;
if (results.find(e => e['name'] == result['name']))
amount++;
else
results.push(result);
}
return results;
}
function GetCharData(result)
{
if (presets[result['name']] != undefined)
return presets[result['name']];
var r = seedrandom(result['name']);
result['body'] = {
'build': process('build', r),
'height': 140 + (Math.floor(r() * 5) * 10),
'eyes': process('eyeColor', r),
};
result['fur'] = {
'color': process('furColor', r),
'pattern': process('furPattern' ,r),
};
result['hair'] = {
'color': process('hairColor', r),
'length': process('hairLength', r),
'style': process('hairStyle', r),
};
var month = Math.floor(r() * 10);
var days = month < 9 ? (month % 2 == 0 ? 25 : 26) : 27;
var day = Math.floor(r() * days);
result['birth'] =
{
'month': 1 + month,
'day': 1 + day,
};
result['likes'] = {
'dress': process('likeDress', r),
'food': process('likeFood', r),
'drink': process('likeDrink', r),
};
return result;
}
function GetChar(req, res)
{
if (req.query['claimant'] != undefined)
{
var myClaims = claims.listClaims(req.query['claimant']);
var ret = [];
for (var e in myClaims)
ret.push(GetChar({'query': { 'amount': 1, 'name': myClaims[e]['name'] }}, res)[0]);
//console.log(ret);
return ret;
}
var amount = parseInt(req.query['amount'] || 1);
if (amount > 100) amount = 100;
if (req.query['name'] != undefined)
{
req.query['name'] = req.query['name'].replace(/[^a-z 0-9]/gi, '');
req.query['name'] = req.query['name'].replace(/\w\S*/g, function(txt) { return txt.charAt(0).toUpperCase() + txt.substr(1); });
amount = 1;
seedrandom(req.query['name'], { global: true });
}
if (req.query['seed'] != undefined)
seedrandom(req.query['seed'], { global: true });
var results = [];
while (amount--)
{
var result = {};
var maybeBless = false;
var startingPattern = '';
if (req.query['male'] != undefined)
startingPattern = 'male';
else if (req.query['female'] != undefined)
startingPattern = 'female';
else if (req.query['blessed'] != undefined)
startingPattern = get(['male','female']);
else
{
maybeBless = true;
startingPattern = get(['male','female']);
}
result['name'] = process(startingPattern);
if (req.query['name'] != undefined)
result['name'] = req.query['name'];
var holoCheat = false;
if (result['name'] == 'Holo') {
result['name'] = 'Horo';
startingPattern = 'blessed';
holoCheat = true;
}
result['gender'] = startingPattern;
if ((maybeBless && Math.random() > 0.98) || (req.query['blessed'] != undefined))
result['gender'] = 'blessed';
result = GetCharData(result);
if (holoCheat)
result['name'] = 'Holo';
if (results.find(e => e['name'] == result['name']))
amount++;
else
results.push(result);
}
return results;
}
function genderIcon(r)
{
if (r['gender'] == 'male')
return '♂';
else if (r['gender'] == 'female')
return '♀';
else if (r['gender'] == 'blessed')
return 'x';
return '?';
}
function genderName(r)
{
if (r['gender'] == 'male')
return 'felir';
else if (r['gender'] == 'female')
return 'fela';
else if (r['gender'] == 'blessed')
return 'felhru';
return 'felin';
}
function heOrShe(r, initial)
{
if (r['gender'] == 'male')
return initial ? 'He' : 'he';
else if (r['gender'] == 'female')
return initial ? 'She' : 'she';
return initial ? 'They' : 'they';
}
function isOrAre(r)
{
if (r['gender'] == 'blessed')
return 'are';
return 'is';
}
function was(r)
{
if (r['gender'] == 'blessed')
return 'were';
return 'was';
}
router.get('/namegen/json', express.json(), function(req, res) {
res.json(GetName(req, res));
});
router.get('/namegen/table', function(req, res) {
var results = GetName(req, res);
var html = header + '<table>\n\t<th>#</th><th>Name</th>\n';
if (req.query['male'] != undefined || req.query['female'] != undefined)
html += '</tr>\n';
else
html += '<th>Gender</th></tr>\n';
var i = 0;
if (req.query['male'] != undefined || req.query['female'] != undefined)
results.forEach(r => { i++; html += `\t<tr><td>${i}</td><td>${r['name']}</td></tr>\n`; });
else
results.forEach(r => { i++; html += `\t<tr><td>${i}</td><td>${r['name']}</td><td>${r['gender']}</td></tr>\n`; });
html += '</table>\n';
res.send(html);
});
router.get('/namegen', function(req, res) {
var results = GetName(req, res);
var html = header + '<ol>\n';
if (req.query['male'] != undefined || req.query['female'] != undefined)
results.forEach(r => { html += `\t<li>${r['name']}</li>\n`; });
else
results.forEach(r => { html += `\t<li>${r['name']} ${genderIcon(r)}</li>\n`; });
html += '</ol>\n';
res.send(html);
});
router.get('/placegen/json', express.json(), function(req, res) {
res.json(GetPlace(req, res));
});
router.get('/placegen/table', function(req, res) {
var results = GetPlace(req, res);
var html = header + '<table>\n\t<th>#</th><th>English</th><th>Felinese</th></tr>\n';
var i = 0;
results.forEach(r => { i++; html += `\t<tr><td>${i}</td><td>${r['name']}</td><td>${r['felinese']}</td></tr>\n`; });
html += '</table>';
res.send(html);
});
router.get('/placegen', function(req, res) {
var results = GetPlace(req, res);
var html = header + '<ol>\n';
results.forEach(r => { html += `\t<li>${r['name']} (${r['felinese']})</li>\n`; });
html += '</ol>\n';
res.send(html);
});
router.get('/chargen/json', express.json(), function(req, res) {
var r = GetChar(req, res);
res.json(r);
});
router.get('/chargen/claim', function(req, res) {
var html = header + '\n';
var name = req.query['name'].replace(/[^a-z0-9]/gi, '');
name = name.replace(/\w\S*/g, function(txt) { return txt.charAt(0).toUpperCase() + txt.substr(1); });
html += 'If you want to claim this character, visit the Firrhna Project or bcord Discord server and say this:<p>\n';
html += `<tt>$char claim ${name}</tt>`;
res.send(html);
});
router.get('/chargen/table', function(req, res) {
var results = GetChar(req, res);
if (results.length == 0) {
res.send(header + '\nNo results to show.');
return;
}
var html = header + '<table>\n';
html += '\t<th rowspan=2>#</th><th rowspan=2>Name</th><th rowspan=2>Gender</th><th colspan=2>Body<th><th colspan=2>Fur</th><th colspan=3>Hair</th>';
html += '<th colspan=3>Birth</th><th colspan=3>Likes</th><th rowspan=2>Status</th></tr>\n';
html += '\t<th>Build</th><th>Height</th><th>Eyes</th><th>Color</th><th>Pattern</th><th>Color</th><th>Length</th><th>Style</th>';
html += '<th colspan=2>Month</th><th>Day</th><th>Dress</th><th>Food</th><th>Drink</th>';
html += '<tr>\n';
var i = 0;
results.forEach(r => {
i++;
var name = r['name'].replace(/[^a-z 0-9]/gi, '');
name = name.replace(/\w\S*/g, function(txt) { return txt.charAt(0).toUpperCase() + txt.substr(1); });
var claimLink = `<a href="/chargen/claim/?name=${r['name']}">`;
var claimHtml = `${claimLink}Claimable</a>`;
var claim = claims.checkCharacterClaim(r['name']);
if (claim) {
if (claim['gender']) r['gender'] = claim['gender'];
if (r['status'] & 1) claimHtml = `Premium, claimed by <a href="/faas/chargen/table/?claimant=${claim['claimant']['name']}">${claim['claimant']['name']}</a>`;
else claimHtml = `Claimed by <a href="/faas/chargen/table/?claimant=${claim['claimant']['name']}">${claim['claimant']['name']}</a>`;
}
else
if (r['status'] & 1) claimHtml = `Premium, ${claimLink}claimable</a>`;
html += `\t<tr><td>${i}</td><td>${r['name']}</td><td>${r['gender']}</td>`;
html += `<td>${r['body']['build']}</td><td>${r['body']['height']}cm</td><td>${r['body']['eyes']}</td>`;
html += `<td>${r['fur']['color']}</td><td>${r['fur']['pattern']}</td>`;
html += `<td>${r['hair']['color']}</td><td>${r['hair']['length']}</td><td>${r['hair']['style']}</td>`;
if (r['birth']['month'])
html += `<td>${r['birth']['month']}</td><td>${patterns['months'][r['birth']['month'] - 1]}</td><td>${r['birth']['day']}</td>`;
else
html += '<td colspan=3>Unknown</td>';
html += `<td>${r['likes']['dress']}</td><td>${r['likes']['food']}</td><td>${r['likes']['drink']}</td>`;
html += `<td>${claimHtml}</td>`;
html += `</tr>\n`; });
html += '</table>';
res.send(html);
});
function aOrAn(thing)
{
if (thing[0] == 'a' ||
thing[0] == 'e' ||
thing[0] == 'i' ||
thing[0] == 'o' ||
thing[0] == 'u')
return 'an ' + thing;
return 'a ' + thing;
}
function getCharProse(r)
{
var birthDay = `${patterns['months'][r['birth']['month'] - 1]} ${r['birth']['day']}`;
if (r['birth']['month'] == 0)
birthDay = 'an unknown date';
var ret = `${r['name']} is ${aOrAn(r['body']['build'])} ${genderName(r)}, ${r['body']['height']} centimeters tall,`;
ret += ` with ${r['hair']['length']}, ${r['hair']['style']}, ${r['hair']['color']} hair, ${r['body']['eyes']} eyes,`;
ret += ` and ${r['fur']['color']} fur${r['fur']['pattern']=='none' ? '' : ' with '+r['fur']['pattern']}.`;
ret += ` ${heOrShe(r,true)} ${r['gender']=='blessed'?'were':'was'} born on ${birthDay}.`;
ret += ` ${heOrShe(r,true)} ${r['gender']=='blessed'?'like':'likes'} ${r['likes']['dress']}, ${r['likes']['food']}, and ${r['likes']['drink']}.`;
return ret;
}
router.get('/chargen/botprose', function(req, res) {
var results = GetChar(req, res);
if (results.length == 0) {
res.send('No results to show.');
return;
}
var r = results[0];
var name = r['name'].replace(/[^a-z0-9]/gi, '');
name = name.replace(/\w\S*/g, function(txt) { return txt.charAt(0).toUpperCase() + txt.substr(1); });
var ret = {
'name': r['name'],
'status': r['status'],
'prose': getCharProse(r),
'claim': claims.checkCharacterClaim(r['name']) || {'status':0},
};
res.json(ret);
});
router.get('/chargen/portrait', async function(req, res) {
var results = GetChar(req, res);
var r = results[0];
var name = r['name'].replace(/[^a-z0-9]/gi, '');
r['name'] = name.replace(/\w\S*/g, function(txt) { return txt.charAt(0).toUpperCase() + txt.substr(1); });
var buffer = await getPortrait(r, req);
res.set('Content-Type', 'image/png')
res.send(buffer);
});
router.get('/chargen', function(req, res) {
var results = GetChar(req, res);
if (results.length == 0) {
res.send(header + '\nNo results to show.');
return;
}
var html = header + '<ol>\n';
results.forEach(r => {
var name = r['name'].replace(/[^a-z0-9]/gi, '');
name = name.replace(/\w\S*/g, function(txt) { return txt.charAt(0).toUpperCase() + txt.substr(1); });
var claimLink = `<a href="/faas/chargen/claim/?name=${r['name']}">`;
var claimText = `${heOrShe(r,true)} can be ${claimLink}claimed</a>.`;
var claim = claims.checkCharacterClaim(r['name']);
if (claim) {
if (claim['gender']) r['gender'] = claim['gender'];
if (r['status'] & 1) claimText = `${heOrShe(r,true)} ${isOrAre(r)} a premium character, claimed by <a href="/faas/chargen/table/?claimant=${claim['claimant']['name']}">${claim['claimant']['name']}</a>.`;
else claimText = `${heOrShe(r,true)} ${isOrAre(r)} claimed by <a href="/faas/chargen/table/?claimant=${claim['claimant']['name']}">${claim['claimant']['name']}</a>.`;
}
else
if (r['status'] & 1) claimText = `${heOrShe(r,true)} ${isOrAre(r)} a premium character and can be ${claimLink}claimed</a>.`;
html += `\t<li>${getCharProse(r)} ${claimText}</li>`;
});
html += '</ol>';
res.send(html);
});
module.exports = router;