mirror of
https://github.com/AlecM33/Werewolf.git
synced 2025-12-26 15:57:50 +01:00
refactor join to intermediary page
This commit is contained in:
@@ -66,23 +66,43 @@ router.get('/:code/availability', function (req, res) {
|
||||
});
|
||||
|
||||
router.patch('/:code/players', function (req, res) {
|
||||
console.log(req.body);
|
||||
if (
|
||||
req.body === null
|
||||
|| req.body.cookie === null
|
||||
|| (typeof req.body.cookie !== 'string' && req.body.cookie !== false)
|
||||
|| (req.body.cookie.length !== globals.USER_SIGNATURE_LENGTH && req.body.cookie !== false)
|
||||
|| !validateAccessCode(req.body.accessCode)
|
||||
|| !validateName(req.body.playerName)
|
||||
) {
|
||||
res.status(400).send();
|
||||
} else {
|
||||
const game = gameManager.activeGameRunner.activeGames[req.body.accessCode];
|
||||
if (game) {
|
||||
gameManager.joinGame(game, req.body.playerName).then((data) => {
|
||||
res.status(200).send({ cookie: data, environment: gameManager.environment });
|
||||
}).catch((code) => {
|
||||
res.status(code).send();
|
||||
});
|
||||
} else {
|
||||
res.status(404).send();
|
||||
}
|
||||
}
|
||||
gameManager.joinGame(req.body.cookie, req.params.code).then((data) => {
|
||||
res.status(200).send(data);
|
||||
}).catch((code) => {
|
||||
res.status(code).send();
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/environment', function (req, res) {
|
||||
res.status(200).send(gameManager.environment);
|
||||
});
|
||||
|
||||
function validateName(name) {
|
||||
return typeof name === 'string' && name.length > 0 && name.length <= 30;
|
||||
}
|
||||
|
||||
function validateCookie(cookie) {
|
||||
return cookie === null
|
||||
|| (typeof cookie !== 'string' && cookie !== false)
|
||||
|| (cookie.length !== globals.USER_SIGNATURE_LENGTH && cookie !== false)
|
||||
}
|
||||
|
||||
function validateAccessCode(accessCode) {
|
||||
return /^[a-zA-Z0-9]+$/.test(accessCode) && accessCode.length === globals.ACCESS_CODE_LENGTH
|
||||
}
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const globals = {
|
||||
ACCESS_CODE_CHAR_POOL: 'abcdefghijklmnopqrstuvwxyz0123456789',
|
||||
ACCESS_CODE_CHAR_POOL: 'BCDFGHJKLMNPQRSTVWXYZ0123456789',
|
||||
ACCESS_CODE_LENGTH: 6,
|
||||
CLOCK_TICK_INTERVAL_MILLIS: 10,
|
||||
STALE_GAME_HOURS: 12,
|
||||
|
||||
@@ -38,7 +38,6 @@ gameManager.namespace = inGameSocketServer;
|
||||
inGameSocketServer.on('connection', function (socket) {
|
||||
socket.on('disconnecting', (reason) => {
|
||||
logger.trace('client socket disconnecting because: ' + reason);
|
||||
gameManager.removeClientFromLobbyIfApplicable(socket);
|
||||
});
|
||||
gameManager.addGameSocketHandlers(inGameSocketServer, socket);
|
||||
});
|
||||
|
||||
@@ -157,7 +157,7 @@ class GameManager {
|
||||
};
|
||||
|
||||
createGame = (gameParams) => {
|
||||
const expectedKeys = ['deck', 'hasTimer', 'timerParams'];
|
||||
const expectedKeys = ['deck', 'hasTimer', 'timerParams', 'moderatorName'];
|
||||
if (typeof gameParams !== 'object'
|
||||
|| expectedKeys.some((key) => !Object.keys(gameParams).includes(key))
|
||||
) {
|
||||
@@ -167,7 +167,8 @@ class GameManager {
|
||||
// to avoid excessive memory build-up, every time a game is created, check for and purge any stale games.
|
||||
pruneStaleGames(this.activeGameRunner.activeGames, this.activeGameRunner.timerThreads, this.logger);
|
||||
const newAccessCode = this.generateAccessCode();
|
||||
const moderator = initializeModerator(UsernameGenerator.generate(), gameParams.hasDedicatedModerator);
|
||||
const moderator = initializeModerator(gameParams.moderatorName, gameParams.hasDedicatedModerator);
|
||||
moderator.assigned = true;
|
||||
if (gameParams.timerParams !== null) {
|
||||
gameParams.timerParams.paused = false;
|
||||
}
|
||||
@@ -181,7 +182,7 @@ class GameManager {
|
||||
gameParams.timerParams
|
||||
);
|
||||
this.activeGameRunner.activeGames[newAccessCode].createTime = new Date().toJSON();
|
||||
return Promise.resolve(newAccessCode);
|
||||
return Promise.resolve({ accessCode: newAccessCode, cookie: moderator.cookie, environment: this.environment });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -253,40 +254,34 @@ class GameManager {
|
||||
}
|
||||
};
|
||||
|
||||
joinGame = (cookie, accessCode) => {
|
||||
const game = this.activeGameRunner.activeGames[accessCode];
|
||||
if (game) {
|
||||
const person = findPersonByField(game, 'cookie', cookie);
|
||||
if (person) {
|
||||
return Promise.resolve(person.cookie);
|
||||
} else {
|
||||
const unassignedPerson = game.moderator.assigned === false
|
||||
? game.moderator
|
||||
: game.people.find((person) => person.assigned === false);
|
||||
if (unassignedPerson) {
|
||||
this.logger.trace('request from client to join game. Assigning: ' + unassignedPerson.name);
|
||||
unassignedPerson.assigned = true;
|
||||
game.isFull = isGameFull(game);
|
||||
this.namespace.in(game.accessCode).emit(
|
||||
globals.EVENTS.PLAYER_JOINED,
|
||||
GameStateCurator.mapPerson(unassignedPerson),
|
||||
game.isFull
|
||||
);
|
||||
return Promise.resolve(unassignedPerson.cookie);
|
||||
} else { // if the game is full, make them a spectator.
|
||||
const spectator = new Person(
|
||||
createRandomId(),
|
||||
createRandomId(),
|
||||
UsernameGenerator.generate(),
|
||||
globals.USER_TYPES.SPECTATOR
|
||||
);
|
||||
this.logger.trace('new spectator: ' + spectator.name);
|
||||
game.spectators.push(spectator);
|
||||
return Promise.resolve(spectator.cookie);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Promise.reject(404);
|
||||
joinGame = (game, name) => {
|
||||
if (isNameTaken(game, name)) {
|
||||
return Promise.reject(400);
|
||||
}
|
||||
const unassignedPerson = game.moderator.assigned === false
|
||||
? game.moderator
|
||||
: game.people.find((person) => person.assigned === false);
|
||||
if (unassignedPerson) {
|
||||
this.logger.trace('request from client to join game. Assigning: ' + unassignedPerson.name);
|
||||
unassignedPerson.assigned = true;
|
||||
unassignedPerson.name = name;
|
||||
game.isFull = isGameFull(game);
|
||||
this.namespace.in(game.accessCode).emit(
|
||||
globals.EVENTS.PLAYER_JOINED,
|
||||
GameStateCurator.mapPerson(unassignedPerson),
|
||||
game.isFull
|
||||
);
|
||||
return Promise.resolve(unassignedPerson.cookie);
|
||||
} else { // if the game is full, make them a spectator.
|
||||
const spectator = new Person(
|
||||
createRandomId(),
|
||||
createRandomId(),
|
||||
name,
|
||||
globals.USER_TYPES.SPECTATOR
|
||||
);
|
||||
this.logger.trace('new spectator: ' + spectator.name);
|
||||
game.spectators.push(spectator);
|
||||
return Promise.resolve(spectator.cookie);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ const http = require('http');
|
||||
const https = require('https');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const secure = require("express-force-https");
|
||||
|
||||
const ServerBootstrapper = {
|
||||
processCLIArgs: () => {
|
||||
@@ -55,6 +56,7 @@ const ServerBootstrapper = {
|
||||
}
|
||||
} else {
|
||||
logger.warn('starting main in PRODUCTION mode. This should not be used for local development.');
|
||||
app.use(secure);
|
||||
main = http.createServer(app);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@ router.get('/create', function (request, response) {
|
||||
response.sendFile(path.join(__dirname, '../../client/src/views/create.html'));
|
||||
});
|
||||
|
||||
router.get('/join/:code', function (request, response) {
|
||||
response.sendFile(path.join(__dirname, '../../client/src/views/join.html'));
|
||||
});
|
||||
|
||||
router.get('/how-to-use', function (request, response) {
|
||||
response.sendFile(path.join(__dirname, '../../client/src/views/how-to-use.html'));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user