-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganize project files and directories
- Loading branch information
Ernest Paśnik
committed
Dec 11, 2023
1 parent
b462e11
commit 08eb58e
Showing
26 changed files
with
765 additions
and
550 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const fs = require('fs'); | ||
const path = `${__dirname}/../../private/authorized_mappers.json`; | ||
|
||
function loadCreators() { | ||
try { | ||
const d = fs.readFileSync(path, 'utf8'); | ||
const obj = JSON.parse(d); | ||
return obj; | ||
} catch (error) { | ||
console.error(error.message); | ||
return {}; | ||
} | ||
} | ||
|
||
function saveCreators(obj) { | ||
try { | ||
const json = JSON.stringify(obj, null, 2); | ||
fs.writeFileSync(path, json, 'utf8'); | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
} | ||
|
||
function randomString(len) { | ||
const c = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
let result = ''; | ||
for (let i = 0; i < len; i++) { | ||
const randomIndex = Math.floor(Math.random() * c.length); | ||
result += c.charAt(randomIndex); | ||
} | ||
return result; | ||
} | ||
|
||
router.get('/', (req, res) => { | ||
res.render('admin/creators', { | ||
page: 'Creators', | ||
user: req.user, | ||
creators: loadCreators() | ||
}); | ||
}); | ||
|
||
router.post('/', (req, res) => { | ||
const obj = loadCreators(); | ||
obj[randomString(50)] = { | ||
shorthand: req.body.shorthand, | ||
allow_creating_new: false, | ||
maps: [] | ||
}; | ||
saveCreators(obj); | ||
res.redirect('/admin/creators'); | ||
}); | ||
|
||
router.get('/:shorthand', (req, res) => { | ||
const obj = loadCreators(); | ||
const c = Object.values(obj).find((obj) => obj.shorthand === req.params.shorthand); | ||
if (!c) { | ||
return res.redirect('/admin/creators'); | ||
} | ||
res.render('admin/creator', { | ||
page: 'Creators', | ||
user: req.user, | ||
c: c | ||
}); | ||
}); | ||
|
||
router.post('/:shorthand', (req, res) => { | ||
const obj = loadCreators(); | ||
const k = Object.keys(obj).find((k) => obj[k].shorthand === req.params.shorthand); | ||
if (!k) { | ||
return res.redirect('/admin/creators'); | ||
} | ||
if (req.body.delete === 'on') { | ||
delete obj[k]; | ||
} else { | ||
obj[k].shorthand = req.body.shorthand; | ||
obj[k].allow_creating_new = req.body.allow_creating_new === 'yes'; | ||
obj[k].maps = req.body.arenas.split('\n').map((line) => line.trim()); | ||
} | ||
saveCreators(obj); | ||
res.redirect('/admin/creators'); | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,43 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const fs = require('fs'); | ||
const path = __dirname + '/../../private/settings.json'; | ||
const path = `${__dirname}/../../private/settings.json`; | ||
|
||
module.exports = { | ||
load: function () { | ||
try { | ||
const fileContent = fs.readFileSync(path, 'utf8'); | ||
const obj = JSON.parse(fileContent); | ||
return obj; | ||
} catch (error) { | ||
console.error(`Error reading settings: ${error.message}`); | ||
return null; | ||
} | ||
}, | ||
function loadSettings() { | ||
try { | ||
const d = fs.readFileSync(path, 'utf8'); | ||
const obj = JSON.parse(d); | ||
return obj; | ||
} catch (error) { | ||
console.error(error.message); | ||
return {}; | ||
} | ||
} | ||
|
||
save: function (obj) { | ||
try { | ||
const json = JSON.stringify(obj, null, 2); | ||
fs.writeFileSync(path, json, 'utf8'); | ||
} catch (error) { | ||
console.error(`Error writing settings ${error.message}`); | ||
return null; | ||
} | ||
function saveSettings(obj) { | ||
try { | ||
const json = JSON.stringify(obj, null, 2); | ||
fs.writeFileSync(path, json, 'utf8'); | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
} | ||
|
||
module.exports = function(locals) { | ||
router.get('/', (req, res) => { | ||
res.render('admin/settings', { | ||
page: 'Settings', | ||
user: req.user | ||
}); | ||
}); | ||
|
||
router.post('/', (req, res) => { | ||
const obj = loadSettings(); | ||
obj.alert = req.body.alert; | ||
locals.alert = req.body.alert; | ||
saveSettings(obj); | ||
res.redirect('/admin/settings'); | ||
}); | ||
|
||
return router; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const moment = require('moment'); | ||
const fs = require('fs'); | ||
const path = `${__dirname}/../../private/users.json`; | ||
|
||
function loadUsers() { | ||
try { | ||
const d = fs.readFileSync(path, 'utf8'); | ||
const obj = JSON.parse(d); | ||
return obj; | ||
} catch (error) { | ||
console.error(error.message); | ||
return {}; | ||
} | ||
} | ||
|
||
router.get('/', (req, res) => { | ||
const users = loadUsers(); | ||
const updatedUsers = Object.fromEntries( | ||
Object.entries(users).map(([userId, user]) => [ | ||
userId, | ||
{ | ||
...user, | ||
lastLogin: moment(user.lastLogin * 1000).fromNow() | ||
} | ||
]) | ||
); | ||
res.render('admin/users', { | ||
page: 'Users', | ||
user: req.user, | ||
users: updatedUsers | ||
}); | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
|
||
router.get('/', (req, res) => { | ||
res.render('admin/visitors', { | ||
page: 'Visitors', | ||
user: req.user | ||
}); | ||
}); | ||
|
||
module.exports = router; |
Oops, something went wrong.