Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 3 additions & 99 deletions src/routes/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ import express from 'express';

import { Game } from '../models/models';
import messages from '../utils/messages';
import words from '../utils/words';
import wordGenerator from '../utils/wordGenerator';
import getNextPasswordHolder from '../utils/getNextPasswordHolder';

const router = express.Router();

const MAX_HINTS = process.env.MAX_HINTS ? Number(process.env.MAX_HINTS) : 4;
const MAX_HINT_LENGTH = process.env.MAX_HINT_LENGTH ? Number(process.env.MAX_HINT_LENGTH) : 25;
const DURATION = process.env.DURATION ? Number(process.env.DURATION) : 60;

router.post('/start', async (req: express.Request, res: express.Response) => {
const {
Expand Down Expand Up @@ -86,99 +82,7 @@ router.post('/next', async (req: express.Request, res: express.Response) => {
res.json({ success: false, message: messages.userNotFound });
return;
}
if (game.solvedBy.length !== (game.players.length - 1)
&& new Date().getTime() < game.time.end) {
const previousPassword = game.usedPasswords.length > 1 ? game.usedPasswords.slice(-2)[0] : '';
const currentPassword = username === game.passwordHolder ? game.password : '';

res.json({
success: true,
message: {
players: game.players,
currentRound: game.currentRound,
rounds: game.rounds,
passwordHolder: game.passwordHolder,
passwordLength: game.password.length,
previousPassword,
currentPassword,
hints: game.hints,
roundEnd: game.time.end,
},
});
return;
}

const { passwordHolder } = game;

const { nextPasswordHolder, currentRound } = getNextPasswordHolder(passwordHolder, game);
game.currentRound = currentRound;

if (game.currentRound > game.rounds) {
game.players = game.players.map((player) => {
const p = player;
p.points = 0;
return p;
});
game.hasStarted = false;
game.rounds = 3;
game.currentRound = 1;
game.password = '';
game.passwordHolder = '';
game.usedPasswords = [];
game.time.start = new Date().getTime();
game.time.end = new Date().getTime();
game.markModified('usedPasswords');
game.solvedBy = [];
game.markModified('solvedBy');
game.hints = [];
game.markModified('hints');
try {
await game.save();
} catch (e) {
if (e.name === 'VersionError') {
res.json({ success: false, message: messages.versionError });
return;
}
}
res.json({ success: false, message: messages.gameEnded });
return;
}

let password = wordGenerator();

if (words.length > game.usedPasswords.length) {
while (game.usedPasswords.includes(password)) {
password = wordGenerator();
}
}

password = password.toLowerCase();

const previousPassword = game.password || '';

const date: Date = new Date();
const time = date.getTime();

game.time.start = time;
game.time.end = time + (DURATION * 1000);
game.password = password;
game.passwordHolder = nextPasswordHolder;
game.usedPasswords.push(password);
game.markModified('usedPasswords');
game.solvedBy = [];
game.markModified('solvedBy');
game.hints = [];
game.markModified('hints');

try {
await game.save();
} catch (e) {
if (e.name === 'VersionError') {
res.json({ success: false, message: messages.versionError });
return;
}
}

const previousPassword = game.usedPasswords.length > 1 ? game.usedPasswords.slice(-2)[0] : '';
const currentPassword = username === game.passwordHolder ? game.password : '';

res.json({
Expand All @@ -187,8 +91,8 @@ router.post('/next', async (req: express.Request, res: express.Response) => {
players: game.players,
currentRound: game.currentRound,
rounds: game.rounds,
passwordHolder: nextPasswordHolder,
passwordLength: password.length,
passwordHolder: game.passwordHolder,
passwordLength: game.password.length,
previousPassword,
currentPassword,
hints: game.hints,
Expand Down
3 changes: 3 additions & 0 deletions src/sockets/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Game } from '../models/models';
import { PlayerInterface } from '../models/player';
import attempt from '../utils/attempt';
import messages from '../utils/messages';
import timeHandler from './timeHandler';

export async function onStart(
data: { roomId: string },
Expand All @@ -22,6 +23,8 @@ export async function onStart(
return;
}

timeHandler(data, io, namespace);

io.of(namespace).in(roomId).emit('start', {
hasStarted: true,
roomId,
Expand Down
84 changes: 84 additions & 0 deletions src/sockets/timeHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import socketio from 'socket.io';

import words from '../utils/words';
import { Game } from '../models/models';
import wordGenerator from '../utils/wordGenerator';
import liveGames from '../utils/liveGames';
import getNextPasswordHolder from '../utils/getNextPasswordHolder';

const DURATION = process.env.DURATION ? Number(process.env.DURATION) : 60;

export default async function timeHandler(
data: { roomId: string },
io: socketio.Server,
namespace: string,
) {
const { roomId } = data;

const game = await Game.findOne({ roomId });

if (!game || !game.hasStarted) {
return;
}

const { passwordHolder } = game;

const { nextPasswordHolder, currentRound } = getNextPasswordHolder(passwordHolder, game);
game.currentRound = currentRound;

if (game.currentRound > game.rounds) {
game.players = game.players.map((player) => {
const p = player;
p.points = 0;
return p;
});
game.hasStarted = false;
game.rounds = 3;
game.currentRound = 1;
game.password = '';
game.passwordHolder = '';
game.usedPasswords = [];
game.time.start = new Date().getTime();
game.time.end = new Date().getTime();
game.markModified('usedPasswords');
game.solvedBy = [];
game.markModified('solvedBy');
game.hints = [];
game.markModified('hints');

await game.save();
return;
}

let password = wordGenerator();

if (words.length > game.usedPasswords.length) {
while (game.usedPasswords.includes(password)) {
password = wordGenerator();
}
}

password = password.toLowerCase();

const date: Date = new Date();
const time = date.getTime();

game.time.start = time;
game.time.end = time + (DURATION * 1000);
game.password = password;
game.passwordHolder = nextPasswordHolder;
game.usedPasswords.push(password);
game.markModified('usedPasswords');
game.solvedBy = [];
game.markModified('solvedBy');
game.hints = [];
game.markModified('hints');

await game.save();

io.of(namespace).in(roomId).emit('next');

liveGames.setGame(roomId, setTimeout(() => {
timeHandler({ roomId }, io, namespace);
}, DURATION * 1000));
}
18 changes: 18 additions & 0 deletions src/utils/liveGames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class LiveGames {
private games: { [roomId: string]: { timeout: NodeJS.Timeout } };

constructor() {
this.games = {};
}

getGame(roomId: string) {
return this.games[roomId];
}

setGame(roomId: string, timeout: NodeJS.Timeout) {
this.games[roomId] = { timeout };
}
}

const liveGames = new LiveGames();
export default liveGames;