From cf59f937c9be881cba1d656788b265b776a12891 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 4 Apr 2021 23:18:26 +0200 Subject: [PATCH] MAPG-223 send error if member already guessed --- multi/index.js | 43 +++++++++++++++------------ src/Controller/GameFlowController.php | 8 +++-- src/Multi/MultiConnector.php | 6 +--- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/multi/index.js b/multi/index.js index cbfc838..0dc5558 100644 --- a/multi/index.js +++ b/multi/index.js @@ -36,19 +36,20 @@ 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)) { - console.error('Room does not exist!') - return; + return { error: 'room_not_found' }; } var room = this.rooms.get(roomId); room.updated = new Date(); if (room.members.has(token)) { - return; + return { error: 'member_already_joined' }; } var data = { userName: userName }; @@ -58,13 +59,13 @@ class MultiGame { }); room.members.set(token, { userName: userName, connection: null }); + + return { ok: true }; } startGame(roomId, places) { if (!this.rooms.has(roomId)) { - //TODO: send something back - console.log('Room does not exist!') - return; + return { error: 'room_not_found' }; } var room = this.rooms.get(roomId); @@ -84,19 +85,23 @@ class MultiGame { room.rounds = rounds; this.nextRound(roomId, 0); + + return { ok: true }; } guess(roomId, token, guessPosition, distance, score) { if (!this.rooms.has(roomId)) { - //TODO: send something back - console.log('Room does not exist!') - return; + return { error: 'room_not_found' }; } 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); @@ -106,14 +111,12 @@ class MultiGame { this._setNewTimeout(room, round); - return allResults; + return { allResults: allResults }; } nextRound(roomId, currentRound) { if (!this.rooms.has(roomId)) { - //TODO: send something back - console.log('Room does not exist!') - return; + return { error: 'room_not_found' }; } var room = this.rooms.get(roomId); @@ -137,6 +140,8 @@ class MultiGame { room.members.forEach(function (member) { self._sendToMember(member, 'new_round', data); }); + + return { ok: true }; } _setNewTimeout(room, round) { @@ -295,30 +300,30 @@ var tcpServer = net.createServer(function (socket) { return; } - var response = { data: null }; + var response; switch (data.func) { case 'create_room': - response.data = multiGame.createRoom(data.args.roomId); + response = multiGame.createRoom(data.args.roomId); break; 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; case 'start_game': - response.data = multiGame.startGame(data.args.roomId, data.args.places); + response = multiGame.startGame(data.args.roomId, data.args.places); break 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; case 'next_round': - response.data = multiGame.nextRound(data.args.roomId, data.args.currentRound); + response = multiGame.nextRound(data.args.roomId, data.args.currentRound); break; } diff --git a/src/Controller/GameFlowController.php b/src/Controller/GameFlowController.php index e668489..cca90c1 100644 --- a/src/Controller/GameFlowController.php +++ b/src/Controller/GameFlowController.php @@ -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']); - $allResults = $this->multiConnector->sendMessage('guess', [ + $responseFromMulti = $this->multiConnector->sendMessage('guess', [ 'roomId' => $roomId, 'token' => $multiState['token'], 'guessPosition' => $guessPosition->toArray(), @@ -172,10 +172,14 @@ class GameFlowController 'score' => $result['score'] ]); + if (isset($responseFromMulti['error'])) { + return new JsonContent(['error' => $responseFromMulti['error']]); + } + $response = [ 'position' => $last['position']->toArray(), 'result' => $result, - 'allResults' => $allResults + 'allResults' => $responseFromMulti['allResults'] ]; return new JsonContent($response); diff --git a/src/Multi/MultiConnector.php b/src/Multi/MultiConnector.php index c874966..7d61385 100644 --- a/src/Multi/MultiConnector.php +++ b/src/Multi/MultiConnector.php @@ -17,10 +17,6 @@ class MultiConnector } fclose($connection); - $response = json_decode($response, true); - - if (isset($response['data'])) { - return $response['data']; - } + return json_decode($response, true); } }