Fixed lobby expiration

- fixed a bug that the lobby expires because of the use of sockets
pull/23/head
Trivernis 6 years ago
parent 6dc331c392
commit 30b71b1fd4

@ -139,7 +139,7 @@ class BingoDataManager {
* @returns {Promise<*>}
*/
async updateLobbyExpiration(lobbyId) {
return await this._queryDatabase(this.queries.updateLobbyExpire.sql, [lobbyId]);
return await this._queryFirstResult(this.queries.updateLobbyExpire.sql, [lobbyId]);
}
/**
@ -919,7 +919,7 @@ class LobbyWrapper {
*/
async _loadLobbyInfo(force) {
if (!this._infoLoaded && !force) {
let row = await bdm.getLobbyInfo(this.id);
let row = await bdm.updateLobbyExpiration(this.id);
this._assignProperties(row);
}
}
@ -940,6 +940,14 @@ class LobbyWrapper {
}
}
/**
* Emits an event is a socket exists for the lobby
*/
emit() {
if (this.socket)
this.socket.emit(...arguments);
}
/**
* Returns if the lobby exists (based on one loaded attribute)
* @returns {Promise<boolean>}
@ -1073,7 +1081,7 @@ class LobbyWrapper {
await this._createRound();
await this._createGrids();
await this.setRoundStatus('ACTIVE');
this.socket.emit('statusChange', 'ACTIVE');
this.emit('statusChange', 'ACTIVE');
}
}
@ -1128,7 +1136,7 @@ class LobbyWrapper {
await this.addWord(word);
for (let word of removedWords)
await this.removeWord(word.id);
this.socket.emit('wordsChange');
this.emit('wordsChange');
}
}
@ -1165,7 +1173,7 @@ class LobbyWrapper {
*/
async addInfoMessage(message) {
let result = await bdm.addInfoMessage(this.id, message);
this.socket.emit('message', await resolveMessage(new MessageWrapper(result)));
this.emit('message', await resolveMessage(new MessageWrapper(result)));
}
/**
@ -1176,7 +1184,7 @@ class LobbyWrapper {
async addPlayer(playerId) {
await bdm.addPlayerToLobby(playerId, this.id);
let playerWrapper = new PlayerWrapper(playerId);
this.socket.emit('playerJoin', await resolvePlayer(playerWrapper));
this.emit('playerJoin', await resolvePlayer(playerWrapper));
let username = await playerWrapper.username();
await this.addInfoMessage(`${username} joined.`);
await this._loadLobbyInfo(true);
@ -1190,7 +1198,7 @@ class LobbyWrapper {
async removePlayer(playerId) {
await bdm.removePlayerFromLobby(playerId, this.id);
let username = await new PlayerWrapper(playerId).username();
this.socket.emit('playerLeave', playerId);
this.emit('playerLeave', playerId);
await this.addInfoMessage(`${username} left.`);
await this._loadLobbyInfo(true);
}
@ -1213,7 +1221,7 @@ class LobbyWrapper {
let currentRound = await this.currentRound();
await currentRound.updateStatus(status);
await this.addInfoMessage(`Admin set round status to ${status}`);
this.socket.emit('statusChange', status);
this.emit('statusChange', status);
if (status === 'FINISHED')
await bdm.clearGrids(this.id);
@ -1617,9 +1625,11 @@ router.init = async (bingoIo, io) => {
if (req.query.g) {
let lobbyWrapper = new LobbyWrapper(req.query.g);
lobbyWrapper.socket.emit('usernameChange',
await resolvePlayer(new PlayerWrapper(playerId), req.query.g));
await lobbyWrapper.addInfoMessage(`${oldName} changed username to ${username}`);
if (await lobbyWrapper.exists()) {
lobbyWrapper.emit('usernameChange',
await resolvePlayer(new PlayerWrapper(playerId), req.query.g));
await lobbyWrapper.addInfoMessage(`${oldName} changed username to ${username}`);
}
}
}
return new PlayerWrapper(playerId);
@ -1633,7 +1643,7 @@ router.init = async (bingoIo, io) => {
if (gridSize > 0 && gridSize < 10) {
let result = await bdm.createLobby(playerId, gridSize);
createSocketIfNotExist(io, result.id);
return new LobbyWrapper(result.id);
return new LobbyWrapper(result.id, result);
} else {
res.status(413);
}

@ -11,7 +11,7 @@ CREATE TABLE IF NOT EXISTS bingo.lobbys (
admin_id serial references bingo.players(id) ON DELETE SET NULL,
grid_size integer DEFAULT 3 NOT NULL,
current_round integer,
expire timestamp DEFAULT (NOW() + interval '1 hour' )
expire timestamp DEFAULT (NOW() + interval '4 hour' )
);
-- lobbys-players table

@ -48,7 +48,7 @@ addLobby:
# params:
# - {Number} - the id of the lobby
updateLobbyExpire:
sql: UPDATE bingo.lobbys SET expire = (NOW() + interval '1 hours') WHERE id = $1;
sql: UPDATE bingo.lobbys SET expire = (NOW() + interval '4 hours') WHERE id = $1 RETURNING *;
# inserts a player into a lobby
# params:

Loading…
Cancel
Save