From 3abdb6910e48993d99f5ddaca5e251ede59b856a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 4 Apr 2021 20:52:28 +0200 Subject: [PATCH 1/3] MAPG-213 clean up init --- public/static/js/game.js | 74 +++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 39 deletions(-) diff --git a/public/static/js/game.js b/public/static/js/game.js index 43d44f9..48ede67 100644 --- a/public/static/js/game.js +++ b/public/static/js/game.js @@ -97,26 +97,36 @@ }, initialize: function (data) { - if (data.history) { - for (var i = 0; i < data.history.length; ++i) { - var round = data.history[i]; - Game.rounds.push({ position: round.position, guessPosition: round.result.guessPosition, realMarker: null, guessMarkers: [] }); - Game.addPositionToResultMap(true); - if (round.result.guessPosition) { - Game.addGuessPositionToResultMap(round.result.guessPosition, null, true); - } - Game.scoreSum += round.result.score; + if (data.history.length === 0 && !data.place) { + var div = document.getElementById('players'); - for (var j = 0; j < round.allResults.length; ++j) { - var result = round.allResults[j]; - Game.addGuessPositionToResultMap(result.guessPosition, result, true); - } + for (var i = 0; i < data.members.length; ++i) { + var member = data.members[i]; + + var p = document.createElement('p'); + p.innerHTML = member.userName + (member.me ? ' (me)' : ''); + div.appendChild(p); } - - document.getElementById('currentRound').innerHTML = String(Game.rounds.length) + '/' + String(Game.NUMBER_OF_ROUNDS); - document.getElementById('currentScoreSum').innerHTML = String(Game.scoreSum) + '/' + String(Game.rounds.length * Game.MAX_SCORE); } + for (var i = 0; i < data.history.length; ++i) { + var round = data.history[i]; + Game.rounds.push({ position: round.position, guessPosition: round.result.guessPosition, realMarker: null, guessMarkers: [] }); + Game.addPositionToResultMap(true); + if (round.result.guessPosition) { + Game.addGuessPositionToResultMap(round.result.guessPosition, null, true); + } + Game.scoreSum += round.result.score; + + for (var j = 0; j < round.allResults.length; ++j) { + var result = round.allResults[j]; + Game.addGuessPositionToResultMap(result.guessPosition, result, true); + } + } + + document.getElementById('currentRound').innerHTML = String(Game.rounds.length) + '/' + String(Game.NUMBER_OF_ROUNDS); + document.getElementById('currentScoreSum').innerHTML = String(Game.scoreSum) + '/' + String(Game.rounds.length * Game.MAX_SCORE); + if (data.timeout) { Game.startCountdown(data.timeout); } @@ -133,16 +143,6 @@ } document.getElementById('loading').style.visibility = 'hidden'; - - var div = document.getElementById('players'); - - for (var i = 0; i < data.members.length; ++i) { - var member = data.members[i]; - - var p = document.createElement('p'); - p.innerHTML = member.userName + (member.me ? ' (me)' : ''); - div.appendChild(p); - } }, memberJoined: function (data) { @@ -253,8 +253,6 @@ initialize: function () { document.getElementById('panoCover').style.visibility = 'visible'; - document.getElementById('currentRound').innerHTML = '1/' + String(Game.NUMBER_OF_ROUNDS); - document.getElementById('currentScoreSum').innerHTML = '0/0'; Game.map.setOptions({ draggableCursor: 'crosshair' @@ -279,19 +277,17 @@ Game.panoId = this.response.place.panoId; Game.pov = this.response.place.pov; - if (this.response.history) { - for (var i = 0; i < this.response.history.length; ++i) { - var round = this.response.history[i]; - Game.rounds.push({ position: round.position, guessPosition: round.result.guessPosition, realMarker: null, guessMarkers: [] }); - Game.addPositionToResultMap(true); - Game.addGuessPositionToResultMap(round.result.guessPosition, null, true); - Game.scoreSum += round.result.score; - } - - document.getElementById('currentRound').innerHTML = String(Game.rounds.length) + '/' + String(Game.NUMBER_OF_ROUNDS); - document.getElementById('currentScoreSum').innerHTML = String(Game.scoreSum) + '/' + String(Game.rounds.length * Game.MAX_SCORE); + for (var i = 0; i < this.response.history.length; ++i) { + var round = this.response.history[i]; + Game.rounds.push({ position: round.position, guessPosition: round.result.guessPosition, realMarker: null, guessMarkers: [] }); + Game.addPositionToResultMap(true); + Game.addGuessPositionToResultMap(round.result.guessPosition, null, true); + Game.scoreSum += round.result.score; } + document.getElementById('currentRound').innerHTML = String(Game.rounds.length) + '/' + String(Game.NUMBER_OF_ROUNDS); + document.getElementById('currentScoreSum').innerHTML = String(Game.scoreSum) + '/' + String(Game.rounds.length * Game.MAX_SCORE); + Game.startNewRound(); }); }, -- 2.45.2 From 6342bb1e796efaeb9248c60d3063728bad66142a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 4 Apr 2021 21:58:17 +0200 Subject: [PATCH 2/3] MAPG-213 send round data at initialization if user guessed --- multi/index.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/multi/index.js b/multi/index.js index 91e9f39..cbfc838 100644 --- a/multi/index.js +++ b/multi/index.js @@ -165,9 +165,18 @@ class MultiGame { } _endRound(room, round) { - var data = { position: round.place.position, allResults: this._collectResultsInRound(room, round) }; + var allResults = this._collectResultsInRound(room, round); var self = this; - room.members.forEach(function (member) { + room.members.forEach(function (member, token) { + var result = { guessPosition: null, distance: null, score: 0 }; + + if (round.results.has(token)) { + result = round.results.get(token); + } else { + round.results.set(token, result); + } + + var data = { position: round.place.position, result: result, allResults: allResults }; self._sendToMember(member, 'end_round', data); }); } @@ -183,9 +192,13 @@ class MultiGame { } data.history = []; - for (var i = 0; i < room.currentRound; ++i) { + for (var i = 0; i <= room.currentRound; ++i) { var round = room.rounds[i]; + if (i === room.currentRound && !round.results.has(token)) { + continue; + } + var result = { guessPosition: null, distance: null, score: 0 }; var allResults = []; @@ -210,6 +223,8 @@ class MultiGame { data.members.push({ userName: currentMember.userName, me: member === currentMember }); }); + data.readyToContinue = room.currentRound >= 0 && room.members.size === room.rounds[room.currentRound].results.size + this._sendToMember(member, 'initialize', data); } -- 2.45.2 From 19f7b50002f9f6ca3f3567c7f17ea8f805492d16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 4 Apr 2021 22:24:40 +0200 Subject: [PATCH 3/3] MAPG-213 show results immediately if user already guessed --- public/static/js/game.js | 63 ++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/public/static/js/game.js b/public/static/js/game.js index 48ede67..4519d62 100644 --- a/public/static/js/game.js +++ b/public/static/js/game.js @@ -132,14 +132,19 @@ } if (data.place) { - Game.readyToContinue = false; + Game.readyToContinue = data.readyToContinue; Game.panoId = data.place.panoId; Game.pov = data.place.pov; document.getElementById('multi').style.visibility = 'hidden'; - document.getElementById('panoCover').style.visibility = 'hidden'; - Game.startNewRound(); + if (Game.rounds.length === 0 || !Game.rounds[Game.rounds.length - 1].position) { + document.getElementById('panoCover').style.visibility = 'hidden'; + Game.startNewRound(); + } else { + Game.loadPano(Game.panoId, Game.pov); + Game.showResultFromHistory(data.history[data.history.length - 1].result); + } } document.getElementById('loading').style.visibility = 'hidden'; @@ -203,7 +208,7 @@ document.getElementById('guessButton').disabled = true; document.getElementById('panoCover').style.visibility = 'visible'; - Game.showResults(data.position, null, { distance: NaN, score: 0 }, data.allResults); + Game.receiveResult(data.position, data.result.guessPosition, { distance: data.result.distance, score: data.result.score }, data.allResults); } }, @@ -411,18 +416,7 @@ Game.panorama.setPano(panoId); }, - showResults: function (position, guessPosition, result, allResults) { - if (Game.adaptGuess) { - document.getElementById('guess').classList.remove('adapt'); - } - - if (Game.guessMarker) { - Game.guessMarker.setMap(null); - Game.guessMarker = null; - } - - document.getElementById('guess').classList.add('result'); - + receiveResult: function (position, guessPosition, result, allResults) { Game.scoreSum += result.score; document.getElementById('currentScoreSum').innerHTML = String(Game.scoreSum) + '/' + String(Game.rounds.length * Game.MAX_SCORE); @@ -445,13 +439,46 @@ } } + Game.showResultMap(result, resultBounds); + }, + + showResultFromHistory: function (result) { + var round = Game.rounds[Game.rounds.length - 1]; + var resultBounds = new google.maps.LatLngBounds(); + + round.realMarker.setVisible(true); + resultBounds.extend(round.position); + + for (var j = 0; j < round.guessMarkers.length; ++j) { + var guessMarker = round.guessMarkers[j]; + guessMarker.marker.setVisible(true); + guessMarker.line.setVisible(true); + + resultBounds.extend(guessMarker.marker.getPosition()); + } + + Game.showResultMap(result, resultBounds); + }, + + showResultMap: function (result, resultBounds) { + if (Game.adaptGuess) { + document.getElementById('guess').classList.remove('adapt'); + } + + if (Game.guessMarker) { + Game.guessMarker.setMap(null); + Game.guessMarker = null; + } + + document.getElementById('guess').classList.add('result'); + Game.map.setOptions({ draggableCursor: 'grab' }); Game.map.fitBounds(resultBounds); var distanceInfo = document.getElementById('distanceInfo'); - if (Number.isNaN(result.distance)) { + if (result.distance === null) { distanceInfo.children[0].style.display = 'none'; distanceInfo.children[1].style.display = 'block'; } else { @@ -506,7 +533,7 @@ return; } - Game.showResults(this.response.position, guessPosition, this.response.result, this.response.allResults); + Game.receiveResult(this.response.position, guessPosition, this.response.result, this.response.allResults); if (this.response.place) { Game.panoId = this.response.place.panoId; -- 2.45.2