Bug Fixes

- setting words won't result in deleting all of them and resaving
- words can now only be set when no round is active
pull/15/head
Trivernis 6 years ago
parent 3e39a023b9
commit a5af6cbb19

@ -50,3 +50,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Bingo button not shown on refresh - Bingo button not shown on refresh
- bingo chat style (images too large) - bingo chat style (images too large)
- backend now returns precise error messages - backend now returns precise error messages
- setting words won't result in deleting all of them and resaving
- words can now only be set when no round is active

@ -236,6 +236,16 @@ class BingoDataManager {
return await this._queryFirstResult(this.queries.addWord.sql, [lobbyId, word]); return await this._queryFirstResult(this.queries.addWord.sql, [lobbyId, word]);
} }
/**
* Removes a word from the lobby
* @param lobbyId {Number} - the id of the lobby
* @param wordId {Number} - the id of the word
* @returns {Promise<*>}
*/
async removeWordFromLobby(lobbyId, wordId) {
return await this._queryFirstResult(this.queries.removeLobbyWord.sql, [lobbyId, wordId]);
}
/** /**
* Returns all words used in a lobby * Returns all words used in a lobby
* @param lobbyId {Number} - the id of the lobby * @param lobbyId {Number} - the id of the lobby
@ -303,6 +313,15 @@ class BingoDataManager {
return await this._queryFirstResult(this.queries.addGrid.sql, [playerId, lobbyId, roundId]); return await this._queryFirstResult(this.queries.addGrid.sql, [playerId, lobbyId, roundId]);
} }
/**
* Clears all grids for a lobby.
* @param lobbyId {Number} - the id of the lobby
* @returns {Promise<*>}
*/
async clearGrids(lobbyId) {
return await this._queryFirstResult(this.queries.clearLobbyGrids.sql, [lobbyId]);
}
/** /**
* Adds a word to a grid with specific location * Adds a word to a grid with specific location
* @param gridId {Number} - the id of the gird * @param gridId {Number} - the id of the gird
@ -1018,20 +1037,65 @@ class LobbyWrapper {
return (result && result.player_id); return (result && result.player_id);
} }
/**
* Adds a word to the lobby
* @param word
* @returns {Promise<void>}
*/
async addWord(word) {
await bdm.addWordToLobby(this.id, word);
}
/**
* Removes a word from the lobby
* @param wordId
* @returns {Promise<void>}
*/
async removeWord(wordId) {
await bdm.removeWordFromLobby(this.id, wordId);
}
/** /**
* Sets the words of the lobby * Sets the words of the lobby
* @param words * @param words
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
async setWords(words) { async setWords(words) {
if (words.length > 0) { if (words.length > 0 && !await this.roundActive()) {
await bdm.clearLobbyWords(this.id); let {newWords, removedWords} = await this._filterWords(words);
for (let word of words) for (let word of newWords)
// eslint-disable-next-line no-await-in-loop await this.addWord(word);
await bdm.addWordToLobby(this.id, word); for (let word of removedWords)
await this.removeWord(word.id);
} }
} }
/**
* Filters the bingo words
* @param words
* @returns {Promise<{removedWords: *[], newWords: *[]}>}
* @private
*/
async _filterWords(words) {
let curWords = await this.words();
let currentWords = [];
let currentWordContent = [];
for (let word of curWords) {
currentWordContent.push(await word.content());
currentWords.push({
id: word.id,
content: (await word.content())
});
}
let newWords = words.filter(x => (!currentWordContent.includes(x)));
let removedWords = currentWords.filter(x => !words.includes(x.content));
return {
newWords: newWords,
removedWords: removedWords
};
}
/** /**
* Adds a player to the lobby. * Adds a player to the lobby.
* @param playerId * @param playerId
@ -1074,6 +1138,9 @@ class LobbyWrapper {
let currentRound = await this.currentRound(); let currentRound = await this.currentRound();
await currentRound.updateStatus(status); await currentRound.updateStatus(status);
await bdm.addInfoMessage(this.id, `Admin set round status to ${status}`); await bdm.addInfoMessage(this.id, `Admin set round status to ${status}`);
if (status === 'FINISHED')
await bdm.clearGrids(this.id);
return currentRound; return currentRound;
} }
} }
@ -1463,6 +1530,7 @@ router.graphqlResolver = async (req, res) => {
let username = await new PlayerWrapper(playerId).username(); let username = await new PlayerWrapper(playerId).username();
if (result) { if (result) {
await bdm.addInfoMessage(lobbyId, `**${username}** won!`); await bdm.addInfoMessage(lobbyId, `**${username}** won!`);
await bdm.clearGrids(lobbyId);
return currentRound; return currentRound;
} else { } else {
res.status(500); res.status(500);

@ -62,7 +62,7 @@ CREATE TABLE IF NOT EXISTS bingo.grids (
-- grids_words table -- grids_words table
CREATE TABLE IF NOT EXISTS bingo.grid_words ( CREATE TABLE IF NOT EXISTS bingo.grid_words (
grid_id serial references bingo.grids(id) ON DELETE CASCADE, grid_id serial references bingo.grids(id) ON DELETE CASCADE,
word_id serial references bingo.words(id) ON DELETE CASCADE, word_id serial references bingo.words(id) ON DELETE RESTRICT,
grid_row integer NOT NULL, grid_row integer NOT NULL,
grid_column integer NOT NULL, grid_column integer NOT NULL,
submitted boolean DEFAULT false, submitted boolean DEFAULT false,

@ -160,6 +160,13 @@ addWord:
clearLobbyWords: clearLobbyWords:
sql: DELETE FROM bingo.words WHERE lobby_id = $1; sql: DELETE FROM bingo.words WHERE lobby_id = $1;
# deletes a word of a lobby
# params:
# - {Number} - the id of the lobby
# - {Number} - the id of the word
removeLobbyWord:
sql: DELETE FROM bingo.words WHERE lobby_id = $1 AND id = $2;
# returns all words for a bingo game (lobby) # returns all words for a bingo game (lobby)
# params: # params:
# - {Number} - the id of the bingo lobby # - {Number} - the id of the bingo lobby
@ -180,6 +187,12 @@ getWordInfo:
addGrid: addGrid:
sql: INSERT INTO bingo.grids (player_id, lobby_id, round_id) VALUES ($1, $2, $3) RETURNING *; sql: INSERT INTO bingo.grids (player_id, lobby_id, round_id) VALUES ($1, $2, $3) RETURNING *;
# deletes all grids of a lobby
# params:
# - {Number} - the id of the lobby
clearLobbyGrids:
sql: DELETE FROM bingo.grids WHERE lobby_id = $1;
# returns the grid entry for a player and lobby id # returns the grid entry for a player and lobby id
# params: # params:
# - {Number} - the id of the player # - {Number} - the id of the player

Loading…
Cancel
Save