summaryrefslogtreecommitdiff
path: root/capturego_server.js
blob: b2b9585ea0e8cd35910427a99dddca5cc0b72a83 (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
#!/usr/bin/env node

const naampje=require("naampje");

const PORT=15937;  // parseInt("cap",36)

// Game: {id: Int, size: Int, board: Board, onturn: Int, players: [Player]}
//   'onturn' is index into 'players', or -1 if game is finished
// Player: {id: Int, name: String, socket: SocketIO_Socket, games: [Game]}
// Board: [(0,1,-1) * (size*size)]
const games=new Map();  // game_id => Game
const players=new Map();  // player_id => Player

{let id=0; function uniqid(){return id++;}}

function mkPlayer(socket){
	const id=uniqid();
	let name;
	while(true){try {name=naampje.name();} catch(e){continue;} break;}
	return {id, name, socket, games: []};
}

function mkGame(size,players){
	const id=uniqid();
	const board=new Array(size*size).fill(0);
	return {id, size, board, onturn: 0, players};
}

function safePlayer(p){
	return {id: p.id, name: p.name, games: p.games.map(g=>g.id)};
}

function safeGame(g){
	return {id: g.id, size: g.size, board: g.board, onturn: g.onturn, players: g.players.map(safePlayer)};
}

function playerLeavesGame(player,game){
	game.onturn=-1;
	const newp=[];
	for(const p of game.players){
		if(p!=player){
			p.socket.emit("game_leave",safeGame(game),safePlayer(player));
			newp.push(p);
		}
	}
	if(newp.length==0)games.delete(game.id);
	else game.players=newp;
}

// Returns winning player (0 or 1) or -1 if none yet
function gameBoardFinished(game){
	const B=game.board,S=game.size;
	for(let i=0;i<S*S;i++){
		if(B[i]==0)continue;
		const flags=new Array(S*S).fill(false);
		const queue=[i];
		let nb0=false;
		while(queue.length){
			const at=queue.shift(),x=at%S,y=~~(at/S);
			flags[at]=true;
			if(x>0&&!flags[at-1]){if(B[at-1]==B[i])queue.push(at-1); else if(B[at-1]==0){nb0=true; break;}}
			if(y>0&&!flags[at-S]){if(B[at-S]==B[i])queue.push(at-S); else if(B[at-S]==0){nb0=true; break;}}
			if(x<S-1&&!flags[at+1]){if(B[at+1]==B[i])queue.push(at+1); else if(B[at+1]==0){nb0=true; break;}}
			if(y<S-1&&!flags[at+S]){if(B[at+S]==B[i])queue.push(at+S); else if(B[at+S]==0){nb0=true; break;}}
		}
		if(!nb0)return 1-B[i]==1?1:0;
	}
	return -1;
}


const app=require("express")();
const httpServer=require("http").Server(app);
const io=require("socket.io")(httpServer);
httpServer.listen(PORT,()=>console.log(`Listening on port ${PORT}`));


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

io.on("connection",(socket)=>{
	const player=mkPlayer(socket);
	console.log(`connect ${player.id}`);
	players.set(player.id,player);
	for(const p of players.values()){
		if(p!=player)p.socket.emit("player_list_change");
	}

	socket.on("my_info",(cb)=>{
		if(typeof cb!="function")return;
		cb(safePlayer(player));
	});

	socket.on("player_info",(pi,cb)=>{
		if(typeof cb!="function")return;
		const p=players.get(pi);
		if(!p)cb(null);
		else cb(safePlayer(p));
	});

	socket.on("list_players",(cb)=>{
		if(typeof cb!="function")return;
		const l=[];
		for(const p of players.values()){
			l.push(safePlayer(p));
		}
		cb(l);
	});

	socket.on("disconnect",()=>{
		console.log(`disconnect ${player.id}`);
		for(const g of player.games)playerLeavesGame(player,g);
		players.delete(player.id);
		for(const p of players.values()){
			p.socket.emit("player_list_change");
		}
	});

	socket.on("create_game",(otherid,size)=>{
		const otherp=players.get(+otherid);
		if(!otherp){
			socket.emit("err",`Player with id ${otherid} not found`);
			return;
		}
		if(typeof size!="number"||isNaN(size)||size<5||size>99||size%1!=0)return;

		const arr=Math.random()<0.5?[player,otherp]:[otherp,player];
		const game=mkGame(size,arr);
		player.games.push(game);
		otherp.games.push(game);
		games.set(game.id,game);

		for(const p of game.players){
			p.socket.emit("game_create",safeGame(game));
		}
		game.players[0].socket.emit("game_turn",safeGame(game));
		game.players[1].socket.emit("game_other_turn",safeGame(game));
	});

	socket.on("leave_game",(gi)=>{
		const g=games.get(gi);
		if(!g)return;
		if(player.games.indexOf(g)==-1)return;
		playerLeavesGame(player,g);
	});

	socket.on("game_move",(gi,idx)=>{
		const g=games.get(gi);
		if(!g)return;
		if(player.games.indexOf(g)==-1)return;
		if(typeof idx!="number"||isNaN(idx)||idx<0||idx>=g.size*g.size||idx%1!=0){
			socket.emit("err","Invalid position sent");
			return;
		}
		if(g.players[g.onturn]!=player){
			socket.emit("err","Not on turn");
			return;
		}
		if(g.board[idx]!=0){
			socket.emit("err","Position already taken");
			return;
		}
		g.board[idx]=[1,-1][g.onturn];
		const win=gameBoardFinished(g)
		if(win!=-1){
			for(const p of g.players){
				p.socket.emit("game_win",safeGame(g),safePlayer(g.players[win]));
			}
		} else {
			g.onturn=1-g.onturn;
			g.players[g.onturn].socket.emit("game_turn",safeGame(g));
			g.players[1-g.onturn].socket.emit("game_other_turn",safeGame(g));
		}
	});
});