-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.js
More file actions
119 lines (95 loc) · 3.29 KB
/
run.js
File metadata and controls
119 lines (95 loc) · 3.29 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
'use strict';
const client = require('./lib/client');
const events = require('./lib/events');
const attack = require('./lib/attack');
const defend = require('./lib/defend');
const role = require('./lib/role');
const MY_TEAM_NAME = 'team_name';
const MY_TEAM_KEY = 'key';
let gameState = {
requests: 30
};
client.channel.once(events.client.disconnected, () => {
console.log('error: client lost connection');
process.exit(1);
});
client.channel.once(events.client.error, err => {
console.log(err);
process.exit(1);
});
client.channel.once(events.client.connected, socket => {
// Keep hold of socket if you want to write commands manually on the wire.
// You probably want to just use attack(), defend() and the role functions though.
});
client.channel.on(events.raw.response, res => {
/*
* We can do whatever we like with the raw responses.
* Here we simply log them, as long as they're not query responses
* as we're already subscribing to the `events.query.gridUpdate` event below.
*/
if (res.result && res.result.grid) {
// It's a query resonse, don't log this.
return;
}
console.log(`> ${JSON.stringify(res)}`);
});
client.channel.on(events.game.requestsRemaining, remaining => {
// Keep track of how many requests we have left this period.
gameState.requests = remaining;
});
client.channel.on(events.game.mineTriggered, mineOwner => {
// You stepped on `mineOwner`'s mine.
console.log(`triggered mine belonging to ${mineOwner}!`);
});
client.channel.on(events.query.cellAttacked, cell => {
// The `cell`'s health just dropped (could have been caused by you).
console.log(`cell ${cell.cellReference} attacked!`);
});
client.channel.on(events.query.cellDefended, cell => {
// The `cell`'s health just increased (could have been caused by you).
console.log(`cell ${cell.cellReference} defended!`);
});
client.channel.on(events.query.cellConquered, cell => {
// The `cell` just became owned by someone new (could now be you).
console.log(`cell ${cell.cellReference} conquered!`);
});
client.channel.on(events.query.gridUpdate, (oldGrid, newGrid) => {
/*
* Every time we get a grid update (every 5s), play a potential strategy.
* Pass in the old and new grid states, in case our strategy will analyse them.
* Also pass in `gameState` which keeps track of remaining requests and the socket.
*/
playStrategy(oldGrid, newGrid, gameState);
});
const playStrategy = (oldGrid, newGrid, gameState) => {
/*
* In this example strategy, we just focus on acquiring cell 0,1.
*
* First we need to make sure we haven't yet owned it, or we'd be
* attacking our own cell.
*
* As long as it's owned by someone else, we hit it with enough
* attacks to conquer it.
*
*/
const cell = newGrid[1][0];
if (gameState.requests === 0) {
// We have no requests left, so return and wait for next run.
return;
}
if (cell.owner.name === MY_TEAM_NAME) {
// We now own the cell, nothing more to do.
return;
}
// We don't own it yet, so work out how many attacks to send.
const requiredAttacks = cell.health < gameState.requests ? cell.health : gameState.requests;
// Fire!
for (let i = 0; i < requiredAttacks; i++) {
attack(client, '0,1');
}
};
client.connect({
host: 'localhost',
port: 9002,
key: MY_TEAM_KEY
});