Compare commits

..

No commits in common. "d3a12fb6f1a25a4634bd89e969fe9a80acabf135" and "7433813337f44d6670c89dcfb47039f0b13e61bd" have entirely different histories.

5 changed files with 106 additions and 164 deletions

View File

@ -2,6 +2,12 @@
process.title = 'mapguesser-multi'; process.title = 'mapguesser-multi';
class State {
static OPEN = 1;
static PLACE_RECEIVED = 2;
static GUESS_SENT = 3;
}
class MultiGame { class MultiGame {
constructor() { constructor() {
this.rooms = new Map(); this.rooms = new Map();
@ -26,7 +32,7 @@ class MultiGame {
var member = room.members.get(token); var member = room.members.get(token);
member.connection = connection; member.connection = connection;
this._sendInitialData(room, member, token); this._sendInitialData(room, member);
} }
createRoom(roomId) { createRoom(roomId) {
@ -52,7 +58,7 @@ class MultiGame {
self._sendToMember(member, 'member_joined', data); self._sendToMember(member, 'member_joined', data);
}); });
room.members.set(token, { userName: userName, connection: null }); room.members.set(token, { userName: userName, state: State.OPEN, connection: null });
} }
startGame(roomId, places) { startGame(roomId, places) {
@ -86,14 +92,14 @@ class MultiGame {
room.updated = new Date(); room.updated = new Date();
var round = room.rounds[room.currentRound]; var round = room.rounds[room.currentRound];
var member = room.members.get(token); var member = this.rooms.get(roomId).members.get(token);
var allResults = this._collectResultsInRound(room, round);
this._sendResultsUntilNow(room, member);
round.results.set(member.userName, { guessPosition: guessPosition, distance: distance, score: score });
member.state = State.GUESS_SENT;
this._broadcastGuess(room, member.userName, guessPosition, distance, score); this._broadcastGuess(room, member.userName, guessPosition, distance, score);
round.results.set(token, { guessPosition: guessPosition, distance: distance, score: score });
return allResults;
} }
nextRound(roomId, currentRound) { nextRound(roomId, currentRound) {
@ -120,10 +126,12 @@ class MultiGame {
var self = this; var self = this;
room.members.forEach(function (member) { room.members.forEach(function (member) {
self._sendToMember(member, 'new_round', data); self._sendToMember(member, 'new_round', data);
member.state = State.PLACE_RECEIVED;
}); });
} }
_sendInitialData(room, member, token) { _sendInitialData(room, member) {
var data = {}; var data = {};
if (room.currentRound >= 0) { if (room.currentRound >= 0) {
@ -133,23 +141,18 @@ class MultiGame {
data.history = []; data.history = [];
for (var i = 0; i < room.currentRound; ++i) { for (var i = 0; i < room.currentRound; ++i) {
var round = room.rounds[i]; var round = room.rounds[i];
var result;
var result = { guessPosition: null, distance: null, score: 0 }; if (round.results.has(member.userName)) {
var allResults = []; result = round.results.get(member.userName);
} else {
round.results.forEach(function (currentResult, currentToken) { result = { guessPosition: null, distance: null, score: 0 };
if (token === currentToken) {
result = currentResult;
return;
} }
allResults.push({ userName: room.members.get(currentToken).userName, guessPosition: currentResult.guessPosition, distance: currentResult.distance, score: currentResult.score });
});
data.history.push({ data.history.push({
position: round.place.position, position: round.place.position,
result: result, guessPosition: result.guessPosition,
allResults: allResults distance: result.distance,
score: result.score
}); });
} }
@ -161,30 +164,30 @@ class MultiGame {
this._sendToMember(member, 'initialize', data); this._sendToMember(member, 'initialize', data);
} }
_collectResultsInRound(room, round) { _sendResultsUntilNow(room, member) {
if (member.state !== State.GUESS_SENT) {
return;
}
var round = room.rounds[room.currentRound];
var results = []; var results = [];
round.results.forEach(function (result, token) { round.results.forEach(function (result, userName) {
results.push({ results.push({ userName: userName, guessPosition: result.guessPosition, distance: result.distance, score: result.score });
userName: room.members.get(token).userName,
guessPosition: result.guessPosition,
distance: result.distance,
score: result.score
});
}); });
return results; this._sendToMember(member, 'results', results);
} }
_broadcastGuess(room, userName, guessPosition, distance, score) { _broadcastGuess(room, userName, guessPosition, distance, score) {
var data = { userName: userName, guessPosition: guessPosition, distance: distance, score: score }; var data = { userName: userName, guessPosition: guessPosition, distance: distance, score: score };
var round = room.rounds[room.currentRound];
var self = this; room.members.forEach(function (member) {
room.members.forEach(function (member, token) { if (!member.state !== State.GUESS_SENT) {
if (!round.results.has(token)) {
return; return;
} }
self._sendToMember(member, 'guess', data); this._sendToMember(member, 'guess', data);
}); });
} }
@ -221,35 +224,34 @@ var tcpServer = net.createServer(function (socket) {
return; return;
} }
var response = { data: null };
switch (data.func) { switch (data.func) {
case 'create_room': case 'create_room':
response.data = multiGame.createRoom(data.args.roomId); multiGame.createRoom(data.args.roomId);
break; break;
case 'join_room': case 'join_room':
response.data = multiGame.joinRoom(data.args.roomId, data.args.token, data.args.userName); multiGame.joinRoom(data.args.roomId, data.args.token, data.args.userName);
break; break;
case 'start_game': case 'start_game':
response.data = multiGame.startGame(data.args.roomId, data.args.places); multiGame.startGame(data.args.roomId, data.args.places);
break break
case 'guess': case 'guess':
response.data = multiGame.guess(data.args.roomId, data.args.token, data.args.guessPosition, data.args.distance, data.args.score); multiGame.guess(data.args.roomId, data.args.token, data.args.guessPosition, data.args.distance, data.args.score);
break; break;
case 'next_round': case 'next_round':
response.data = multiGame.nextRound(data.args.roomId, data.args.currentRound); multiGame.nextRound(data.args.roomId, data.args.currentRound);
break; break;
} }
socket.write(JSON.stringify(response)); socket.write('OK');
socket.end(); socket.end();
}); });
}); });

View File

@ -1,10 +0,0 @@
<!-- Original image: Copyright (c) 2019 The Bootstrap Authors. License can be found in 'USED_SOFTWARE' in section 'Bootstrap Icons'. -->
<svg viewBox="0 0 12 16" xmlns="http://www.w3.org/2000/svg">
<path
fill="#3183ce"
fill-rule="evenodd"
stroke="#19456d"
stroke-width="0.3"
stroke-linecap="round"
d="m 5.9999998,15.849652 c 0,0 5.8511182,-5.579947 5.8511182,-9.8134832 a 5.8511179,5.8880898 0 0 0 -11.7022358,0 c 0,4.2335362 5.8511176,9.8134832 5.8511176,9.8134832" />
</svg>

Before

Width:  |  Height:  |  Size: 529 B

View File

@ -66,8 +66,12 @@
Game.MultiConnector.newRound(json.data); Game.MultiConnector.newRound(json.data);
break; break;
case 'results':
//TODO
break;
case 'guess': case 'guess':
Game.MultiConnector.guess(json.data); //TODO
break; break;
} }
}; };
@ -90,17 +94,9 @@
if (data.history) { if (data.history) {
for (var i = 0; i < data.history.length; ++i) { for (var i = 0; i < data.history.length; ++i) {
var round = data.history[i]; var round = data.history[i];
Game.rounds.push({ position: round.position, guessPosition: round.result.guessPosition, realMarker: null, guessMarkers: [] }); Game.rounds.push({ position: round.position, guessPosition: round.guessPosition, realMarker: null, guessMarker: null, line: null });
Game.addPositionToResultMap(true); Game.addRealGuessPair(round.position, round.guessPosition, true);
if (round.result.guessPosition) { Game.scoreSum += round.score;
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('currentRound').innerHTML = String(Game.rounds.length) + '/' + String(Game.NUMBER_OF_ROUNDS);
@ -151,7 +147,7 @@
// if player didn't guess - TODO: show everything on a map // if player didn't guess - TODO: show everything on a map
if (data.result && Game.rounds.length > 0 && !Game.rounds[Game.rounds.length - 1].position) { if (data.result && Game.rounds.length > 0 && !Game.rounds[Game.rounds.length - 1].position) {
Game.rounds[Game.rounds.length - 1].position = data.result.position; Game.rounds[Game.rounds.length - 1].position = data.result.position;
Game.addPositionToResultMap(); Game.addRealGuessPair(data.result.position, null);
} }
Game.panoId = data.place.panoId; Game.panoId = data.place.panoId;
@ -160,15 +156,6 @@
document.getElementById('multi').style.visibility = 'hidden'; document.getElementById('multi').style.visibility = 'hidden';
Game.resetRound(); Game.resetRound();
Game.startNewRound(); Game.startNewRound();
},
guess: function (data) {
var resultBounds = Game.map.getBounds();
Game.addGuessPositionToResultMap(data.guessPosition, data);
resultBounds.extend(data.guessPosition);
Game.map.fitBounds(resultBounds);
} }
}, },
@ -247,10 +234,9 @@
if (this.response.history) { if (this.response.history) {
for (var i = 0; i < this.response.history.length; ++i) { for (var i = 0; i < this.response.history.length; ++i) {
var round = this.response.history[i]; var round = this.response.history[i];
Game.rounds.push({ position: round.position, guessPosition: round.result.guessPosition, realMarker: null, guessMarkers: [] }); Game.rounds.push({ position: round.position, guessPosition: round.guessPosition, realMarker: null, guessMarker: null, line: null });
Game.addPositionToResultMap(true); Game.addRealGuessPair(round.position, round.guessPosition, true);
Game.addGuessPositionToResultMap(round.result.guessPosition, null, true); Game.scoreSum += round.score;
Game.scoreSum += round.result.score;
} }
document.getElementById('currentRound').innerHTML = String(Game.rounds.length) + '/' + String(Game.NUMBER_OF_ROUNDS); document.getElementById('currentRound').innerHTML = String(Game.rounds.length) + '/' + String(Game.NUMBER_OF_ROUNDS);
@ -273,13 +259,9 @@
if (round.realMarker) { if (round.realMarker) {
round.realMarker.setMap(null); round.realMarker.setMap(null);
} }
for (var j = 0; j < round.guessMarkers.length; ++j) { if (round.guessMarker) {
var guessMarker = round.guessMarkers[j]; round.guessMarker.setMap(null);
guessMarker.marker.setMap(null); round.line.setMap(null);
guessMarker.line.setMap(null);
if (guessMarker.info) {
guessMarker.info.close();
}
} }
} }
@ -309,17 +291,12 @@
var lastRound = Game.rounds[Game.rounds.length - 1]; var lastRound = Game.rounds[Game.rounds.length - 1];
lastRound.realMarker.setVisible(false); lastRound.realMarker.setVisible(false);
for (var i = 0; i < lastRound.guessMarkers.length; ++i) { if (lastRound.guessMarker) {
var guessMarker = lastRound.guessMarkers[i]; lastRound.guessMarker.setVisible(false);
guessMarker.marker.setVisible(false); lastRound.line.setVisible(false);
guessMarker.line.setVisible(false);
if (guessMarker.info) {
guessMarker.info.close();
} }
} }
}
document.getElementById('panoCover').style.visibility = 'hidden'; document.getElementById('panoCover').style.visibility = 'hidden';
document.getElementById('showGuessButton').style.visibility = null; document.getElementById('showGuessButton').style.visibility = null;
document.getElementById('guess').style.visibility = null; document.getElementById('guess').style.visibility = null;
@ -339,7 +316,7 @@
}, },
startNewRound: function () { startNewRound: function () {
Game.rounds.push({ position: null, guessPosition: null, realMarker: null, guessMarkers: [] }); Game.rounds.push({ position: null, guessPosition: null, realMarker: null, guessMarker: null, line: null });
document.getElementById('currentRound').innerHTML = String(Game.rounds.length) + '/' + String(Game.NUMBER_OF_ROUNDS); document.getElementById('currentRound').innerHTML = String(Game.rounds.length) + '/' + String(Game.NUMBER_OF_ROUNDS);
@ -379,7 +356,7 @@
Game.panorama.setPano(panoId); Game.panorama.setPano(panoId);
}, },
guess: function () { evaluateGuess: function () {
if (!Game.guessMarker) { if (!Game.guessMarker) {
return; return;
} }
@ -415,22 +392,13 @@
Game.scoreSum += this.response.result.score; Game.scoreSum += this.response.result.score;
document.getElementById('currentScoreSum').innerHTML = String(Game.scoreSum) + '/' + String(Game.rounds.length * Game.MAX_SCORE); document.getElementById('currentScoreSum').innerHTML = String(Game.scoreSum) + '/' + String(Game.rounds.length * Game.MAX_SCORE);
Game.rounds[Game.rounds.length - 1].position = this.response.result.position;
Game.addRealGuessPair(this.response.result.position, guessPosition);
var resultBounds = new google.maps.LatLngBounds(); var resultBounds = new google.maps.LatLngBounds();
resultBounds.extend(this.response.result.position);
Game.rounds[Game.rounds.length - 1].position = this.response.position;
Game.addPositionToResultMap();
resultBounds.extend(this.response.position);
Game.addGuessPositionToResultMap(guessPosition);
resultBounds.extend(guessPosition); resultBounds.extend(guessPosition);
if (roomId) {
for (var i = 0; i < this.response.allResults.length; ++i) {
var result = this.response.allResults[i];
Game.addGuessPositionToResultMap(result.guessPosition, result);
resultBounds.extend(result.guessPosition);
}
}
Game.map.setOptions({ Game.map.setOptions({
draggableCursor: 'grab' draggableCursor: 'grab'
}); });
@ -463,9 +431,8 @@
}, data); }, data);
}, },
addPositionToResultMap: function (hidden) { addRealGuessPair: function (position, guessPosition, hidden) {
var round = Game.rounds[Game.rounds.length - 1]; var round = Game.rounds[Game.rounds.length - 1];
var position = round.position;
round.realMarker = new google.maps.Marker({ round.realMarker = new google.maps.Marker({
map: Game.map, map: Game.map,
@ -486,25 +453,20 @@
round.realMarker.addListener('click', function () { round.realMarker.addListener('click', function () {
window.open('https://www.google.com/maps/search/?api=1&query=' + this.getPosition().toUrlValue(), '_blank'); window.open('https://www.google.com/maps/search/?api=1&query=' + this.getPosition().toUrlValue(), '_blank');
}); });
},
addGuessPositionToResultMap: function (guessPosition, result, hidden) { if (!guessPosition) {
var round = Game.rounds[Game.rounds.length - 1]; return;
var position = round.position; }
var guessMarker = { marker: null, line: null, info: null }; round.guessMarker = new google.maps.Marker({
var markerSvg = result ? 'marker-gray-empty.svg' : 'marker-blue-empty.svg';
var markerLabel = result ? result.userName.charAt(0).toUpperCase() : '?';
guessMarker.marker = new google.maps.Marker({
map: Game.map, map: Game.map,
visible: !hidden, visible: !hidden,
position: guessPosition, position: guessPosition,
zIndex: Game.rounds.length, zIndex: Game.rounds.length,
clickable: !!result, clickable: false,
draggable: false, draggable: false,
icon: { icon: {
url: STATIC_ROOT + '/img/markers/' + markerSvg + '?rev=' + REVISION, url: STATIC_ROOT + '/img/markers/marker-gray-empty.svg?rev=' + REVISION,
size: new google.maps.Size(24, 32), size: new google.maps.Size(24, 32),
scaledSize: new google.maps.Size(24, 32), scaledSize: new google.maps.Size(24, 32),
anchor: new google.maps.Point(12, 32), anchor: new google.maps.Point(12, 32),
@ -515,11 +477,11 @@
fontFamily: 'Roboto', fontFamily: 'Roboto',
fontSize: '16px', fontSize: '16px',
fontWeight: '500', fontWeight: '500',
text: markerLabel text: '?'
} }
}); });
guessMarker.line = new google.maps.Polyline({ round.line = new google.maps.Polyline({
map: Game.map, map: Game.map,
visible: !hidden, visible: !hidden,
path: [ path: [
@ -542,19 +504,6 @@
draggable: false, draggable: false,
editable: false editable: false
}); });
if (result) {
guessMarker.info = new google.maps.InfoWindow({
content: '<p class="small bold">' + result.userName + '</p>' +
'<p class="small">' + Util.printDistanceForHuman(result.distance) + ' | ' + result.score + ' points</p>',
});
guessMarker.marker.addListener('click', function () {
guessMarker.info.open(Game.map, this);
});
}
round.guessMarkers.push(guessMarker);
}, },
calculateScoreBarProperties: function (score, maxScore) { calculateScoreBarProperties: function (score, maxScore) {
@ -606,14 +555,14 @@
}); });
round.realMarker.setVisible(true); round.realMarker.setVisible(true);
if (round.guessMarker) {
round.guessMarker.setVisible(true);
round.line.setVisible(true);
}
resultBounds.extend(round.position); resultBounds.extend(round.position);
if (round.guessMarker) {
for (var j = 0; j < round.guessMarkers.length; ++j) { resultBounds.extend(round.guessPosition);
var guessMarker = round.guessMarkers[j];
guessMarker.marker.setVisible(true);
guessMarker.line.setVisible(true);
resultBounds.extend(guessMarker.marker.getPosition());
} }
} }
@ -690,7 +639,7 @@
clickable: false, clickable: false,
draggable: true, draggable: true,
icon: { icon: {
url: STATIC_ROOT + '/img/markers/marker-blue-empty.svg?rev=' + REVISION, url: STATIC_ROOT + '/img/markers/marker-gray-empty.svg?rev=' + REVISION,
size: new google.maps.Size(24, 32), size: new google.maps.Size(24, 32),
scaledSize: new google.maps.Size(24, 32), scaledSize: new google.maps.Size(24, 32),
anchor: new google.maps.Point(12, 32), anchor: new google.maps.Point(12, 32),
@ -738,7 +687,7 @@
} }
document.getElementById('guessButton').onclick = function () { document.getElementById('guessButton').onclick = function () {
Game.guess(); Game.evaluateGuess();
} }
document.getElementById('continueButton').onclick = function () { document.getElementById('continueButton').onclick = function () {

View File

@ -61,11 +61,9 @@ class GameFlowController
$round = $state['rounds'][$i]; $round = $state['rounds'][$i];
$response['history'][] = [ $response['history'][] = [
'position' => $round['position']->toArray(), 'position' => $round['position']->toArray(),
'result' => [
'guessPosition' => $round['guessPosition']->toArray(), 'guessPosition' => $round['guessPosition']->toArray(),
'distance' => $round['distance'], 'distance' => $round['distance'],
'score' => $round['score'] 'score' => $round['score']
]
]; ];
} }
@ -128,8 +126,11 @@ class GameFlowController
$last['score'] = $result['score']; $last['score'] = $result['score'];
$response = [ $response = [
'result' => [
'position' => $last['position']->toArray(), 'position' => $last['position']->toArray(),
'result' => $result 'distance' => $result['distance'],
'score' => $result['score']
]
]; ];
$state['rounds'][$state['currentRound']] = $last; $state['rounds'][$state['currentRound']] = $last;
@ -164,18 +165,20 @@ class GameFlowController
$guessPosition = new Position((float) $this->request->post('lat'), (float) $this->request->post('lng')); $guessPosition = new Position((float) $this->request->post('lat'), (float) $this->request->post('lng'));
$result = $this->evalueteGuess($last['position'], $guessPosition, $state['area']); $result = $this->evalueteGuess($last['position'], $guessPosition, $state['area']);
$allResults = $this->multiConnector->sendMessage('guess', [ $this->multiConnector->sendMessage('guess', [
'roomId' => $roomId, 'roomId' => $roomId,
'token' => $multiState['token'], 'token' => $multiState['token'],
'guessPosition' => $guessPosition->toArray(), 'guess' => $guessPosition->toArray(),
'distance' => $result['distance'], 'distance' => $result['distance'],
'score' => $result['score'] 'score' => $result['score']
]); ]);
$response = [ $response = [
'result' => [
'position' => $last['position']->toArray(), 'position' => $last['position']->toArray(),
'result' => $result, 'distance' => $result['distance'],
'allResults' => $allResults 'score' => $result['score']
]
]; ];
return new JsonContent($response); return new JsonContent($response);

View File

@ -2,7 +2,7 @@
class MultiConnector class MultiConnector
{ {
public function sendMessage(string $func, array $args = []) public function sendMessage(string $func, array $args = []): void
{ {
$message = json_encode([ $message = json_encode([
'func' => $func, 'func' => $func,
@ -17,10 +17,8 @@ class MultiConnector
} }
fclose($connection); fclose($connection);
$response = json_decode($response, true); if ($response !== 'OK') {
throw new \Exception('Sending message failed with response: ' . $response);
if (isset($response['data'])) {
return $response['data'];
} }
} }
} }