words instead of random hex
Signed-off-by: James Ketrenos <james_eikona@ketrenos.com>
This commit is contained in:
parent
12a93c9938
commit
01a1c215e8
@ -23,6 +23,7 @@
|
||||
"node-fetch": "^2.6.0",
|
||||
"node-gzip": "^1.1.2",
|
||||
"nodemailer": "^6.3.0",
|
||||
"random-words": "^1.1.2",
|
||||
"sequelize": "^5.21.6",
|
||||
"sqlite3": "^4.1.1",
|
||||
"typeface-roboto": "0.0.75"
|
||||
|
@ -2,7 +2,10 @@
|
||||
|
||||
const express = require("express"),
|
||||
crypto = require("crypto"),
|
||||
{ readFile, writeFile } = require("fs").promises;
|
||||
{ readFile, writeFile } = require("fs").promises,
|
||||
fs = require("fs"),
|
||||
accessSync = fs.accessSync,
|
||||
randomWords = require("random-words");
|
||||
|
||||
let gameDB;
|
||||
|
||||
@ -504,6 +507,7 @@ const sendGame = async (req, res, game, error) => {
|
||||
/* Enforce game limit of >= 2 players */
|
||||
if (active < 2 && game.state != 'lobby' && game.state != 'invalid') {
|
||||
let message = "Insufficient players in game. Setting back to lobby."
|
||||
console.log(game);
|
||||
game.chat.push({ date: Date.now(), message: message });
|
||||
console.log(message);
|
||||
game.state = 'lobby';
|
||||
@ -568,7 +572,20 @@ const sendGame = async (req, res, game, error) => {
|
||||
}
|
||||
|
||||
const createGame = (id) => {
|
||||
id = id ? id : crypto.randomBytes(8).toString('hex');
|
||||
/* Look for a new game with random words that does not already exist */
|
||||
while (!id) {
|
||||
id = randomWords(4).join('_');
|
||||
console.log(`Looking for ${id}`);
|
||||
try {
|
||||
/* If file can be read, it already exists so look for a new name */
|
||||
accessSync(`games/${id}`, fs.F_OK);
|
||||
id = '';
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const game = {
|
||||
startTime: Date.now(),
|
||||
turns: 0,
|
||||
@ -611,7 +628,7 @@ router.post("/:id?", (req, res/*, next*/) => {
|
||||
}
|
||||
|
||||
const game = createGame(id);
|
||||
|
||||
|
||||
return sendGame(req, res, game);
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user