-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
219 lines (202 loc) · 5.85 KB
/
index.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env node
'use strict';
const program = require('commander');
const fs = require('fs');
const game = require('./game');
const os = require('os');
const path = require('path');
const request = require('request-promise-native');
const SERVER = 'http://grot-server.games.stxnext.pl';
const TOKEN_FILE = path.join(os.homedir(), '.grot_token');
const TOKEN_LENGTH = 36;
program
.option('--debug', 'debug flag')
.option('--proxy <proxyUrl>', 'proxy url');
program
.command('register <token>')
.description('Register your unique token')
.action(function(token) {
try {
validateToken(token);
fs.writeFileSync(TOKEN_FILE, token, {'encoding': 'utf-8'});
} catch (error) {
console.error(`
${error.message}
Sign in to ${SERVER} to get your token.
`);
process.exit(1);
}
console.log('Token have been saved.');
});
program
.command('new_room <title>')
.description('Create new game room')
// .option('--title <title>', 'Room title')
.option('--board-size <boardSize>', 'GROT board size', 5)
.option('--max-players <maxPlayers>', 'Maximum players in the room', 15)
.option('--allow-multi <allowMulti>', 'Allow users to connect multiple time with the same token', false)
.option('--auto-start [autoStart]', 'Automatically start game after X minutes', 5)
.option('--auto-restart [autoRestart]', 'Automatically clear results after X minutes', 5)
.action(function(title, command){
newRoom(
command.parent.proxy,
command.maxPlayers,
command.autoStart,
command.autoRestart,
false,
title,
command.boardSize,
command.allowMulti
)
.then(roomId => console.log(`New game room_id is ${roomId}`))
});
program
.command('remove <roomId>')
.description('Remove game room')
.action(removeRoom);
program
.command('start <roomId>')
.description('Start game')
.action(function (roomId) {
const url = `${SERVER}/games/${roomId}`;
const token = getToken();
const payload = {'token': token};
const options = {
'json': true,
'body': payload
};
request.post(url, options)
.catch(error => {
console.error(error.message);
process.exit(1);
});
});
program
.command('join <roomId>')
.description('Join game room and wait for start')
.option('--alias <userAlias>', 'Added to your name displayed on results page.')
.action(function (roomId, command) {
const token = getToken();
const gameUrl = `${SERVER}/games/${roomId}/board?token=${token}`
+ (command.userAlias ? `&alias=${command.userAlias}` : '');
console.log(`Check game results ${SERVER}/games/${roomId}`);
game.play(gameUrl, command.parent.debug, command.parent.proxy)
.then(() => showResults(roomId));
});
program
.command('results <roomId>')
.description('Show game results')
.action(function (roomId) {
showResults(roomId);
});
program
.command('play_devel')
.description('Play one move in loop (development mode)')
.action(function (command) {
const token = getToken();
const gameUrl = `${SERVER}/games/000000000000000000000000/board?token=${token}`;
game.play(gameUrl, true, command.parent.proxy);
});
/**
* @TODO: fix bug: 404 after creating a room
*/
program
.command('play_vs_bot')
.description('Play full game against STX Bot')
.action(function (command) {
const token = getToken();
command.parent.debug && console.log('Creating new room');
newRoom(command.parent.proxy, 2, 1, null, true)
.then(roomId => {
try {
command.parent.debug && console.log(`Room ${roomId} created`);
const gameUrl = `${SERVER}/games/${roomId}/board?token=${token}`;
game.play(gameUrl, command.parent.debug, command.parent.proxy);
showResults(roomId);
} finally {
removeRoom(roomId)
}
})
});
program.parse(process.argv);
if (!program.args.length) program.help();
function getToken() {
try {
const token = fs.readFileSync(TOKEN_FILE, {'encoding': 'utf-8'});
validateToken(token);
return token;
} catch (error) {
console.error(`
${error.message}
Sign in to ${SERVER} to get your token.
Use 'node index.js register token' before using other commands.
`);
process.exit(1);
}
}
function validateToken(token) {
if (token.length !== TOKEN_LENGTH) {
throw new Error(`Invalid token ${token}`);
}
}
function newRoom(
proxy,
maxPlayers,
autoStart,
autoRestart,
withBot,
title,
boardSize,
allowMulti
) {
const url = `${SERVER}/games`;
const token = getToken();
const payload = {
'title': title || null,
'board-size': boardSize || 5,
'max-players': maxPlayers || 15,
'auto-start': autoStart || 5,
'auto-restart': typeof autoRestart !== 'undefined' ? autoRestart : 5,
'allow-multi': allowMulti || false,
'with-bot': withBot,
'token': token
};
const options = {
'json': true,
'forever': true,
'body': JSON.stringify(payload),
'proxy': proxy,
};
return request.post(url, options)
.then(body => JSON.parse(body).room_id)
.catch(error => {
console.error(`Failed to create new room. ${error.message}`);
process.exit(1);
});
}
function removeRoom(roomId) {
const token = getToken();
const url = `${SERVER}/games/${roomId}?token=${token}`;
request.delete(url)
.catch(error => {
console.error(error.message);
process.exit(1);
})
}
function showResults(roomId) {
const token = getToken();
const url = `${SERVER}/games/${roomId}/results/?token=${token}`;
const headers = {
'Accept': 'application/json'
};
const options = {
'headers': headers
};
request(url, options)
.then(response => {
const results = response.players.map(
(player, index) => `${index + 1}. ${player.login} - ${player.score}`
);
console.log(results.join('\n'));
})
}