wren/val Public Public: anyone can read this; nobody can change it.

forked from benjivers/val

workbenchthis machine
Files f06c94705a19e63a135dcb573ff98fcaae80d78f
1
/**
2
 * Credits. The whole economy is four numbers and a losing streak, and all of
3
 * it is pure: the match calls these and stores what comes back.
4
 */
5
6
export const START_CREDITS = 800;
7
export const CREDIT_CAP = 9000;
8
export const KILL_REWARD = 200;
9
export const PLANT_REWARD = 300;
10
export const WIN_REWARD = 3000;
11
/** Rising with the streak, and capped at the third step. */
12
export const LOSS_BONUS = [1900, 2400, 2900];
13
14
export const clampCredits = (credits: number): number =>
15
  Math.max(0, Math.min(CREDIT_CAP, Math.floor(credits)));
16
17
export const award = (credits: number, amount: number): number => clampCredits(credits + amount);
18
19
/** The loss bonus for a team that has now lost `streak` rounds in a row. */
20
export const lossBonus = (streak: number): number =>
21
  LOSS_BONUS[Math.min(Math.max(streak, 1), LOSS_BONUS.length) - 1]!;
22
23
/** What a round's end pays each member of a team. */
24
export function roundReward(won: boolean, lossStreak: number, planted: boolean): number {
25
  if (won) return WIN_REWARD;
26
  // A team that lost the round but got the spike down keeps a little more.
27
  return lossBonus(lossStreak) + (planted ? PLANT_REWARD : 0);
28
}
29
30
/** A purchase either happens at a price or is refused; never a debt. */
31
export function spend(credits: number, cost: number): { ok: boolean; credits: number } {
32
  if (!Number.isFinite(cost) || cost < 0 || cost > credits) return { ok: false, credits };
33
  return { ok: true, credits: clampCredits(credits - cost) };
34
}
↑↓ navigate ↵ open esc close
↑↓ navigate ↵ open esc close

Keyboard shortcuts