Fixed username submission

- setting a username now gives better feedback
- old username appeares in input field on load
- fixed bug where users with expired bingo session but still existent session couldn't set a username
- fixed status indicator
pull/15/head
Trivernis 6 years ago
parent e35cfabc49
commit 9d806a7935

888
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -30,7 +30,8 @@
"devDependencies": { "devDependencies": {
"eslint": "^5.16.0", "eslint": "^5.16.0",
"eslint-plugin-graphql": "^3.0.3", "eslint-plugin-graphql": "^3.0.3",
"eslint-plugin-promise": "^4.1.1" "eslint-plugin-promise": "^4.1.1",
"standard": "^12.0.1"
}, },
"eslintConfig": { "eslintConfig": {
"parserOptions": { "parserOptions": {

@ -33,7 +33,7 @@ async function submitUsername() {
let username = unameInput.value.replace(/^\s+|\s+$/g, ''); let username = unameInput.value.replace(/^\s+|\s+$/g, '');
if (username.length > 1) { if (username.length > 1) {
await setUsername(username); return await setUsername(username);
} else { } else {
showError('You need to provide a username (minimum 2 characters)!'); showError('You need to provide a username (minimum 2 characters)!');
return false; return false;
@ -58,7 +58,7 @@ async function setUsername(username) {
if (response.status === 200) { if (response.status === 200) {
return true; return true;
} else { } else {
showError(`Failed to submit username. HTTP Error: ${response.status}`); showError(`Failed to submit username. Error: ${response.errors.join(', ')}`);
console.error(response); console.error(response);
return false; return false;
} }

@ -237,6 +237,9 @@
button button
width: 100% width: 100%
#statusbar
@include gridPosition(5, 6, 1, 4)
#container-bingo-lobby #container-bingo-lobby
@include fillWindow @include fillWindow
overflow: hidden overflow: hidden

@ -670,7 +670,7 @@ class PlayerWrapper {
/** /**
* Loads all player information * Loads all player information
* @returns {Promise<void>} * @returns {Promise<Boolean>}
* @private * @private
*/ */
async _loadPlayerInfo() { async _loadPlayerInfo() {
@ -680,10 +680,21 @@ class PlayerWrapper {
this._uname = result.username; this._uname = result.username;
this.expire = result.expire; this.expire = result.expire;
this._infoLoaded = true; this._infoLoaded = true;
return true;
} else {
return false;
} }
} }
} }
/**
* Returns if the player exists
* @returns {Promise<Boolean>}
*/
async exists() {
return await this._loadPlayerInfo();
}
/** /**
* Returns the grid for a specific lobby * Returns the grid for a specific lobby
* @param lobbyId {Number} - the id of the lobby * @param lobbyId {Number} - the id of the lobby
@ -1317,7 +1328,7 @@ async function getGridData(lobbyId, playerId) {
for (let i = 0; i < await lobbyWrapper.gridSize(); i++) { for (let i = 0; i < await lobbyWrapper.gridSize(); i++) {
fieldGrid[i] = []; fieldGrid[i] = [];
for (let j = 0; j < await lobbyWrapper.gridSize(); j++) { for (let j = 0; j < await lobbyWrapper.gridSize(); j++) {
let field = fields.find(x => (x.row === i && x.column === j)) let field = fields.find(x => (x.row === i && x.column === j));
fieldGrid[i][j] = { fieldGrid[i][j] = {
row: field.row, row: field.row,
column: field.column, column: field.column,
@ -1349,7 +1360,8 @@ router.get('/', async (req, res) => {
let playerId = req.session.bingoPlayerId; let playerId = req.session.bingoPlayerId;
let info = req.session.acceptedCookies? null: globals.cookieInfo; let info = req.session.acceptedCookies? null: globals.cookieInfo;
let lobbyWrapper = new LobbyWrapper(req.query.g); let lobbyWrapper = new LobbyWrapper(req.query.g);
if (playerId && req.query.g && await lobbyWrapper.exists()) { let playerWrapper = new PlayerWrapper(playerId);
if (playerId && await playerWrapper.exists() && req.query.g && await lobbyWrapper.exists()) {
let lobbyId = req.query.g; let lobbyId = req.query.g;
if (!(await lobbyWrapper.roundActive())) { if (!(await lobbyWrapper.roundActive())) {
@ -1395,7 +1407,10 @@ router.get('/', async (req, res) => {
} }
} }
} else { } else {
res.render('bingo/bingo-create', {info: info}); res.render('bingo/bingo-create', {
info: info,
username: await playerWrapper.username()
});
} }
}); });
@ -1422,8 +1437,9 @@ router.graphqlResolver = async (req, res) => {
// mutations // mutations
setUsername: async ({username}) => { setUsername: async ({username}) => {
username = replaceTagSigns(username.substring(0, 30)); // only allow 30 characters username = replaceTagSigns(username.substring(0, 30)); // only allow 30 characters
let playerWrapper = new PlayerWrapper(playerId);
if (!playerId) { if (!playerId || !(await playerWrapper.exists())) {
req.session.bingoPlayerId = (await bdm.addPlayer(username)).id; req.session.bingoPlayerId = (await bdm.addPlayer(username)).id;
playerId = req.session.bingoPlayerId; playerId = req.session.bingoPlayerId;
} else { } else {

@ -6,6 +6,7 @@ block content
input(id='input-username' input(id='input-username'
type='text' type='text'
placeholder='Enter your name' placeholder='Enter your name'
value=username
onkeydown='submitOnEnter(event, () => indicateStatus(submitUsername, "#username-status"))') onkeydown='submitOnEnter(event, () => indicateStatus(submitUsername, "#username-status"))')
button( button(
id='submit-username' id='submit-username'
@ -14,3 +15,4 @@ block content
div(id='lobby-form') div(id='lobby-form')
button(id='join-lobby' onclick='joinLobby()') Join Lobby button(id='join-lobby' onclick='joinLobby()') Join Lobby
button(id='create-lobby' onclick='createLobby()') Create Lobby button(id='create-lobby' onclick='createLobby()') Create Lobby
include includes/bingo-statusbar

Loading…
Cancel
Save