Skip to content

Commit

Permalink
Fix circular JSON reference
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarrough committed Jul 15, 2023
1 parent b874843 commit dd2eda0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
3 changes: 0 additions & 3 deletions public/content/cards.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Here you'll find all the default cards used in the game.
// See game/cards.js for the details on how they work.

//put cards here, to make it easier to add more

export default [
{
name: 'Strike',
Expand Down Expand Up @@ -481,7 +479,6 @@ export default [
},*/
// {name: 'Flex', energy: 0, type: 'skill', description: 'Gain 2 Strength.'},
]
//cards should be split into seperate card files to make editing a single card easier

// 'codices.jpg'
// 'alice-holds-the-white-king.jpg'
Expand Down
9 changes: 6 additions & 3 deletions public/game/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,14 @@ export function useCardActions(state, {target, card}) {
if (action.conditions && !conditionsAreValid(action.conditions, state)) {
return newState
}
// Make sure the action is called with a target.
if (!action.parameter) action.parameter = {}
// Prefer the target you dropped the card on.

// Make sure the action is called with a target, preferably the target you dropped the card on.
action.parameter.target = target
action.parameter.card = card

// We used to set the card here, which caused a circular JSON structure. Removing this line fixed that, but keeping this comment here for now, in case something breaks.
// action.parameter.card = card

// Run the action
newState = allActions[action.type](newState, action.parameter)
})
Expand Down
20 changes: 19 additions & 1 deletion public/game/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import {isDungeonCompleted} from '../game/utils-state.js'

const apiUrl = 'https://api.slaytheweb.cards/api/runs'
// const apiUrl = 'http://localhost:3000/api/runs'
//

/**
* @typedef {object} Run
* @property {string} name - user inputted player name
* @property {boolean} win - whether the player won the game
* @property {object} state - the final state
* @property {Array<Object>} past - a list of past states
*/

/**
* Saves a "game" object into a remote database.
Expand All @@ -16,13 +25,22 @@ export function postRun(game, name) {
state: game.state,
past: game.past.list,
}

let body
try {
body = JSON.stringify(run)
} catch (err) {
console.log(err, run)
throw new Error('Could not stringify run')
}

return fetch(apiUrl, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(run),
body,
})
}

Expand Down
1 change: 1 addition & 0 deletions public/game/cards.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const CardTargets = {
* @prop {CardTargets} target - a special "target" string to specify which targets the card affects.
* color = [RED, GREEN, BLUE, PURPLE, COLORLESS, CURSE]
* rarity = [BASIC, SPECIAL, COMMON, UNCOMMON, RARE, CURSE]
* @prop {boolean} exhaust - whether the card will exhaust when played.
*
* @prop {CARDPOWERS} [powers] - Cards can apply POWERS with the `powers` object. Powers are hardcoded in the game actions, but feel free to add more.
* @prop {Array<CardAction>} [actions] - Cards can _optionally_ define a list of `actions`. These actions will be run, in defined order, when the card is played.
Expand Down

0 comments on commit dd2eda0

Please sign in to comment.