-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
211 lines (173 loc) · 5.16 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
207
208
209
210
211
require('dotenv').config();
const express = require('express');
const uuid = require('uuid');
const Redis = require('ioredis');
const redis = new Redis({password: process.env.DB_PW, db: 2});
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const redisAdapter = require('socket.io-redis');
io.adapter(redisAdapter({ host: 'localhost', port: 6379, password: process.env.DB_PW}));
const lifespan = 2 * 60;
const DEBUG = true;
app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
if (DEBUG) {
app.use('/assets', express.static('assets'));
app.use('/weeks', express.static('weeks'));
}
const getPeople = async () => {
const batch = redis.pipeline();
const queue = await redis.lrange('queue', 0, -1);
queue.forEach(k => batch.call('json.get', `q:${k}`));
return (await batch.exec()).map(r => JSON.parse(r[1]));
}
const exists = async (id) => {
const queue = await redis.lrange('queue', 0, -1);
return queue.includes(id);
}
app.get('/admin', async (req, res) => {
res.render('admin', {
people: await getPeople()
});
});
app.get('/', async (req, res) => {
res.render('index');
});
app.get('/api/queue', async (req, res) => {
res.json({
people: await getPeople()
});
});
app.post('/', async (req, res) => {
if (!req.body.name) {
return;
}
const uid = uuid.v1();
const data = {
name: req.body.name,
complete: false,
current: false,
joined: new Date(),
id: uid
};
redis.rpush('queue', uid);
redis.call('json.set', `q:${uid}`, '.', JSON.stringify(data));
res.json({
people: await getPeople()
});
});
app.post('/current', async (req, res) => {
const { id } = req.body;
if (!exists(id)) {
return res.status(400).end();
}
const people = await getPeople();
const isCurrent = people.find(person => person.current);
if (isCurrent) {
isCurrent.current = false;
redis.call('json.set', `q:${isCurrent.id}`, '.', JSON.stringify(isCurrent));
// toggling current person
if (isCurrent.id === id) {
return res.json({
people: await getPeople()
});
}
}
const nextCurrent = people.find(person => person.id === id);
if (nextCurrent) {
nextCurrent.current = true;
redis.call('json.set', `q:${nextCurrent.id}`, '.', JSON.stringify(nextCurrent));
}
return res.json({
people: await getPeople()
});
});
app.post('/complete', async (req, res) => {
const { id } = req.body;
if (!exists(id)) {
return res.status(400).end();
}
const people = await getPeople();
const person = people.find(person => person.id === id);
if (person) {
person.complete = true;
person.current = false;
redis.call('json.set', `q:${person.id}`, '.', JSON.stringify(person));
}
return res.json({
people: await getPeople()
});
});
app.post('/remove', async (req, res) => {
const { id } = req.body;
if (!exists(id)) {
return res.status(400).end();
}
redis.del(`q:${id}`);
redis.lrem('queue', 1, id);
return res.json({
people: await getPeople()
});
});
app.get('/poem-data', async (req, res) => {
let poemData = {};
const keys = await redis.keys('poem:*');
for(k of keys) {
poemData[k.split(':')[1]] = await redis.hgetall(k);
}
res.json(poemData);
});
const getCreatures = async (ignore = '') => {
const keys = await redis.keys('u:*');
const pipe = redis.pipeline();
keys.forEach(k => { if (k !== ignore) pipe.hgetall(k); });
return (await pipe.exec()).map(([e, r]) => r);
}
const getActivities = async () => {
const keys = await redis.keys('a:*');
const cursor = redis.pipeline();
keys.forEach(k => cursor.hgetall(k));
const res = (await cursor.exec()).map(([e, r]) => {
return { ts: Date.now(), ...r};
});
return res;
}
io.on('connection', async socket => {
console.log('socket connected...');
socket.on('clickEvt', async data => {
const k = `a:${data.id}:${data.evtIdx}`;
data = { created: Date.now(), lifespan, ...data};
redis.hset(k, 'x', data.x, 'y', data.y, 'id', data.id, 'creature', data.creature, 'nickname', data.nickname, 'evtIdx', data.evtIdx, 'created', Date.now(), 'lifespan', lifespan);
redis.expire(k, lifespan);
socket.broadcast.emit('activity', data);
});
// broadcast curent events
const activity = await getActivities();
socket.emit('activity', activity);
socket.on('frame', async data => {
const k = `u:${data.id}`;
redis.hset(k, 'x', data.x, 'y', data.y, 'id', data.id, 'creature', data.creature, 'nickname', data.nickname);
redis.expire(k, 15);
const creatures = await getCreatures();
socket.emit('pong', creatures);
});
socket.on('typing', data => {
socket.broadcast.emit('question', data.text);
});
socket.on('poem-input', data => {
socket.broadcast.emit('poem-output', data);
const [ person, part ] = data.key.split('-');
redis.hset(`poem:${part}`, person, data.text);
});
socket.on('get-code', async () => {
let poemData = {};
const keys = await redis.keys('poem:*');
for(k of keys) {
poemData[k.split(':')[1]] = await redis.hvals(k);
}
socket.emit('code', poemData);
});
});
server.listen(4000);