feature/MAPG-205-set-timeout-in-multiplayer-rooms #15
@ -3,6 +3,11 @@
|
|||||||
process.title = 'mapguesser-multi';
|
process.title = 'mapguesser-multi';
|
||||||
|
|
||||||
class MultiGame {
|
class MultiGame {
|
||||||
|
static ROUND_TIMEOUT_DEFAULT = 120000;
|
||||||
|
static ROUND_TIMEOUT_MINIMUM = 15000;
|
||||||
|
static ROUND_TIMEOUT_DIVIDER = 2;
|
||||||
|
static ROUND_TIMEOUT_OFFSET = 500;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.rooms = new Map();
|
this.rooms = new Map();
|
||||||
}
|
}
|
||||||
@ -67,7 +72,13 @@ class MultiGame {
|
|||||||
|
|
||||||
var rounds = [];
|
var rounds = [];
|
||||||
places.forEach(function (place) {
|
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;
|
room.rounds = rounds;
|
||||||
@ -86,9 +97,24 @@ class MultiGame {
|
|||||||
room.updated = new Date();
|
room.updated = new Date();
|
||||||
|
|
||||||
var round = room.rounds[room.currentRound];
|
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 member = room.members.get(token);
|
||||||
var allResults = this._collectResultsInRound(room, round);
|
var allResults = this._collectResultsInRound(room, round);
|
||||||
|
|
||||||
|
this._broadcastTimeout(room, round);
|
||||||
this._broadcastGuess(room, member.userName, guessPosition, distance, score);
|
this._broadcastGuess(room, member.userName, guessPosition, distance, score);
|
||||||
|
|
||||||
round.results.set(token, { guessPosition: guessPosition, distance: distance, score: score });
|
round.results.set(token, { guessPosition: guessPosition, distance: distance, score: score });
|
||||||
@ -110,12 +136,15 @@ class MultiGame {
|
|||||||
|
|
||||||
var round = room.rounds[room.currentRound];
|
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 = {};
|
var data = {};
|
||||||
data.place = { panoId: round.place.panoId, pov: round.place.pov };
|
data.place = { panoId: round.place.panoId, pov: round.place.pov };
|
||||||
|
data.timeout = round.timeout;
|
||||||
if (room.currentRound > 0) {
|
|
||||||
data.result = { position: room.rounds[room.currentRound - 1].place.position };
|
|
||||||
}
|
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
room.members.forEach(function (member) {
|
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) {
|
_sendInitialData(room, member, token) {
|
||||||
var data = {};
|
var data = {};
|
||||||
|
|
||||||
if (room.currentRound >= 0) {
|
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 = [];
|
data.history = [];
|
||||||
@ -175,6 +221,13 @@ class MultiGame {
|
|||||||
return results;
|
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) {
|
_broadcastGuess(room, userName, guessPosition, distance, score) {
|
||||||
var data = { userName: userName, guessPosition: guessPosition, distance: distance, score: score };
|
var data = { userName: userName, guessPosition: guessPosition, distance: distance, score: score };
|
||||||
var round = room.rounds[room.currentRound];
|
var round = room.rounds[room.currentRound];
|
||||||
|
Loading…
Reference in New Issue
Block a user