Source: lib/classes.js

import { LZString } from "../lib/importet/lzstring_min.js";
import { getCockpitSettings, saveToStorage, cockpitModules } from "./functions.js?cb=20240904-01";

/**
 * CockpitUser mit allen Einstellungen und Daten, die im Browser gespeichert werden
 */
export class CockpitUser {
    /**
     * constructor CockpitUser
     * @param {string} username ReSi-Nickname
     */
    constructor (username) {
        this.username = username;
        this.version = { v: "1.1.0", cb: "20240904-01" };
    }
    get usernotes () {
        return localStorage.usno_notes ? JSON.parse(LZString.decompressFromUTF16(localStorage.usno_notes)) : [];
    }
    /**
     * @param {array} val
     */
    set usernotes (val) {
        this._usernotes = val;
        localStorage.usno_notes = LZString.compressToUTF16(JSON.stringify(val));
    }
    get buildingnotes () {
        return localStorage.build_notice ? JSON.parse(LZString.decompressFromUTF16(localStorage.build_notice)) : {};
    }
    /**
     * @param {{}} val 
     */
    set buildingnotes (val) {
        this._buildingnotes = val;
        localStorage.build_notice = LZString.compressToUTF16(JSON.stringify(val));
    }
    get aaoColors () {
        return localStorage.cockpit_aao_colors ? JSON.parse(localStorage.cockpit_aao_colors) : [];
    }
    set aaoColors (val) {
        this._aaoColors = val;
        localStorage.cockpit_aao_colors = JSON.stringify(val);
    }
    get renameValues () {
        return localStorage.cockpit_renamevalues ? JSON.parse(LZString.decompressFromUTF16(localStorage.cockpit_renamevalues)) : { buildingTypes: {}, vehicleTypes: {}, buildings: {} };
    }
    set renameValues (obj) {
        this._renameValues = obj;
        localStorage.cockpit_renamevalues = LZString.compressToUTF16(JSON.stringify(obj));
    }
    get settings () {
        /**
         * @param {string} key 
         * @param {{}} init 
         * @returns {{}}
         */
        const getSettingsFromLocalStorage = (key, init) => localStorage[key] ? JSON.parse(localStorage[key]) : init;

        const settings = {
            usernotes: getSettingsFromLocalStorage("usno_settings", { show_on_top: true, headline_class: "s8" }),
            buildnotice: getSettingsFromLocalStorage("buno_settings", { hide_notes: false, headline_class: "s8", ad_visible: true }),
            missionhotkeys: getSettingsFromLocalStorage(
                "mission_short_keys",
                { alarm: "w", alarm_close: "s", alarm_share: "q", alarm_share_close: "e", previous: "a", next: "d", alarm_next: "f", alarm_share_next: "r" }
            ),
            missiondistance: +localStorage?.missiondistance || Number.POSITIVE_INFINITY,
            muenzlimit: +localStorage?.mm_value || 0,
            radioCalls: getSettingsFromLocalStorage("nfms5_settings", { color: "default", del_on_open: false }),
            missionkeywords: getSettingsFromLocalStorage("missionkeywords", { default: true, keywords: [], only_keyword: false }),
            renamemanager: getSettingsFromLocalStorage(
                "cockpit_renamesettings",
                { visible: false, zero_before: false }
            ),
            cockpit: getCockpitSettings()
        };

        return settings;
    }
    /**
     * @param {{}} settings object with settings
     */
    set settings (settings) {

        if (typeof settings !== "object") throw new TypeError("'settings' has to be an object");

        this._settings = settings;

        const { usernotes, buildnotice, missiondistance, muenzlimit, cockpit, missionhotkeys, radioCalls, missionkeywords, renamemanager } = settings,
            { modules } = cockpit;

        saveToStorage("cockpit_settings", cockpit, "local");

        if (modules.usernotes) saveToStorage("usno_settings", usernotes, "local");

        if (modules.buildingnotes) saveToStorage("buno_settings", buildnotice, "local");

        if (modules.missiondistance) saveToStorage("missiondistance", missiondistance, "local");

        if (modules.muenzenmarker) saveToStorage("mm_value", muenzlimit, "local");

        if (modules.missionhotkeys) saveToStorage("mission_short_keys", missionhotkeys, "local");

        if (modules.radioCalls) saveToStorage("nfms5_settings", radioCalls, "local");

        if (modules.missionkeywords) saveToStorage("missionkeywords", missionkeywords, "local");

        if (modules.renamemanager) saveToStorage("cockpit_renamesettings", renamemanager, "local");
    }
}

/**
 * ReSi-Cockpit Modul für die Modulübersicht
 */
export class CockpitModule {
    /**
     * constructor CockpitModule
     * @param {boolean} active
     * @param {string} key  
     */
    constructor (active, key) {
        if (typeof active !== "boolean") throw new TypeError("'active' has to be a boolean!");
        if (typeof key !== "string") throw new TypeError("'key' has to be a string!");

        const { name, description } = cockpitModules[key];

        this.name = name;
        this.active = active;
        this.key = key;
        this.description = description;
    }
    get card () {
        const html =
            `<div class="element-card" key="${ this.key }">
                <div class="element-card-image">
                    <i class="fa-solid fa-file-code"></i>
                </div>
                <div class="element-card-description">
                    <div class="element-card-description-name">${ this.name }</div>
                    <div class="element-card-description-task">${ this.description }</div>
                    <br>
                </div>
                <div class="element-card-buy cockpit-module-${ this.active ? "active" : "inactive" }" key="${ this.key }">
                    Modul ${ this.active ? "deaktivieren" : "aktivieren" }
                </div>
            </div>`;
        return html;
    }
}

/**
 * ReSi-Einsatz
 */
export class CockpitMission {
    /**
     * constructor CockpitMission
     * @param {{}} mission 
     * @param {{}} aVehicleCategories 
     */
    constructor (mission, aVehicleCategories) {
        const { id, name, duration, credits, neededVehicles, patients, maxCredits } = mission;

        const getMinMaxPatientCredits = (min, max, na) => na ? { min: min * 300, max: max * 320 } : { min: min * 300, max: max * 300 },
            getPatientsCredits = () => patients ? getMinMaxPatientCredits(patients.min, patients.max, patients.naChance) : { min: 0, max: 0 },
            mapNeededVehicles = (obj, readable = false) => {
                return Object.keys(obj).map(i => { return readable ? { name: aVehicleCategories[i].name, value: obj[i] } : { name: i, value: obj[i] } });
            }

        this.id = id;
        this.name = name;
        this.duration = duration;
        this.credits = { player: credits, alliance: credits * 0.8, patients: getPatientsCredits() };
        this.maxCredits = { player: maxCredits, alliance: maxCredits * 0.8 };
        this.neededVehicles = typeof neededVehicles === "object" ? mapNeededVehicles(neededVehicles) : neededVehicles;
        this.patients = patients || null;
        this.readableNeededVehicles = typeof neededVehicles === "object" ? mapNeededVehicles(neededVehicles, true) : [];
    }
    get readableVehicleHTML () {
        let html = `<table><tbody>`;
        for (const vehicle of this.readableNeededVehicles) {
            const { name, value } = vehicle;
            html += `<tr><td>${ value }</td><td>${ name }</td></tr>`;
        }
        html += `</tbody></table>`;
        return html;
    }
    get additionalContentHTML () {
        const getPatients = (min, max) => max > min ? `${ min.toLocaleString() } bis ${ max.toLocaleString() }` : min.toLocaleString();
        const getPatientMuenzen = (min, max) => max > min ? `Min: ${ min.toLocaleString() }, Max: ${ max.toLocaleString() } Münzen` : `${ min.toLocaleString() } Münzen`;

        let html = `<table><tbody>`;
        html += `<tr><td>Vergütung Spieler:</td><td>${ this.credits.player.toLocaleString() } bis ${ this.maxCredits.player.toLocaleString() } Münzen</td></tr>`;
        html += `<tr><td>Vergütung Verband:</td><td>${ this.credits.alliance.toLocaleString() } bis ${ this.maxCredits.alliance.toLocaleString() } Münzen</td></tr>`;
        if (this.patients) {
            html += `<tr>
                        <td>Vergütung Patienten:</td>
                        <td>${ getPatientMuenzen(this.credits.patients.min, this.credits.patients.max) }</td>
                    </tr>
                    <tr>
                        <td>Anzahl Patienten:</td>
                        <td>${ getPatients(this.patients.min, this.patients.max) }</td>
                    </tr>`;
            if (this.patients.naChance > 0) {
                html += `<tr><td>Notarztwahrscheinlichkeit:</td><td>${ this.patients.naChance } %</td></tr>`
            }
        }
        if (this.duration > 0) {
            html += `<tr><td>Einsatzdauer:</td><td>circa ${ (this.duration / 60).toLocaleString() } Minuten</td></tr>`;
        }
        html += `</tbody></table>`;
        return html;
    }
}

/**
 * ReSi Fahrzeug in der Gebäudeübersicht 
 */
export class CockpitVehicle {
    /**
     * Constructor CockpitVehicle
     */
    constructor ({ id, typeId, buildingId, buildingType, vehicleName, shortTypeName, typeName, buildingName }) {
        this.id = id;
        this.buildingId = buildingId;
        this.buildingType = buildingType;
        this.typeId = typeId;
        this.name = vehicleName;
        this.vehicleTypeName = { short: shortTypeName, long: typeName };
        this.buildingName = buildingName;
    }
    get renamed () {
        return this._renamed;
    }
    /**
     * @param {string} newName 
     */
    set renamed (newName) {
        this._renamed = newName;
    }
    get count () {
        return this._count;
    }
    /**
     * @param {{type: number, alias: number}} obj 
     */
    set count (obj) {
        this._count = obj;
    }
}