diff --git a/README.md b/README.md index 996d5d5..67c845f 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,7 @@ ### General - [X] Routing URLs ? - [X] Add logout -- [ ] Cancel background image animation cancel -- [ ] Translate values +- [X] Translate values ### Navbar - [X] Fix navbar @@ -34,11 +33,11 @@ See register.js comments - [X] Search bar - [X] Modal create game - [X] Join game -- [ ] Validations on create game +- [X] Validations on create game - [X] Color on table points ### Game - [X] Game name - [X] *'Kiriki'* on roll - [X] Value table -- [ ] Winning a game, update game +- [X] Winning a game, update game diff --git a/game.html b/game.html index 5a67968..9185398 100644 --- a/game.html +++ b/game.html @@ -182,6 +182,28 @@
+ + diff --git a/js/game.js b/js/game.js index 1eefdc6..8f6b2ce 100644 --- a/js/game.js +++ b/js/game.js @@ -27,6 +27,7 @@ function gameData() { }, error: function (result) { console.log('Cant load data'); + console.log(result); loadError(); } }); @@ -162,6 +163,15 @@ function loadData(result) { hideBid(); getOwnRoll(); } + + // if winner show winner modal + if (result.winner !== null) { + $('#spectate').addClass('d-none'); + $('#decide').addClass('d-none'); + $('#roll').addClass('d-none'); + $('#bid').addClass('d-none'); + loadWinnerModal(result); + } } function loadError() { @@ -283,10 +293,7 @@ function hideAnnounce() { } function loadPointResultModal(json) { - - - - console.log("Loser: " + json.point_loser); + console.log('Loading point result modal'); if (json.point_loser === localStorage.getItem('username')) { $('#whosLoser').addClass('text-danger').html('Has perdido'); @@ -317,6 +324,11 @@ function loadPointResultModal(json) { return false; } +function loadWinnerModal(json) { + $('#winner').html(json.winner); + $('#winnerModal').modal('show'); +} + function changeGameName(json) { console.log('Changing game name'); console.log(json); @@ -391,11 +403,4 @@ function lessValue(bid, bidImage) { }); $(bidImage).addClass("dice-" + val); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - -$('#test').on('click', function (e) { - displayBidResult(); - -}); \ No newline at end of file +} \ No newline at end of file diff --git a/js/games.js b/js/games.js index 2964a59..3726fdb 100644 --- a/js/games.js +++ b/js/games.js @@ -6,6 +6,7 @@ jQuery(function () { } else { ajaxGetGames(); ajaxGetStartedGames(); + ajaxGetFinishedGames(); } }); @@ -76,6 +77,41 @@ function loadStartedGamesWherePlayerIs(json) { }; +function loadFinishedGamesWherePlayerIs(json) { + console.log("finished games"); + let games = json.results; + console.log(games); + + + $('#gamesFinished').html(''); + + games.forEach(game => { + $('#gamesFinished').append(` +
+
+
+
${game.name}
+ ${game.id} +
+ + + + + + + + + ${getPlayersFinished(game)} + +
NombrePuntos
+
+
`); + + }); + + +}; + function colourPoints(points) { if (points <= 1) { return `${points}`; @@ -184,9 +220,44 @@ function getPlayersUnestarted(game) { } +function getPlayersFinished(game) { + let players = game.players.results; + let result = []; + console.log(game); + + players.forEach(player => { + console.log(player); + result.push(` + ${player.name} + ${colourPoints(player.points)} + `); + + if (player.name === localStorage.getItem('username')) { + result[result.length - 1] = ` + ${player.name} + ${colourPoints(player.points)} + `; + } + }); + + return result.join(''); +} + function serverGameValidator(data) { - if (data.error === "There is already a game with that name") { - alert("Ya existe una partida con ese nombre"); + + switch (data.error) { + case "There is already a game with that name": + return alert("Ya existe una partida con ese nombre"); + case "Invalid game name": + return alert("Nombre de partida no válido, ha de ser de entre 3 y 20 caracteres alfanuméricos"); + case "There is already a game with that name": + return alert("Ya existe una partida con ese nombre"); + case "Invalid game password": + return alert("Contraseña no válida, puede ser opcional"); + case "Invalid max points": + return alert("Los puntos han de ser entre 3 y 9"); + default: + return alert("Error desconocido"); } } @@ -348,6 +419,23 @@ function ajaxGetStartedGames() { return false; } +function ajaxGetFinishedGames() { + + $.ajax({ + type: "GET", + datatype: "json", + url: "https://api-kiriki.herokuapp.com/api/finished_games/?player_name=" + localStorage.getItem('username'), + headers: { "Authorization": 'Bearer ' + JSON.parse(localStorage.getItem('token')).value }, + success: function (result) { + loadFinishedGamesWherePlayerIs(result); + }, + error: function (result) { + console.log(result); + } + }); + return false; + +} @@ -393,6 +481,7 @@ $("#aceptPassword").on("submit", function (event) { }); $("#acceptCreateGame").on("click", function (event) { + console.log("Creating game..."); event.preventDefault(); let gameName = $('#createGameName').val(); diff --git a/js/register.js b/js/register.js index 96b63a9..7421620 100644 --- a/js/register.js +++ b/js/register.js @@ -104,9 +104,7 @@ function formHasError() { setValids(errors, elementsId); - if (errors.size > 0) { - return true; - } + return errors.size === 0; } function serverValidator(data) {