mirror of
https://gitgud.io/BondageProjects/Bondage-College.git
synced 2026-04-28 04:19:50 +00:00
188 lines
6.7 KiB
JavaScript
188 lines
6.7 KiB
JavaScript
"use strict";
|
|
/** @type {LogRecord[]} */
|
|
var Log = [];
|
|
|
|
/**
|
|
* Adds a new entry to the player's logs, renews the value if it already exists.
|
|
* @template {LogGroupType} T
|
|
* @param {LogNameType[T]} NewLogName - The name of the log
|
|
* @param {T} NewLogGroup - The name of the log's group
|
|
* @param {number} [NewLogValue] - Value for the log as the time in ms. Is undefined if the value is permanent
|
|
* @param {boolean} [Push=true] - TRUE if we must push the log to the server
|
|
* @returns {void} - Nothing
|
|
*/
|
|
function LogAdd(NewLogName, NewLogGroup, NewLogValue, Push) {
|
|
|
|
// Makes sure the value is numeric
|
|
if (typeof NewLogValue === "string") NewLogValue = CommonParseInt(NewLogValue) ?? undefined;
|
|
|
|
// Checks to make sure we don't duplicate a log
|
|
const entry = Log.find(l => l.Group === NewLogGroup && l.Name === NewLogName);
|
|
if (entry) {
|
|
entry.Value = NewLogValue;
|
|
} else {
|
|
/** @type {LogRecord} */
|
|
const newEntry = {
|
|
Name: NewLogName,
|
|
Group: NewLogGroup,
|
|
Value: NewLogValue
|
|
};
|
|
Log.push(newEntry);
|
|
}
|
|
|
|
// Sends the log to the server
|
|
if ((Push == null) || Push)
|
|
ServerPlayerLogSync();
|
|
|
|
}
|
|
|
|
/**
|
|
* Deletes a log entry.
|
|
* @template {LogGroupType} T
|
|
* @param {LogNameType[T]} DelLogName - The name of the log
|
|
* @param {T} DelLogGroup - The name of the log's group
|
|
* @param {boolean} [Push=true] - TRUE if we must push the log to the server
|
|
* @returns {void} - Nothing
|
|
*/
|
|
function LogDelete(DelLogName, DelLogGroup, Push) {
|
|
|
|
// Finds the log entry and deletes it
|
|
for (let L = 0; L < Log.length; L++)
|
|
if ((Log[L].Name == DelLogName) && (Log[L].Group == DelLogGroup)) {
|
|
Log.splice(L, 1);
|
|
break;
|
|
}
|
|
|
|
// Sends the new log to the server
|
|
if ((Push == null) || Push)
|
|
ServerPlayerLogSync();
|
|
|
|
}
|
|
|
|
/**
|
|
* Deletes all log entries to starts with the name.
|
|
* @param {string} DelLogName - The name of the log
|
|
* @param {LogGroupType} DelLogGroup - The name of the log's group
|
|
* @param {boolean} [Push=true] - TRUE if we must push the log to the server
|
|
* @returns {void} - Nothing
|
|
*/
|
|
function LogDeleteStarting(DelLogName, DelLogGroup, Push) {
|
|
for (let L = 0; L < Log.length; L++)
|
|
if ((Log[L].Name.substring(0, DelLogName.length) == DelLogName) && (Log[L].Group == DelLogGroup)) {
|
|
LogDelete(Log[L].Name, DelLogGroup, Push);
|
|
L = L - 1;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Deletes all log entries in a particular log group.
|
|
* @param {LogGroupType} DelLogGroup - The name of the log's group
|
|
* @param {boolean} [Push=true] - TRUE if we must push the log to the server
|
|
* @returns {void} - Nothing
|
|
*/
|
|
function LogDeleteGroup(DelLogGroup, Push) {
|
|
Log = Log.filter(L => L.Group !== DelLogGroup);
|
|
|
|
// Sends the new log to the server
|
|
if ((Push == null) || Push)
|
|
ServerPlayerLogSync();
|
|
}
|
|
|
|
// Checks if the log exists, return true if it does (if there's a value, it counts as an expiry time)
|
|
|
|
/**
|
|
* Searches for an existing log entry.
|
|
* @template {LogGroupType} T
|
|
* @param {LogNameType[T]} QueryLogName - The name of the log to search for
|
|
* @param {T} QueryLogGroup - The name of the log's group
|
|
* @returns {boolean} - Returns TRUE if there is an existing log matching the Name/Group with no value or a value above the current time in ms.
|
|
*/
|
|
function LogQuery(QueryLogName, QueryLogGroup) {
|
|
const entry = Log.find(l => l.Group === QueryLogGroup && l.Name === QueryLogName);
|
|
if (!entry) return false;
|
|
|
|
// Loose null-check here in case there's a null or an undefined stuck in there
|
|
return entry.Value == null || entry.Value >= CurrentTime;
|
|
}
|
|
|
|
/**
|
|
* Checks if there's a log entry with extra ID characters in the log of the player (Exemple: BlockScreenABC return true for ID: A, B or C)
|
|
* @template {LogGroupType} T
|
|
* @param {LogNameType[T]} LogName - The log name to scan
|
|
* @param {T} LogGroup - The log group to scan
|
|
* @param {string} ID - The ID to validate (letter, number or other chars are fine)
|
|
* @returns {boolean} - Returns true, if the log contains that ID
|
|
*/
|
|
function LogContain(LogName, LogGroup, ID) {
|
|
if (Log == null) return false;
|
|
for (let L of Log)
|
|
if (LogGroup == L.Group)
|
|
if (L.Name.substring(0, LogName.length) == LogName)
|
|
return (L.Name.substring(LogName.length, 100).indexOf(ID) >= 0);
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Returns the value associated to a log.
|
|
* @template {LogGroupType} T
|
|
* @param {LogNameType[T]} QueryLogName - The name of the log to query the value
|
|
* @param {T} QueryLogGroup - The name of the log's group
|
|
* @returns {number | null | undefined} - Returns the value of the log which is a date represented in ms or undefined. Returns null if no matching log is found.
|
|
*/
|
|
function LogValue(QueryLogName, QueryLogGroup) {
|
|
const entry = Log.find(l => l.Group === QueryLogGroup && l.Name === QueryLogName);
|
|
if (!entry) return null;
|
|
return entry.Value;
|
|
}
|
|
|
|
/**
|
|
* Loads the account log.
|
|
* @param {readonly LogRecord[]} NewLog - Existing logs received by the server
|
|
* @returns {void} - Nothing
|
|
*/
|
|
function LogLoad(NewLog) {
|
|
|
|
// Make sure we have something to load
|
|
Log = [];
|
|
if (NewLog != null) {
|
|
|
|
// Add each log entry one by one, validates the type to prevent client crashes
|
|
for (let L = 0; L < NewLog.length; L++)
|
|
if ((typeof NewLog[L].Name === "string") && (typeof NewLog[L].Group === "string") && ((NewLog[L].Value == null) || (typeof NewLog[L].Value === "number")))
|
|
LogAdd(NewLog[L].Name, NewLog[L].Group, NewLog[L].Value, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Searches for an existing log entry on another character.
|
|
* @template {LogGroupType} T
|
|
* @param {Character} C - Character to search on
|
|
* @param {LogNameType[T]} QueryLogName - The name of the log to search for
|
|
* @param {T} QueryLogGroup - The name of the log's group
|
|
* @returns {boolean} - Returns TRUE if there is an existing log matching the Name/Group with no value or a value above the current time in ms.
|
|
*/
|
|
function LogQueryRemote(C, QueryLogName, QueryLogGroup) {
|
|
if (C.IsPlayer()) return LogQuery(QueryLogName, QueryLogGroup);
|
|
if (!C.Rule || !Array.isArray(C.Rule)) return false;
|
|
var R = C.Rule.find(r => r.Name == QueryLogName && r.Group == QueryLogGroup);
|
|
return (R != null) && ((R.Value == null) || (R.Value >= CurrentTime));
|
|
}
|
|
|
|
/**
|
|
* Filters the Player's log and returns the rule entries that the player's owner is allowed to see.
|
|
* @param {boolean} OwnerIsLover - Indicates that the requester is also the player's lover.
|
|
* @returns {LogRecord[]} - A list of rules that the player's owner is permitted to see
|
|
*/
|
|
function LogGetOwnerReadableRules(OwnerIsLover) {
|
|
return Log.filter(L => L.Group == "OwnerRule" || (L.Group == "LoverRule" && (OwnerIsLover || L.Name.includes("Owner"))));
|
|
}
|
|
|
|
/**
|
|
* Filters the Player's log and returns the rule entries that the player's lover is allowed to see.
|
|
* @returns {LogRecord[]} - A list of rules that the player's lover is permitted to see
|
|
*/
|
|
function LogGetLoverReadableRules() {
|
|
return Log.filter(L => L.Group == "LoverRule");
|
|
}
|