This repository has been archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
177 lines (132 loc) · 4.01 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
// IMPORTS AND SETUP
const express = require('express');
const cache = require('memory-cache');
const { assignRoles } = require('./helpers/roles');
const PORT = process.env.PORT || 3000;
const app = express();
var rooms = new cache.Cache();
// LISTEN TO API ROUTES
app.get("/api/hostUpdate", async (req, res) => {
let { roles, code } = req.query;
roles = JSON.parse(roles);
let room = await rooms.get(code);
if(room) {
Object.keys(roles).forEach((role) => {
if(!roles[role] || parseInt(roles[role]) < 0) {
roles[role] = "" + 0;
}
});
room = {
players: room.players,
roles: roles,
assignment: room.assignment
}
rooms.put(code, room, 6*60*60*1000, function(key, value) {
console.log('Room with code ' + code + ' has closed');
});
res.json(room);
} else {
res.json({
players: []
});
}
});
app.get("/api/guestUpdate", async (req, res) => {
let { code, name } = req.query;
let room = await rooms.get(code);
if(room) {
let assignment = (room.assignment && room.assignment[name]) ? room.assignment[name] : "";
res.json({
players: room.players,
roles: room.roles,
assignment
});
} else {
res.json({
players: [],
roles: {},
assignment: ""
});
}
});
app.get("/api/createRoles", async (req, res) => {
let { roles, code } = req.query;
roles = JSON.parse(roles);
let room = await rooms.get(code);
if(room) {
Object.keys(roles).forEach((role) => {
if(parseInt(roles[role]) < 0) {
roles[role] = "" + 0;
}
});
let assignment = await assignRoles(roles, room.players);
room = {
players: room.players,
roles,
assignment
}
rooms.put(code, room, 6*60*60*1000, function(key, value) {
console.log('Room with code ' + code + ' has closed');
});
res.json({
success: true,
assignment
})
} else {
res.json({
success: false
});
}
});
// LISTEN TO VIEW ROUTES
app.get("/", (req, res) => {
res.render(__dirname + '/views/landing.ejs');
});
app.get("/new", (req, res) => {
// generate a game code
var code = '' + Math.floor(10000 + Math.random() * 90000);
while(rooms.get(code)) {
code = '' + Math.floor(10000 + Math.random() * 90000);
}
// store code as new game in memory cache
rooms.put(code, {
roles: {},
players: [],
assignment: {}
}, 6*60*60*1000, function(key, value) {
console.log('Room with code ' + code + ' has closed');
});
console.log('Room with code ' + code + ' has opened');
// render page
res.render(__dirname + '/views/host.ejs', { pageName: "Host Game", code });
});
app.get("/join", (req, res) => {
res.render(__dirname + '/views/join.ejs', { pageName: "Join Game", code: null, name: "" });
});
app.get("/game/:code", (req, res) => {
let { code } = req.params;
let { name } = req.query;
let room = rooms.get('' + code);
if(!name) {
name = "Player" + Math.floor(Math.random() * 1000);
}
if(!room) {
res.render(__dirname + '/views/join.ejs', { pageName: "Join Game", code, name });
} else {
rooms.put(code, {
roles: room.roles,
players: [ name, ...room.players ],
assignment: room.assignment
}, 6*60*60*1000, function(key, value) {
console.log('Room with code ' + code + ' has closed');
});
res.render(__dirname + '/views/guest.ejs', { pageName: "Game " + code + " - " + name, code, name });
}
});
app.get("*", (req, res) => {
res.redirect('/');
});
// START SERVER
app.listen(PORT, (req, res) => {
console.log(`Server Started at PORT ${PORT}`);
});