Salta al contenuto principale

Framework in codice

Ricetta: una statistica derivata

Calcola un valore a partire da altri valori con una funzione del registro.

Obiettivo

Mostra un Ward pari alla metà dei PF massimi, arrotondata per difetto.

La modifica

Dichiara la funzione nello schema.

schemas/keeper.json
  "ledgerFunctions": [
    {
      "id": "ward",
      "name": "Ward",
      "field": "ward",
      "source": "rules/ward.ts"
    }
  ]
  1. Righe 19-22 field è il valore salvato che Ward riempie.
rules/ward.ts
export function ward(currentValue: number, self: ElementRef): number {
  const max_hp: number = element.var(self, "max_hp") as number;
  return math.floor(max_hp / 2);
}
  1. Riga 1 currentValue è il valore finora. La maggior parte dei valori derivati lo ignora.
  2. Riga 2 Leggi ciò che ti serve con element.var.
  3. Riga 3 Restituisci il numero derivato.

Il costruttore restituisce ancora il campo, con valore iniziale 0.

rules/keeper.ts
export function construct(
  name: string,
  max_hp: number,
): { name: string; max_hp: number; hp: number; ward: number } {
  return { name: name, max_hp: max_hp, hp: max_hp, ward: 0 };
}
  1. Righe 4-5 ward viene restituito come 0. La funzione del registro lo ricava subito dopo.

Provalo

scenarios/new-keeper.json
  "ledger": [{ "function": "ward", "currentValue": 0, "expected": 5 }]
  1. Riga 6 Controlla la funzione: parte da 0 e deve restituire 5.

Aggiungi ward anche a expectedValues in ogni scenario del Keeper.

Avanti