aboutsummaryrefslogtreecommitdiff
path: root/server.js
blob: 81df97c5fbb97d0e5c5871ef2993a7ba9313ee1a (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
#!/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], started: Bool}
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("startBall");
					gameobj.pl[1].emit("start");
				}, 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};
			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);
			}
		}
	});
});

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