UNPKG

@azurapi/azurapi

Version:

Open Source Azur Lane Local Database

92 lines 3.16 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); // CacheUpdater.ts /** * Functions relating to updating the cache * @packageDocumentation */ const Data_1 = require("./Data"); const fs_1 = __importDefault(require("fs")); const UpdateChecker_1 = require("./UpdateChecker"); class Updater { /** * Constructor * @param client AzurAPI instance */ constructor(client) { this.client = client; } /** * Check for updates then update and load cache */ async update() { const updates = await UpdateChecker_1.check(); if (updates.length > 0) { this.client.emit('updateAvalible', updates); return Promise.all(updates.map(async (type) => { const raw = JSON.parse(await UpdateChecker_1.fetch(Data_1.data[type])) ?? []; if (type === 'voicelines') { /** * Voice lines have to be massaged. * We set the ship's ID as a property of each Voiceline. */ let mappedVls = []; for (const [shipId, voicelines] of Object.entries(raw)) { //@ts-ignore voicelines['id'] = shipId; //@ts-ignore mappedVls.push(voicelines); } this.client.set('voicelines', mappedVls); } else { this.client.set(type, raw); } fs_1.default.writeFileSync(Data_1.local[type], JSON.stringify(raw)); })); } } /** * Start cron job */ start() { if (!this.cron) this.client.emit('debug', 'Notify for new data updates enabled. AzurAPI Client will check for data updates every hour.'); if (this.cron) clearInterval(this.cron); this.cron = setInterval(() => this.update(), this.client.rate); } /** * Stop cron job */ stop() { if (this.cron) clearInterval(this.cron); this.cron = undefined; } /** * Check if folder and JSON files exist then load cache */ async init() { if (!fs_1.default.existsSync(Data_1.baseFolder)) fs_1.default.mkdirSync(Data_1.baseFolder); await this.load(); this.client.emit('ready'); } /** * Load the cache */ load() { for (let i = 0; i < Object.keys(Data_1.local).length; i++) { let key = Object.keys(Data_1.local)[i]; if (!fs_1.default.existsSync(Data_1.local[key])) return this.update(); this.client.set(key, Object.values(JSON.parse(fs_1.default.readFileSync(Data_1.local[key]).toString()) || [])); } } } exports.default = Updater; //# sourceMappingURL=CacheUpdater.js.map