Skip to content
Snippets Groups Projects
Commit 97110973 authored by Fredrik Fonn Hansen's avatar Fredrik Fonn Hansen :8ball:
Browse files

Resolve "Backend nice-to-haves"

parent 17ab0ee1
No related branches found
No related tags found
1 merge request!48Resolve "Backend nice-to-haves"
import { Request, Response } from 'express';
export const heartbeat = async (req: Request, res: Response): Promise<void> => {
res.status(200).send('Server is alive');
};
import { GameHandler } from '../gameHandler';
import { log } from './console';
export function disposeInactiveGames() {
const gameHandler = GameHandler.getInstance();
// loop through all games
gameHandler.getGames().forEach((game) => {
// if game is inactive for 10 minutes
if (Date.now() - game.getLastActionTimeStamp() > 10 * 60 * 1000) {
// dispose game and lobby
log(
'removing inactive game in lobby:' +
game.lobby.id +
' containing users:' +
game.lobby
.getUsers()
.map((u) => u.username)
.join(', ')
);
gameHandler.removeLobby(game.lobby);
gameHandler.removeGame(game);
}
});
}
......@@ -5,8 +5,10 @@ import lobbyRoutes from './routes/lobbyRoutes';
import userRoutes from './routes/userRoutes';
import gameRoutes from './routes/gameRoutes';
import highscoreRoutes from './routes/highscoreRoutes';
import serverRoutes from './routes/serverRoutes';
import { log } from './functions/console';
import { welcome } from './functions/welcomeScreen';
import { disposeInactiveGames } from './functions/disposeInactiveGames';
new GameHandler(); // singleton ;)
......@@ -23,6 +25,7 @@ app.use('/lobby', lobbyRoutes);
app.use('/user', userRoutes);
app.use('/game', gameRoutes);
app.use('/highscore', highscoreRoutes);
app.use('/server', serverRoutes);
// in testing, we don't want to cache the results
app.set('etag', false);
......@@ -31,6 +34,10 @@ app.use((req, res, next) => {
next();
});
setInterval(() => {
disposeInactiveGames();
}, 60 * 1000);
app.listen(port, () => {
welcome();
if (process.env.NODE_ENV === 'production') {
......
......@@ -12,6 +12,7 @@ export interface IGame {
gameStatus: boolean;
gameId: string;
terrain: ITerrain;
lastActionTimeStamp: number;
notifyUsers(): void;
......@@ -37,4 +38,5 @@ export interface IGame {
getUsers(): [User, IStats][];
getTerrain(): ITerrain;
getLastActionTimeStamp(): number;
}
......@@ -17,12 +17,14 @@ export class Game implements IGame {
gameId: string;
terrain: ITerrain;
users: [User, IStats][]; // left [0] and right [1] user
lastActionTimeStamp: number;
constructor(lobby: ILobby) {
this.lobby = lobby;
this.gameStatus = false; // game not finished
this.gameId = Math.random().toString(36);
this.terrain = new Terrain();
this.lastActionTimeStamp = Date.now();
// insert the lobby users into the game and create a new stats object for each user
this.users = lobby.getUsers().map((user) => [user, new Stats()]);
......@@ -46,6 +48,9 @@ export class Game implements IGame {
this.notifyUsers(); // TODO: implement this method
log('Game ' + this.gameId + ' created.');
}
getLastActionTimeStamp(): number {
return this.lastActionTimeStamp;
}
getTerrain(): ITerrain {
return this.terrain;
}
......@@ -173,6 +178,7 @@ export class Game implements IGame {
setGameState(gameState: IGame): void {
this.gameStatus = gameState.gameStatus;
this.users = gameState.users;
this.lastActionTimeStamp = Date.now();
}
getGameState(): IGame {
......
import express from 'express';
import * as serverController from '../controllers/serverController';
const router = express.Router();
router.get('/heartbeat', serverController.heartbeat);
export default router;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment