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

41 lines
1.6 KiB
TypeScript

/**
* Vehicle Research Bonus battles used today, per country and vehicle type (research/research-efficiency.ts). Kept in a block of the local server's own; the client's profile
* parser ignores it:
*
* wtoResearchBonus{ day:i=<days since 1970-01-01 UTC> country_usa{ Tank:i=2 } }
*
* The count resets when the UTC day changes (the bonus battles reset at 00:00 GMT).
*/
import { ensureBlock, findBlock, findParam, setParam, type BlkBlock } from '@wto/blk';
const BLOCK = 'wtoResearchBonus';
const DAY = 'day';
const SECONDS_PER_DAY = 86_400;
export function researchBonusBattlesUsed(profile: BlkBlock, country: string, vehicleType: string, nowUnixSec: number): number {
const block = findBlock(profile, BLOCK);
if (!block || intParam(block, DAY) !== dayOf(nowUnixSec)) return 0;
const countryBlock = findBlock(block, country);
return countryBlock ? intParam(countryBlock, vehicleType) : 0;
}
export function useResearchBonusBattle(profile: BlkBlock, country: string, vehicleType: string, nowUnixSec: number): void {
const used = researchBonusBattlesUsed(profile, country, vehicleType, nowUnixSec);
const block = ensureBlock(profile, BLOCK);
const day = dayOf(nowUnixSec);
if (intParam(block, DAY) !== day) {
block.blocks.length = 0;
setParam(block, DAY, { type: 'int', value: day });
}
setParam(ensureBlock(block, country), vehicleType, { type: 'int', value: used + 1 });
}
function dayOf(unixSec: number): number {
return Math.floor(unixSec / SECONDS_PER_DAY);
}
function intParam(block: BlkBlock, name: string): number {
const value = findParam(block, name)?.value;
return value?.type === 'int' ? value.value : 0;
}