-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
63 lines (54 loc) · 1.62 KB
/
util.js
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
const crypto = require('crypto');
const {UnitType} = require('./config');
function randString(length) {
return crypto.randomBytes(length / 2).toString('hex');
}
class SquareState {
constructor(options = {}) {
Object.assign(this, {
units: [],
baseId: null,
baseHP: 0,
isFog: false
}, options);
}
currentOwner() {
// function assumes that only units of one player are on the square
if (this.units.length === 0) {
return null;
}
return this.units[0].playerId;
}
getUnit() {
// function assumes only one unit is in the units array
return this.units.length > 0 ? this.units[0] : null;
}
peekUnitById(id) {
for (let idx = 0; idx < this.units.length; idx++) {
if (this.units[idx].id === id) {
let temp = this.units[idx];
return temp;
}
}
return null;
}
popUnitById(id) {
for (let idx = 0; idx < this.units.length; idx++) {
if (this.units[idx].id === id) {
let temp = this.units[idx];
this.units.splice(idx, 1);
return temp;
}
}
return null;
}
hasDefenderId(playerId) {
for (let idx = 0; idx < this.units.length; idx++) {
if (this.units[idx].playerId === playerId) {
return this.units[idx].type === UnitType.DEFENDER;
}
}
return false;
}
}
module.exports = {randString, SquareState};