This repository has been archived by the owner on Sep 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
206 lines (190 loc) · 5.92 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
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
const express = require('express');
const session = require("express-session");
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
require('dotenv').config();
app.set('view engine', 'ejs');
app.use(express.static('public'));
const sessionMiddleware = session({
name: 'sessionid',
secret: 'FAISALN.CQ',
resave: false,
saveUninitialized: false,
cookie: { maxAge: 86400000 }
});
app.use(sessionMiddleware);
const wrap = middleware => (socket, next) => middleware(socket.request, {}, next);
io.use(wrap(sessionMiddleware));
var rooms = { public: [] };
var roomId = 'public';
var roomType = 'public';
function allRoutes(req) {
if (!req.session.account) {
if (process.env.NODE_ENV === "production") {
req.session.account = [];
} else {
req.session.account = [];
};
};
};
app.get('/', (req, res) => {
allRoutes(req);
res.render('index', { roomId: 'public', roomType: 'public', rooms, players: rooms.public || [], socket: io, error: null, session: req.session });
});
app.get("/login", async (req, res) => {
allRoutes(req);
if (req.session.loggedIn === true) {
res.redirect('/')
} else {
res.redirect(`https://my.dangoweb.com/?auth=true&redirect=http://${req.hostname}/sso&faisaln=true`)
}
});
app.get("/sso", async (req, res) => {
allRoutes(req);
if (req.query.username) {
await fetch(`https://api.dangoweb.com/secure/getaccount/${req.query.username}?api-key=${process.env.DANGOAPI_KEY_faisaln}`, {
method: 'GET',
headers: {
Accept: '*/*',
'User-Agent': 'Dango Portal (https://portal.dangoweb.com)'
}
})
.then(sso => sso.json())
.then(sso => {
if (sso.username) {
console.log(`${sso.username} logged in`)
req.session.loggedIn = true;
req.session.account = {
username: sso.username,
email: sso.email,
firstname: sso.firstname,
lastname: sso.lastname,
role: sso.role,
profileimg: sso.profileimg,
dangos: sso.dangos,
blocked: sso.blocked,
points: 0
};
} else {
console.log(sso);
console.log("Failed login");
};
res.redirect('/');
});
} else {
res.redirect('/');
};
});
app.get("/logout", (req, res) => {
const sessionId = req.session.id;
req.session.destroy(() => {
io.in(sessionId).disconnectSockets();
res.redirect('/');
});
});
app.get('/:roomId', (req, res) => {
allRoutes(req);
console.log(`Attempting to join ${req.params.roomId}`);
if (req.params.roomId) {
if (rooms[req.params.roomId]) {
res.render('index', { roomId: req.params.roomId, roomType: 'friends', rooms, players: rooms[roomId] || [], socket: io, error: null, session: req.session });
} else {
console.log(`Room ${req.params.roomId} does not exist`);
res.render('index', { roomId, roomType, rooms, players: rooms.public || [], socket: io, error: "Room does not exist", session: req.session });
};
} else {
res.redirect('/');
};
});
io.on('connection', (socket) => {
userSession = socket.request.session;
socket.on('create room', () => {
var IDX = 36, HEX = '';
while (IDX--) HEX += IDX.toString(36);
var str = '', num = 6 || 11;
while (num--) str += HEX[Math.random() * 36 | 0];
roomId = str;
roomType = 'friends';
socket.emit('new room', roomType, roomId);
userSession.account.id = socket.id;
rooms[roomId] = [userSession.account];
io.to(roomId).emit('update players', rooms[roomId] || []);
socket.join(roomId);
});
socket.on('join room', (roomId) => {
console.log(`New User: ${socket.id} (${userSession.username || 'Guest'}) -> ${roomId}`);
if (roomId === 'public') {
if (!userSession.account) {
userSession.account = [];
};
var user = (userSession.account.length === 0) ? { role: 'guest', blocked: 0, points: 0 } : userSession.account;
user.id = socket.id;
userSession.account = user;
if (!JSON.stringify(rooms[roomId]).includes(socket.id)) {
rooms.public.push(user);
};
} else {
if (!JSON.stringify(rooms[roomId]).includes(socket.id)) {
rooms[roomId].push(user);
};
};
socket.join(roomId);
io.to(roomId).emit('update players', ((roomId === 'public') ? rooms.public : rooms[roomId]));
});
socket.on('disconnect', () => {
console.log(`User ${socket.id} disconnected`);
var list = [];
if (rooms[roomId]) {
rooms[roomId].forEach((player) => {
if (!player.id) {
console.log(player);
};
if (player.id != socket.id) {
list.push(player);
};
});
rooms[roomId] = list;
io.to(roomId).emit('update players', rooms[roomId]);
}
});
socket.on('join private room', (roomId) => {
if (rooms[roomId]) {
var wanted = rooms[roomId].length;
rooms[roomId].forEach(player => {
if (player.id != socket.id) {
wanted--;
if (wanted === 0) {
userSession.account.id = socket.id;
rooms[roomId].push(userSession.account);
socket.join(roomId);
io.to(roomId).emit('update players', rooms[roomId] || []);
};
};
});
};
});
socket.on('rpoint', () => {
Object.keys(rooms).forEach(roomId => {
rooms[roomId].forEach(player => {
if (player.id === socket.id) {
player.points--;
io.to(roomId).emit('update players', rooms[roomId] || []);
};
});
});
});
socket.on('point', (points) => {
Object.keys(rooms).forEach(roomId => {
rooms[roomId].forEach(player => {
if (player.id === socket.id) {
player.points = player.points + points;
io.to(roomId).emit('update players', rooms[roomId] || []);
};
});
});
});
});
http.listen(80, () => {
console.log('Server is listening on port 80');
});