-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
175 lines (166 loc) · 5.54 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"use strict";
const express = require("express");
const path = require("path");
const { createServer } = require("node:http");
const { join } = require("node:path");
const app = express();
const server = createServer(app);
const { Server } = require("socket.io");
const PORT = process.env.PORT || 3000;
const { get } = require("http");
const io = new Server(server);
const {
users,
checkIfunique,
userJoin,
userLeave,
getUserIndex,
setWordspermin,
getRoomUsers,
getCurrentUser,
getUniqueId,
Racer,
} = require("./utils/functions.js");
const { quotes, getRandomWords, getQuote } = require("./utils/quotes.js");
const { BoyNames, GirlNames } = require("./utils/usernames.js");
app.use(express.static(path.join(__dirname, "public")));
app.get("/get-users-leaderboard", async (req, res) => {
const players = await Racer.find().sort({ highscore: -1 });
res.json(players);
});
io.on("connection", (socket) => {
socket.on("join", ({ username, userAvatar, userIdentity }) => {
const userJoinId = Number(getUniqueId());
userJoin(socket.id, username, 0, userAvatar, userJoinId);
const newPlayer = new Racer({
username: username,
joinId: userJoinId,
userAvatar: userAvatar,
userContact: userIdentity,
});
newPlayer.save();
socket.emit("joining id", userJoinId);
io.emit("add user progress", users);
io.emit("display board", users);
console.log("user-joined, " + users.length + " users on server");
});
socket.on("join by Id", joinId=>{
async function joinUser() {
const data = await Racer.find({ joinId: joinId });
console.log(data[0]);
if(data[0]){
console.log("yes");
console.log(joinId);
userJoin(socket.id, data[0].username, 0, data[0].userAvatar, joinId);
socket.emit("joining by id", joinId, data[0].username);
io.emit("add user progress", users);
io.emit("display board", users);
console.log("user-joined, " + users.length + " users on server");
}else{
socket.emit("wrong join id error");
}
}
joinUser();
})
socket.on("ready status", function (userLevel) {
const i = getUserIndex(socket.id);
users[i].status = true;
users[i].difficulty = userLevel;
const easyNum = users.reduce(
(acc, curr) => (curr.difficulty === "easy" ? (acc = acc + 1) : acc),
0
);
let levelOfQuote = 0;
if (users.length - easyNum > easyNum) {
levelOfQuote = 1;
} else {
levelOfQuote = 0;
}
io.emit("sendStatusReady", socket.id);
if (users.every((user) => user.status === true) && users.length > 1) {
const quoteFromServer = getQuote(levelOfQuote);
for (let i = 0; i < users.length; i++) {
users[i].progress = 0;
}
io.emit("start game", { quoteFromServer, levelOfQuote });
} else if (users.length === 1) {
socket.emit("insufficient players");
}
});
socket.on("not ready", function () {
const i = getUserIndex(socket.id);
io.emit("sendStatusNotReady", socket.id);
users[i].status = false;
});
socket.on("progress", (value, wpm) => {
const index = getUserIndex(socket.id);
users[index].progress = Number(value);
users[index].currWpm = Number(wpm);
io.emit("user progress", users);
});
socket.on("user score", (object) => {
const i = getUserIndex(socket.id);
const joinuserid = users[i].userJoinId;
if ((object.userDevice === "mobile")) {
if (object.quoteLevel === 1) {
async function ScoreMediumMobile() {
await Racer.updateOne(
{ "joinId" : Number(joinuserid), "highScore.Medium.Mobile": { $lt: object.wpm } },
{ $set: { "highScore.Medium.Mobile": object.wpm } }
);
io.emit("update user leaderboard");
}
ScoreMediumMobile();
} else {
async function ScoreEasyMobile() {
await Racer.updateOne(
{ "joinId" : Number(joinuserid), "highScore.Easy.Mobile": { $lt: object.wpm } },
{ $set: { "highScore.Easy.Mobile": object.wpm } }
);
io.emit("update user leaderboard");
}
ScoreEasyMobile();
}
} else {
if (object.quoteLevel === 1) {
async function ScoreMediumLaptop() {
await Racer.updateOne(
{ "joinId" : Number(joinuserid), "highScore.Medium.Laptop": { $lt: object.wpm } },
{ $set: { "highScore.Medium.Laptop": object.wpm } }
);
io.emit("update user leaderboard");
}
ScoreMediumLaptop();
} else {
async function ScoreEasyLaptop() {
await Racer.updateMany(
{"joinId" : Number(joinuserid), "highScore.Easy.Laptop": { $lt: object.wpm }},
{ $set: { "highScore.Easy.Laptop": object.wpm } }
);
io.emit("update user leaderboard");
}
ScoreEasyLaptop();
}
}
});
socket.on("get rank", () =>{
const findRank = users;
const sorted = findRank.sort((a,b)=>b.currWpm-a.currWpm);
const rank = sorted.findIndex(player=> player.id ==socket.id) + 1;
const playerId = socket.id;
io.emit("set player rank", {playerId, rank});
})
socket.on("disconnect", () => {
const user = userLeave(socket.id);
const usersArr = getRoomUsers();
if (user) {
io.emit("game users", usersArr);
io.emit("remove user progress", users);
console.log("user-left" + " no. of users on server: " + users.length);
}
});
});
server.listen(PORT, () => {
console.log("server running at http://localhost:3000");
});
// mongodb+srv://anis42390:[email protected]/typeUserDB