enka-network-api
Version:
Enka-network API wrapper for Genshin Impact.
62 lines (61 loc) • 4.22 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenshinUser = void 0;
const config_file_js_1 = require("config_file.js");
const enka_system_1 = require("enka-system");
const Costume_1 = require("./character/Costume");
const Material_1 = require("./material/Material");
const ProfilePicture_1 = require("./ProfilePicture");
const Element_1 = require("./Element");
const TheaterMode_1 = require("./theater/TheaterMode");
class GenshinUser extends enka_system_1.User {
constructor(data, enka) {
const json = new config_file_js_1.JsonReader(config_file_js_1.defaultJsonOptions, data);
super(json);
this.enka = enka;
if (!enka.cachedAssetsManager.hasAllContents())
throw new Error("Complete Genshin data cache not found.\nYou need to fetch Genshin data by EnkaClient#cachedAssetsManager#fetchAllContents.");
this.uid = Number(json.getValue("uid"));
const playerInfo = json.get("playerInfo");
this.nickname = playerInfo.getAsStringWithDefault(null, "nickname");
this.signature = playerInfo.getAsStringWithDefault(null, "signature");
const profilePic = playerInfo.get("profilePicture");
this.profilePicture = profilePic.has("id") ? ProfilePicture_1.ProfilePicture.getById(profilePic.getAsNumber("id"), enka)
: profilePic.has("avatarId") ? ProfilePicture_1.ProfilePicture.getByOldFormat(profilePic.getAsNumber("avatarId"), profilePic.getAsNumberWithDefault(null, "costumeId"), enka)
: null;
this.showConstellationPreview = playerInfo.getAsBooleanWithDefault(false, "isShowAvatarTalent");
this.charactersPreview = playerInfo.has("showAvatarInfoList") ? playerInfo.get("showAvatarInfoList").mapArray((_, p) => {
const costumeId = p.getAsNumberWithDefault(null, "costumeId");
const costume = costumeId === null ? Costume_1.Costume.getDefaultCostumeByCharacterId(p.getAsNumber("avatarId"), enka) : Costume_1.Costume.getById(p.getAsNumber("avatarId"), costumeId, enka);
const elementType = p.has("energyType") ? Element_1.elementList[p.getAsNumber("energyType")] : null;
const preview = {
costume: costume,
level: p.getAsNumber("level"),
element: elementType ? Element_1.Element.getByElementType(elementType, enka) : null,
constellation: p.getAsNumberWithDefault(null, "talentLevel"),
};
return preview;
}) : [];
this.nameCards = playerInfo.has("showNameCardIdList") ? playerInfo.get("showNameCardIdList").mapArray((_, id) => Material_1.Material.getMaterialById(id.getAsNumber(), enka)) : [];
this.level = playerInfo.getAsNumber("level");
this.worldLevel = playerInfo.getAsNumberWithDefault(0, "worldLevel");
this.profileCard = Material_1.Material.getMaterialById(playerInfo.getAsNumber("nameCardId"), enka);
this.achievements = playerInfo.getAsNumberWithDefault(0, "finishAchievementNum");
this.maxFriendshipCount = playerInfo.getAsNumberWithDefault(0, "fetterCount");
this.spiralAbyss = playerInfo.has("towerFloorIndex") && playerInfo.has("towerLevelIndex") ? {
floor: playerInfo.getAsNumber("towerFloorIndex"),
chamber: playerInfo.getAsNumber("towerLevelIndex"),
stars: playerInfo.getAsNumberWithDefault(0, "towerStarIndex"),
} : null;
this.theater = playerInfo.has("theaterActIndex") && playerInfo.has("theaterStarIndex") && playerInfo.has("theaterModeIndex") ? {
act: playerInfo.getAsNumber("theaterActIndex"),
stars: playerInfo.getAsNumber("theaterStarIndex"),
mode: TheaterMode_1.TheaterMode.getById(playerInfo.getAsNumber("theaterModeIndex"), enka),
} : null;
this.ttl = json.getAsNumberWithDefault(-1, "ttl");
this.enkaProfile = json.has("owner") ? new enka_system_1.EnkaProfile(enka.options.enkaSystem, json.getAsJsonObject("owner")) : null;
this.enkaGameAccountHash = json.getAsStringWithDefault(null, "owner", "hash");
this.url = `${enka.options.enkaUrl}/u/${this.uid}/`;
}
}
exports.GenshinUser = GenshinUser;