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

111 lines
5.3 KiB
TypeScript

/**
* Choosing what to research and putting research points into a vehicle.
*
* - Choosing a research target (cln_set_researchable) makes a researchable vehicle the
* target of its country and type and closes the "research finished" prompt.
* - Dismissing that prompt (cln_reset_researchable) only closes it.
* - Converting free research points (cln_convert_exp) costs the points and golden eagles:
* one golden eagle per researchPointsPerGoldenEagle points, rounded up. The stock window
* (scripts/convertExp/convertExpHandler.nut) never asks for more than the vehicle still
* needs; more is cut down to that.
* - Spending banked excess research points (cln_move_exp_to_unit) puts the branch's
* researchUnits/<country>/exp<Type> into the chosen vehicle, up to what it still needs.
* - When a vehicle's research points reach what it needs and it is the target, the target is
* cleared and the vehicle recorded as just finished, which makes the client offer the next
* research (research-state.ts).
*/
import type { BlkBlock } from '@wto/blk';
import { balanceOf, pay } from '../profile/profile-currency.ts';
import { freeResearchPoints, spendFreeResearchPoints } from '../profile/profile-research-points.ts';
import {
branchExcessRp,
researchTarget,
setBranchExcessRp,
setFinishedResearch,
setResearchTarget,
} from '../profile/profile-research-targets.ts';
import { researchPointsOf, setResearchPoints } from '../profile/profile-vehicle-research.ts';
import type { ResearchRules } from './research-rules.ts';
import { currentStatus } from './research-state.ts';
import { ACCEPTED, refused, type RuleOutcome } from './rule-outcome.ts';
import type { TreeVehicle } from './tech-tree.ts';
import { VehicleStatus } from './vehicle-status.ts';
import { vehicleTypeById } from './vehicle-types.ts';
export function chooseResearchTarget(
profile: BlkBlock,
rules: ResearchRules,
vehicleName: string,
typeId: number,
): RuleOutcome {
const vehicle = rules.techTree.vehicle(vehicleName);
if (!vehicle || vehicleTypeById(typeId) !== vehicle.type) return refused('BAD_PARAMS');
if (currentStatus(profile, rules, vehicleName) !== VehicleStatus.CAN_RESEARCH) return refused('NOT_AVAILABLE');
setResearchTarget(profile, vehicle.country, vehicle.type, vehicle.name);
setFinishedResearch(profile, vehicle.country, vehicle.type, '');
return ACCEPTED;
}
export function dismissFinishedResearch(
profile: BlkBlock,
rules: ResearchRules,
country: string,
typeId: number,
): RuleOutcome {
const type = vehicleTypeById(typeId);
if (!type || !rules.techTree.hasCountry(country)) return refused('BAD_PARAMS');
setFinishedResearch(profile, country, type, '');
return ACCEPTED;
}
export function convertFreeResearchPoints(
profile: BlkBlock,
rules: ResearchRules,
vehicleName: string,
requested: number,
): RuleOutcome {
const vehicle = rules.techTree.vehicle(vehicleName);
if (!vehicle || !Number.isSafeInteger(requested) || requested <= 0) return refused('BAD_PARAMS');
if (currentStatus(profile, rules, vehicleName) !== VehicleStatus.CAN_RESEARCH) return refused('NOT_AVAILABLE');
const amount = Math.min(requested, vehicle.researchPoints - researchPointsOf(profile, vehicle.name));
const goldenEagles = Math.ceil(amount / rules.researchPointsPerGoldenEagle);
if (freeResearchPoints(profile) < amount) return refused('NOT_ENOUGH_RESEARCH_POINTS');
if (balanceOf(profile).goldenEagles < goldenEagles) return refused('NOT_ENOUGH_MONEY');
pay(profile, { silverLions: 0, goldenEagles });
spendFreeResearchPoints(profile, amount);
addResearchPoints(profile, vehicle, amount);
return ACCEPTED;
}
export function moveExcessExpToUnit(profile: BlkBlock, rules: ResearchRules, vehicleName: string): RuleOutcome {
const vehicle = rules.techTree.vehicle(vehicleName);
if (!vehicle) return refused('BAD_PARAMS');
if (currentStatus(profile, rules, vehicleName) !== VehicleStatus.CAN_RESEARCH) return refused('NOT_AVAILABLE');
const available = branchExcessRp(profile, vehicle.country, vehicle.type);
const before = researchPointsOf(profile, vehicle.name);
const needed = Math.max(0, vehicle.researchPoints - before);
const moved = Math.min(available, needed);
if (moved <= 0) return ACCEPTED;
const remaining = available - moved;
setBranchExcessRp(profile, vehicle.country, vehicle.type, remaining);
setResearchPoints(profile, vehicle.name, before + moved);
if (before + moved >= vehicle.researchPoints) {
setResearchTarget(profile, vehicle.country, vehicle.type, '');
setFinishedResearch(profile, vehicle.country, vehicle.type, remaining > 0 ? '' : vehicle.name);
} else {
setResearchTarget(profile, vehicle.country, vehicle.type, vehicle.name);
setFinishedResearch(profile, vehicle.country, vehicle.type, '');
}
return ACCEPTED;
}
export function addResearchPoints(profile: BlkBlock, vehicle: TreeVehicle, amount: number): void {
const before = researchPointsOf(profile, vehicle.name);
setResearchPoints(profile, vehicle.name, before + amount);
const finished = before < vehicle.researchPoints && before + amount >= vehicle.researchPoints;
if (finished && researchTarget(profile, vehicle.country, vehicle.type) === vehicle.name) {
setResearchTarget(profile, vehicle.country, vehicle.type, '');
setFinishedResearch(profile, vehicle.country, vehicle.type, vehicle.name);
}
}