forked from digitalocean/sample-websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
234 lines (191 loc) · 7.5 KB
/
helper.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
require('dotenv').config()
const PublicGoogleSheetsParser = require('public-google-sheets-parser')
const spreadsheetId = process.env.GSHEET_ID
const parser = new PublicGoogleSheetsParser(spreadsheetId)
// Importing 'crypto' module
const crypto = require('crypto'), hash = crypto.getHashes();
const { uniqueNamesGenerator, colors, animals, names, NumberDictionary } = require('unique-names-generator');
const sleepAdjectives = require('./adjectives.json')
const db = require('./db');
exports.getSessionId = () => {
const now = new Date()
return now.getHours() + "-" + (now.getMinutes() >= 30 ? "halfpast" : "zero")
}
exports.getUniqueId = () => {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
return s4() + s4() + '-' + s4();
}
exports.getSessionHash = () => {
// Create hash of SHA1 type
x = exports.getSessionId()
// 'digest' is the output
// of hash function containing
// only hexadecimal digits
hashPwd = crypto.createHash('sha1')
.update(x).digest('hex');
return hashPwd
}
exports.generateDisplayName = () => {
const numberDictionary = NumberDictionary.generate({ min: 69, max: 420 });
const dictOptions = [
[sleepAdjectives, animals, numberDictionary],
[colors, animals, numberDictionary],
[sleepAdjectives, names, numberDictionary]
]
let randomName = uniqueNamesGenerator({
dictionaries: dictOptions[Math.floor(Math.random() * dictOptions.length)],
style: Math.random() > 0.5 ? "capital" : "lowerCase",
separator: ""
})
if (Math.random() < 0.1) {
randomName = randomName.replace(/[a-z]/gi, c => c[`to${randomName ? 'Upp' : 'Low'}erCase`](randomName = !randomName))
}
return randomName
}
exports.dbCreatePlayer = async () => {
const uid = exports.getUniqueId();
console.log(uid)
const displayName = exports.generateDisplayName()
await db.query('INSERT INTO users (device_uid, display_name) VALUES ($1, $2)', [uid, displayName])
return uid
}
exports.dbGetPlayerName = async (uid) => {
try {
let result = await db.query(`select display_name from users where device_uid = $1`, [uid])
return result.rows[0].display_name
} catch (err) {
console.log(err)
return exports.generateDisplayName()
}
}
exports.dbGetChatMessage = async (url_part) => {
let result = await db.query(`select chat_message from dreams where url_part = $1`, [url_part])
return result.rows[0].chat_message
}
// TODO why is event logging ever throwing errors
exports.dbLogEvent = async (uid, session_hash, data) => {
try {
await db.query(
'INSERT INTO events (device_uid, session_hash, type, impact_key, currency_value, dream_url_part) VALUES ($1, $2, $3, $4, $5, $6)',
[uid, session_hash, data.type, data.key || null, data.value || null, data.dream_url_part || null]
)
} catch (err) {
console.log(err)
}
}
exports.dbSyncDreamTable = async () => {
const fs = require('fs');
const oldEvents = await db.query('select * from events')
if (oldEvents.rowCount > 0) {
fs.writeFile(`events-old_${Date.now()}.json`, JSON.stringify(oldEvents.rows), (err) => {
if (err) throw err
console.log("events-old.json written")
})
}
var QRCode = require('qrcode')
QRCode.toFile(
`qr/quinton.png`,
`https://dreamscape-explorer.app/quinton.wav`,
{
color: {
dark: "#a50997",
light: "#f4d4b1"
}
},
function (err) {
if (err) throw err
}
)
// drop outdated rows
await db.query('DELETE FROM events')
await db.query('DELETE FROM dreams')
const authorIds = (await db.query('select id, name from creators order by id asc')).rows.map((row)=> {
return row.name
})
const dirs = [
"bitsy/",
"twine/",
"puzzlescript/",
"downpour/"
]
const extensions = [
"bitsy",
"twee",
"ps",
"json"
]
let testEntryCreated = false
for (let dirIndex in dirs) {
fs.readdir(`public/bridges/src/${dirs[dirIndex]}`, function (err, files) {
if (err) {
console.error("Could not list the directory.", err);
process.exit(1);
}
files.forEach(async function (file, index) {
if (file.indexOf(`.${extensions[dirIndex]}`) == -1) return
let url_part = file.split('.')[0]
if (url_part.includes("TEST_") || url_part == "editor") {
url_part = "debug"
}
let author
let title
try {
const dreamSrc = fs.readFileSync(`public/bridges/src/${dirs[dirIndex]}/${file}`, 'utf8');
switch (extensions[dirIndex]) {
case "bitsy":
title = dreamSrc.match(`(})(.*?)({)`)[0]
title = title.substring(
title.indexOf("}") + 1,
title.lastIndexOf("{")
)
let authorStart = dreamSrc.substring(dreamSrc.indexOf("by ") + 3)
author = authorStart.split("\n")[0]
break
case "twee":
title = dreamSrc.split('\n')[1]
let storyData = dreamSrc.substring(
dreamSrc.indexOf("{"),
dreamSrc.indexOf("}") + 1
)
storyData = JSON.parse(storyData)
author = storyData.author
break
case "ps":
title = dreamSrc.split('\n')[0].replace("title ", "").replace(/[\u0000-\u001F\u007F-\u009F]/g, "")
author = dreamSrc.split('\n')[1].replace("author ", "").replace(/[\u0000-\u001F\u007F-\u009F]/g, "")
break
case "json":
let json = JSON.parse(dreamSrc)
title = json.name
author = json.author
break
}
if (url_part == "debug") {
title = "test"
author = "izzy kestrel"
if (testEntryCreated) return
testEntryCreated = true
}
QRCode.toFile(
`qr/${url_part}.png`,
`https://dreamscape-explorer.app/bridges/${url_part}.html`,
{
color: {
dark: "#a50997",
light: "#f4d4b1"
}
},
function (err) {
if (err) throw err
}
)
await db.query('insert into dreams (url_part, creator_id, title, chat_message) values ($1, $2, $3, $4)', [url_part, authorIds.indexOf(author) + 1, title, "this is a placeholder message! replace me! @~@"])
} catch (err) {
console.error(err);
}
});
});
}
}