Compare commits

...
4 changed files with 41 additions and 30 deletions

No files matched your search

+24 -19
View File
@@ -36,19 +36,20 @@ class MultiGame {
createRoom(roomId) { createRoom(roomId) {
this.rooms.set(roomId, { members: new Map(), rounds: [], currentRound: -1, updated: new Date() }); this.rooms.set(roomId, { members: new Map(), rounds: [], currentRound: -1, updated: new Date() });
return { ok: true };
} }
joinRoom(roomId, token, userName) { joinRoom(roomId, token, userName) {
if (!this.rooms.has(roomId)) { if (!this.rooms.has(roomId)) {
console.error('Room does not exist!') return { error: 'room_not_found' };
return;
} }
var room = this.rooms.get(roomId); var room = this.rooms.get(roomId);
room.updated = new Date(); room.updated = new Date();
if (room.members.has(token)) { if (room.members.has(token)) {
return; return { error: 'member_already_joined' };
} }
var data = { userName: userName }; var data = { userName: userName };
@@ -58,13 +59,13 @@ class MultiGame {
}); });
room.members.set(token, { userName: userName, connection: null }); room.members.set(token, { userName: userName, connection: null });
return { ok: true };
} }
startGame(roomId, places) { startGame(roomId, places) {
if (!this.rooms.has(roomId)) { if (!this.rooms.has(roomId)) {
//TODO: send something back return { error: 'room_not_found' };
console.log('Room does not exist!')
return;
} }
var room = this.rooms.get(roomId); var room = this.rooms.get(roomId);
@@ -84,19 +85,23 @@ class MultiGame {
room.rounds = rounds; room.rounds = rounds;
this.nextRound(roomId, 0); this.nextRound(roomId, 0);
return { ok: true };
} }
guess(roomId, token, guessPosition, distance, score) { guess(roomId, token, guessPosition, distance, score) {
if (!this.rooms.has(roomId)) { if (!this.rooms.has(roomId)) {
//TODO: send something back return { error: 'room_not_found' };
console.log('Room does not exist!')
return;
} }
var room = this.rooms.get(roomId); var room = this.rooms.get(roomId);
room.updated = new Date(); room.updated = new Date();
var round = room.rounds[room.currentRound]; var round = room.rounds[room.currentRound];
if (round.results.has(token)) {
return { error: 'already_guessed' };
}
var member = room.members.get(token); var member = room.members.get(token);
var allResults = this._collectResultsInRound(room, round); var allResults = this._collectResultsInRound(room, round);
@@ -106,14 +111,12 @@ class MultiGame {
this._setNewTimeout(room, round); this._setNewTimeout(room, round);
return allResults; return { allResults: allResults };
} }
nextRound(roomId, currentRound) { nextRound(roomId, currentRound) {
if (!this.rooms.has(roomId)) { if (!this.rooms.has(roomId)) {
//TODO: send something back return { error: 'room_not_found' };
console.log('Room does not exist!')
return;
} }
var room = this.rooms.get(roomId); var room = this.rooms.get(roomId);
@@ -137,6 +140,8 @@ class MultiGame {
room.members.forEach(function (member) { room.members.forEach(function (member) {
self._sendToMember(member, 'new_round', data); self._sendToMember(member, 'new_round', data);
}); });
return { ok: true };
} }
_setNewTimeout(room, round) { _setNewTimeout(room, round) {
@@ -295,30 +300,30 @@ var tcpServer = net.createServer(function (socket) {
return; return;
} }
var response = { data: null }; var response;
switch (data.func) { switch (data.func) {
case 'create_room': case 'create_room':
response.data = multiGame.createRoom(data.args.roomId); response = 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); response = 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); response = 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); response = 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); response = multiGame.nextRound(data.args.roomId, data.args.currentRound);
break; break;
} }
+10 -4
View File
@@ -120,9 +120,11 @@
for (var j = 0; j < round.allResults.length; ++j) { for (var j = 0; j < round.allResults.length; ++j) {
var result = round.allResults[j]; var result = round.allResults[j];
if (result.guessPosition) {
Game.addGuessPositionToResultMap(result.guessPosition, result, true); 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);
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);
@@ -330,6 +332,7 @@
scoreInfo.children[0].style.display = null; scoreInfo.children[0].style.display = null;
scoreInfo.children[1].style.display = null; scoreInfo.children[1].style.display = null;
document.getElementById('continueButton').style.display = null; document.getElementById('continueButton').style.display = null;
document.getElementById('showSummaryButton').style.display = null;
document.getElementById('startNewGameButton').style.display = null; document.getElementById('startNewGameButton').style.display = null;
document.getElementById('showGuessButton').style.visibility = null; document.getElementById('showGuessButton').style.visibility = null;
@@ -434,10 +437,12 @@
if (allResults) { if (allResults) {
for (var i = 0; i < allResults.length; ++i) { for (var i = 0; i < allResults.length; ++i) {
var currentResult = allResults[i]; var currentResult = allResults[i];
if (currentResult.guessPosition) {
Game.addGuessPositionToResultMap(currentResult.guessPosition, currentResult); Game.addGuessPositionToResultMap(currentResult.guessPosition, currentResult);
resultBounds.extend(currentResult.guessPosition); resultBounds.extend(currentResult.guessPosition);
} }
} }
}
Game.showResultMap(result, resultBounds); Game.showResultMap(result, resultBounds);
}, },
@@ -735,15 +740,16 @@
clearInterval(Game.countdownHandler); clearInterval(Game.countdownHandler);
} }
Game.countdownElement = document.getElementById('countdown');
Game.countdownTimeElement = document.getElementById('countdownTime');
Game.setCountdownTime(Math.round(timeout / 1000));
if (timeout <= 0) { if (timeout <= 0) {
return; return;
} }
Game.timeoutEnd = new Date(new Date().getTime() + timeout); Game.timeoutEnd = new Date(new Date().getTime() + timeout);
Game.countdownElement = document.getElementById('countdown');
Game.countdownTimeElement = document.getElementById('countdownTime');
Game.setCountdownTime(Math.round(timeout / 1000));
Game.countdownHandler = setInterval(function () { Game.countdownHandler = setInterval(function () {
var timeLeft = Math.round((Game.timeoutEnd - new Date()) / 1000); var timeLeft = Math.round((Game.timeoutEnd - new Date()) / 1000);
+6 -2
View File
@@ -164,7 +164,7 @@ 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', [ $responseFromMulti = $this->multiConnector->sendMessage('guess', [
'roomId' => $roomId, 'roomId' => $roomId,
'token' => $multiState['token'], 'token' => $multiState['token'],
'guessPosition' => $guessPosition->toArray(), 'guessPosition' => $guessPosition->toArray(),
@@ -172,10 +172,14 @@ class GameFlowController
'score' => $result['score'] 'score' => $result['score']
]); ]);
if (isset($responseFromMulti['error'])) {
return new JsonContent(['error' => $responseFromMulti['error']]);
}
$response = [ $response = [
'position' => $last['position']->toArray(), 'position' => $last['position']->toArray(),
'result' => $result, 'result' => $result,
'allResults' => $allResults 'allResults' => $responseFromMulti['allResults']
]; ];
return new JsonContent($response); return new JsonContent($response);
+1 -5
View File
@@ -17,10 +17,6 @@ class MultiConnector
} }
fclose($connection); fclose($connection);
$response = json_decode($response, true); return json_decode($response, true);
if (isset($response['data'])) {
return $response['data'];
}
} }
} }