From the archive

This commit is contained in:
Nevernown
2025-03-16 15:02:23 +01:00
commit 7ed0202142
21 changed files with 1081 additions and 0 deletions

64
backbone/cache.js Normal file
View File

@@ -0,0 +1,64 @@
const _ = require('underscore');
const state = require('./state.js');
let cache = {};
function i (id) {
return new Promise((resolve, reject) => {
state.create(id);
state.read(id)
.then(data => {
cache[id] = data;
resolve();
})
.catch(err => reject(err));
});
}
function c (id) {
if(_.has(cache, id)) {
state.update(id, cache[id]);
console.info(`[${id}] Committed new state`);
} else {
console.error(`[${id}] Could not commit nonexistant state`);
}
}
function r (id) {
state.read(id)
.then(data => {
cache[id] = data
console.info(`[${id}] Reverted state`);
})
.catch(console.error);
}
function e (id) {
if(_.has(cache, id)) {
delete cache[id];
console.info(`[${id}] Erased state`);
state.delete(id);
} else {
console.error(`[${id}] Could not erase nonexistant state`);
}
}
function s (id) {
return new Promise((resolve, reject) => {
if(_.has(cache, id)) {
resolve(cache[id]);
} else {
state.read(id)
.then(data => {
cache[id] = data;
console.info(`[${id}] read and cached state`);
resolve(cache[id]);
})
.catch(err => reject(err));
}
})
}
module.exports = {
init: i,
commit: c,
revert: r,
erase: e,
state: s
}

26
backbone/setup.js Normal file
View File

@@ -0,0 +1,26 @@
const cache = require('./cache.js');
module.exports = client => {
return new Promise((resolve, reject) => {
console.info(`Logged in as ${client.user.tag}!`);
console.info(`[BOOT] Initiating state manager...`);
Promise.all(client.guilds.cache.map(guild => cache.init(guild.id)))
.then(() => resolve())
.catch(err => console.error(err));
console.info(`[BOOT] Locating game channels...`);
client.guilds.cache.each(guild => {
let channel = guild.channels.cache.find(channel => channel.name === 'machine');
if(channel) {
console.info(`[${guild.id}] Found machine channel`);
channel.send(`\`[${guild.id}] NeverSteam Engine v2 startup successful\``)
.then(() => console.info(`[${guild.id}] Sent startup notification`))
.catch(console.error);
} else {
console.info(`[${guild.id}] Could not find machine channel`);
}
});
})
}

90
backbone/state.js Normal file
View File

@@ -0,0 +1,90 @@
const fs = require('fs');
const location = './state'
if(!fs.existsSync(location)) {
fs.mkdir(location, err => {
if(err) { console.error(err); }
});
}
function l (id) { return `${location}/${id}.json` }
function c (id) {
let path = l(id);
if(fs.existsSync(path)) {
console.info(`[${id}] State already exists`);
} else {
fs.writeFile(path, JSON.stringify({}), err => {
if(err) {
console.error(err);
} else {
console.info(`[${id}] Created new state`);
}
});
}
}
function r (id) {
return new Promise((resolve, reject) => {
let path = l(id);
if(fs.existsSync(path)) {
console.info(`[${id}] Reading existing state`);
fs.readFile(path, (err, data) => {
if(err) {
console.error(err);
reject(err);
} else {
console.info(`[${id}] Read state`);
try {
let state = JSON.parse(data);
console.info(`[${id}] Deserialized state`);
resolve(state);
} catch(err) {
reject(err);
}
}
});
} else {
console.error(`[${id}] Failed to locate state`);
reject(new Error('State not found.'));
}
});
}
function u (id, state) {
let path = l(id);
if(fs.existsSync(path)) {
fs.writeFile(path, JSON.stringify(state), err => {
if(err) {
console.error(err);
} else {
console.info(`[${id}] Updated state`);
}
});
} else {
console.error(`[${id}] Failed to locate state`);
}
}
function d (id) {
let path = l(id);
if(fs.existsSync(path)) {
fs.unlink(path, err=> {
if(err) {
console.error(err);
} else {
console.info(`[${id}] Removed state`);
}
});
} else {
console.error(`[${id}] Failed to locate state`);
}
}
module.exports = {
create: c,
read: r,
update: u,
delete: d
}

7
backbone/util.js Normal file
View File

@@ -0,0 +1,7 @@
function c(o) {
return JSON.parse(JSON.stringify(o));
}
module.exports = {
clone: c
}