Skip to content

Commit

Permalink
Lint a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarrough committed Dec 6, 2020
1 parent bbb1e0a commit 8f4e0b2
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 78 deletions.
2 changes: 1 addition & 1 deletion public/game/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ function decreasePlayerPowerStacks(state) {

// Decrease monster's power stacks.
function decreaseMonsterPowerStacks(state) {
return produce(state, (draft) => {
return produce(state, () => {
getCurrRoom(state).monsters.forEach((monster) => {
decreasePowers(monster.powers)
})
Expand Down
72 changes: 37 additions & 35 deletions public/game/cards.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export const cards = [
energy: 1,
block: 5,
target: 'player',
description: 'Gain 5 Block'
description: 'Gain 5 Block',
},
{
name: 'Strike',
type: 'Attack',
energy: 1,
target: 'enemy',
damage: 6,
description: 'Deal 6 Damage.'
description: 'Deal 6 Damage.',
},
{
name: 'Bash',
Expand All @@ -43,10 +43,10 @@ export const cards = [
conditions: [
{
action: 'onlyType',
type: 'Attack'
}
type: 'Attack',
},
],

description: 'Can only be played if every card in your hand is an Attack. Deal 14 damage.',
},
{
Expand Down Expand Up @@ -97,8 +97,8 @@ export const cards = [
conditions: [
{
action: 'shouldNotHaveMoreThen',
percentage: 50
}
percentage: 50,
},
],
powers: {
regen: 5,
Expand All @@ -115,62 +115,61 @@ export const cards = [
action: 'addHealth',
parameter: {
target: 'playerX',
amount: 1
}
amount: 1,
},
},
{
action: 'drawCards',
preConditions: [
{
action: 'shouldNotHaveLessThen',
percentage: 100
}
percentage: 100,
},
],
parameter: 2
}
parameter: 2,
},
],
conditions: [
{
action: 'onlyType',
type: 'Skill'
type: 'Skill',
},
{
action: 'shouldNotHaveLessThen',
percentage: 60
}
]
}
percentage: 60,
},
],
},
// {name: 'Flex', energy: 0, type: 'Skill', description: 'Gain 2 Strength.'},
// {name: 'Body Slam', energy: 1, type: 'Attack', description: 'Deal Damage equal to your Block'},
]

function checkConditions(conditions, state) {
let boolean = false;
let boolean = false
if (conditions && state) {
conditions.forEach(condition => {
conditions.forEach((condition) => {
if (!boolean && conditionsMethods[condition.action]) {
boolean = conditionsMethods[condition.action](state, condition)
}
});
})
}

return boolean
}

function runOnUse(actions, state, target) {
let newState = state;
let newState = state

if (actions && state) {
actions.forEach(onUse => {

actions.forEach((onUse) => {
if (onUse.preConditions && checkConditions(onUse.preConditions, state)) {
return newState;
return newState
}

// Trick to replace the target in the parameters
// It creates problem now
// It creates problem now
// TODO fix it
let param = onUse.parameter;
let param = onUse.parameter
// if (onUse.parameter.target) {
// delete onUse.parameter.target
// param = {
Expand All @@ -179,13 +178,12 @@ function runOnUse(actions, state, target) {
// }
// }
newState = actionsMethods[onUse.action](newState, param)
});
})
}

return newState
}


// All cards extend this class.
export class Card {
constructor(props) {
Expand All @@ -201,12 +199,16 @@ export class Card {
this.conditions = props.conditions
this.onUse = props.onUse

this.use = this.onUse ? (state, target) => {
return runOnUse(this.onUse, state, target)
} : null;
this.condition = this.conditions ? (state) => {
return checkConditions(this.conditions, state)
} : null;
this.use = this.onUse
? (state, target) => {
return runOnUse(this.onUse, state, target)
}
: null
this.condition = this.conditions
? (state) => {
return checkConditions(this.conditions, state)
}
: null
}
}

Expand Down
49 changes: 23 additions & 26 deletions public/game/conditions.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@


function onlyType(state, condition) {
let boolean = false;
state.hand.forEach(card => {
if(card.type !== condition.type) {
boolean = true;
}
})
let boolean = false
state.hand.forEach((card) => {
if (card.type !== condition.type) {
boolean = true
}
})

return boolean
return boolean
}

function shouldNotHaveLessThen(state, condition) {
let boolean = false;
const player = state.player;
if((player.currentHealth / player.maxHealth) * 100 < condition.percentage) {
boolean = true
}
return boolean
let boolean = false
const player = state.player
if ((player.currentHealth / player.maxHealth) * 100 < condition.percentage) {
boolean = true
}
return boolean
}

function shouldNotHaveMoreThen(state, condition) {
let boolean = false;
const player = state.player;
if((player.currentHealth / player.maxHealth) * 100 > condition.percentage) {
boolean = true
}
return boolean
let boolean = false
const player = state.player
if ((player.currentHealth / player.maxHealth) * 100 > condition.percentage) {
boolean = true
}
return boolean
}


export default {
onlyType,
shouldNotHaveLessThen,
shouldNotHaveMoreThen
}
onlyType,
shouldNotHaveLessThen,
shouldNotHaveMoreThen,
}
6 changes: 2 additions & 4 deletions public/ui/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class App extends Component {
game: this.game,
update: this.update.bind(this),
createCard,
dealCards: this.dealCards.bind(this)
dealCards: this.dealCards.bind(this),
// component: this,
}
}
Expand Down Expand Up @@ -92,9 +92,7 @@ export default class App extends Component {
goToNextRoom() {
this.game.enqueue({type: 'endTurn'})
this.game.enqueue({type: 'goToNextRoom'})
this.update(() =>
this.update(this.dealCards)
)
this.update(() => this.update(this.dealCards))
}
handlePlayerReward(card) {
this.game.enqueue({type: 'rewardPlayer', card: card})
Expand Down
12 changes: 6 additions & 6 deletions public/ui/cards.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ export default class Cards extends Component {
render(props) {
return html`
<div class="Cards">
${props.gameState[props.type].map(card => Card(card, props.gameState))}
${props.gameState[props.type].map((card) => Card(card, props.gameState))}
</div>
`
}
}

function isCardDisabled(card, gameState) {
let isDisabled = false
if(gameState.player.currentEnergy < card.energy) {
if (gameState.player.currentEnergy < card.energy) {
isDisabled = true
} else if (card.conditions && card.condition && gameState) {
isDisabled = card.condition(gameState)
}
return isDisabled;

return isDisabled
}

export function Card(card, gameState) {
let isDisabled;
let isDisabled
if (gameState) {
isDisabled = isCardDisabled(card, gameState);
isDisabled = isCardDisabled(card, gameState)
}

return html`
Expand Down
5 changes: 3 additions & 2 deletions public/ui/dragdrop.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {html, Component} from '../../web_modules/htm/preact/standalone.module.js'
import {Draggable} from './../web_modules/gsap/Draggable.js'
import gsap from './animations.js'
import {cards} from '../game/cards.js'
Expand Down Expand Up @@ -38,7 +37,9 @@ export default function enableDragDrop(container, onAdd) {
while (--i > -1) {
// Highlight only if valid target.
if (this.hitTest(targets[i], '50%')) {
if (cardHasValidTarget(getCardFromElement(cardEl), getTargetStringFromElement(targets[i]))) {
if (
cardHasValidTarget(getCardFromElement(cardEl), getTargetStringFromElement(targets[i]))
) {
targets[i].classList.add(overClass)
}
} else {
Expand Down
8 changes: 4 additions & 4 deletions tests/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,15 @@ test('can not play card if target doesnt match', (t) => {
const {state} = t.context
const card = createCard('Strike')
t.throws(() => {
const nextState = a.playCard(state, {card, target: 'yolo'})
a.playCard(state, {card, target: 'yolo'})
})
t.throws(() => {
const nextState = a.playCard(state, {card, target: 'enemy1'})
a.playCard(state, {card, target: 'enemy1'})
})
t.throws(() => {
const nextState = a.playCard(state, {card, target: 'naaah'})
a.playCard(state, {card, target: 'naaah'})
})
const nextState = a.playCard(state, {card, target: 'player'})
a.playCard(state, {card, target: 'player'})
})

test.todo('playing defend on an enemy ?')
Expand Down

0 comments on commit 8f4e0b2

Please sign in to comment.