Extract calculation of spell price to its own file

This commit is contained in:
Johannes Loher 2021-05-13 20:37:53 +02:00
parent a2e2aea78b
commit 473ec3a903
2 changed files with 52 additions and 50 deletions

View file

@ -1,10 +1,10 @@
import { DS4Actor } from "../actor/actor";
import { hoursPerDay, minutesPerHour, secondsPerMinute, secondsPerRound } from "../common/time-helpers";
import { DS4 } from "../config";
import { createCheckRoll } from "../rolls/check-factory";
import notifications from "../ui/notifications";
import { AttackType, DS4ItemData, DS4SpellDataData, ItemType, TemporalUnit, UnitData } from "./item-data";
import { AttackType, DS4ItemData, ItemType } from "./item-data";
import { DS4ItemPreparedData } from "./item-prepared-data";
import { calculateSpellPrice } from "./type-specific-helpers/spell";
/**
* The Item class for DS4
@ -29,57 +29,10 @@ export class DS4Item extends Item<DS4ItemData, DS4ItemPreparedData> {
this.data.data.rollable = false;
}
if (this.data.type === "spell") {
this.data.data.price = this.calculateSpellPrice(this.data.data);
this.data.data.price = calculateSpellPrice(this.data.data);
}
}
protected calculateSpellPrice(data: DS4SpellDataData): number | null {
const spellPriceFactor = DS4Item.calculateSpellPriceFactor(data.cooldownDuration);
const baseSpellPrices = [
data.minimumLevels.healer !== null ? 10 + (data.minimumLevels.healer - 1) * 35 : null,
data.minimumLevels.wizard !== null ? 10 + (data.minimumLevels.wizard - 1) * 50 : null,
data.minimumLevels.sorcerer !== null ? 10 + (data.minimumLevels.sorcerer - 1) * 65 : null,
].filter((baseSpellPrice: number | null): baseSpellPrice is number => baseSpellPrice !== null);
const baseSpellPrice = Math.min(...baseSpellPrices);
return baseSpellPrice === Infinity ? null : baseSpellPrice * spellPriceFactor;
}
protected static calculateSpellPriceFactor(temporalData: UnitData<TemporalUnit>): number {
let days: number;
if (Number.isNumeric(temporalData.value)) {
switch (temporalData.unit) {
case "days": {
days = temporalData.value;
break;
}
case "hours": {
days = temporalData.value / hoursPerDay;
break;
}
case "minutes": {
days = temporalData.value / (hoursPerDay * minutesPerHour);
break;
}
case "rounds": {
days = (temporalData.value * secondsPerRound) / (hoursPerDay * minutesPerHour * secondsPerMinute);
break;
}
}
} else {
switch (temporalData.unit) {
case "days": {
days = 2;
break;
}
default: {
days = 0;
break;
}
}
}
return Math.clamped(Math.floor(days), 0, 2) + 1;
}
isNonEquippedEuipable(): boolean {
return "equipped" in this.data.data && !this.data.data.equipped;
}

View file

@ -0,0 +1,49 @@
import { hoursPerDay, minutesPerHour, secondsPerMinute, secondsPerRound } from "../../common/time-helpers";
import { DS4SpellDataData, TemporalUnit, UnitData } from "../item-data";
export function calculateSpellPrice(data: DS4SpellDataData): number | null {
const spellPriceFactor = calculateSpellPriceFactor(data.cooldownDuration);
const baseSpellPrices = [
data.minimumLevels.healer !== null ? 10 + (data.minimumLevels.healer - 1) * 35 : null,
data.minimumLevels.wizard !== null ? 10 + (data.minimumLevels.wizard - 1) * 50 : null,
data.minimumLevels.sorcerer !== null ? 10 + (data.minimumLevels.sorcerer - 1) * 65 : null,
].filter((baseSpellPrice: number | null): baseSpellPrice is number => baseSpellPrice !== null);
const baseSpellPrice = Math.min(...baseSpellPrices);
return baseSpellPrice === Infinity ? null : baseSpellPrice * spellPriceFactor;
}
function calculateSpellPriceFactor(temporalData: UnitData<TemporalUnit>): number {
let days: number;
if (Number.isNumeric(temporalData.value)) {
switch (temporalData.unit) {
case "days": {
days = temporalData.value;
break;
}
case "hours": {
days = temporalData.value / hoursPerDay;
break;
}
case "minutes": {
days = temporalData.value / (hoursPerDay * minutesPerHour);
break;
}
case "rounds": {
days = (temporalData.value * secondsPerRound) / (hoursPerDay * minutesPerHour * secondsPerMinute);
break;
}
}
} else {
switch (temporalData.unit) {
case "days": {
days = 2;
break;
}
default: {
days = 0;
break;
}
}
}
return Math.clamped(Math.floor(days), 0, 2) + 1;
}