Skip to content

Commit 76c8317

Browse files
authored
Merge pull request #246 from oskarrough/fix/deal-damage-equal-to-block
Fix/deal damage equal to block
2 parents 46f89d5 + c80654e commit 76c8317

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

src/game/actions.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -250,21 +250,24 @@ function playCard(state, {card, target}) {
250250
*/
251251
export function useCardActions(state, {target, card}) {
252252
if (!card.actions) return state
253-
let newState = state
253+
254+
let nextState = state
255+
254256
card.actions.forEach((action) => {
255257
// Don't run action if it has an invalid condition.
256258
if (action.conditions && !conditionsAreValid(state, action.conditions)) {
257-
return newState
259+
return
258260
}
259-
if (!action.parameter) action.parameter = {}
260261

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

264266
// Run the action (and add the `card` to the parameters
265-
newState = allActions[action.type](newState, {...action.parameter, card})
267+
nextState = allActions[action.type](nextState, {...action.parameter, card})
266268
})
267-
return newState
269+
270+
return nextState
268271
}
269272

270273
/**
@@ -321,7 +324,7 @@ function addEnergyToPlayer(state, props) {
321324
* Removes health from a target, respecting vulnerable and block.
322325
* @type {ActionFn<{target: string, amount: number}>}
323326
*/
324-
const removeHealth = (state, {target, amount}) => {
327+
const removeHealth = (state, {target, amount = 0}) => {
325328
return produce(state, (draft) => {
326329
getRoomTargets(draft, target).forEach((t) => {
327330
if (t.powers.vulnerable) amount = powers.vulnerable.use(amount)

src/game/utils-state.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,17 @@ export function getCurrRoom(state) {
4747
*/
4848
export function getRoomTargets(state, targetQuery) {
4949
if (!targetQuery || typeof targetQuery !== 'string') throw new Error('Bad query string')
50-
const room = getCurrRoom(state)
5150

5251
// Player
5352
if (targetQuery.includes(CardTargets.player)) return [state.player]
53+
5454
// All enemies
55-
if (targetQuery === CardTargets.allEnemies) return room.monsters
55+
if (targetQuery === CardTargets.allEnemies) return getCurrRoom(state).monsters
56+
5657
// Single enemy
5758
if (targetQuery.startsWith(CardTargets.enemy)) {
5859
const index = Number(targetQuery.split('enemy')[1])
59-
const monster = room.monsters[index]
60+
const monster = getCurrRoom(state).monsters[index]
6061
if (monster) return [monster]
6162
}
6263

tests/actions.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,6 @@ test('Flourish card adds a healing "regen" buff', (t) => {
373373
t.is(state.player.currentHealth, 72)
374374
let state2 = a.playCard(state, {target: 'player', card: flourish})
375375

376-
// Pacify the monster...
377-
// produce(state2, (draft) => {
378-
// getCurrRoom(draft).monsters[0].intents = []
379-
// getTargets(state, 'enemy0').intents = []
380-
// })
381-
382376
t.is(state2.player.powers.regen, flourish.powers.regen, 'regen is applied to player')
383377
state2 = a.endTurn(state2)
384378
t.is(state2.player.currentHealth, 72, 'it doesnt go above max hp')
@@ -409,10 +403,7 @@ test('target "allEnemies" works for damage as well as power', (t) => {
409403
t.is(room.monsters.length, 2, 'we have two enemies')
410404
t.is(room.monsters[0].currentHealth, 24)
411405
t.is(room.monsters[1].currentHealth, 13)
412-
t.falsy(
413-
room.monsters[0].powers.vulnerable && room.monsters[1].powers.vulnerable,
414-
'none are vulnerable',
415-
)
406+
t.falsy(room.monsters[0].powers.vulnerable && room.monsters[1].powers.vulnerable, 'none are vulnerable')
416407
const card = createCard('Thunderclap')
417408
const nextState = a.playCard(state, {card})
418409
t.is(getRoomTargets(nextState, 'enemy0')[0].currentHealth, 24 - card.damage)
@@ -508,3 +499,13 @@ test('upgraded cards are really upgraded', (t) => {
508499

509500
test.todo('playing defend on an enemy ?')
510501
test.todo('can apply a power to a specific monster')
502+
503+
test('"Deal damage equal to block" mechanic works', (t) => {
504+
const {state} = t.context
505+
t.is(state.player.currentHealth, 72)
506+
t.is(getRoomTargets(state, 'enemy0')[0].currentHealth, 42)
507+
const state2 = a.playCard(state, {card: createCard('Defend'), target: 'player'})
508+
t.is(state2.player.block, 5)
509+
const state3 = a.playCard(state2, {card: createCard('Body Slam'), target: 'enemy0'})
510+
t.is(getRoomTargets(state3, 'enemy0')[0].currentHealth, 42 - 5)
511+
})

0 commit comments

Comments
 (0)