Skip to content

Commit 247c2f5

Browse files
committed
Implement 'node start-game'
1 parent d2550a7 commit 247c2f5

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

Diff for: assets/tm_settings_2p_game.json

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"players": [
3+
{
4+
"index": 1,
5+
"name": "Red",
6+
"color": "red",
7+
"beginner": false,
8+
"handicap": 0,
9+
"first": false
10+
},
11+
{
12+
"index": 2,
13+
"name": "Green",
14+
"color": "green",
15+
"beginner": false,
16+
"handicap": 0,
17+
"first": true
18+
}
19+
],
20+
"corporateEra": true,
21+
"prelude": false,
22+
"draftVariant": true,
23+
"showOtherPlayersVP": false,
24+
"venusNext": false,
25+
"colonies": false,
26+
"turmoil": false,
27+
"customCorporationsList": [],
28+
"customColoniesList": [],
29+
"cardsBlackList": [],
30+
"board": "tharsis",
31+
"seed": 0.9599998682049586,
32+
"solarPhaseOption": false,
33+
"promoCardsOption": false,
34+
"communityCardsOption": false,
35+
"aresExtension": false,
36+
"undoOption": false,
37+
"fastModeOption": false,
38+
"removeNegativeGlobalEventsOption": false,
39+
"includeVenusMA": true,
40+
"startingCorporations": 2,
41+
"soloTR": false,
42+
"initialDraft": false,
43+
"randomMA": "No randomization",
44+
"shuffleMapOption": false,
45+
"beginnerOption": false,
46+
"randomFirstPlayer": true,
47+
"requiresVenusTrackCompletion": false
48+
}

Diff for: assets/tm_settings_solo_game.json

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"players": [
3+
{
4+
"index": 1,
5+
"name": "Bot",
6+
"color": "red",
7+
"beginner": false,
8+
"handicap": 0,
9+
"first": true
10+
}
11+
],
12+
"corporateEra": true,
13+
"prelude": false,
14+
"draftVariant": true,
15+
"showOtherPlayersVP": false,
16+
"venusNext": false,
17+
"colonies": false,
18+
"turmoil": false,
19+
"customCorporationsList": [],
20+
"customColoniesList": [],
21+
"cardsBlackList": [],
22+
"board": "tharsis",
23+
"seed": 0.28529731680252757,
24+
"solarPhaseOption": false,
25+
"promoCardsOption": false,
26+
"communityCardsOption": false,
27+
"aresExtension": false,
28+
"undoOption": false,
29+
"fastModeOption": false,
30+
"removeNegativeGlobalEventsOption": false,
31+
"includeVenusMA": true,
32+
"startingCorporations": 2,
33+
"soloTR": false,
34+
"initialDraft": false,
35+
"randomMA": "No randomization",
36+
"shuffleMapOption": false,
37+
"beginnerOption": false,
38+
"randomFirstPlayer": true,
39+
"requiresVenusTrackCompletion": false
40+
}

Diff for: lib/request.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright © 2020 Jan Keromnes.
2+
// The following code is covered by the MIT license.
3+
4+
const http = require('http');
5+
const https = require('https');
6+
7+
module.exports = async (method, url, data) => {
8+
const protocols = { http, https };
9+
const client = url.includes('://') ? protocols[url.split('://')[0]] : http;
10+
if (!client) {
11+
throw new Error(`Unsupported protocol: ${url}`);
12+
}
13+
return new Promise((resolve, reject) => {
14+
const req = client.request(url, { method }, res => {
15+
if (res.statusCode >= 400) {
16+
reject(new Error(`Couldn't ${method} ${url} - Response status: ${res.statusCode}`));
17+
return;
18+
}
19+
let body = '';
20+
res.on('data', chunk => { body += chunk; });
21+
res.on('error', error => { reject(error); });
22+
res.on('end', () => {
23+
try {
24+
resolve(JSON.parse(body));
25+
} catch (_) {
26+
resolve(body);
27+
}
28+
});
29+
}).on('error', error => {
30+
reject(error);
31+
});
32+
req.on('error', error => { reject(error); });
33+
if (data) {
34+
req.write(JSON.stringify(data, null, 2));
35+
}
36+
req.end();
37+
});
38+
};

Diff for: start-game.js

+9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright © 2020 Jan Keromnes.
22
// The following code is covered by the MIT license.
33

4+
const fs = require('fs');
45
const minimist = require('minimist');
6+
const request = require('./lib/request');
57

68
const usage = `Usage: node start-game [SERVER]`;
79
const argv = minimist(process.argv.slice(2));
@@ -13,3 +15,10 @@ if (argv.help || argv._.length > 1) {
1315

1416
const serverUrl = argv._[0] || 'http://localhost:8080';
1517
console.log('Server URL:', serverUrl);
18+
19+
const settings = JSON.parse(fs.readFileSync('./assets/tm_settings_solo_game.json', 'utf-8'));
20+
21+
(async () => {
22+
const data = await request('PUT', `${serverUrl}/game`, settings);
23+
console.log('Started new game:', data);
24+
})();

0 commit comments

Comments
 (0)