Commit 7f0bc29f authored by Monika's avatar Monika
Browse files

whoIsOnServe

parent 1827acee
/**
* WhoIsTurn
* @returns {{getCount: Function, addPlayer: Function}}
* todo refactor create common parent class for whoIsTurn and WhoIsOnServe methods resetTeamStack, getNext
*/
module.exports = function (PlayersColliction, Score) {
var _playersColliction = PlayersColliction;
var _score = Score;
var _whichTeam = 1;
var _team0 = [];
var _team1 = [];
var _actId = 0;
return {
resetTeamStack: function () {
var allPlayers = _playersColliction.getAllPlayers();
for (var key in allPlayers) {
if (allPlayers[key].getTeam() == 0) {
_team0.push(key);
} else {
_team1.push(key);
}
}
},
getTeam: function() {
return _whichTeam;
},
getActId: function() {
return _actId;
},
getNext: function () {
var next;
if (_whichTeam) {
if (_team1.length == 0) {
this.resetTeamStack(1);
}
next = _team1[0];
_team1.splice(0, 1);
} else {
if (_team0.length == 0) {
this.resetTeamStack(0);
}
next = _team0[0];
_team0.splice(0, 1);
}
_actId = next;
return next;
},
getWhoIsOnServe: function () {
var sumScoreTeams = _score.getScore();
var sumScore = sumScoreTeams.team0 + sumScoreTeams.team1;
if(sumScore % 2 == 0){
if (_whichTeam) {
_whichTeam = 0;
} else {
_whichTeam = 1;
}
}
console.log(this.getNext(), 'serve');
return this.getNext();
}
};
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment