-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (130 loc) · 4.07 KB
/
index.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
const express = require("express");
const http = require("http");
const socket = require("socket.io");
const cors = require("cors");
const app = express();
const server = http.createServer(app);
const io = socket(server, {
cors: {
origin: "*",
},
});
const PORT = process.env.PORT || 5000;
// const {addUser, removeUser, getUser, getUsersInRoom} = require('./user');
const route = require("./routes");
const { use } = require("./routes");
const {
addUser,
removeUser,
getUser,
getOtherUser,
userMove,
otherUserMove,
getUsersInRoom,
} = require("./Player");
app.use(route);
app.use(cors());
// app.use('*', (req, res, next) => {
// res.header('Access-Control-Allow-Origin', '*');
// next();
// })
io.on("connection", (socket) => {
console.log("Connected!");
socket.on("join", ({ Name, room }, callback) => {
// console.log(name, room);
const { error, user } = addUser({ id: socket.id, Name, room });
if (error) return callback({ error: error });
if (!user) return callback({ error: "Something Went Wrong" });
if (!user.name || !user.room || !socket || !socket.id)
return callback({ error: "Something Went Wrong" });
const otherUser = getOtherUser(socket.id, user.room);
let otherUserName;
console.log(otherUser);
if (otherUser) otherUserName = otherUser.name;
socket.emit("message", {
user: "admin",
text: `${user.name}, welcome to the room`,
otherUser: otherUserName,
});
// socket.emit("symbol", {Symbol: user.symbol})
socket.broadcast
.to(user.room)
.emit("message", { user: "admin", text: `${user.name}, has joined` });
socket.join(user.room);
console.log(getUsersInRoom(user.room), "PLAYERS");
io.to(user.room).emit("roomData", {
room: user.room,
users: getUsersInRoom(user.room),
});
callback({ symbol: user.symbol });
});
// socket.on('sendMessage', (message, callback) => {
// const user = getUser(socket.id);
// if(!user || !socket || !user.room || !socket.id){
// callback("Something Went Wrong.")
// }
// io.to(user.room).emit('message', {user: user.name, text: message});
// io.to(user.room).emit('roomData', {room: user.room, users: getUsersInRoom(user.room)});
// callback();
// })
socket.on("move", ({ row, col }, callback) => {
const user = getUser(socket.id);
// console.log(user);
if (!user || !socket || !user.room || !socket.id) {
return callback("Something went wrong");
}
const otherUser = getOtherUser(socket.id, user.room);
if (!otherUser) {
return callback("Please wait for player use to join");
}
const Score = userMove(socket.id, row, col);
const Board2 = otherUserMove(otherUser.id, row, col);
// console.log(Score.board);
if (Score.winner) {
io.to(user.room).emit("moveMade", {
user: user.name,
otherUser: otherUser.name,
board: Score.board,
winner: user.name,
message: `${user.name} won the game`,
});
} else if (Score.draw) {
io.to(user.room).emit("moveMade", {
user: user.name,
otherUser: otherUser.name,
board: Score.board,
draw: true,
message: `This match is a draw`,
});
} else {
io.to(user.room).emit("moveMade", {
user: user.name,
otherUser: otherUser.name,
board: Score.board,
message: `${user.name} turn is over. Now it's${otherUser.name}'s turn.`,
});
}
});
socket.on("disconnect", () => {
console.log("User has left!");
const user = removeUser(socket.id);
if (user) {
io.to(user.room).emit("message", {
user: "admin",
text: `${user.name} has left`,
leave: true,
});
}
});
});
// app.get('/', (req, res) => {
// res.send("HELLO WORLD!")
// })
if (process.env.NODE_ENV == "production") {
app.use(express.static("client/build"));
const path = require("path");
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
server.listen(PORT, () => console.log("Server running"));