94 lines
2.6 KiB
JavaScript
94 lines
2.6 KiB
JavaScript
const moment = require('moment');
|
|
require('moment-duration-format');
|
|
module.exports = {}
|
|
const change = 62167219200; // Subtract a year, whoops
|
|
//const change = 62135683200
|
|
module.exports.convertSLTimestamp = (timestamp) => {
|
|
// Convert SCP SL timestamp to seconds since SCP epoch
|
|
const ourTs = Number(BigInt(timestamp) / 10000000n);
|
|
|
|
// Convert to Unix timestamp (seconds since Jan 1, 1970 UTC)
|
|
const unixTs = ourTs - change;
|
|
|
|
// Adjust for local timezone (Mountain Time in this case)
|
|
const date = new Date(unixTs * 1000); // Convert to milliseconds for JavaScript Date constructor
|
|
// add 1 year
|
|
date.setFullYear(date.getFullYear() + 1)
|
|
return date;
|
|
}
|
|
|
|
|
|
module.exports.toSLTimestamp = (timestamp) => {
|
|
return String(((Number(timestamp) / 1000) + change) * 10000000)
|
|
}
|
|
|
|
module.exports.slToJSON = (slDataLine) => {
|
|
raw = slDataLine.split(";");
|
|
data = {
|
|
banned_username: raw[0],
|
|
banned_id: raw[1],
|
|
expires: module.exports.convertSLTimestamp(raw[2]),
|
|
reason: raw[3],
|
|
moderator: raw[4],
|
|
banned_timestamp: module.exports.convertSLTimestamp(raw[5])
|
|
}
|
|
return data;
|
|
}
|
|
|
|
module.exports.makeBansTable = (newContents) => {
|
|
tmp = {}
|
|
lines = newContents.split("\n");
|
|
lines.forEach(line => {
|
|
if (line === "") return;
|
|
data = module.exports.slToJSON(line)
|
|
tmp[data.banned_id] = data
|
|
})
|
|
return tmp
|
|
}
|
|
|
|
module.exports.diff = (oldData, newData) => {
|
|
let added = {};
|
|
let removed = {};
|
|
|
|
// Find added keys (in newData but not in oldData)
|
|
Object.keys(newData).forEach(key => {
|
|
if (!oldData.hasOwnProperty(key)) {
|
|
added[key] = newData[key];
|
|
}
|
|
});
|
|
|
|
// Find removed keys (in oldData but not in newData)
|
|
Object.keys(oldData).forEach(key => {
|
|
if (!newData.hasOwnProperty(key)) {
|
|
removed[key] = oldData[key];
|
|
}
|
|
});
|
|
|
|
// Construct the result object with added and removed changes
|
|
let result = {
|
|
added: added,
|
|
removed: removed
|
|
};
|
|
|
|
return result;
|
|
}
|
|
|
|
module.exports.generateBanFile = (bans) => {
|
|
let banFile = "";
|
|
Object.keys(bans).forEach(key => {
|
|
ban = bans[key];
|
|
banFile += `${ban.banned_username};${ban.banned_id};${module.exports.toSLTimestamp(ban.expires)};${ban.reason};${ban.moderator};${module.exports.toSLTimestamp(ban.banned_timestamp)}\n`
|
|
})
|
|
return banFile;
|
|
};
|
|
|
|
module.exports.generateBanLine = (ban) => {
|
|
return `${ban.banned_username};${ban.banned_id};${module.exports.toSLTimestamp(ban.expires)};${ban.reason};${ban.moderator};${module.exports.toSLTimestamp(ban.banned_timestamp)}`
|
|
}
|
|
|
|
module.exports.removeBan = (bans, id) => {
|
|
delete bans[id];
|
|
return bans;
|
|
}
|
|
|
|
return module.exports |