-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·186 lines (156 loc) · 5.43 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
const GameServer = require("./gameserver");
const Games = require("./games.js");
const WebServer = require("./webserver");
const WebSocketGameServer = require("./clients/websocket");
const CLICreator = require("./clients/cli");
const http = require('http');
const url = require('url');
const fs = require("fs");
const readline = require('readline');
const process = require('process');
let config_file = "config.json";
if (process.argv.length > 2) {
config_file = process.argv.slice(2)[0];
}
let config = JSON.parse(fs.readFileSync(config_file));
let applicationConfig = config["application"];
let gameConfig = config["game"];
/**
* TODO
* command line arguments:
* --help
* prints help
* --gui
* enables the web gui
**/
let httpServer = null;
let webserver = null;
let websocketserver = null;
let clicreator = null;
let websocketBoardAObserverServer = null;
let websocketBoardBObserverServer = null;
const gameserver = new GameServer(() => {
return new Games.ChessCLIGame(gameConfig);
}, gameConfig);
gameserver.on("server.close", function() {
console.log("[application] closing the server");
if (httpServer !== null) {
httpServer.close();
}
process.exit(0);
});
gameserver.on("state.change", function() {
switch (gameserver.state) {
case "ready":
if (applicationConfig["start_if_enough_players_connected"] === true) {
console.log("[application] four players connected. starting the game!");
gameserver.processMessage("go");
} else {
console.log("[application] the game is ready to start. Type 'go' to start");
}
}
});
gameserver.on("communicator.remove", function(data) {
if (applicationConfig["disconnect_as_defeat"] === true) {
gameserver.defeat(data.client, "disconnected from the server");
}
});
function testSingleGame() {
if (applicationConfig["play_single_game_only"] === true) {
console.log("[application] server is set to single game mode");
gameserver.processMessage("close");
}
}
gameserver.on("game.end", function(bpgn) {
//TODO duplicated code, ... "close" should wait for the game to get saved ...
if (applicationConfig["save"]["save_games"] === true) {
gameserver.saveBpgn(applicationConfig["save"]["save_dir"], applicationConfig["save"]["filename"],
applicationConfig["save"]["meta"], (answer, filename) => {
console.log("[application] saved bpgn to "+filename);
testSingleGame();
});
} else {
testSingleGame();
}
});
if (config["httpServer"]["serve_webclient"] === true) {
webserver = new WebServer(config["httpServer"]);
}
if (config["httpServer"]["enabled"] === true) {
httpServer = http.createServer(webserver.getApp());
}
if (config["clients"]["websocket"]["enabled"] === true) {
websocketserver = new WebSocketGameServer();
websocketserver.on("client.new", (data) => {
let gameCommunicator = data.communicator;
let position = data.position;
if (!gameserver.addCommunicator(gameCommunicator, position)) {
client.close();
}
});
}
if (config["clients"]["cli"]["enabled"] === true) {
clicreator = new CLICreator(config["clients"]["cli"]);
clicreator.on("client.new", (data) => {
let communicator = data.communicator;
let position = data.position;
if (!gameserver.addCommunicator(communicator, position)) {
client.close();
}
});
}
//observers
if (config["observers"]["websocket"]["enabled"] === true) {
websocketBoardAObserverServer = new WebSocketGameServer();
websocketBoardAObserverServer.on("client.new", (client) => {
if (!gameserver.addObserver("a", client.communicator)) {
client.close();
}
});
websocketBoardBObserverServer = new WebSocketGameServer();
websocketBoardBObserverServer.on("client.new", (client) => {
if (!gameserver.addObserver("b", client.communicator)) {
client.close();
}
});
}
if (httpServer !== null) {
httpServer.on('upgrade', function upgrade(request, socket, head) {
const pathname = url.parse(request.url).pathname;
if (pathname === config["clients"]["websocket"]["path"]) {
let wss = websocketserver.getServer();
wss.handleUpgrade(request, socket, head, function done(ws) {
wss.emit('connection', ws, request);
});
} else if (pathname === config["observers"]["websocket"]["pathBoardA"]) {
let wss = websocketBoardAObserverServer.getServer();
wss.handleUpgrade(request, socket, head, function done(ws) {
wss.emit('connection', ws, request);
});
} else if (pathname === config["observers"]["websocket"]["pathBoardB"]) {
let wss = websocketBoardBObserverServer.getServer();
wss.handleUpgrade(request, socket, head, function done(ws) {
wss.emit('connection', ws, request);
});
} else {
console.log("rejected", pathname);
socket.destroy();
}
});
httpServer.listen(config["httpServer"]["port"]);
}
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
if (config["application"]["listen_to_stdin"]) {
rl.on('line', (line) => {
gameserver.processMessage(line.trim());
}).on('close', () => {
gameserver.processMessage("close");
});
}
if (clicreator !== null) {
clicreator.createClients();
}
console.log("Setup of the game server completed. Waiting for players to connect.");