-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
96 lines (87 loc) · 3.02 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
require("dotenv").config();
const COLORS = require("./server/shared/colors");
const express = require("express");
const app = express();
const cookieParser = require("cookie-parser");
const passport = require("passport");
const passportConf = require("./server/config/passport.conf");
const PORT = process.env.PORT || 8080;
const userRoutes = require("./server/routes/user.routes");
const server = require("http").createServer(app);
const io = require("socket.io")(server, {
cors: {
origin: "*",
},
});
io.on("connection", (socket) => {
const { roomNum } = socket.handshake.query;
console.log("Room Connected");
const randColor = COLORS[Math.floor(Math.random() * COLORS.length)];
socket.emit("userColor", { color: randColor });
// Join the specific room
socket.on("joinRoom", ({ username }) => {
socket.join(roomNum);
const time = new Date().toString().split(" ")[4];
io.in(roomNum).emit("chatMessage", {
username: "Game Master",
time: time,
msg: `${username} has joined the room`,
color: "yellowgreen",
});
});
// When message received, send to room
socket.on("chatMessage", (newMsg) => {
const time = new Date().toString().split(" ")[4];
io.in(roomNum).emit("chatMessage", { ...newMsg, time });
});
// When guess received, send to room
socket.on("sendGuess", ({ newGuess, wasHost }) => {
console.log("Received sendGuess from Front End successfuly", newGuess);
io.to(roomNum).emit("sendGuess", { newGuess, wasHost });
});
// When sunk ship is recieved, send to opponent
socket.on("sunkShip", ({ username, boat }) => {
const ships = ["destroyer", "submarine", "battleship", "aircraft carrier"]
const time = new Date().toString().split(" ")[4];
io.in(roomNum).emit("chatMessage", {
username: "Game Master",
time: time,
msg: `${username} has sunk the ${ships[boat - 2]}!`,
color: "crimson",
});
});
// When both players confirm, set boats ready?
socket.on("boatsReady", ({ boardData, wasHost }) => {
//
// have button emit boatsReady event
console.log("Received boatsReady from Front End");
io.in(roomNum).emit("boatsReady", { boardData, wasHost });
});
// When game is ended, send to room
// socket.on("gameEnd", (something) => {
// io.in(roomNum).emit("gameEnd", { something });
// });
// Leave the room on disconnect
socket.on("disconnect", ({ username }) => {
socket.leave(roomNum);
const time = new Date().toString().split(" ")[4];
io.in(roomNum).emit("chatMessage", {
username: "Game Master",
time: time,
msg: `Your opponent has left the room`,
color: randColor,
});
});
});
app.use(express.json());
passportConf(passport);
app.use(cookieParser());
app.use(passport.initialize());
app.use("/api/users", userRoutes);
app.use(express.static(__dirname + "/build"));
app.get("*", (req, res) => {
return res.sendFile("/build/index.html", { root: __dirname + "/" });
});
server.listen(PORT, () =>
console.log(`Hi there! Get ready to battle on port: ${PORT}`)
);