feature/MAPG-205-set-timeout-in-multiplayer-rooms #15

Merged
bence merged 5 commits from feature/MAPG-205-set-timeout-in-multiplayer-rooms into develop 2021-04-04 14:22:59 +02:00
Showing only changes of commit 42224b074e - Show all commits

View File

@ -3,6 +3,11 @@
process.title = 'mapguesser-multi';
class MultiGame {
static ROUND_TIMEOUT_DEFAULT = 120000;
static ROUND_TIMEOUT_MINIMUM = 15000;
static ROUND_TIMEOUT_DIVIDER = 2;
static ROUND_TIMEOUT_OFFSET = 500;
constructor() {
this.rooms = new Map();
}
@ -67,7 +72,13 @@ class MultiGame {
var rounds = [];
places.forEach(function (place) {
rounds.push({ place: place, results: new Map() })
rounds.push({
place: place,
results: new Map(),
timeout: MultiGame.ROUND_TIMEOUT_DEFAULT,
timeoutStarted: null,
timeoutHandler: null
})
});
room.rounds = rounds;
@ -86,9 +97,24 @@ class MultiGame {
room.updated = new Date();
var round = room.rounds[room.currentRound];
clearTimeout(round.timeoutHandler);
round.timeout = round.timeout - (new Date() - round.timeoutStarted);
if (round.timeout > MultiGame.ROUND_TIMEOUT_DIVIDER * MultiGame.ROUND_TIMEOUT_MINIMUM) {
round.timeout = round.timeout / MultiGame.ROUND_TIMEOUT_DIVIDER;
} else if (round.timeout > MultiGame.ROUND_TIMEOUT_MINIMUM) {
round.timeout = MultiGame.ROUND_TIMEOUT_MINIMUM;
}
round.timeoutStarted = new Date();
var self = this;
round.timeoutHandler = setTimeout(function () {
self._endRoundTimeout(room, round);
}, round.timeout + MultiGame.ROUND_TIMEOUT_OFFSET);
var member = room.members.get(token);
var allResults = this._collectResultsInRound(room, round);
this._broadcastTimeout(room, round);
this._broadcastGuess(room, member.userName, guessPosition, distance, score);
round.results.set(token, { guessPosition: guessPosition, distance: distance, score: score });
@ -110,12 +136,15 @@ class MultiGame {
var round = room.rounds[room.currentRound];
round.timeoutStarted = new Date();
var self = this;
round.timeoutHandler = setTimeout(function () {
self._endRoundTimeout(room, round);
}, round.timeout + MultiGame.ROUND_TIMEOUT_OFFSET);
var data = {};
data.place = { panoId: round.place.panoId, pov: round.place.pov };
if (room.currentRound > 0) {
data.result = { position: room.rounds[room.currentRound - 1].place.position };
}
data.timeout = round.timeout;
var self = this;
room.members.forEach(function (member) {
@ -123,11 +152,28 @@ class MultiGame {
});
}
_endRoundTimeout(room, round) {
clearTimeout(round.timeoutHandler);
var data = { position: round.place.position, allResults: this._collectResultsInRound(room, round) };
var self = this;
room.members.forEach(function (member, token) {
if (round.results.has(token)) {
return;
}
self._sendToMember(member, 'end_round', data);
});
}
_sendInitialData(room, member, token) {
var data = {};
if (room.currentRound >= 0) {
data.place = room.rounds[room.currentRound].place;
var round = room.rounds[room.currentRound];
data.place = round.place;
data.timeout = round.timeout - (new Date() - round.timeoutStarted);
}
data.history = [];
@ -175,6 +221,13 @@ class MultiGame {
return results;
}
_broadcastTimeout(room, round) {
var self = this;
room.members.forEach(function (member) {
self._sendToMember(member, 'timeout_changed', { timeout: round.timeout });
});
}
_broadcastGuess(room, userName, guessPosition, distance, score) {
var data = { userName: userName, guessPosition: guessPosition, distance: distance, score: score };
var round = room.rounds[room.currentRound];