summaryrefslogtreecommitdiff
path: root/server.js
blob: 618e03365bb39bb27e92ffefb1ad75d5be20ef42 (plain)
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
#!/usr/bin/env node

const net = require("net");
const util = require("util");
const protocol = require("./protocol.js");

if (process.argv.length != 4) {
	console.log("Usage: ./server.js <hostname> <port>");
	console.log("Pass address of doomrooms server as arguments.");
	process.exit(1);
}

const GAME_ID = "tictactoe-tom";
const GAME_NAME = "Tic Tac Toe";

function messageHandler(msg) {
	console.log("Message:", msg);
}

async function registerGame(conn) {
	let msg = await conn.send(new protocol.Message("make-game", GAME_ID, GAME_NAME));
	if (msg.err) {
		msg = await conn.send(new protocol.Message("attach-game", GAME_ID, false));
		if (msg.err) {
			console.log("Game already managed by another gameserver, and not forcing");
			process.exit(1);
		}
	}
}

const conn = new protocol.Connection(net.createConnection(+process.argv[3], process.argv[2]), messageHandler);
conn.conn.on("connect", () => {
	console.log("Connected to roomserver");

	conn.conn.on("end", () => {
		console.log("Connection with roomserver unexpectedly closed!");
		process.exit(1);
	});

	registerGame(conn);
});