Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/deal damage equal to block #246

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/game/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,21 +250,24 @@ function playCard(state, {card, target}) {
*/
export function useCardActions(state, {target, card}) {
if (!card.actions) return state
let newState = state

let nextState = state

card.actions.forEach((action) => {
// Don't run action if it has an invalid condition.
if (action.conditions && !conditionsAreValid(state, action.conditions)) {
return newState
return
}
if (!action.parameter) action.parameter = {}

// Make sure the action is called with a target, preferably the target you dropped the card on.
if (!action.parameter) action.parameter = {}
action.parameter.target = target

// Run the action (and add the `card` to the parameters
newState = allActions[action.type](newState, {...action.parameter, card})
nextState = allActions[action.type](nextState, {...action.parameter, card})
})
return newState

return nextState
}

/**
Expand Down Expand Up @@ -321,7 +324,7 @@ function addEnergyToPlayer(state, props) {
* Removes health from a target, respecting vulnerable and block.
* @type {ActionFn<{target: string, amount: number}>}
*/
const removeHealth = (state, {target, amount}) => {
const removeHealth = (state, {target, amount = 0}) => {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was the fix 🤷‍♂️

return produce(state, (draft) => {
getRoomTargets(draft, target).forEach((t) => {
if (t.powers.vulnerable) amount = powers.vulnerable.use(amount)
Expand Down Expand Up @@ -584,6 +587,9 @@ function dealDamageEqualToBlock(state, {target}) {
if (state.player.block) {
const block = state.player.block
return removeHealth(state, {target, amount: block})
getRoomTargets(state, target).forEach((t) => {

})
oskarrough marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/game/utils-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,17 @@ export function getCurrRoom(state) {
*/
export function getRoomTargets(state, targetQuery) {
if (!targetQuery || typeof targetQuery !== 'string') throw new Error('Bad query string')
const room = getCurrRoom(state)

// Player
if (targetQuery.includes(CardTargets.player)) return [state.player]

// All enemies
if (targetQuery === CardTargets.allEnemies) return room.monsters
if (targetQuery === CardTargets.allEnemies) return getCurrRoom(state).monsters

// Single enemy
if (targetQuery.startsWith(CardTargets.enemy)) {
const index = Number(targetQuery.split('enemy')[1])
const monster = room.monsters[index]
const monster = getCurrRoom(state).monsters[index]
if (monster) return [monster]
}

Expand Down
21 changes: 11 additions & 10 deletions tests/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,6 @@ test('Flourish card adds a healing "regen" buff', (t) => {
t.is(state.player.currentHealth, 72)
let state2 = a.playCard(state, {target: 'player', card: flourish})

// Pacify the monster...
// produce(state2, (draft) => {
// getCurrRoom(draft).monsters[0].intents = []
// getTargets(state, 'enemy0').intents = []
// })

t.is(state2.player.powers.regen, flourish.powers.regen, 'regen is applied to player')
state2 = a.endTurn(state2)
t.is(state2.player.currentHealth, 72, 'it doesnt go above max hp')
Expand Down Expand Up @@ -409,10 +403,7 @@ test('target "allEnemies" works for damage as well as power', (t) => {
t.is(room.monsters.length, 2, 'we have two enemies')
t.is(room.monsters[0].currentHealth, 24)
t.is(room.monsters[1].currentHealth, 13)
t.falsy(
room.monsters[0].powers.vulnerable && room.monsters[1].powers.vulnerable,
'none are vulnerable',
)
t.falsy(room.monsters[0].powers.vulnerable && room.monsters[1].powers.vulnerable, 'none are vulnerable')
const card = createCard('Thunderclap')
const nextState = a.playCard(state, {card})
t.is(getRoomTargets(nextState, 'enemy0')[0].currentHealth, 24 - card.damage)
Expand Down Expand Up @@ -508,3 +499,13 @@ test('upgraded cards are really upgraded', (t) => {

test.todo('playing defend on an enemy ?')
test.todo('can apply a power to a specific monster')

test('"Deal damage equal to block" mechanic works', (t) => {
const {state} = t.context
t.is(state.player.currentHealth, 72)
t.is(getRoomTargets(state, 'enemy0')[0].currentHealth, 42)
const state2 = a.playCard(state, {card: createCard('Defend'), target: 'player'})
t.is(state2.player.block, 5)
const state3 = a.playCard(state2, {card: createCard('Body Slam'), target: 'enemy0'})
t.is(getRoomTargets(state3, 'enemy0')[0].currentHealth, 42 - 5)
})
Loading