warframe-worldstate-parser
Version:
An Open parser for Warframe's Worldstate in Javascript
134 lines (133 loc) • 4.59 kB
JavaScript
import { cdn, deProxy } from "../supporting/ImgCdn.mjs";
import { n as __decorateMetadata, t as __decorate } from "../../decorate-BCC26nnB.mjs";
import { WorldStateObject } from "./WorldStateObject.mjs";
import { Type } from "class-transformer";
import { IsBoolean, IsDate, IsObject, IsString } from "class-validator";
import { fromNow, languageString, parseDate, timeDeltaToString, toNow } from "warframe-worldstate-data/utilities";
//#region lib/models/News.ts
const updateReg = /(update|hotfix|patch-notes)/i;
const primeAccessReg = /(access)/i;
const streamReg = /(devstream|prime-time|warframeinternational|stream)/i;
const langString = /(\/Lotus\/Language\/)/i;
/**
* Represents a game news item
* @augments {WorldStateObject}
*/
var News = class extends WorldStateObject {
/**
* The news message
*/
message;
/**
* The link to the forum post
*/
link;
/**
* The news's image link
*/
imageLink;
/**
* Whether this has priority over other news or not
*/
priority;
/**
* The date at which the post was published
*/
date;
/**
* The message translated into different locales
*/
translations;
/**
* Whether this is an update news item
*/
update;
/**
* Whether this is a prime access news item
*/
primeAccess;
/**
* Whether or not this is a stream
*/
stream;
/**
* Whether or not this news is mobile only.
*/
mobileOnly;
/**
* @param data The news data
* @param locale Locale to use for determining language
*/
constructor(data, { locale } = { locale: "en" }) {
super({
...data,
Activation: data.EventStartDate,
Expiry: data.EventEndDate
});
this.message = (data.Messages.find((msg) => msg.LanguageCode === locale) || { Message: "" }).Message;
if (langString.test(this.message)) this.message = languageString(this.message, locale);
this.link = data.Prop;
if (!this.link?.length && data.Links && data.Links.length) this.link = (data.Links.find((l) => l.LanguageCode === locale) || { Link: "https://www.warframe.com/" }).Link;
this.imageLink = data.ImageUrl ? deProxy(data.ImageUrl) : cdn("img/news-placeholder.png");
this.priority = data.Priority;
this.date = parseDate(data.Date);
this.translations = {};
data.Messages.forEach((message) => {
this.translations[message.LanguageCode] = message.Message;
if (langString.test(message.Message)) this.translations[message.LanguageCode] = languageString(message.Message, message.LanguageCode);
});
this.update = this.isUpdate();
this.primeAccess = this.isPrimeAccess();
this.stream = this.isStream();
this.mobileOnly = data.MobileOnly;
}
/**
* Whether or not this is about a game update
*/
isUpdate() {
return updateReg.test(this.link);
}
/**
* Whether this is about a new Prime Access
*/
isPrimeAccess() {
return primeAccessReg.test(this.link);
}
/**
* Whether this is about a new stream
*/
isStream() {
return streamReg.test(this.message);
}
/**
* ETA string (at time of object creation)
*/
get eta() {
if (this.expiry) return `in ${timeDeltaToString(fromNow(this.expiry))}`;
return `${timeDeltaToString(toNow(this.date))} ago`;
}
/**
* The title of the news item in the specified language
* @param {string} langCode Ex. 'es', 'de', 'fr'
* @returns {string} The title of the news item in the specified language
*/
getTitle(langCode) {
return langCode in this.translations ? this.translations[langCode] : this.message;
}
};
__decorate([IsString(), __decorateMetadata("design:type", String)], News.prototype, "message", void 0);
__decorate([IsString(), __decorateMetadata("design:type", String)], News.prototype, "link", void 0);
__decorate([IsString(), __decorateMetadata("design:type", String)], News.prototype, "imageLink", void 0);
__decorate([IsBoolean(), __decorateMetadata("design:type", Boolean)], News.prototype, "priority", void 0);
__decorate([
IsDate(),
Type(() => Date),
__decorateMetadata("design:type", typeof Date === "undefined" ? Object : Date)
], News.prototype, "date", void 0);
__decorate([IsObject(), __decorateMetadata("design:type", typeof Record === "undefined" ? Object : Record)], News.prototype, "translations", void 0);
__decorate([IsBoolean(), __decorateMetadata("design:type", Boolean)], News.prototype, "update", void 0);
__decorate([IsBoolean(), __decorateMetadata("design:type", Boolean)], News.prototype, "primeAccess", void 0);
__decorate([IsBoolean(), __decorateMetadata("design:type", Boolean)], News.prototype, "stream", void 0);
__decorate([IsBoolean(), __decorateMetadata("design:type", Boolean)], News.prototype, "mobileOnly", void 0);
//#endregion
export { News };