Compare commits

..

No commits in common. "c5d25913714c5bd97c772706459352bb06e138b8" and "257f59c96edad845a927367d8823c9118159c245" have entirely different histories.

3 changed files with 26 additions and 31 deletions

View File

@ -36,20 +36,19 @@ class MultiGame {
createRoom(roomId) {
this.rooms.set(roomId, { members: new Map(), rounds: [], currentRound: -1, updated: new Date() });
return { ok: true };
}
joinRoom(roomId, token, userName) {
if (!this.rooms.has(roomId)) {
return { error: 'room_not_found' };
console.error('Room does not exist!')
return;
}
var room = this.rooms.get(roomId);
room.updated = new Date();
if (room.members.has(token)) {
return { error: 'member_already_joined' };
return;
}
var data = { userName: userName };
@ -59,13 +58,13 @@ class MultiGame {
});
room.members.set(token, { userName: userName, connection: null });
return { ok: true };
}
startGame(roomId, places) {
if (!this.rooms.has(roomId)) {
return { error: 'room_not_found' };
//TODO: send something back
console.log('Room does not exist!')
return;
}
var room = this.rooms.get(roomId);
@ -85,23 +84,19 @@ class MultiGame {
room.rounds = rounds;
this.nextRound(roomId, 0);
return { ok: true };
}
guess(roomId, token, guessPosition, distance, score) {
if (!this.rooms.has(roomId)) {
return { error: 'room_not_found' };
//TODO: send something back
console.log('Room does not exist!')
return;
}
var room = this.rooms.get(roomId);
room.updated = new Date();
var round = room.rounds[room.currentRound];
if (round.results.has(token)) {
return { error: 'already_guessed' };
}
var member = room.members.get(token);
var allResults = this._collectResultsInRound(room, round);
@ -111,12 +106,14 @@ class MultiGame {
this._setNewTimeout(room, round);
return { allResults: allResults };
return allResults;
}
nextRound(roomId, currentRound) {
if (!this.rooms.has(roomId)) {
return { error: 'room_not_found' };
//TODO: send something back
console.log('Room does not exist!')
return;
}
var room = this.rooms.get(roomId);
@ -140,8 +137,6 @@ class MultiGame {
room.members.forEach(function (member) {
self._sendToMember(member, 'new_round', data);
});
return { ok: true };
}
_setNewTimeout(room, round) {
@ -300,30 +295,30 @@ var tcpServer = net.createServer(function (socket) {
return;
}
var response;
var response = { data: null };
switch (data.func) {
case 'create_room':
response = multiGame.createRoom(data.args.roomId);
response.data = multiGame.createRoom(data.args.roomId);
break;
case 'join_room':
response = multiGame.joinRoom(data.args.roomId, data.args.token, data.args.userName);
response.data = multiGame.joinRoom(data.args.roomId, data.args.token, data.args.userName);
break;
case 'start_game':
response = multiGame.startGame(data.args.roomId, data.args.places);
response.data = multiGame.startGame(data.args.roomId, data.args.places);
break
case 'guess':
response = multiGame.guess(data.args.roomId, data.args.token, data.args.guessPosition, data.args.distance, data.args.score);
response.data = multiGame.guess(data.args.roomId, data.args.token, data.args.guessPosition, data.args.distance, data.args.score);
break;
case 'next_round':
response = multiGame.nextRound(data.args.roomId, data.args.currentRound);
response.data = multiGame.nextRound(data.args.roomId, data.args.currentRound);
break;
}

View File

@ -164,7 +164,7 @@ class GameFlowController
$guessPosition = new Position((float) $this->request->post('lat'), (float) $this->request->post('lng'));
$result = $this->evalueteGuess($last['position'], $guessPosition, $state['area']);
$responseFromMulti = $this->multiConnector->sendMessage('guess', [
$allResults = $this->multiConnector->sendMessage('guess', [
'roomId' => $roomId,
'token' => $multiState['token'],
'guessPosition' => $guessPosition->toArray(),
@ -172,14 +172,10 @@ class GameFlowController
'score' => $result['score']
]);
if (isset($responseFromMulti['error'])) {
return new JsonContent(['error' => $responseFromMulti['error']]);
}
$response = [
'position' => $last['position']->toArray(),
'result' => $result,
'allResults' => $responseFromMulti['allResults']
'allResults' => $allResults
];
return new JsonContent($response);

View File

@ -17,6 +17,10 @@ class MultiConnector
}
fclose($connection);
return json_decode($response, true);
$response = json_decode($response, true);
if (isset($response['data'])) {
return $response['data'];
}
}
}