aboutsummaryrefslogtreecommitdiff
path: root/server.js
blob: 47896e857fb5eebcc649e8370376549566623d6f (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
#!/usr/bin/env node
const http = require("http");
const express = require("express");
const socketio = require("socket.io")

const PORT = 8080;

const app = express();
const server = http.Server(app);
const io = socketio(server);

// gameid -> {pl: [socket]*{1,2}, started: Bool, nextServe: Int, score: [Int, Int]}
const games = new Map();

function genId() {
	for (let i = 0; i < 10; i++) {
		// 2176782336 == 36^6
		const id = ("000000" + (2176782336 * Math.random() | 0).toString(36)).slice(-6);
		if (!games.has(id)) return id;
	}
	return "";
}

function validId(id) {
	return /^[0-9a-z]{6}$/.test(id);
}

app.get("/", (req, res) => {
	const gameid = genId();
	if (gameid == "") {
		res.sendStatus(500);
		return;
	}
	res.redirect("/game/" + gameid);
});

app.param("gameid", (req, res, next, gameid) => {
	if (!validId(gameid)) {
		res.sendStatus(404);
	} else {
		next();
	}
});

app.get("/game/:gameid", (req, res) => {
	res.sendFile(__dirname + "/game.html");
});

app.get("/game.css", (req, res) => res.sendFile(__dirname + "/game.css"));
app.get("/game.js", (req, res) => res.sendFile(__dirname + "/game.js"));

io.on("connection", (conn) => {
	let gameid, gameobj;
	conn.on("open", (id) => {
		console.log("open " + id);
		if (!validId(id)) {
			console.log("  invalid");
			conn.emit("redirect", "/");
			conn.disconnect(true);
			return;
		}
		gameobj = games.get(id);
		if (gameobj != undefined) {
			if (gameobj.pl.length == 1 && !gameobj.started) {
				console.log("  serve");
				gameid = id;
				gameobj.pl.push(conn);
				gameobj.pl[0].emit("status", "Get ready...");
				gameobj.pl[1].emit("status", "Get ready...");
				gameobj.pl[0].emit("serve", "left", 0.5, 0.5);
				gameobj.pl[1].emit("serve", "right", 0.5, 0.5);
				gameobj.started = true;
				setTimeout(() => {
					if (!gameobj || gameobj.pl.length != 2) return;
					gameobj.pl[0].emit("status", "");
					gameobj.pl[1].emit("status", "");
					gameobj.pl[0].emit(gameobj.nextServe == 0 ? "startBall" : "start");
					gameobj.pl[1].emit(gameobj.nextServe == 1 ? "startBall" : "start");
					gameobj.nextServe = 1 - gameobj.nextServe;
				}, 2000);
			} else {
				console.log("  full");
				gameobj = undefined;
				conn.emit("status", "Game full! Opening new game...");
				setTimeout(() => {
					conn.emit("redirect", "/");
					conn.disconnect(true);
				}, 2000);
			}
		} else {
			console.log("  new");
			gameid = id;
			gameobj = {pl: [conn], started: false, nextServe: 0, score: [0, 0]};
			games.set(gameid, gameobj);
			conn.emit("status", "Waiting for other player...");
		}
	});

	conn.on("disconnect", () => {
		if (gameobj == undefined) return;

		console.log("stop " + gameid);

		const newpl = [];
		for (const p of gameobj.pl) {
			if (p != conn) {
				p.emit("stop");
				p.emit("status", "Other player left.");
				setTimeout(() => {
					p.emit("redirect", "/");
					p.disconnect(true);
				}, 2000);
				newpl.push(p);
			}
		}

		if (newpl.length == 0) {
			console.log("  delete");
			games.delete(gameid);
		} else {
			gameobj.pl = newpl;
		}
	});

	conn.on("padvec", (pos, vel) => {
		if (!gameobj) return;
		for (const p of gameobj.pl) {
			if (p != conn) {
				p.emit("padvec", pos, vel);
			}
		}
	});

	conn.on("ballvec", (x, y, vx, vy) => {
		if (!gameobj) return;
		for (const p of gameobj.pl) {
			if (p != conn) {
				p.emit("ballvec", x, y, vx, vy);
			}
		}
	});

	conn.on("ballout", () => {
		if (!gameobj || gameobj.pl.length != 2) return;

		gameobj.pl[0].emit("stop");
		gameobj.pl[1].emit("stop");
		gameobj.pl[0].emit("status", "Ball out!");
		gameobj.pl[1].emit("status", "Ball out!");

		for (let i = 0; i < gameobj.pl.length; i++) {
			if (gameobj.pl[i] != conn) {
				gameobj.score[i]++;
			}
		}

		gameobj.pl[0].emit("score", gameobj.score);
		gameobj.pl[1].emit("score", gameobj.score);

		setTimeout(() => {
			if (!gameobj || gameobj.pl.length != 2) return;
			gameobj.pl[0].emit("status", "Get ready...");
			gameobj.pl[1].emit("status", "Get ready...");
			gameobj.pl[0].emit("serve", "left", 0.5, 0.5);
			gameobj.pl[1].emit("serve", "right", 0.5, 0.5);

			setTimeout(() => {
				if (!gameobj || gameobj.pl.length != 2) return;
				gameobj.pl[0].emit("status", "");
				gameobj.pl[1].emit("status", "");
				gameobj.pl[0].emit(gameobj.nextServe == 0 ? "startBall" : "start");
				gameobj.pl[1].emit(gameobj.nextServe == 1 ? "startBall" : "start");
				gameobj.nextServe = 1 - gameobj.nextServe;
			}, 2000);
		}, 2000);
	});
});

server.listen(PORT, () => console.log("Listening on port " + PORT));