MAPG-204 clean up result sending

This commit is contained in:
Bence Pőcze 2021-04-03 13:38:16 +02:00
parent 7433813337
commit 9e5d20796c
Signed by: bence
GPG Key ID: AA52B11A3269D1C1
4 changed files with 72 additions and 79 deletions

View File

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

View File

@ -66,10 +66,6 @@
Game.MultiConnector.newRound(json.data);
break;
case 'results':
//TODO
break;
case 'guess':
//TODO
break;
@ -94,9 +90,9 @@
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.guessPosition, realMarker: null, guessMarker: null, line: null });
Game.addRealGuessPair(round.position, round.guessPosition, true);
Game.scoreSum += round.score;
Game.rounds.push({ position: round.position, guessPosition: round.result.guessPosition, realMarker: null, guessMarker: null, line: null });
Game.addRealGuessPair(round.position, round.result.guessPosition, true);
Game.scoreSum += round.result.score;
}
document.getElementById('currentRound').innerHTML = String(Game.rounds.length) + '/' + String(Game.NUMBER_OF_ROUNDS);
@ -234,9 +230,9 @@
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.guessPosition, realMarker: null, guessMarker: null, line: null });
Game.addRealGuessPair(round.position, round.guessPosition, true);
Game.scoreSum += round.score;
Game.rounds.push({ position: round.position, guessPosition: round.result.guessPosition, realMarker: null, guessMarker: null, line: null });
Game.addRealGuessPair(round.position, round.result.guessPosition, true);
Game.scoreSum += round.result.score;
}
document.getElementById('currentRound').innerHTML = String(Game.rounds.length) + '/' + String(Game.NUMBER_OF_ROUNDS);
@ -356,7 +352,7 @@
Game.panorama.setPano(panoId);
},
evaluateGuess: function () {
guess: function () {
if (!Game.guessMarker) {
return;
}
@ -392,11 +388,11 @@
Game.scoreSum += this.response.result.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);
Game.rounds[Game.rounds.length - 1].position = this.response.position;
Game.addRealGuessPair(this.response.position, guessPosition);
var resultBounds = new google.maps.LatLngBounds();
resultBounds.extend(this.response.result.position);
resultBounds.extend(this.response.position);
resultBounds.extend(guessPosition);
Game.map.setOptions({
@ -687,7 +683,7 @@
}
document.getElementById('guessButton').onclick = function () {
Game.evaluateGuess();
Game.guess();
}
document.getElementById('continueButton').onclick = function () {

View File

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

View File

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