-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
155 lines (134 loc) · 5 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
const socketIO = require('socket.io')
const express = require('express')
const http = require('http')
const fs = require('fs')
const fetch = require('node-fetch')
require('dotenv').config()
const app = express()
const server = http.createServer(app)
app.use(express.static('dist'));
const io = socketIO(server)
let sockets = []
let currentIndex = -1
const aliases = JSON.parse(fs.readFileSync('./aliases.json'))
const cachedImageUrls = []
function getRandomAlias() {
return aliases[Math.floor(Math.random() * aliases.length)]
}
app.get('/alias', (request, response) => {
response.send(JSON.stringify([getRandomAlias()]))
})
app.get('/image', (request, response) => {
const q = request.param('q')
const cache = cachedImageUrls.find(item => item.query == q)
if(cache) {
response.send(JSON.stringify([cache.image]))
}
else {
fetch(`https://www.googleapis.com/customsearch/v1?key=AIzaSyBY7rmwpqw4CvR1rbLbnyxFPvlHM-o04-o&q=${encodeURI(q)}&cx=004773877283740196199:kstva1a2x93&searchType=image&num=1`)
.then(res => res.json())
.then(json => {
cachedImageUrls.push({
query: q,
image: json.items[0].link
})
response.send(JSON.stringify([json.items[0].link]))
})
.catch(err => {
response.status(503).send(JSON.stringify(['error']))
})
}
})
io.on('connection', socket => {
console.log('new connection')
let userAdded = false
socket.on('addUser', ({ username, lobby }) => {
if(userAdded) return
socket.username = username
socket.alias = getRandomAlias()
socket.lobby = lobby
userAdded = true
socket.emit('login')
socket.join(socket.lobby)
console.log(`user ${socket.username} joined ${socket.lobby}`)
sockets.filter(item => item.lobby == socket.lobby).forEach(user => {
socket.emit('userJoined', {username: user.username, alias: user.alias})
})
sockets.push(socket)
socket.to(lobby).emit('userJoined', {
username: socket.username,
alias: socket.alias
})
})
socket.on('rejoin', ({ username }) => {
console.log('rejoin request ', username)
let oldSocketIndex = sockets.findIndex(item => item.username == username)
if(oldSocketIndex == -1) return
socket.username = username
socket.lobby = sockets[oldSocketIndex].lobby
socket.alias = sockets[oldSocketIndex].alias
socket.join(socket.lobby)
clearTimeout(sockets[oldSocketIndex].timeout)
sockets[oldSocketIndex] = socket
console.log(`user ${socket.username} rejoined to ${socket.lobby}`)
userAdded = true
socket.to(socket.lobby).emit('userRejoined', {
username: socket.username,
alias: socket.alias
})
})
socket.on('changeAlias', user => {
const aliasUser = sockets.find(item => item.username == user.username && item.lobby == socket.lobby)
if(aliasUser) {
aliasUser.alias = user.alias
console.log('new alias for', aliasUser.username, ': ', aliasUser.alias)
socket.to(socket.lobby).emit('aliasChange', {
username: aliasUser.username,
alias: aliasUser.alias
})
}
})
socket.on('moveToNext', () => {
const room = sockets.filter(item => item.lobby == socket.lobby)
currentIndex = (currentIndex + 1) % room.length
console.log('moving to next ', room[currentIndex].username, socket.lobby)
socket.to(socket.lobby).emit('setCurrentUser', {
username: room[currentIndex].username
})
socket.emit('setCurrentUser', {
username: room[currentIndex].username
})
})
socket.on('moveToPrevious', () => {
const room = sockets.filter(item => item.lobby == socket.lobby)
currentIndex = (currentIndex - 1) % room.length
if(currentIndex < 0)
currentIndex = room.length - 1
console.log('moving to prev ', room[currentIndex].username)
socket.to(socket.lobby).emit('setCurrentUser', {
username: room[currentIndex].username
})
socket.emit('setCurrentUser', {
username: room[currentIndex].username
})
})
socket.on('disconnect', () => {
console.log(`user ${socket.username} disconnected`)
if(userAdded) {
socket.to(socket.lobby).emit('userLost', {
username: socket.username,
lastSeen: Date.now()
})
socket.timeout = setTimeout(() => {
console.log(`user ${socket.username} timed out`)
socket.to(socket.lobby).emit('userLeft', {
username: socket.username
})
sockets = sockets.filter(item => item.username != socket.username)
}, 1000 * 60 * 30)
}
})
})
server.listen((process.env.PORT || 80), () => {
console.log('server started')
})