-
Notifications
You must be signed in to change notification settings - Fork 2
/
test-game.js
60 lines (49 loc) · 1.79 KB
/
test-game.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
const vm = require('vm');
const fs = require('fs');
const assert = require('assert');
new vm.Script(fs.readFileSync('./src/shared.js')).runInThisContext();
new vm.Script('GAME_SPEED = 1000;').runInThisContext();
assert.ok(isFromBack({x: 0, y: 0}, {x: 1, y: 0}, 'right'));
assert.ok(!isFromBack({x: 0, y: 0}, {x: 1, y: 0}, 'left'));
assert.ok(isFromBack({x: 5, y: 9}, {x: 5, y: 0}, 'up'));
assert.ok(isFromBack({x: 5, y: 9}, {x: 50, y: 100}, 'down'));
new vm.Script(fs.readFileSync('./src/game.js')).runInThisContext();
assert.ok(Game);
const game = new Game({
participants: 2,
onevent: e => {/*console.log(e)*/}
});
assert.ok(game);
assert.ok(game.players.length === 0);
assert.ok(!game.state);
assert.ok(game.chars.length === 0);
game.addPlayer('A');
assert.ok(game.state === 'wait');
assert.ok(game.players.length === 1);
game.addPlayer('B');
assert.ok(game.players.length === 2);
assert.ok(game.state === 'pick');
assert.ok(game.chars.length > 0);
assert.ok(game.chars[0].directions.length === 4);
assert.ok(game.getChar(game.chars[0].id).owner === null);
assert.ok(game.getInPos(game.chars[0].pos) === game.chars[0]);
game.pick('A', game.chars[0].id);
assert.ok(game.getChar(game.chars[0].id).owner === 'A');
game.pick('B', game.chars[1].id);
game.pick('A', game.chars[2].id);
game.pick('B', game.chars[3].id);
game.pick('A', game.chars[4].id);
game.pick('A', game.chars[5].id);
game.pick('B', game.chars[6].id);
game.pick('B', game.chars[7].id);
assert.ok(game.state === 'play');
assert.ok(game.chars.length === 8);
(async function play() {
while(game.state === 'play') {
let pid = game.players[game.turn % 2].id;
let cid = pick(game.chars.filter(c => c.owner === pid && c.hp > 0)).id;
await game.move(pid, cid);
}
assert.ok(game.state === 'game-over');
console.log('Game test OK');
})();