Closed Haviles04 closed 3 weeks ago
Todo: Add the rest of the cards to the unit test fixtures
The other option to this code would look something like this
@itsalaidbacklife , @seriouslysean , Thoughts on which is better?
const keys = ['p0', 'p1', 'deck', 'scrap', 'twos', 'oneOff'];
const validateCard = (card, allowAttachments) => {
if (card.attachments.length && !allowAttachments) {
throw new Error('Only Points and Face Cards can have attachments');
}
if (cardIds.has(card.id)) {
throw new Error('Duplicate Card');
}
if (!deckIds.has(card.id)) {
throw new Error('Invalid Card');
}
cardIds.add(card.id);
};
for (const key of keys) {
let allowAttachments = false;
if (!gameState[key]) {
return;
}
if (key === 'oneOff') {
return validateCard(gameState[key], allowAttachments);
}
if (['p0', 'p1'].includes(key)) {
['hand', 'points', 'faceCards'].forEach((playerKey) => {
if (['points', 'faceCards'].includes(playerKey)) {
allowAttachments = true;
} else {
allowAttachments = false;
}
return gameState[key][playerKey].forEach((card) => {
validateCard(card, allowAttachments);
if (allowAttachments) {
card.attachments.forEach((card) => {
validateCard(card, false);
});
}
});
});
}
gameState[key].forEach((card) => {
validateCard(card, allowAttachments);
});
}
Let's add unit tests for the new validate function to show that it errors on a few known cases:
Issue number
Relevant issue number
Please check the following
Please describe additional details for testing this change
-Added Validation of all cards in gamestate -Added unit test -Updated
load-fixture
andload-fixture-gamestate
to fill the deck or the scrap