-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTurnList.js
More file actions
67 lines (55 loc) · 1.5 KB
/
TurnList.js
File metadata and controls
67 lines (55 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict';
function TurnList() {}
TurnList.prototype.reset = function (charactersById) {
this._charactersById = charactersById;
this._turnIndex = -1;
this.turnNumber = 0;
this.activeCharacterId = null;
this.list = this._sortByInitiative();
};
TurnList.prototype.next = function () {
// Haz que calcule el siguiente turno y devuelva el resultado
// según la especificación. Recuerda que debe saltar los personajes
// muertos.
var firstTurn = this.turnNumber;
var characterDead = false;
this.turnNumber++;
while(!characterDead){
firstTurn = firstTurn % this.list.length;
if(!this._charactersById[this.list[firstTurn]].isDead()) {
this.activeCharacterId = this.list[firstTurn];
characterDead = true;
}
firstTurn++;
}
var nextTurn = {
number: this.turnNumber,
party: this._charactersById[this.activeCharacterId].party,
activeCharacterId: this.activeCharacterId
};
return nextTurn;
};
TurnList.prototype._sortByInitiative = function () {
var list = [];
var auxList = [];
for(var characterName in this._charactersById){
auxList.push({
name: characterName,
initiative: this._charactersById[characterName].initiative
});
}
auxList.sort(function (a, b) {
if(a.initiative < b.initiative){
return 1;
}else if(a.initiative > b.initiative){
return -1;
}else{
return 0;
}
});
list = auxList.map(function(character){
return character.name;
});
return list;
};
module.exports = TurnList;