forked from matthew-sheehan/guess-what-be-copy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
158 lines (124 loc) · 4.11 KB
/
app.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
const express = require("express");
const app = express();
const server = require("http").createServer(app);
const { getAliens } = require("./controllers/aliens.controllers");
const { getEndpoints } = require("./controllers/api.controllers");
const { getQuestions } = require("./controllers/questions.controllers");
const { getUsers, postUsers } = require("./controllers/users.controllers");
const {
handleMongoErrors,
handleCustomErrors,
handleInternalErrors,
} = require("./errors/errors");
const cors = require("cors");
const connectDB = require("./db/connectMongo");
console.log("ON THE COPY VERSION");
const port = process.env.PORT || 8080;
const io = require("socket.io")(server, {
cors: {
origin: [
"https://peaceful-cupcake-57dcec.netlify.app",
"https://spontaneous-valkyrie-34ea0c.netlify.app",
"https://guess-what-gitkermit.netlify.app",
"http://localhost:3000",
],
},
});
server.listen(port, () => {
console.log(`listening on port ${port}`);
});
let arr = [];
let playingArray = [];
let alienArray = [];
let resetGameArray = [];
let newBoard = {};
let turnCounter = 0;
console.log(arr, "<<<<<< arr");
console.log(playingArray, "<<<<< playArray");
console.log(alienArray, "<<<<< alienArray");
console.log("backend log");
io.on("connection", (socket) => {
console.log("socket connection successful");
socket.on("find", (e) => {
socket.emit("your-socketid", socket.id);
console.log(socket.id, "<<<<<socket.id");
console.log(e, "<<<<<<<< e");
console.log(e.name, "<<<<<<< e.name");
console.log(typeof e.name, "<<<<<<< type of e.name");
if (e.name !== null && e.name !== "") {
console.log(arr, "<<<<<<<< arr before check and push");
arr.forEach((item, index) => {
if (item.name === e.name) {
arr.splice(index, 1);
index -= 1;
}
});
arr.push({ name: e.name, socket_id: socket.id });
alienArray.push(e.aliens);
if (arr.length >= 2 && arr[0].socket_id !== arr[1].socket_id) {
let p1obj = {
p1name: arr[0].name,
p1socketId: arr[0].socket_id,
p1alien: alienArray[0][Math.floor(Math.random() * 24)],
};
let p2obj = {
p2name: arr[1].name,
p2socketId: arr[1].socket_id,
p2alien: alienArray[0][Math.floor(Math.random() * 24)],
};
let obj = {
p1: p1obj,
p2: p2obj,
allAliens: alienArray[0],
};
playingArray.push(obj);
console.log(playingArray, "<<<<<< playing array");
io.emit("find", { allPlayers: playingArray });
// arr.splice(0, (arr.length -1));
// alienArray.splice(0, (arr.length -1));
// playingArray.splice(0, (arr.length -1));
arr = [];
alienArray = [];
playingArray = [];
}
}
});
socket.on("start-game", () => {
io.emit("proceed");
turnCounter = 0;
});
socket.on("turnPlayed", () => {
turnCounter += 1;
io.emit("turnIncreased", turnCounter);
});
socket.on("winner", () => {
io.emit("endGame");
});
// socket.on("reset", (e) => {
// console.log(e.length, "<<<<<<<< e length in reset");
// if (e) resetGameArray.push(e);
// if (e.length === 2) {
// newBoard.alienArray = [...resetGameArray[0]];
// console.log(newBoardalienArray, "<<<<< newBoard.alienArray");
// newBoard.p1alien = newBoard.alienArray[Math.floor(Math.random() * 24)]
// newBoard.p2alien = newBoard.alienArray[Math.floor(Math.random() * 24)]
// console.log(newBoard, "<<<<<< playing array");
// io.emit("reset", { newBoard });
// }
// });
});
require("dotenv").config();
connectDB();
app.use(cors({ origin: "*" }));
app.use(express.json());
app.get("/api", getEndpoints);
app.get("/api/aliens", getAliens);
app.get("/api/questions", getQuestions);
app.get("/api/users", getUsers);
app.post("/api/users", postUsers);
// app.get("/api/aliens/names", getAliensNames);
app.all("*", (_, res) => res.status(404).send({ msg: "Not Found" }));
app.use(handleMongoErrors);
app.use(handleCustomErrors);
app.use(handleInternalErrors);
module.exports = app;