Files
WT-Offline/apps/server/src/profile/profile-clan.ts
T

137 lines
5.6 KiB
TypeScript

/**
* Squadron (clan) membership and squadron vehicle research fields in `profile.blk`
* (aces.exe 0x91fa31..0x91fd00 and 0x906790..0x906ac5).
*/
import { ensureBlock, findBlock, setParam, type BlkBlock } from '@wto/blk';
import { int64Field, intField, removeParam, stringField } from './blk-fields.ts';
const CLAN_RESEARCH_BLOCK = 'clanResearchUnits';
export type ClanKind = 'normal' | 'battalion';
export interface ProfileClanMembership {
readonly clanId: string;
readonly clanTag: string;
readonly clanName: string;
readonly clanType: ClanKind;
readonly role: number;
readonly requestedClanId: string;
}
export interface SquadronResearchState {
readonly researchUnitName: string;
readonly spentExp: number;
readonly adminExp: number;
readonly storedLastEarnedExp: number;
}
export function clanMembershipOf(profile: BlkBlock): ProfileClanMembership {
const clanIdNum = int64Field(profile, 'clanId', -1n);
const reqNum = int64Field(profile, 'storedRequestClanId', -1n);
return {
clanId: clanIdNum > 0n ? clanIdNum.toString() : '-1',
clanTag: stringField(profile, 'clanTag'),
clanName: stringField(profile, 'clanName'),
clanType: stringField(profile, 'clanType') === 'battalion' ? 'battalion' : 'normal',
role: intField(profile, 'clanMemberRole'),
requestedClanId: clanIdNum <= 0n && reqNum > 0n ? reqNum.toString() : '-1',
};
}
export function setClanMembership(
profile: BlkBlock,
clan: { readonly clanId: string; readonly clanTag: string; readonly clanName: string; readonly clanType: ClanKind; readonly role: number },
): void {
setParam(profile, 'clanId', { type: 'int64', value: BigInt(clan.clanId) });
setParam(profile, 'clanMemberRole', { type: 'int', value: clan.role });
setParam(profile, 'clanType', { type: 'string', value: clan.clanType });
setParam(profile, 'clanTag', { type: 'string', value: clan.clanTag });
setParam(profile, 'clanName', { type: 'string', value: clan.clanName });
removeParam(profile, 'storedRequestClanId');
}
export function clearClanMembership(profile: BlkBlock): void {
removeParam(profile, 'clanId');
removeParam(profile, 'clanMemberRole');
removeParam(profile, 'clanType');
removeParam(profile, 'clanTag');
removeParam(profile, 'clanName');
removeParam(profile, 'storedRequestClanId');
}
export function setPendingClanRequest(
profile: BlkBlock,
request: { readonly clanId: string; readonly clanTag: string; readonly clanName: string } | undefined,
): void {
clearClanMembership(profile);
if (!request) return;
setParam(profile, 'storedRequestClanId', { type: 'int64', value: BigInt(request.clanId) });
setParam(profile, 'clanTag', { type: 'string', value: request.clanTag });
setParam(profile, 'clanName', { type: 'string', value: request.clanName });
}
export function squadronResearchOf(profile: BlkBlock): SquadronResearchState {
const block = findBlock(profile, CLAN_RESEARCH_BLOCK);
return {
researchUnitName: stringField(block, 'researchUnitName'),
spentExp: intField(block, 'spentExp'),
adminExp: intField(block, 'adminExp'),
storedLastEarnedExp: intField(block, 'storedLastEarnedExp'),
};
}
/** Exact formula of `clan_get_exp` at aces.exe 0x1780ba0..0x1780be5. */
export function availableSquadronRp(profile: BlkBlock, clanActivityEarnedExp = 0): number {
const state = squadronResearchOf(profile);
const earned = Math.max(clanActivityEarnedExp, state.storedLastEarnedExp);
return Math.max(0, earned + state.adminExp - state.spentExp);
}
export function setSquadronResearchTarget(profile: BlkBlock, unitName: string): void {
const block = ensureClanResearchBlock(profile);
setParam(block, 'researchUnitName', { type: 'string', value: unitName });
}
export function syncStoredClanEarnedExp(profile: BlkBlock, clanEarnedExp: number): void {
if (clanEarnedExp <= 0) return;
const state = squadronResearchOf(profile);
if (clanEarnedExp > state.storedLastEarnedExp) {
const block = ensureClanResearchBlock(profile);
setParam(block, 'storedLastEarnedExp', { type: 'int', value: clanEarnedExp });
}
}
export function spendSquadronRp(profile: BlkBlock, amount: number): void {
if (amount <= 0) return;
const state = squadronResearchOf(profile);
const block = ensureClanResearchBlock(profile);
setParam(block, 'spentExp', { type: 'int', value: state.spentExp + amount });
}
/** Set the player's available squadron RP directly (for save-cli.ts / save editor). */
export function setAvailableSquadronRp(profile: BlkBlock, desiredRp: number, clanActivityEarnedExp = 0): void {
const target = Math.max(0, Math.trunc(desiredRp));
const state = squadronResearchOf(profile);
const block = ensureClanResearchBlock(profile);
const earnedBase = Math.max(clanActivityEarnedExp, state.storedLastEarnedExp);
if (earnedBase + state.adminExp - state.spentExp === target) return;
if (target >= earnedBase) {
setParam(block, 'spentExp', { type: 'int', value: 0 });
setParam(block, 'adminExp', { type: 'int', value: target - earnedBase });
} else {
setParam(block, 'adminExp', { type: 'int', value: 0 });
setParam(block, 'spentExp', { type: 'int', value: earnedBase - target });
}
}
function ensureClanResearchBlock(profile: BlkBlock): BlkBlock {
const existing = findBlock(profile, CLAN_RESEARCH_BLOCK);
if (existing) return existing;
const block = ensureBlock(profile, CLAN_RESEARCH_BLOCK);
setParam(block, 'researchUnitName', { type: 'string', value: '' });
setParam(block, 'spentExp', { type: 'int', value: 0 });
setParam(block, 'adminExp', { type: 'int', value: 0 });
setParam(block, 'storedLastEarnedExp', { type: 'int', value: 0 });
return block;
}