summaryrefslogtreecommitdiff
path: root/client.js
blob: 46fc0eb6816cfea0fb9ebd64a22d1b1c308dfc23 (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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env node

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

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

const GAME_ID = "tictactoe-tom";

keyboard.init();

function fatalError(err) {
	console.log("A fatal error occurred:");
	console.log(" ", err);
	process.exit(1);
}

function playerToString(player) {
	return player.nick;
}

function roomToString(room) {
	let res = `  '${room.name}' (${room.id})`;
	if (room.hidden) res += " (HIDDEN)";
	res += "\n";
	res += "  Players:";
	for (const player of room.players) {
		if (player.nick == room.admin) res += "\x1B[4m";
		res += " " + playerToString(player);
		if (player.nick == room.admin) res += "\x1B[0m";
	}
	return res;
}

async function showMenu(title, options) {
	console.log(title);
	for (let i = 0; i < options.length; i++) {
		console.log(" " + (i + 1) + ") " + options[i]);
	}
	while (true) {
		const line = await keyboard.prompt("> ");
		const num = parseInt(line.trim(), 10);
		if (num >= 1 && num <= options.length) {
			return num - 1;
			break;
		}
		console.log(
				"That's an invalid choice. " +
				"Please enter a number between 1 and " + options.length + ".");
	}
}

function messageHandler(msg) {
	console.log("ATTENTION: UNHANDLED INCOMING MESSAGE");
	console.log("Message: " + util.inspect(msg));
}


let currentRoom = null;
let currentGame = null;


// Returns: 0 for re-loop, 1 for quit, 2 for roomMenu
async function mainMenu() {
	const title = "Main Menu";
	const options = [
		"Create a room",
		"Join an existing room",
		"Search for rooms",
		"Quit",
	];

	switch (await showMenu(title, options)) {
		case 0: {
			const roomName = await keyboard.prompt("Room name to make: ");
			const shown = await showMenu("Should the room be shown in searches?", ["Yes", "No"]) == 0;
			const reply = await conn.send(new protocol.Message("make-room", roomName, !shown, {}));
			if (reply.err) {
				console.log("Error: " + reply.err);
			} else {
				currentRoom = reply.res;
				return 2;
			}
			break;
		}

		case 1: {
			const roomName = await keyboard.prompt("Room id to join: ");
			const reply = await conn.send(new protocol.Message("join-room", roomName));
			if (reply.err) {
				console.log("Error: " + reply.err);
			} else {
				currentRoom = reply.res;
				return 2;
			}
			break;
		}

		case 2: {
			const query = await keyboard.prompt("String to search for: ");
			const reply = await conn.send(new protocol.Message("search-rooms", query));
			if (reply.err) {
				console.log("Error: " + reply.err);
			} else {
				console.log(reply.res.length + " results:");
				for (const room of reply.res) {
					console.log(roomToString(room));
				}
			}
			break;
		}

		case 3:
			return 1;
	}
	return 0;
}

// Returns: 0 for re-loop, 1 for quit, 2 for mainMenu, 3 for gameMenu
async function roomMenu() {
	const title =
			"Room " + currentRoom.name +
			" (" + currentRoom.players.map(p => p.nick).join(", ") + ")";
	const options = [
		"Start a game (not fully implemented)",
		"Send room chat message",
		"Invite a player into this room",
		"Kick a player from the room",
		"Leave the room",
		"Quit",
	];

	switch (await showMenu(title, options)) {
		case 0: {
			const reply = await conn.send(new protocol.Message("start"));
			break;
		}

		case 1: {
			const line = await keyboard.prompt("Message to send: ");
			const reply = await conn.send(new protocol.Message("send-room-chat", line));
			if (reply.err) fatalError(reply.err);
			break;
		}

		case 2: {
			const name = await keyboard.prompt("Player name to invite: ");
			const reply = await conn.send(new protocol.Message("invite-player", name));
			if (reply.err) {
				console.log(err);
			} else {
				console.log("Player successfully invited.");
			}
			break;
		}

		case 3: {
			const name = await keyboard.prompt("Player name to kick: ");
			const reply = await conn.send(new protocol.Message("kick-player", name));
			if (reply.err) {
				console.log(err);
			} else {
				console.log("Player successfully kicked.");
			}
			break;
		}

		case 4: {
			const reply = await conn.send(new protocol.Message("leave-room"));
			if (reply.err) fatalError(reply.err);
			return 2;
		}

		case 5:
			return 1;
	}
}

const conn = new protocol.Connection(net.createConnection(+process.argv[3], process.argv[2]), messageHandler);
conn.conn.on("connect", async () => {
	try {
		console.log("Connected to Doomrooms.");
		let choice = await showMenu("Register or log in?", [
			"Register new account",
			"Log in to existing account",
			"Quit",
		]);
		if (choice == 2) process.exit(0);

		const username = await keyboard.prompt("Username: ");
		const password = await keyboard.promptPassword("Password: ");
		let msg;
		if (choice == 0) {
			msg = await conn.send(new protocol.Message("make-player", username, password));
		} else {
			msg = await conn.send(new protocol.Message("login", username, password));
		}
		if (msg.err) fatalError(msg.err);

		msg = await conn.send(new protocol.Message("set-game", GAME_ID));
		if (msg.err) fatalError(msg.err);

		let inGame = false;
		let doQuit = false;
		while (!doQuit) {
			if (inGame) {
				switch (await roomMenu()) {
					case 0: break;
					case 1: doQuit = true; break;
					case 2: inGame = false; break;
				}
			} else {
				switch (await mainMenu()) {
					case 0: break;
					case 1: doQuit = true; break;
					case 2: inGame = true; break;
				}
			}
		}
	} catch(e) {
		console.log(e);
	}

	conn.end();
	process.exit(0);
});