cuttle-cards / cuttle

A two-player battle card game for all ages, built with nodejs, sailsjs, and vuejs
MIT License
145 stars 115 forks source link

Feature #1034 Validate All Cards #1085

Closed Haviles04 closed 3 weeks ago

Haviles04 commented 1 month ago

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 and load-fixture-gamestate to fill the deck or the scrap

Haviles04 commented 1 month ago

Todo: Add the rest of the cards to the unit test fixtures

Haviles04 commented 1 month ago

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);
      });
    }
itsalaidbacklife commented 1 month ago

Let's add unit tests for the new validate function to show that it errors on a few known cases: