-
Notifications
You must be signed in to change notification settings - Fork 0
/
connections.routes.js
96 lines (70 loc) · 2.79 KB
/
connections.routes.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
const express = require('express')
const router = express.Router()
const fs = require('fs')
const path = require("path")
// write metaData.json on server boot
let dir = fs.readdirSync(path.resolve(__dirname, "./public/data"), "utf8")
let metaDataContents = { visibleGames: {}, invisibleGames: {} }
dir.forEach(file => {
if (file.startsWith("game")) {
filedata = JSON.parse(fs.readFileSync(path.resolve(__dirname, "./public/data", file)))
metaDataContents.visibleGames[(filedata.title)] = file
}
})
// write to metadata file
fs.writeFileSync(path.resolve(__dirname, "./public/data/metaData.json"), JSON.stringify(metaDataContents), (err) => {
if (err) throw err;
console.log('MetaData has been written');
}, { flag: "w" })
router.use(express.static(path.join(__dirname, 'public')));
router.post("/addGame", function (req, res) {
let body = req.body.game
// check
let goodInput = true
if (body.title && body.author) {
let items = new Set()
let titles = new Set()
for (cat of body.categories) {
titles.add(cat.title)
cat.elements.forEach(elem => items.add(elem))
if (!cat.color) {
goodInput = false
break
}
};
if (items.size != 16 || titles.size != 4) goodInput = false
} else {
goodInput = false
}
// creating game file
if (goodInput) {
let gameJSON = JSON.stringify(body)
let metaData = JSON.parse(fs.readFileSync(path.resolve(__dirname, "./public/data/metaData.json"), "utf8"))
let gameName = body.title
let numOfGames = Object.keys(metaData.visibleGames).length
let gameFileName = "game_" + numOfGames + ".json"
//let gameFileName = "game_" + gameName.replace(/\s+/g, '') + "_" + body.author.replace(/\s+/g, '') + ".json"
// checks if filename already exists
let existsAlready = fs.readdirSync(path.resolve(__dirname, "./public/data")).includes(gameFileName)
while (existsAlready) {
numOfGames++
gameFileName = "game_" + numOfGames + ".json"
existsAlready = fs.readdirSync(path.resolve(__dirname, "./public/data")).includes(gameFileName)
}
// create file
fs.writeFile(path.resolve(__dirname, "./public/data/", gameFileName), gameJSON, (err) => {
if (err) throw err;
console.log('The file has been saved! Filename: ' + gameFileName);
})
// edit metaData visible games
metaData.visibleGames[gameName] = gameFileName
fs.writeFile(path.resolve(__dirname, "./public/data/metaData.json"), JSON.stringify(metaData), (err) => {
if (err) throw err;
console.log('MetaData has been altered');
})
res.sendStatus(200)
return
}
res.sendStatus(404)
})
module.exports = { "path": "/connections", "router": router }