UNPKG

genshin-manager

Version:

<div align="center"> <p> <a href="https://www.npmjs.com/package/genshin-manager"><img src="https://img.shields.io/npm/v/genshin-manager.svg?maxAge=3600" alt="npm version" /></a> <a href="https://www.npmjs.com/package/genshin-manager"><img src="https:

174 lines (173 loc) 6.64 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NoticeManager = exports.NoticeManagerEvents = void 0; const ts_deepmerge_1 = require("ts-deepmerge"); const AnnContentNotFoundError_1 = require("../errors/AnnContentNotFoundError"); const AnnError_1 = require("../errors/AnnError"); const OutOfRangeError_1 = require("../errors/OutOfRangeError"); const Notice_1 = require("../models/Notice"); const PromiseEventEmitter_1 = require("../utils/PromiseEventEmitter"); /** * NoticeManager events * @see {@link NoticeManager} */ var NoticeManagerEvents; (function (NoticeManagerEvents) { /** * When a notice is added, fires * @event ADD_NOTICE * @listener * | param | type | description | * | --- | --- | --- | * | notice | {@link Notice} | Added Notice | */ NoticeManagerEvents["ADD_NOTICE"] = "ADD_NOTICE"; /** * When a notice is removed, fires * @event REMOVE_NOTICE * @listener * | param | type | description | * | --- | --- | --- | * | notice | {@link Notice} | Removed Notice | */ NoticeManagerEvents["REMOVE_NOTICE"] = "REMOVE_NOTICE"; })(NoticeManagerEvents || (exports.NoticeManagerEvents = NoticeManagerEvents = {})); /** * Class for fetching notices from mihoyo */ class NoticeManager extends PromiseEventEmitter_1.PromiseEventEmitter { /** * Create a NoticeManager * @param language Language of notices * @param updateInterval Update interval(ms) Min: 1 minute * @param urlParams URL params */ constructor(language, updateInterval, urlParams) { super(); /** * Notices * @key Notice ID * @value Notice */ this.notices = new Map(); this.language = language; this.updateInterval = updateInterval; if (this.updateInterval && (this.updateInterval < NoticeManager.MIN_UPDATE_INTERVAL || this.updateInterval > 2147483647)) { throw new OutOfRangeError_1.OutOfRangeError('level', this.updateInterval, NoticeManager.MIN_UPDATE_INTERVAL, 2147483647); } this.urlParams = ts_deepmerge_1.merge.withOptions({ mergeArrays: false }, NoticeManager.defaultURLParams, urlParams !== null && urlParams !== void 0 ? urlParams : {}); if (this.updateInterval) void setInterval(() => void this.update(), this.updateInterval); } /** * Update notices */ update() { return __awaiter(this, void 0, void 0, function* () { const annContent = yield this.getAnnContent(); const annEnContent = yield this.getAnnContent('en'); const annList = yield this.getAnnList(); const annListDatas = annList.data.list.flatMap((tab) => tab.list); const annListIds = annListDatas.map((data) => data.ann_id); this.notices.forEach((notice, id) => { if (!annListIds.includes(id)) { this.emit(NoticeManagerEvents.REMOVE_NOTICE, notice); this.notices.delete(id); } }); annListDatas.forEach((data) => { if (!this.notices.has(data.ann_id)) { const content = annContent.data.list.find((content) => content.ann_id === data.ann_id); const enContent = annEnContent.data.list.find((content) => content.ann_id === data.ann_id); if (!content || !enContent) throw new AnnContentNotFoundError_1.AnnContentNotFoundError(data.ann_id); const notice = new Notice_1.Notice(data, content, enContent, this.urlParams.region); this.emit(NoticeManagerEvents.ADD_NOTICE, notice); this.notices.set(data.ann_id, notice); } }); }); } /** * Get AnnContent * @param lang Language of notices * @returns AnnContent */ getAnnContent(lang) { return __awaiter(this, void 0, void 0, function* () { return (yield this._getAnn(NoticeManager.GIT_CONTENT_URL, lang)); }); } /** * Get AnnList * @returns AnnList */ getAnnList() { return __awaiter(this, void 0, void 0, function* () { return (yield this._getAnn(NoticeManager.GIT_LIST_URL)); }); } /** * Get Ann * @param urlText URL * @param lang Language of notices * @returns Ann */ _getAnn(urlText, lang) { return __awaiter(this, void 0, void 0, function* () { const url = new URL(urlText); Object.keys(this.urlParams).forEach((key) => { if (key === 'lang') url.searchParams.append(key, lang !== null && lang !== void 0 ? lang : this.language); else url.searchParams.append(key, this.urlParams[key]); }); const res = yield fetch(url.toString()); if (!res.ok) throw new AnnError_1.AnnError(res); return (yield res.json()); }); } } exports.NoticeManager = NoticeManager; /** * Minimum update interval(ms) * @default 1 minute */ NoticeManager.MIN_UPDATE_INTERVAL = 1000 * 60 * 1; /** * URL of getAnnContent */ NoticeManager.GIT_CONTENT_URL = 'https://sg-hk4e-api-static.hoyoverse.com/common/hk4e_global/announcement/api/getAnnContent'; /** * URL of getAnnList */ NoticeManager.GIT_LIST_URL = 'https://sg-hk4e-api.hoyoverse.com/common/hk4e_global/announcement/api/getAnnList'; /** * Default URL params */ NoticeManager.defaultURLParams = { game: 'hk4e', game_biz: 'hk4e_global', lang: 'en', auth_appid: 'announcement', bundle_id: 'hk4e_global', channel_id: '1', level: '60', platform: 'pc', region: 'os_asia', sdk_presentation_style: 'fullscreen', sdk_screen_transparent: 'true', uid: '888888888', };