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:
168 lines (167 loc) • 7.19 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Notice = void 0;
const cheerio = __importStar(require("cheerio"));
const ImageAssets_1 = require("../models/assets/ImageAssets");
const convertToUTC_1 = require("../utils/convertToUTC");
/**
* Class for compiling in-game announcement information
*/
class Notice {
/**
* Create a Notice
* @param annList AnnList
* @param annContent AnnContent
* @param enAnnContent AnnContent(lang=en)
* @param region Region
*/
constructor(annList, annContent, enAnnContent, region) {
this.region = region;
if (annList.ann_id !== annContent.ann_id)
throw new Error('ID mismatch');
this.id = annList.ann_id;
this.lang = annContent.lang;
this.type = annList.type;
this.typeLabel = annList.type_label;
this.tag = Number(annList.tag_label);
this.tagIcon = ImageAssets_1.ImageAssets.fromURL(annList.tag_icon);
this.version = annList.remind_ver;
this.title = annContent.title;
this.subtitle = annContent.subtitle
.replace(/<br.*?>/g, '\n')
.replace(/\r/g, '');
this.banner = ImageAssets_1.ImageAssets.fromURL(annContent.banner);
const unescapedContent = unescape(annContent.content);
this.$ = cheerio.load(unescapedContent);
const unescapedEnContent = unescape(enAnnContent.content);
this._en$ = cheerio.load(unescapedEnContent);
let durationResult = '';
let nextElement = this.$(this.durationTitleElement).next();
while (nextElement.length && !nextElement.text().includes('〓')) {
if (!/shop|reword|Shop|Reword/g.test(nextElement.text()))
durationResult += `${nextElement.text()}\n`;
nextElement = nextElement.next();
}
const timeStrings = this.convertLocalDate(durationResult.trim()).match(/\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}/g);
if (timeStrings &&
(timeStrings === null || timeStrings === void 0 ? void 0 : timeStrings.length) >= 2 &&
!(this.tag === 3 && !this.$(this.durationTitleElement).next().is('p'))) {
timeStrings.sort((a, b) => new Date(a).getTime() - new Date(b).getTime());
this.eventStart = new Date(timeStrings[0]);
this.eventEnd = new Date(timeStrings[timeStrings.length - 1]);
}
const rewardImgURL = this.$('img').attr('src');
this.rewardImg = rewardImgURL
? ImageAssets_1.ImageAssets.fromURL(rewardImgURL)
: undefined;
}
/**
* Get the text of the notice
* @warning This method does not exclude table tags
* @returns Notice all text
*/
get text() {
return this.convertLocalDate(this.$('p')
.map((i, el) => this.$(el).text())
.get()
.join('\n'));
}
/**
* Get the duration of the event
* @returns Event duration
*/
get eventDuration() {
if (this.eventStart && this.eventEnd)
return `${this.eventStart.toLocaleDateString('ja-JP', Notice.dateTimeFormatOptions)} ~ ${this.eventEnd.toLocaleDateString('ja-JP', Notice.dateTimeFormatOptions)}`;
if (this.tag === 2) {
return this.convertLocalDate(this.$('td')
.toArray()
.map((el) => this.$(el).text())[3]
.replace('~', ' ~')
.replace('—', ' — ')
.replace('-', ' -'));
}
if (!this.$(this.durationTitleElement).next().is('p')) {
const trFirst = this.$('tr').first();
const tdList = this.$('td')
.toArray()
.filter((el) => el.type === 'tag');
const colWidths = this.$(trFirst)
.children()
.toArray()
.filter((el) => el.type === 'tag')
.map((el) => el.attribs['data-colwidth']);
if (colWidths.length > 2) {
const startTimeColWidth = colWidths[colWidths.length - 2];
const endTimeColWidth = colWidths[colWidths.length - 1];
const startTimeRows = tdList.filter((el) => el.attribs['data-colwidth'] === startTimeColWidth);
const endTimeRows = tdList.filter((el) => el.attribs['data-colwidth'] === endTimeColWidth);
const startTime = this.$(startTimeRows[1]).text();
const endTime = this.$(endTimeRows[endTimeRows.length - 1]).text();
return `${this.convertLocalDate(startTime)} ~ ${this.convertLocalDate(endTime)}`;
}
}
let durationResult = '';
let nextElement = this.$(this.durationTitleElement).next();
while (nextElement.length && !nextElement.text().includes('〓')) {
durationResult += `${nextElement.text()}\n`;
nextElement = nextElement.next();
}
if (durationResult.trim() === '')
return undefined;
return this.convertLocalDate(durationResult.trim());
}
get durationTitleElement() {
const durationTitleElementIndex = this._en$('p')
.toArray()
.findIndex((el) => /〓.*?(Time|Duration|Wish).*?〓/g.test(this._en$(el).text()));
if (durationTitleElementIndex === -1)
return undefined;
return this.$('p').toArray()[durationTitleElementIndex];
}
/**
* Convert t tag to region time
* @warning t tags work fine when inserted with text() content because the original is \> or \<.
* @param text Text
* @returns Converted text
*/
convertLocalDate(text) {
return text
.replace(/(?<=<t class="(t_lc|t_gl)".*?>)(.*?)(?=<\/t>)/g, ($1) => (0, convertToUTC_1.convertToUTC)($1, this.region).toLocaleString('ja-JP', Notice.dateTimeFormatOptions))
.replace(/<t class="(t_lc|t_gl).*?">|<\/t>/g, '');
}
}
exports.Notice = Notice;
/**
* Date and time format options
*/
Notice.dateTimeFormatOptions = {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
};