@fnlb-project/fnbr
Version:
A library to interact with Epic Games' Fortnite HTTP and XMPP services
484 lines • 18.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const async_queue_1 = require("@sapphire/async-queue");
const Util_1 = require("../../util/Util");
const Endpoints_1 = tslib_1.__importDefault(require("../../../resources/Endpoints"));
const ClientPartyMemberMeta_1 = tslib_1.__importDefault(require("./ClientPartyMemberMeta"));
const PartyMember_1 = tslib_1.__importDefault(require("./PartyMember"));
const enums_1 = require("../../../resources/enums");
const EpicgamesAPIError_1 = tslib_1.__importDefault(require("../../exceptions/EpicgamesAPIError"));
/**
* Represents the client's party member
*/
class ClientPartyMember extends PartyMember_1.default {
/**
* @param party The party this member belongs to
* @param data The member data
*/
constructor(party, data) {
super(party, data);
this.meta = new ClientPartyMemberMeta_1.default(this, data.meta);
this.patchQueue = new async_queue_1.AsyncQueue();
this.update({ id: this.id, displayName: this.client.user.self.displayName, externalAuths: this.client.user.self.externalAuths });
if (this.client.lastPartyMemberMeta)
this.meta.update(this.client.lastPartyMemberMeta, true);
}
/**
* Sends a meta patch to Epicgames's servers
* @param updated The updated schema
* @throws {EpicgamesAPIError}
*/
async sendPatch(updated) {
await this.patchQueue.wait();
try {
await this.client.http.epicgamesRequest({
method: 'PATCH',
url: `${Endpoints_1.default.BR_PARTY}/parties/${this.party.id}/members/${this.id}/meta`,
headers: {
'Content-Type': 'application/json',
},
data: {
delete: [],
revision: this.revision,
update: updated,
},
}, enums_1.AuthSessionStoreKey.Fortnite);
}
catch (e) {
if (e instanceof EpicgamesAPIError_1.default && e.code === 'errors.com.epicgames.social.party.stale_revision') {
this.revision = parseInt(e.messageVars[1], 10);
this.patchQueue.shift();
return this.sendPatch(updated);
}
this.patchQueue.shift();
throw e;
}
this.revision += 1;
this.patchQueue.shift();
if (this.client.config.savePartyMemberMeta)
this.client.lastPartyMemberMeta = this.meta.schema;
return undefined;
}
/**
* Updates the client party member's readiness
* @param ready Whether the client party member is ready
* @throws {EpicgamesAPIError}
*/
async setReadiness(ready) {
let data = this.meta.get('Default:MatchmakingInfo_j');
data = this.meta.set('Default:MatchmakingInfo_j', {
...data,
MatchmakingInfo: {
...data.MatchmakingInfo,
readyStatus: ready ? 'Ready' : 'NotReady',
readyInputType: ready ? 'Touch' : 'Count',
readyStatusMMId: ready ? (0, Util_1.getUUID)().replaceAll('-', '').toUpperCase() : '',
},
});
await this.sendPatch({
'Default:MatchmakingInfo_j': data,
});
}
/**
* Updates the client party member's sitting out state
* @param sittingOut Whether the client party member is sitting out
* @throws {EpicgamesAPIError}
*/
async setSittingOut(sittingOut) {
let data = this.meta.get('Default:MatchmakingInfo_j');
data = this.meta.set('Default:MatchmakingInfo_j', {
...data,
MatchmakingInfo: {
...data.MatchmakingInfo,
readyStatus: sittingOut ? 'SittingOut' : 'NotReady',
readyStatusMMId: '',
},
});
await this.sendPatch({
'Default:MatchmakingInfo_j': data,
});
}
/**
* Updates the client party member's level
* @param level The new level
* @throws {EpicgamesAPIError}
*/
async setLevel(level) {
var _a, _b;
return this.setBattlePass((_b = (_a = this.meta.battlepass) === null || _a === void 0 ? void 0 : _a.bHasPurchasedPass) !== null && _b !== void 0 ? _b : false, level);
}
/**
* Updates the client party member's battle pass info
* @param isPurchased Whether the battle pass is purchased
* @param level The battle pass level
* @throws {EpicgamesAPIError}
*/
async setBattlePass(isPurchased, level) {
let data = this.meta.get('Default:BattlePassInfo_j');
data = this.meta.set('Default:BattlePassInfo_j', {
...data,
BattlePassInfo: {
...data.BattlePassInfo,
bHasPurchasedPass: isPurchased,
passLevel: level,
},
});
await this.sendPatch({
'Default:BattlePassInfo_j': data,
});
}
/**
* Updates the client party member's banner
* @param bannerId The new banner's id
* @param color The new banner's color
* @throws {EpicgamesAPIError}
*/
async setBanner(bannerId, color) {
var _a, _b, _c, _d, _e;
let data = this.meta.get('Default:MpLoadout1_j');
data = this.meta.set('Default:MpLoadout1_j', {
...data,
MpLoadout1: {
...data.MpLoadout1,
s: {
...(_a = data.MpLoadout1) === null || _a === void 0 ? void 0 : _a.s,
li: {
...(((_c = (_b = data.MpLoadout1) === null || _b === void 0 ? void 0 : _b.s) === null || _c === void 0 ? void 0 : _c.li) || {}),
i: bannerId,
},
lc: {
...(((_e = (_d = data.MpLoadout1) === null || _d === void 0 ? void 0 : _d.s) === null || _e === void 0 ? void 0 : _e.lc) || {}),
i: color,
},
},
},
});
await this.sendPatch({
'Default:MpLoadout1_j': data,
});
}
/**
* Updates multiple cosmetics for the client party member.
* If a cosmetic is set to `null`, it will be cleared.
* If a cosmetic is set to `undefined` or is not provided, it will remain unchanged.
* @param cosmetics An object specifying the cosmetics to update, including outfit, backpack, pickaxe and shoes.
* @throws {EpicgamesAPIError}
*/
async setCosmetics(cosmetics = {}) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
const { outfit, backpack, pickaxe, shoes, sidekick } = cosmetics;
let data = this.meta.get('Default:MpLoadout1_j');
let loadoutMetaData = this.meta.get('Default:LoadoutMeta_j');
const nextSlots = {
...(((_a = data.MpLoadout1) === null || _a === void 0 ? void 0 : _a.s) || {}),
};
if (Object.hasOwn(cosmetics, 'outfit')) {
if (outfit === null) {
delete nextSlots.ac;
loadoutMetaData = this.meta.set('Default:LoadoutMeta_j', {
...loadoutMetaData,
LoadoutMeta: {
...loadoutMetaData.LoadoutMeta,
scratchpad: [],
},
});
}
else if (outfit !== undefined) {
nextSlots.ac = {
i: outfit.id,
v: (_c = (_b = outfit.variants) === null || _b === void 0 ? void 0 : _b.map((v) => v.variantIndex.toString())) !== null && _c !== void 0 ? _c : [],
};
loadoutMetaData = this.meta.set('Default:LoadoutMeta_j', {
...loadoutMetaData,
LoadoutMeta: {
...loadoutMetaData.LoadoutMeta,
scratchpad: ((_d = outfit.enlightment) === null || _d === void 0 ? void 0 : _d.length) === 2
? [{
t: outfit.enlightment[0],
v: outfit.enlightment[1],
}]
: [],
},
});
}
}
if (Object.hasOwn(cosmetics, 'backpack')) {
if (backpack === null) {
delete nextSlots.ab;
}
else if (backpack !== undefined) {
nextSlots.ab = {
i: backpack.id,
v: (_f = (_e = backpack.variants) === null || _e === void 0 ? void 0 : _e.map((v) => v.variantIndex.toString())) !== null && _f !== void 0 ? _f : []
};
}
}
if (Object.hasOwn(cosmetics, 'pickaxe')) {
if (pickaxe === null) {
delete nextSlots.ap;
}
else if (pickaxe !== undefined) {
nextSlots.ap = {
i: pickaxe.id,
v: (_h = (_g = pickaxe.variants) === null || _g === void 0 ? void 0 : _g.map((v) => v.variantIndex.toString())) !== null && _h !== void 0 ? _h : []
};
}
}
if (Object.hasOwn(cosmetics, 'shoes')) {
if (shoes === null) {
delete nextSlots.as;
}
else if (shoes !== undefined) {
nextSlots.as = {
i: shoes.id,
v: (_k = (_j = shoes.variants) === null || _j === void 0 ? void 0 : _j.map((v) => v.variantIndex.toString())) !== null && _k !== void 0 ? _k : []
};
}
}
if (Object.hasOwn(cosmetics, 'sidekick')) {
if (sidekick === null) {
delete nextSlots.mm;
}
else if (sidekick !== undefined) {
nextSlots.mm = {
i: "CosmeticMimosa:" + sidekick.id
};
}
}
data = this.meta.set('Default:MpLoadout1_j', {
...data,
MpLoadout1: {
...data.MpLoadout1,
s: nextSlots,
},
});
const patch = {
'Default:MpLoadout1_j': data,
};
if (Object.hasOwn(cosmetics, 'outfit')) {
patch['Default:LoadoutMeta_j'] = loadoutMetaData;
}
await this.sendPatch(patch);
}
/**
* Updates the client party member's outfit
* @param id The outfit's ID
* @param variants The outfit's variants
* @param enlightment The outfit's enlightment
* @throws {EpicgamesAPIError}
*/
async setOutfit(id, variants = [], enlightment) {
return this.setCosmetics({ outfit: { id, variants, enlightment } });
}
/**
* Updates the client party member's backpack
* @param id The backpack's ID
* @param variants The backpack's variants
* @param path The backpack's path in the game files
* @throws {EpicgamesAPIError}
*/
async setBackpack(id, variants = [], path) {
return this.setCosmetics({ backpack: { id, variants, path } });
}
/**
* Updates the client party member's pet
* @param id The pet's ID
* @param variants The pet's variants
* @param path The pet's path in the game files
*/
async setPet(id, variants = [], path) {
return this.setCosmetics({ backpack: { id, variants, path: path !== null && path !== void 0 ? path : '/BRCosmetics/Athena/Items/Cosmetics/PetCarriers' } });
}
/**
* Updates the client party member's pickaxe
* @param id The pickaxe's ID
* @param variants The pickaxe's variants
* @param path The pickaxe's path in the game files
* @throws {EpicgamesAPIError}
*/
async setPickaxe(id, variants = [], path) {
return this.setCosmetics({ pickaxe: { id, variants, path } });
}
/**
* Updates the client party member's shoes
* @param id The shoes' ID
* @param variants The shoes' variants
* @param path The shoes' path in the game files
* @throws {EpicgamesAPIError}
*/
async setShoes(id, variants = [], path) {
return this.setCosmetics({ shoes: { id, variants, path } });
}
/**
* Updates the client party member's sidekick
* @param id The sidekick's ID
* @param variants The sidekick's variants
* @param path The sidekick's path in the game files
* @throws {EpicgamesAPIError}
*/
async setSidekick(id, variants = [], path) {
return this.setCosmetics({ sidekick: { id, variants, path } });
}
/**
* Updates the client party member's emote
* @param id The emote's ID
* @param path The emote's path in the game files
* @throws {EpicgamesAPIError}
*/
async setEmote(id, path) {
var _a;
if (this.meta.get('Default:FrontendEmote_j').FrontendEmote.pickable !== 'None')
await this.clearEmote();
let data = this.meta.get('Default:FrontendEmote_j');
data = this.meta.set('Default:FrontendEmote_j', {
...data,
FrontendEmote: {
...data.FrontendEmote,
pickable: `${(_a = path === null || path === void 0 ? void 0 : path.replace(/\/$/, '')) !== null && _a !== void 0 ? _a : '/BRCosmetics/Athena/Items/Cosmetics/Dances'}/${id}.${id}`,
emoteSection: -2,
},
});
await this.sendPatch({
'Default:FrontendEmote_j': data,
});
}
/**
* Updates the client party member's emoji
* @param id The emoji's ID
* @param path The emoji's path in the game files
* @throws {EpicgamesAPIError}
*/
async setEmoji(id, path) {
return this.setEmote(id, path !== null && path !== void 0 ? path : '/BRCosmetics/Athena/Items/Cosmetics/Dances/Emoji');
}
/**
* Clears the client party member's emote and emoji
* @throws {EpicgamesAPIError}
*/
async clearEmote() {
let data = this.meta.get('Default:FrontendEmote_j');
data = this.meta.set('Default:FrontendEmote_j', {
...data,
FrontendEmote: {
...data.FrontendEmote,
pickable: 'None',
emoteSection: -1,
},
});
await this.sendPatch({
'Default:FrontendEmote_j': data,
});
}
/**
* Clears the client party member's backpack
* @throws {EpicgamesAPIError}
*/
async clearBackpack() {
return this.setCosmetics({ backpack: null });
}
/**
* Clears the client party member's shoes
* @throws {EpicgamesAPIError}
*/
async clearShoes() {
return this.setCosmetics({ shoes: null });
}
/**
* Clears the client party member's sidekick
* @throws {EpicgamesAPIError}
*/
async clearSidekick() {
return this.setCosmetics({ sidekick: null });
}
/**
* Updates the client party member's match state.
* NOTE: This is visually, the client will not actually join a match
* @param isPlaying Whether the client is in a match
* @param playerCount The match player count (must be between 0 and 255)
* @param startedAt The start date of the match
* @throws {EpicgamesAPIError}
*/
async setPlaying(isPlaying = true, playerCount = 100, startedAt = new Date()) {
await this.sendPatch({
'Default:PackedState_j': this.meta.set('Default:PackedState_j', {
...this.meta.get('Default:PackedState_j'),
PackedState: {
...this.meta.get('Default:PackedState_j').PackedState,
location: isPlaying ? 'InGame' : 'PreLobby',
gameMode: isPlaying ? 'InBattleRoyale' : 'None',
},
}),
'Default:MatchmakingInfo_j': this.meta.set('Default:MatchmakingInfo_j', {
...this.meta.get('Default:MatchmakingInfo_j'),
MatchmakingInfo: {
...this.meta.get('Default:MatchmakingInfo_j').MatchmakingInfo,
hasPreloadedAthena: isPlaying,
},
}),
'Default:NumAthenaPlayersLeft_U': this.meta.set('Default:NumAthenaPlayersLeft_U', playerCount),
'Default:UtcTimeStartedMatchAthena_s': this.meta.set('Default:UtcTimeStartedMatchAthena_s', startedAt.toISOString()),
});
}
/**
* Updates the client party member's pre lobby map marker.
* [0, 0] would be the center of the map
* @param isSet Whether the marker is set
* @param locationX The marker x location
* @param locationY The marker y location
* @throws {EpicgamesAPIError}
*/
async setMarker(isSet, locationX, locationY) {
let data = this.meta.get('Default:FrontEndMapMarker_j');
data = this.meta.set('Default:FrontEndMapMarker_j', {
...data,
FrontEndMapMarker: {
...data.FrontEndMapMarker,
bIsSet: isSet,
markerLocation: {
...data.FrontEndMapMarker.markerLocation,
x: locationY || 0,
y: locationX || 0,
},
},
});
await this.sendPatch({
'Default:FrontEndMapMarker_j': data,
});
}
/**
* Updates the client party member's cosmetic stats.
* Crowns are shown when using the EID_Coronet emote
* @param crowns The amount of crowns / "Royal Royales"
* @param rankedProgression The ranked progression
* @throws {EpicgamesAPIError}
*/
async setCosmeticStats(crowns, rankedProgression) {
var _a;
let data = this.meta.get('Default:LoadoutMeta_j');
const currentStats = ((_a = data.LoadoutMeta) === null || _a === void 0 ? void 0 : _a.stats) || [];
const setStat = (name, value) => {
const idx = currentStats.findIndex((s) => s.statName === name);
if (idx > -1)
currentStats[idx].statValue = value;
else
currentStats.push({ statName: name, statValue: value });
};
setStat('HabaneroProgression', rankedProgression);
setStat('TotalRoyalRoyales', crowns);
setStat('TotalVictoryCrowns', crowns);
setStat('HasCrown', crowns > 0 ? 1 : 0);
data = this.meta.set('Default:LoadoutMeta_j', {
...data,
LoadoutMeta: {
...data.LoadoutMeta,
stats: currentStats,
},
});
await this.sendPatch({
'Default:LoadoutMeta_j': data,
});
}
}
exports.default = ClientPartyMember;
//# sourceMappingURL=ClientPartyMember.js.map