cuttle icon indicating copy to clipboard operation
cuttle copied to clipboard

[DevEx]: Update socket messages to reduce need for type-checks by making all game attributes appear every time

Open itsalaidbacklife opened this issue 2 years ago • 5 comments

Improvement Summary

Currently the gameStore.updateGame() function checks for the existence of every property on the game before assigning them to the store, because the object is sparsely populated by the server. We could simplify this logic and remove the existence checks if we update the server calls that create games to always include all the properties referenced in the frontend.

Detailed Description

In particular we should look at all the properties consumed in gameStore.updateGame():

    updateGame(newGame) {
      if (Object.hasOwnProperty.call(newGame, 'lastEvent')) {
        if (Object.hasOwnProperty.call(newGame.lastEvent, 'change')) {
          this.lastEventChange = newGame.lastEvent.change;
        } else {
          this.lastEventChange = null;
        }
        if (Object.hasOwnProperty.call(newGame.lastEvent, 'oneOff')) {
          this.lastEventOneOffRank = newGame.lastEvent.oneOff.rank;
        } else {
          this.lastEventOneOffRank = null;
        }
        if (Object.hasOwnProperty.call(newGame.lastEvent, 'oneOffTargetType')) {
          this.lastEventTargetType = newGame.lastEvent.oneOffTargetType;
        } else {
          this.lastEventTargetType = null;
        }
      }
      this.waitingForOpponentToStalemate = false;
      if (Object.hasOwnProperty.call(newGame, 'id')) this.id = newGame.id;
      if (Object.hasOwnProperty.call(newGame, 'turn')) this.turn = newGame.turn;
      if (Object.hasOwnProperty.call(newGame, 'chat')) this.chat = cloneDeep(newGame.chat);
      if (Object.hasOwnProperty.call(newGame, 'deck')) this.deck = cloneDeep(newGame.deck);
      if (Object.hasOwnProperty.call(newGame, 'scrap')) this.scrap = cloneDeep(newGame.scrap);
      if (Object.hasOwnProperty.call(newGame, 'log')) this.log = cloneDeep(newGame.log);
      if (Object.hasOwnProperty.call(newGame, 'name')) this.name = newGame.name;
      if (Object.hasOwnProperty.call(newGame, 'p0Ready')) this.p0Ready = newGame.p0Ready;
      if (Object.hasOwnProperty.call(newGame, 'p1Ready')) this.p1Ready = newGame.p1Ready;
      if (Object.hasOwnProperty.call(newGame, 'passes')) this.passes = newGame.passes;
      if (Object.hasOwnProperty.call(newGame, 'players')) this.players = cloneDeep(newGame.players);
      if (Object.hasOwnProperty.call(newGame, 'spectatingUsers')) {
        this.spectatingUsers = newGame.spectatingUsers;
      }
      if (Object.hasOwnProperty.call(newGame, 'twos')) this.twos = cloneDeep(newGame.twos);

      if (Object.hasOwnProperty.call(newGame, 'topCard')) this.topCard = cloneDeep(newGame.topCard);
      else this.topCard = null;

      if (Object.hasOwnProperty.call(newGame, 'secondCard')) this.secondCard = cloneDeep(newGame.secondCard);
      else this.secondCard = null;

      if (Object.hasOwnProperty.call(newGame, 'oneOff')) this.oneOff = cloneDeep(newGame.oneOff);
      else this.oneOff = null;

      if (Object.hasOwnProperty.call(newGame, 'oneOffTarget'))
        this.oneOffTarget = cloneDeep(newGame.oneOffTarget);
      else this.oneOffTarget = null;

      if (Object.hasOwnProperty.call(newGame, 'isRanked')) this.isRanked = newGame.isRanked;
      if (Object.hasOwnProperty.call(newGame, 'currentMatch')) this.currentMatch = newGame.currentMatch;
    },

All of these Object.hasOwnProperty() calls make it difficult to read what the function is really doing, and generally make the update more complex than it needs to be. They exist because the various properties on the Game sent by the server are sometimes missing. We should rectify this by updating all the server-generated data where a game is sent so that the game includes all of the properties that are consumed in gameStore.updateGame(). In particular, we should look at the calls to updateGame() that are called outside of the GameView.vue e.g. in gameStore.requestSubscribe() and gameStore.requestSpectate().

We should look at the server generated data and ensure that it always has for example an array of players who have hands, points etc, even if the array of players is empty or if the players exist but have no hands (this could be represented with an empty array for the player hands).

This should then allow us to make the gameStore.updateGame() function safely assume that all properties are available and therefore skip all the Object.hasOwnProperty.call() checks, simply assigning values to state as passed by the server created game data.

itsalaidbacklife avatar Oct 06 '23 13:10 itsalaidbacklife

Hi, can you assigned this issue to me?

fuveluck avatar Oct 11 '23 08:10 fuveluck

@fuveluck Are you working on this? If not I'll take it.

Haviles04 avatar Oct 20 '23 22:10 Haviles04

@fuveluck Are you working on this? If not I'll take it.

no, i ve got another issue so you can take this one :)

fuveluck avatar Oct 21 '23 13:10 fuveluck

How can i work in thsi issue?

NIKHIL-bot27 avatar Dec 10 '23 11:12 NIKHIL-bot27

Hi, can you please assign this issue to me ?

Shubh-Dev avatar May 29 '24 04:05 Shubh-Dev