mirror of
https://github.com/AlecM33/Werewolf.git
synced 2025-12-26 15:57:50 +01:00
simpler routing, fix socket reconnection bug, expand mod view
This commit is contained in:
@@ -90,12 +90,17 @@ app.use('/favicon.ico', (req, res) => {
|
||||
});
|
||||
|
||||
const router = require('./routes/router');
|
||||
const faviconRouter = require('./routes/favicon-router');
|
||||
const staticRouter = require('./routes/static-router');
|
||||
|
||||
app.use('', router);
|
||||
app.use('', staticRouter);
|
||||
app.use('', faviconRouter);
|
||||
|
||||
app.use('/images', express.static(path.join(__dirname, '../client/images')));
|
||||
app.use('/scripts', express.static(path.join(__dirname, '../client/scripts')));
|
||||
app.use('/styles', express.static(path.join(__dirname, '../client/styles')));
|
||||
app.use('/styles/third-party', express.static(path.join(__dirname, '../client/styles/third_party')));
|
||||
app.use('/modules/third-party', express.static(path.join(__dirname, '../client/modules/third_party')))
|
||||
app.use('/modules', express.static(path.join(__dirname, '../client/modules')))
|
||||
app.use('/model', express.static(path.join(__dirname, '../client/model')))
|
||||
app.use('/config', express.static(path.join(__dirname, '../client/config')))
|
||||
app.use('/webfonts', express.static(path.join(__dirname, '../client/webfonts')));
|
||||
|
||||
app.use(function (req, res) {
|
||||
res.sendFile(path.join(__dirname, '../client/views/404.html'));
|
||||
|
||||
@@ -244,7 +244,16 @@ function handleRequestForGameState(namespace, logger, gameRunner, accessCode, pe
|
||||
matchingPerson.socketId = socket.id;
|
||||
ackFn(GameStateCurator.getGameStateFromPerspectiveOfPerson(game, matchingPerson, gameRunner, socket, logger));
|
||||
} else {
|
||||
rejectClientRequestForGameState(ackFn);
|
||||
logger.trace('this person is already associated with a socket connection');
|
||||
let alreadyConnectedSocket = namespace.connected[matchingPerson.socketId];
|
||||
if (alreadyConnectedSocket && alreadyConnectedSocket.leave) {
|
||||
alreadyConnectedSocket.leave(accessCode, ()=> {
|
||||
logger.trace('kicked existing connection out of room ' + accessCode);
|
||||
socket.join(accessCode);
|
||||
matchingPerson.socketId = socket.id;
|
||||
ackFn(GameStateCurator.getGameStateFromPerspectiveOfPerson(game, matchingPerson, gameRunner, socket, logger));
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -271,11 +280,13 @@ function handleRequestForGameState(namespace, logger, gameRunner, accessCode, pe
|
||||
);
|
||||
} else {
|
||||
rejectClientRequestForGameState(ackFn);
|
||||
logger.trace('this game is full');
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
rejectClientRequestForGameState(ackFn);
|
||||
logger.trace('the game' + accessCode + ' was not found');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -71,7 +71,8 @@ function mapPeopleForModerator(people, client) {
|
||||
userType: person.userType,
|
||||
gameRole: person.gameRole,
|
||||
gameRoleDescription: person.gameRoleDescription,
|
||||
alignment: person.alignment
|
||||
alignment: person.alignment,
|
||||
out: person.out
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -82,12 +83,13 @@ function mapPeopleForTempModerator(people, client) {
|
||||
})
|
||||
.map((person) => ({
|
||||
name: person.name,
|
||||
userType: person.userType
|
||||
userType: person.userType,
|
||||
out: person.out
|
||||
}));
|
||||
}
|
||||
|
||||
function mapPerson(person) {
|
||||
return { name: person.name, userType: person.userType };
|
||||
return { name: person.name, userType: person.userType, out: person.out };
|
||||
}
|
||||
|
||||
module.exports = GameStateCurator;
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
|
||||
function stepFn (serverTimerInstance, expected) {
|
||||
const now = Date.now();
|
||||
const now = Date.now(); //
|
||||
serverTimerInstance.currentTimeInMillis = serverTimerInstance.totalTime - (now - serverTimerInstance.start);
|
||||
if (now - serverTimerInstance.start >= serverTimerInstance.totalTime) {
|
||||
clearTimeout(serverTimerInstance.ticking);
|
||||
if (now - serverTimerInstance.start >= serverTimerInstance.totalTime) { // check if the time has elapsed
|
||||
serverTimerInstance.logger.debug(
|
||||
'ELAPSED: ' + (now - serverTimerInstance.start) + 'ms (~'
|
||||
+ (Math.abs(serverTimerInstance.totalTime - (now - serverTimerInstance.start)) / serverTimerInstance.totalTime).toFixed(3) + '% error).'
|
||||
);
|
||||
serverTimerInstance.timesUpResolver(); // this is a reference to the callback defined in the construction of the promise in runTimer()
|
||||
clearTimeout(serverTimerInstance.ticking);
|
||||
return;
|
||||
}
|
||||
const delta = now - expected;
|
||||
@@ -64,10 +64,6 @@ class ServerTimer {
|
||||
clearTimeout(this.ticking);
|
||||
}
|
||||
let now = Date.now();
|
||||
this.logger.debug(
|
||||
'ELAPSED (PAUSE): ' + (now - this.start) + 'ms (~'
|
||||
+ (Math.abs(this.totalTime - (now - this.start)) / this.totalTime).toFixed(3) + '% error).'
|
||||
);
|
||||
}
|
||||
|
||||
resumeTimer() {
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
const express = require('express');
|
||||
const faviconRouter = express.Router();
|
||||
const path = require('path');
|
||||
const checkIfFileExists = require("./util");
|
||||
|
||||
faviconRouter.use('/client/favicon_package/*', (req, res) => {
|
||||
let filePath = path.join(__dirname, ('../../' + req.baseUrl));
|
||||
let extension = path.extname(filePath);
|
||||
checkIfFileExists(filePath).then((fileExists) => {
|
||||
if (fileExists && (extension === '.png' || extension === '.ico' || extension === '.svg' || extension === 'xml')) {
|
||||
res.sendFile(filePath);
|
||||
} else {
|
||||
res.sendStatus(404);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = faviconRouter;
|
||||
@@ -1,5 +1,5 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const router = express.Router({ strict: true });
|
||||
const path = require('path');
|
||||
|
||||
router.get('/', function (request, response) {
|
||||
|
||||
@@ -1,115 +0,0 @@
|
||||
const express = require('express');
|
||||
const staticRouter = express.Router();
|
||||
const path = require('path');
|
||||
const checkIfFileExists = require("./util");
|
||||
|
||||
staticRouter.use('/styles/**', (req, res) => {
|
||||
let filePath = path.join(__dirname, ('../../client/' + req.baseUrl));
|
||||
let extension = path.extname(filePath);
|
||||
checkIfFileExists(filePath).then((fileExists) => {
|
||||
if (fileExists && (extension === '.css' || extension === '.min.css')) {
|
||||
res.sendFile(filePath);
|
||||
} else {
|
||||
res.sendStatus(404);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
staticRouter.use('/client/webfonts/*', (req, res) => {
|
||||
let filePath = path.join(__dirname, ('../' + req.baseUrl));
|
||||
let extension = path.extname(filePath);
|
||||
checkIfFileExists(filePath).then((fileExists) => {
|
||||
if (fileExists && (extension === '.ttf' || extension === '.woff2')) {
|
||||
res.sendFile(filePath);
|
||||
} else {
|
||||
res.sendStatus(404);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
staticRouter.use('/images/*', (req, res) => {
|
||||
let filePath = path.join(__dirname, ('../../client/' + req.baseUrl));
|
||||
let extension = path.extname(filePath);
|
||||
checkIfFileExists(filePath).then((fileExists) => {
|
||||
if (fileExists && (extension === '.svg' || extension === '.png' || extension === '.jpg' || extension === '.gif')) {
|
||||
res.sendFile(filePath);
|
||||
} else {
|
||||
res.sendStatus(404);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
staticRouter.use('/scripts/*', (req, res) => {
|
||||
let filePath = path.join(__dirname, ('../../client/' + req.baseUrl));
|
||||
let extension = path.extname(filePath);
|
||||
checkIfFileExists(filePath).then((fileExists) => {
|
||||
if (fileExists && (extension === '.js')) {
|
||||
res.sendFile(filePath);
|
||||
} else {
|
||||
res.sendStatus(404);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
staticRouter.use('/webfonts/*', (req, res) => {
|
||||
let filePath = path.join(__dirname, ('../../client/' + req.baseUrl));
|
||||
let extension = path.extname(filePath);
|
||||
checkIfFileExists(filePath).then((fileExists) => {
|
||||
if (fileExists && (extension === '.woff2')) {
|
||||
res.sendFile(filePath);
|
||||
} else {
|
||||
res.sendStatus(404);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
staticRouter.use('/views/*', (req, res) => {
|
||||
let filePath = path.join(__dirname, ('../../client/' + req.baseUrl));
|
||||
let extension = path.extname(filePath);
|
||||
checkIfFileExists(filePath).then((fileExists) => {
|
||||
if (fileExists && (extension === '.html')) {
|
||||
res.sendFile(filePath);
|
||||
} else {
|
||||
res.sendFile('../views/404.html');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
staticRouter.use('/config/*', (req, res) => {
|
||||
let filePath = path.join(__dirname, ('../../client/' + req.baseUrl));
|
||||
let extension = path.extname(filePath);
|
||||
checkIfFileExists(filePath).then((fileExists) => {
|
||||
if (fileExists && (extension === '.js')) {
|
||||
res.sendFile(filePath);
|
||||
} else {
|
||||
res.sendFile('../views/404.html');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
staticRouter.use('/modules/**', (req, res) => {
|
||||
let filePath = path.join(__dirname, ('../../client/' + req.baseUrl));
|
||||
let extension = path.extname(filePath);
|
||||
checkIfFileExists(filePath).then((fileExists) => {
|
||||
if (fileExists && (extension === '.js' || extension === '.min.js')) {
|
||||
res.sendFile(filePath);
|
||||
} else {
|
||||
res.sendFile('../views/404.html');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
staticRouter.use('/model/**', (req, res) => {
|
||||
let filePath = path.join(__dirname, ('../../client/' + req.baseUrl));
|
||||
let extension = path.extname(filePath);
|
||||
checkIfFileExists(filePath).then((fileExists) => {
|
||||
if (fileExists && (extension === '.js')) {
|
||||
res.sendFile(filePath);
|
||||
} else {
|
||||
res.sendFile('../views/404.html');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = staticRouter;
|
||||
Reference in New Issue
Block a user