Skip to content

Commit

Permalink
Fixed spell casting
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaumRystra committed Jan 25, 2025
1 parent 0b499b9 commit 4993506
Show file tree
Hide file tree
Showing 25 changed files with 628 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ describe('Apply Action Properties', function () {
{
contents: [{
inline: true,
name: 'Attribute damaged',
name: 'Stat damaged',
value: '−2 Resource Name',
}],
targetIds: [creatureId],
Expand Down Expand Up @@ -435,7 +435,7 @@ describe('Apply Action Properties', function () {
contents: [
{
inline: true,
name: 'Attribute restored',
name: 'Stat restored',
value: '+13 Attribute Reset By testEvent Event',
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,22 @@ export default async function applyActionProperty(
task: PropTask, action: EngineAction, result: TaskResult, userInput: InputProvider
): Promise<void> {
const prop = task.prop;
if (prop.type !== 'action' && prop.type !== 'spell') {
throw new Meteor.Error('wrong-property', `Expected an action or a spell, got ${prop.type} instead`);
}
const targetIds = prop.target === 'self' ? [action.creatureId] : task.targetIds;

// If the action is a a spell, make sure we have spell slot defined
if (prop.type === 'spell') {
const scope = await getEffectiveActionScope(action);
if (!('slotLevel' in scope)) {
result.pushScope = {
'~slotLevel': { value: prop.level },
'slotLevel': { value: prop.level },
};
}
}

//Log the name and summary, check that the property has enough resources to fire
if (prop.summary?.text) {
await recalculateInlineCalculations(prop.summary, action, 'reduce', userInput);
Expand All @@ -31,7 +45,7 @@ export default async function applyActionProperty(
}, targetIds);

// Check Uses
if (prop.usesLeft <= 0) {
if (prop.usesLeft !== undefined && prop.usesLeft <= 0) {
result.appendLog({
name: 'Error',
value: `${getPropertyTitle(prop)} does not have enough uses left`,
Expand All @@ -52,7 +66,7 @@ export default async function applyActionProperty(

await spendResources(action, prop, targetIds, result, userInput);

const attack: CalculatedField = prop.attackRoll || prop.attackRollBonus;
const attack = prop.attackRoll;

// Attack if there is an attack roll
if (attack && attack.calculation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('Apply Adjustment Properties', function () {
contents: [
{
inline: true,
name: 'Attribute damaged',
name: 'Ability damaged',
value: '−2 Attribute',
}
],
Expand All @@ -132,7 +132,7 @@ describe('Apply Adjustment Properties', function () {
contents: [
{
inline: true,
name: 'Attribute damaged',
name: 'Ability damaged',
value: '−2 Attribute',
}
],
Expand All @@ -148,7 +148,7 @@ describe('Apply Adjustment Properties', function () {
contents: [
{
inline: true,
name: 'Attribute damaged',
name: 'Ability damaged',
value: '−2 Attribute',
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('Apply Damage Properties', function () {
}, {
contents: [{
inline: true,
name: 'Attribute damaged',
name: 'Health bar damaged',
value: '−13 Hit Points',
}],
updates: [
Expand Down Expand Up @@ -145,7 +145,7 @@ describe('Apply Damage Properties', function () {
contents: [
{
inline: true,
name: 'Attribute damaged',
name: 'Health bar damaged',
value: '−14 Hit Points',
}
],
Expand Down Expand Up @@ -181,7 +181,7 @@ describe('Apply Damage Properties', function () {
contents: [
{
inline: true,
name: 'Attribute damaged',
name: 'Health bar damaged',
value: '−14 Hit Points',
}
],
Expand All @@ -197,7 +197,7 @@ describe('Apply Damage Properties', function () {
contents: [
{
inline: true,
name: 'Attribute damaged',
name: 'Health bar damaged',
value: '−14 Hit Points',
}
],
Expand Down Expand Up @@ -228,7 +228,7 @@ describe('Apply Damage Properties', function () {
contents: [
{
inline: true,
name: 'Attribute damaged',
name: 'Health bar damaged',
value: '−22 Hit Points',
}
],
Expand Down
1 change: 1 addition & 0 deletions app/imports/api/engine/action/functions/applyAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default async function applyAction(action: EngineAction, userInput: Input
!action.task.targetIds?.length
&& action.tabletopId
&& 'prop' in action.task
&& 'target' in action.task.prop
&& (
action.task.prop?.target === 'singleTarget' ||
action.task.prop?.target === 'multipleTargets'
Expand Down
10 changes: 0 additions & 10 deletions app/imports/api/engine/action/functions/userInput/InputProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ type InputProvider = {
* Get the details of a check or save
*/
check(suggestedParams: CheckParams): Promise<CheckParams>;
/**
* Get the details of casting a spell
*/
castSpell(suggestedParams: Partial<CastSpellParams>): Promise<CastSpellParams>;
}

export type Advantage = 0 | 1 | -1;
Expand All @@ -53,10 +49,4 @@ export type CheckParams = {
targetAbilityVariableName?: string;
}

export type CastSpellParams = {
spellId: string,
slotId: string | undefined,
ritual: boolean,
}

export default InputProvider;
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ export default function getReplayChoicesInputProvider(actionId: string, decision
check() {
return Promise.resolve(decisionStack.pop());
},
castSpell() {
return Promise.resolve(decisionStack.pop());
},
}
return replaySavedInput;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import InputProvider, { CastSpellParams } from '/imports/api/engine/action/functions/userInput/InputProvider';
import InputProvider from '/imports/api/engine/action/functions/userInput/InputProvider';

const inputProviderForTests: InputProvider = {
async targetIds(target, currentTargetIds = []) {
Expand Down Expand Up @@ -42,9 +42,6 @@ const inputProviderForTests: InputProvider = {
async check(suggestedParams) {
return suggestedParams;
},
async castSpell(suggestedParams) {
return suggestedParams as CastSpellParams;
},
}

export const critInputProvider: InputProvider = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ export default function saveInputChoices(action: EngineAction, userInput: InputP
}

// For every function in the given input provider
for (const key in userInput) {
let key: keyof InputProvider;
for (key in userInput) {
const oldFn = userInput[key];
if (!oldFn) continue;
// Make a new function that does the same thing, but saves the result to action._decisions
const newFn = async (...args) => {
const result = await oldFn(...args);
Expand Down
19 changes: 8 additions & 11 deletions app/imports/api/engine/action/tasks/Task.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CreatureProperty, CreaturePropertyTypes } from '/imports/api/creature/creatureProperties/CreatureProperties';
import { CheckParams } from '/imports/api/engine/action/functions/userInput/InputProvider';

type Task = PropTask | DamagePropTask | ItemAsAmmoTask | CheckTask | ResetTask | CastSpellTask;
Expand All @@ -9,14 +10,8 @@ type BaseTask = {
silent?: boolean | undefined;
}

type Prop = {
_id: string;
type: string;
[key: string]: any,
}

export type PropTask = BaseTask & {
prop: Prop;
prop: CreatureProperty;
subtaskFn?: undefined;
silent?: undefined;
}
Expand All @@ -30,13 +25,13 @@ export type DamagePropTask = BaseTask & {
title?: string;
operation: 'increment' | 'set';
value: number;
targetProp: Prop;
targetProp: CreatureProperty;
};
}

export type ItemAsAmmoTask = BaseTask & {
subtaskFn: 'consumeItemAsAmmo';
prop: Prop;
prop: CreatureProperty;
silent?: undefined;
params: {
value: number;
Expand All @@ -57,10 +52,12 @@ export type ResetTask = BaseTask & {
}

export type CastSpellTask = BaseTask & {
prop?: Prop | undefined;
prop: CreaturePropertyTypes['spell'];
silent?: undefined;
subtaskFn: 'castSpell';
params: {
spellId: string | undefined;
slotId: string | undefined;
ritual: boolean | undefined;
withoutSpellSlot: boolean | undefined;
};
}
4 changes: 2 additions & 2 deletions app/imports/api/engine/action/tasks/TaskResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class TaskResult {
this.scope = {};
}
// Appends the log content to the latest mutation
appendLog(content: LogContent & { silenced: boolean }, targetIds: string[]) {
appendLog(content: LogContent & { silenced: boolean | undefined }, targetIds: string[]) {
// Create a shallow copy of the content
const logContent: LogContent = { ...content };
// remove false silenced properties
Expand All @@ -42,7 +42,7 @@ export default class TaskResult {
}
latestMutation.contents.push(logContent);
}
appendParserContextErrors(context: Context, targetIds) {
appendParserContextErrors(context: Context, targetIds: string[]) {
if (!context.errors?.length) return;
if (!this.mutations.length) {
this.mutations.push({ targetIds, contents: [] });
Expand Down
Loading

0 comments on commit 4993506

Please sign in to comment.