-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
230 lines (213 loc) · 9.79 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
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
const http = require('http');
const express = require('express');
const socketIo = require('socket.io');
const mysql = require('mysql2');
const dotenv = require('dotenv');
const path = require('path');
const PORT = process.env.PORT || 3000; // Render uses dynamic ports, so we use process.env.PORT
const HOST = '0.0.0.0';
dotenv.config({ path: '.env-online' });
const app = express();
const server = http.createServer(app);
const io = socketIo(server, {
cors: {
origin: '*',
methods: ['GET', 'POST']
}
});
// Start the server
server.listen(PORT, HOST, () => {
console.log(`Server running on http://${HOST}:${PORT}`);
});
// Serve static files
app.use(express.static(path.join(__dirname, 'public')));
// Serve the main page
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Database connection
const db = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
port: process.env.DB_PORT
});
db.connect(err => {
if (err) throw err;
console.log('Database connected...');
});
io.on('connection', (socket) => {
socket.on('createGame', (callback) => {
const gameId = generateGameId();
const query = 'INSERT INTO games (game_id, player1_id, current_turn, status) VALUES (?, ?, ?, ?)';
db.query(query, [gameId, socket.id, socket.id, 'ongoing'], (err, result) => {
if (err) throw err;
callback({ success: true, gameId, playerId: socket.id });
});
});
socket.on('resigned', (gameId, player_resigned_id) => {
let info = 'SELECT * FROM games WHERE game_id = ?';
db.query(info, [gameId], (err, results) => {
if (err) throw err;
let query = 'UPDATE games SET status = ? WHERE game_id = ?';
db.query(query, ['finished', gameId], (err) => {
if (err) throw err;
});
let query2 = 'UPDATE games SET winner_id = ? WHERE game_id = ?';
db.query(query2, [player_resigned_id, gameId], (err) => {
if (err) throw err;
});
io.to(gameId).emit('playerResigned', { playerId: player_resigned_id });
});
})
socket.on('joinGame', (gameId,callback) => {
const query2 = 'SELECT * FROM games WHERE game_id = ?';
const query = 'SELECT player2_id FROM games WHERE game_id = ?';
db.query(query, [gameId], (err, results) => {
if (err) throw err;
if (results.length === 0) {
callback({ success: false, message: 'Game does not exist' });
} else if (results[0].player2_id) {
callback({ success: false, message: 'Game is full' });
} else {
const updateQuery = 'UPDATE games SET player2_id = ? WHERE game_id = ?';
db.query(updateQuery, [socket.id, gameId], (updateErr) => {
if (updateErr) throw updateErr;
callback({ success: true, gameId, playerId: socket.id });
db.query(query2, [gameId], (err, results2) => {
if (err) throw err;
io.to(results2[0].player1_id).emit('gameStart', { gameId, player1Id: results2[0].player1_id, player2Id: socket.id });
io.to(socket.id).emit('gameStart', { gameId, player1Id: results2[0].player2_id, player2Id: socket.id });
});
});
}
});
});
socket.on('playerMove', ({ gameIdLocal, x, y, mark, win }) => {
if (win) {
if (mark == 'x') {
let winnerQuery = 'SELECT * FROM games WHERE game_id = ?';
db.query(winnerQuery, [gameIdLocal], (err, winner_info) => {
if (err) throw err;
let winQuery = "UPDATE games SET status = 'finished' WHERE game_id = ?";
let winQuery2 = 'UPDATE games SET winner_id = ? WHERE game_id = ?';
db.query(winQuery, [gameIdLocal]);
db.query(winQuery2, [winner_info[0].player1_id,gameIdLocal]);
});
} else {
let winnerQuery = 'SELECT * FROM games WHERE game_id = ?';
db.query(winnerQuery, [gameIdLocal], (err, winner_info) => {
if (err) throw err;
let winQuery = "UPDATE games SET status = 'finished' WHERE game_id = ?";
let winQuery2 = 'UPDATE games SET winner_id = ? WHERE game_id = ?';
db.query(winQuery, [gameIdLocal]);
db.query(winQuery2, [winner_info[0].player2_id,gameIdLocal]);
});
}
}
const query = 'INSERT INTO moves (game_id, player_id, x_coordinate, y_coordinate, mark) VALUES (?, ?, ?, ?, ?)';
const query2 = 'SELECT * FROM games WHERE game_id = ?';
db.query(query, [gameIdLocal, socket.id, x, y, mark], (err, result) => {
if (err) throw err;
db.query(query2, [gameIdLocal], (err,results3) => {
if (err) throw err;
if (results3[0].current_turn == results3[0].player1_id) {
io.to(results3[0].player2_id).emit('playerMoved', { playerId: results3[0].player1_id, x, y, mark });
let query4 = 'UPDATE games SET current_turn = ? WHERE game_id = ?';
socket.emit('playerMove' , { playerId: results3[0].player1_id, x, y, mark })
db.query(query4, [results3[0].player2_id, gameIdLocal], (err) => {
if (err) throw err;
});
} else {
io.to(results3[0].player1_id).emit('playerMoved', { playerId: results3[0].player2_id, x, y, mark });
let query4 = 'UPDATE games SET current_turn = ? WHERE game_id = ?';
socket.emit('playerMove', { playerId: results3[0].player2_id, x, y, mark });
db.query(query4, [results3[0].player1_id, gameIdLocal], (err) => {
if (err) throw err;
});
}
});
});
socket.emit('showMove', { x, y, mark });
});
socket.on('rematchGame', (gameIdLocal) => {
var rematches = 0;
let p1_id = '';
let p2_id = '';
let queryx = 'SELECT * FROM games WHERE game_id = ?';
db.query(queryx, gameIdLocal, (err,resultx) => {
if (err) throw err;
p1_id = resultx[0].player1_id;
p2_id = resultx[0].player2_id;
rematches = resultx[0].rematches;
rematches = rematches + 1;
let query3 = 'UPDATE games SET rematches = ? WHERE game_id = ?';
db.query(query3, [rematches, gameIdLocal], (err) => {
if (err) throw err;
if (rematches == 2) {
const gameId = generateGameId();
let query2 = 'INSERT INTO games (game_id, player1_id, player2_id,current_turn, status) VALUES (?, ?, ?, ?, ?)';
db.query(query2, [gameId, p1_id, p2_id, p1_id, 'ongoing'], (err, result) => {
if (err) throw err;
});
io.to(p1_id).emit('AddOneRematchRequest', { gameId: gameIdLocal });
io.to(p2_id).emit('AddOneRematchRequest', { gameId: gameIdLocal });
io.to(p1_id).emit('gameStart', { gameId, player1Id: p1_id, player2Id: p2_id });
io.to(p2_id).emit('gameStart', { gameId, player1Id: p1_id, player2Id: p2_id });
} else {
io.to(p1_id).emit('AddOneRematchRequest', { gameId: gameIdLocal });
io.to(p2_id).emit('AddOneRematchRequest', { gameId: gameIdLocal });
}
})
})
})
socket.on('disconnect', () => {
let p2;
let search = "SELECT * FROM games WHERE status = ? AND player1_id = ?";
let search2 = "SELECT * FROM games WHERE status = ? AND player2_id = ?";
let updateQuery = "UPDATE games SET status = 'finished' WHERE player1_id = ?";
let updateQuery2 = "UPDATE games SET status = 'finished' WHERE player2_id = ?";
let winnerQuery = "UPDATE games SET winner_id = ? WHERE player1_id = ?";
let winnerQuery2 = "UPDATE games SET winner_id = ? WHERE player2_id = ?";
db.query(search, ['ongoing', socket.id], (err,result) => {
if (err) throw err;
if (result.length !== 0) {
// just disconnect the player
} else
if (result[0]) {
io.to(result[0].player2_id).emit('opponentResigned');
db.query(updateQuery, [socket.id], (err2) => {
if (err2) throw err2;
});
db.query(winnerQuery, [result[0].player2_id,socket.id], (err3) => {
if (err3) throw err3;
});
} else {
db.query(search2, ['ongoing',socket.id], (err4,result2) => {
if (err4) throw err4;
if (result2[0]) {
io.to(result2[0].player1_id).emit('opponentResigned');
db.query(updateQuery2, [socket.id], (err5) => {
if (err5) throw err5;
});
db.query(winnerQuery2, [result2[0].player2_id,socket.id], (err6) => {
if (err6) throw err6;
});
}
});
}
})
});
});
function generateGameId() {
let chars = '0123456789';
let gameId = '';
for (let i = 0; i < 5; i++) {
gameId += chars.charAt(Math.floor(Math.random() * chars.length));
}
return gameId;
}
server.listen(3000, () => {
console.log('Server is running on port 3000');
});