Show native receipts for store purchases

Persist the actual grant in the purchase log and open one native receipt per unread entry. Keep pending receipts during log trimming and resume them when the price table becomes available.

Verified TypeScript types, GUI patch application, and Quirrel syntax.
This commit is contained in:
N0VAN0M4LY committed 2026-11-26 10:00:19 +08:00
1 parent d7fc94e551
commit 4c2aa876b6
13 files changed
+376 -6

No files matched your search

@@ -4,12 +4,16 @@
* - `userlogs{ <16-hex-id>{ type:i=<EULT> time:i=<unixSec> roomId:i64=0 [disabled:b=yes] body{ ... } } }`
* (UserLog::serialize 0x95f160, entry fields 0x95ec30; EULT_OPEN_TROPHY = 30 at 0x1a5599b).
*/
import { ensureBlock, findBlock, setParam, type BlkBlock, type BlkParam } from '@wto/blk';
import { appendParam, emptyBlock, ensureBlock, findBlock, findParam, setParam, type BlkBlock, type BlkParam } from '@wto/blk';
import { intField } from './blk-fields.ts';
import { serverTimeToUnixSeconds, type ServerTime } from './server-time.ts';
const TROPHIES = 'trophies';
const USERLOGS = 'userlogs';
const PURCHASE_RECEIPT = 'purchaseReceipt';
// Registered by the client at 0x1a5569e (2.61.0.37).
const EULT_BUYENTITLEMENT = 13;
export const EULT_OPEN_TROPHY = 30;
export const EULT_BUY_TROPHY = 20;
@@ -34,8 +38,16 @@ export interface NewUserLogEntry {
export function appendUserLog(profile: BlkBlock, entry: NewUserLogEntry): string {
const logs = ensureBlock(profile, USERLOGS);
const seq = logs.blocks.length + 1;
const idHex = ((BigInt(entry.timeSec) * 1000n) + BigInt(seq)).toString(16).padStart(16, '0');
const timeId = BigInt(entry.timeSec) * 1000n;
// Older entries may be out of timestamp order after a system clock correction.
let highestId = 0n;
for (const log of logs.blocks) {
if (!/^[0-9a-f]{16}$/i.test(log.name)) continue;
const logId = BigInt('0x' + log.name);
if (logId > highestId) highestId = logId;
}
const id = timeId > highestId ? timeId : highestId + 1n;
const idHex = id.toString(16).padStart(16, '0');
logs.blocks.push({
name: idHex,
params: [
@@ -52,6 +64,40 @@ export function appendUserLog(profile: BlkBlock, entry: NewUserLogEntry): string
return idHex;
}
interface EntitlementPurchase {
readonly productId: string;
readonly goldenEagleCost: number | undefined;
readonly goldenEagles: number;
readonly silverLions: number;
readonly premiumDays: number;
readonly vehicles: readonly string[];
}
export function appendEntitlementPurchase(
profile: BlkBlock, purchase: EntitlementPurchase, time: ServerTime,
): void {
const bodyParams: BlkParam[] = [
{ name: 'name', value: { type: 'string', value: purchase.productId } },
];
if (purchase.goldenEagleCost !== undefined) {
bodyParams.push({ name: 'cost', value: { type: 'int', value: purchase.goldenEagleCost } });
}
const receipt = emptyBlock(PURCHASE_RECEIPT);
if (purchase.goldenEagles > 0) setParam(receipt, 'goldIncome', { type: 'int', value: purchase.goldenEagles });
if (purchase.silverLions > 0) setParam(receipt, 'wpIncome', { type: 'int64', value: BigInt(purchase.silverLions) });
if (purchase.premiumDays > 0) setParam(receipt, 'premiumDays', { type: 'int', value: purchase.premiumDays });
for (const vehicle of purchase.vehicles) {
appendParam(receipt, 'aircraftGift', { type: 'string', value: vehicle });
}
appendUserLog(profile, {
type: EULT_BUYENTITLEMENT,
timeSec: serverTimeToUnixSeconds(time),
bodyParams,
bodyBlocks: [receipt],
});
}
/** Mark seen user logs as disabled (`cln_update` `haveToDisable:b=yes` -> `disabled:b=yes`, 0x95ed29/0x95ed66). */
export function disableUserLogs(profile: BlkBlock, ids: Iterable<string>): void {
const logs = findBlock(profile, USERLOGS);
@@ -62,8 +108,18 @@ export function disableUserLogs(profile: BlkBlock, ids: Iterable<string>): void
}
}
/** Keep only the newest `keep` user logs (tuning/limits.jsonc userLogsKept); run on every profile refresh. */
function isPendingPurchaseReceipt(log: BlkBlock): boolean {
if (intField(log, 'type') !== EULT_BUYENTITLEMENT) return false;
const disabled = findParam(log, 'disabled')?.value;
if (disabled?.type === 'bool' && disabled.value) return false;
const body = findBlock(log, 'body');
return body !== undefined && findBlock(body, PURCHASE_RECEIPT) !== undefined;
}
/** Keep the newest `keep` user logs and any purchase receipts the client has not seen. */
export function trimUserLogs(profile: BlkBlock, keep: number): void {
const logs = findBlock(profile, USERLOGS);
if (logs && logs.blocks.length > keep) logs.blocks.splice(0, logs.blocks.length - keep);
if (!logs || logs.blocks.length <= keep) return;
const cutoff = logs.blocks.length - keep;
logs.blocks = logs.blocks.filter((log, index) => index >= cutoff || isPendingPurchaseReceipt(log));
}
@@ -21,6 +21,10 @@ export function serverTimeNow(): ServerTime {
return serverTimeFromUnixMs(Date.now());
}
export function serverTimeToUnixSeconds(time: ServerTime): number {
return Number((time - UNIX_EPOCH) / (1000n * TICKS_PER_MILLISECOND));
}
/** A whole or fractional number of hours as ticks. */
export function hoursToTicks(hours: number): bigint {
return BigInt(Math.round(hours * 3_600_000)) * TICKS_PER_MILLISECOND;
@@ -9,6 +9,7 @@ import type { BlkBlock } from '@wto/blk';
import { addGoldenEagles, addSilverLions, balanceOf, canAfford, pay, payGjnCents } from '../profile/profile-currency.ts';
import { extendPremiumAccount, hasEntitlement, recordEntitlementPurchase } from '../profile/profile-entitlements.ts';
import { addOwnedVehicle, ownsVehicle } from '../profile/profile-hangar.ts';
import { appendEntitlementPurchase } from '../profile/profile-trophies-log.ts';
import { ACCEPTED, refused, type RuleOutcome } from '../research/rule-outcome.ts';
import type { StoreCatalogue, StoreProduct } from './store-catalogue.ts';
@@ -65,16 +66,30 @@ export function buyFreeStoreProduct(profile: BlkBlock, product: StoreProduct, no
}
function grantProductRewards(profile: BlkBlock, product: StoreProduct, nowFiletime: bigint): void {
const before = balanceOf(profile);
const newVehicles: string[] = [];
if (product.id !== 'PremiumAccount') recordEntitlementPurchase(profile, product.id);
if (product.goldIncome) addGoldenEagles(profile, product.goldIncome);
if (product.silverIncome) addSilverLions(profile, product.silverIncome);
if (product.premiumDays) extendPremiumAccount(profile, product.premiumDays, nowFiletime);
if (product.vehicles) {
for (const vehicle of product.vehicles) {
if (!ownsVehicle(profile, vehicle.name)) addOwnedVehicle(profile, vehicle.name);
if (!ownsVehicle(profile, vehicle.name)) {
addOwnedVehicle(profile, vehicle.name);
newVehicles.push(vehicle.name);
}
for (const part of vehicle.parts) {
if (!ownsVehicle(profile, part)) addOwnedVehicle(profile, part);
}
}
}
const after = balanceOf(profile);
appendEntitlementPurchase(profile, {
productId: product.id,
goldenEagleCost: product.goldCost,
goldenEagles: after.goldenEagles - before.goldenEagles,
silverLions: after.silverLions - before.silverLions,
premiumDays: product.premiumDays ?? 0,
vehicles: newVehicles,
}, nowFiletime);
}