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<*>} * @returns {Promise<*>}
*/ */
async updateLobbyExpiration(lobbyId) { 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) { async _loadLobbyInfo(force) {
if (!this._infoLoaded && !force) { if (!this._infoLoaded && !force) {
let row = await bdm.getLobbyInfo(this.id); let row = await bdm.updateLobbyExpiration(this.id);
this._assignProperties(row); 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 if the lobby exists (based on one loaded attribute)
* @returns {Promise<boolean>} * @returns {Promise<boolean>}
@ -1073,7 +1081,7 @@ class LobbyWrapper {
await this._createRound(); await this._createRound();
await this._createGrids(); await this._createGrids();
await this.setRoundStatus('ACTIVE'); await this.setRoundStatus('ACTIVE');
this.socket.emit('statusChange', 'ACTIVE'); this.emit('statusChange', 'ACTIVE');
} }
} }
@ -1128,7 +1136,7 @@ class LobbyWrapper {
await this.addWord(word); await this.addWord(word);
for (let word of removedWords) for (let word of removedWords)
await this.removeWord(word.id); await this.removeWord(word.id);
this.socket.emit('wordsChange'); this.emit('wordsChange');
} }
} }
@ -1165,7 +1173,7 @@ class LobbyWrapper {
*/ */
async addInfoMessage(message) { async addInfoMessage(message) {
let result = await bdm.addInfoMessage(this.id, 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) { async addPlayer(playerId) {
await bdm.addPlayerToLobby(playerId, this.id); await bdm.addPlayerToLobby(playerId, this.id);
let playerWrapper = new PlayerWrapper(playerId); let playerWrapper = new PlayerWrapper(playerId);
this.socket.emit('playerJoin', await resolvePlayer(playerWrapper)); this.emit('playerJoin', await resolvePlayer(playerWrapper));
let username = await playerWrapper.username(); let username = await playerWrapper.username();
await this.addInfoMessage(`${username} joined.`); await this.addInfoMessage(`${username} joined.`);
await this._loadLobbyInfo(true); await this._loadLobbyInfo(true);
@ -1190,7 +1198,7 @@ class LobbyWrapper {
async removePlayer(playerId) { async removePlayer(playerId) {
await bdm.removePlayerFromLobby(playerId, this.id); await bdm.removePlayerFromLobby(playerId, this.id);
let username = await new PlayerWrapper(playerId).username(); let username = await new PlayerWrapper(playerId).username();
this.socket.emit('playerLeave', playerId); this.emit('playerLeave', playerId);
await this.addInfoMessage(`${username} left.`); await this.addInfoMessage(`${username} left.`);
await this._loadLobbyInfo(true); await this._loadLobbyInfo(true);
} }
@ -1213,7 +1221,7 @@ class LobbyWrapper {
let currentRound = await this.currentRound(); let currentRound = await this.currentRound();
await currentRound.updateStatus(status); await currentRound.updateStatus(status);
await this.addInfoMessage(`Admin set round status to ${status}`); await this.addInfoMessage(`Admin set round status to ${status}`);
this.socket.emit('statusChange', status); this.emit('statusChange', status);
if (status === 'FINISHED') if (status === 'FINISHED')
await bdm.clearGrids(this.id); await bdm.clearGrids(this.id);
@ -1617,11 +1625,13 @@ router.init = async (bingoIo, io) => {
if (req.query.g) { if (req.query.g) {
let lobbyWrapper = new LobbyWrapper(req.query.g); let lobbyWrapper = new LobbyWrapper(req.query.g);
lobbyWrapper.socket.emit('usernameChange', if (await lobbyWrapper.exists()) {
lobbyWrapper.emit('usernameChange',
await resolvePlayer(new PlayerWrapper(playerId), req.query.g)); await resolvePlayer(new PlayerWrapper(playerId), req.query.g));
await lobbyWrapper.addInfoMessage(`${oldName} changed username to ${username}`); await lobbyWrapper.addInfoMessage(`${oldName} changed username to ${username}`);
} }
} }
}
return new PlayerWrapper(playerId); return new PlayerWrapper(playerId);
} else { } else {
res.status(400); res.status(400);
@ -1633,7 +1643,7 @@ router.init = async (bingoIo, io) => {
if (gridSize > 0 && gridSize < 10) { if (gridSize > 0 && gridSize < 10) {
let result = await bdm.createLobby(playerId, gridSize); let result = await bdm.createLobby(playerId, gridSize);
createSocketIfNotExist(io, result.id); createSocketIfNotExist(io, result.id);
return new LobbyWrapper(result.id); return new LobbyWrapper(result.id, result);
} else { } else {
res.status(413); 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, admin_id serial references bingo.players(id) ON DELETE SET NULL,
grid_size integer DEFAULT 3 NOT NULL, grid_size integer DEFAULT 3 NOT NULL,
current_round integer, current_round integer,
expire timestamp DEFAULT (NOW() + interval '1 hour' ) expire timestamp DEFAULT (NOW() + interval '4 hour' )
); );
-- lobbys-players table -- lobbys-players table

@ -48,7 +48,7 @@ addLobby:
# params: # params:
# - {Number} - the id of the lobby # - {Number} - the id of the lobby
updateLobbyExpire: 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 # inserts a player into a lobby
# params: # params:

Loading…
Cancel
Save