-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
147 lines (135 loc) · 4.47 KB
/
server.js
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
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
require('dotenv').config();
const cors = require('cors');
const path = require('path');
app.use(cors());
const PORT = process.env.PORT || 5000;
let rooms = {};
const newBoard = () => {
return {
topLeft: '',
topMiddle: '',
topRight: '',
midLeft: '',
center: '',
midRight: '',
botLeft: '',
botMiddle: '',
botRight: '',
};
};
const calculateBoard = (b, cnt, current) => {
if (
(b.topLeft === b.topMiddle &&
b.topMiddle === b.topRight &&
b.topLeft !== '') ||
(b.midLeft === b.center && b.center === b.midRight && b.midLeft !== '') ||
(b.botLeft === b.botMiddle &&
b.botMiddle === b.botRight &&
b.botLeft !== '') ||
(b.topLeft === b.midLeft && b.midLeft === b.botLeft && b.topLeft !== '') ||
(b.topMiddle === b.center &&
b.center === b.botMiddle &&
b.topMiddle !== '') ||
(b.topRight === b.midRight &&
b.midRight === b.botRight &&
b.topRight !== '') ||
(b.topLeft === b.center && b.center === b.botRight && b.topLeft !== '') ||
(b.topRight === b.center && b.center === b.botLeft && b.topRight !== '')
) {
return { status: `${current} Wins` };
} else if (cnt >= 9) return { status: 'Match Draw' };
else return { status: 'playing' };
};
io.on('connection', (socket) => {
console.log('New connection');
socket.on('join room', ({ id }) => {
if (!rooms[id]) {
rooms = { ...rooms, [id]: [socket.id] };
socket.join(id);
socket.emit('message', 'Welcome to the game');
socket.broadcast.to(id).emit('message', 'A player joined');
io.to(id).emit('playersCount', rooms[id].length);
} else {
if (rooms[id].length >= 2) {
socket.emit('message', 'Lobby full');
} else {
rooms[id].unshift(socket.id);
socket.join(id);
socket.emit('message', 'Welcome to the game');
socket.broadcast.to(id).emit('message', 'A player joined');
io.to(id).emit('playersCount', rooms[id].length);
}
}
socket.on('start', () => {
if (rooms[id].length == 2) {
rooms[id + 'board'] = newBoard();
rooms[id + 'count'] = 0;
if (rooms[id + 'current'] == null) rooms[id + 'current'] = rooms[id][0];
io.to(id).emit('set current move', rooms[id + 'current']);
}
});
socket.on('reset game', () => {
if (rooms[id].length == 2) {
rooms[id + 'board'] = newBoard();
rooms[id + 'count'] = 0;
rooms[id + 'current'] = rooms[id][0];
io.to(id).emit('game board', {
gameState: rooms[id + 'board'],
current: rooms[id + 'current'],
});
io.to(id).emit('message', '');
}
});
socket.on('made move', ({ tile_name }) => {
if (rooms[id].length == 2) {
if (rooms[id + 'current'] == rooms[id][0])
rooms[id + 'current'] = rooms[id][1];
else if (rooms[id + 'current'] == rooms[id][1])
rooms[id + 'current'] = rooms[id][0];
if (socket.id === rooms[id][0]) {
rooms[id + 'board'] = { ...rooms[id + 'board'], [tile_name]: 'X' };
} else if (socket.id === rooms[id][1]) {
rooms[id + 'board'] = { ...rooms[id + 'board'], [tile_name]: 'O' };
}
rooms[id + 'count'] += 1;
var res = calculateBoard(
rooms[id + 'board'],
rooms[id + 'count'],
rooms[id + 'current'] === rooms[id][0] ? 'O' : 'X' //Reversing because move is already changed above
);
io.to(id).emit('message', '');
if (res.status !== 'playing') {
io.to(id).emit('message', res.status);
}
io.to(id).emit('game board', {
gameState: rooms[id + 'board'],
current: rooms[id + 'current'],
});
}
});
});
socket.on('disconnect', () => {
Object.keys(rooms).map((key) => {
if (
key.indexOf('board') === -1 &&
key.indexOf('current') === -1 &&
key.indexOf('count') === -1
) {
rooms[key] = rooms[key].filter((id) => id !== socket.id);
}
});
console.log('User left');
});
});
if (process.env.NODE_ENV === 'production') {
//Set static folder
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
server.listen(PORT, () => console.log(`Server is running at port ${PORT}`));