mirror of
				https://github.com/bra1n/townsquare.git
				synced 2025-10-21 16:55:12 +00:00 
			
		
		
		
	cleaning up vueX code to reuse existing player update methods, and add form flow logic to block infinite loops
This commit is contained in:
		
							parent
							
								
									58263e2f6d
								
							
						
					
					
						commit
						ecb205e027
					
				
					 4 changed files with 24 additions and 24 deletions
				
			
		| 
						 | 
					@ -248,11 +248,7 @@ export default {
 | 
				
			||||||
        "Player preffered pronouns",
 | 
					        "Player preffered pronouns",
 | 
				
			||||||
        this.player.pronouns
 | 
					        this.player.pronouns
 | 
				
			||||||
      );
 | 
					      );
 | 
				
			||||||
      this.$store.commit("players/setPronouns", {
 | 
					      this.updatePlayer("pronouns", pronouns, true);
 | 
				
			||||||
        player: this.player,
 | 
					 | 
				
			||||||
        pronouns
 | 
					 | 
				
			||||||
      });
 | 
					 | 
				
			||||||
      this.isMenuOpen = false;
 | 
					 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    toggleStatus() {
 | 
					    toggleStatus() {
 | 
				
			||||||
      if (this.grimoire.isPublic) {
 | 
					      if (this.grimoire.isPublic) {
 | 
				
			||||||
| 
						 | 
					@ -282,7 +278,12 @@ export default {
 | 
				
			||||||
      this.updatePlayer("reminders", reminders, true);
 | 
					      this.updatePlayer("reminders", reminders, true);
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    updatePlayer(property, value, closeMenu = false) {
 | 
					    updatePlayer(property, value, closeMenu = false) {
 | 
				
			||||||
      if (this.session.isSpectator && property !== "reminders") return;
 | 
					      if (
 | 
				
			||||||
 | 
					        this.session.isSpectator &&
 | 
				
			||||||
 | 
					        property !== "reminders" &&
 | 
				
			||||||
 | 
					        property !== "pronouns"
 | 
				
			||||||
 | 
					      )
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
      this.$store.commit("players/update", {
 | 
					      this.$store.commit("players/update", {
 | 
				
			||||||
        player: this.player,
 | 
					        player: this.player,
 | 
				
			||||||
        property,
 | 
					        property,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -107,12 +107,6 @@ const mutations = {
 | 
				
			||||||
      state.players[index][property] = value;
 | 
					      state.players[index][property] = value;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  setPronouns(state, { player, pronouns }) {
 | 
					 | 
				
			||||||
    const index = state.players.indexOf(player);
 | 
					 | 
				
			||||||
    if (index >= 0) {
 | 
					 | 
				
			||||||
      state.players[index].pronouns = pronouns;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
  add(state, name) {
 | 
					  add(state, name) {
 | 
				
			||||||
    state.players.push({
 | 
					    state.players.push({
 | 
				
			||||||
      ...NEWPLAYER,
 | 
					      ...NEWPLAYER,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -137,7 +137,6 @@ module.exports = store => {
 | 
				
			||||||
      case "players/set":
 | 
					      case "players/set":
 | 
				
			||||||
      case "players/swap":
 | 
					      case "players/swap":
 | 
				
			||||||
      case "players/move":
 | 
					      case "players/move":
 | 
				
			||||||
      case "players/setPronouns":
 | 
					 | 
				
			||||||
        if (state.players.players.length) {
 | 
					        if (state.players.players.length) {
 | 
				
			||||||
          localStorage.setItem(
 | 
					          localStorage.setItem(
 | 
				
			||||||
            "players",
 | 
					            "players",
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -488,24 +488,29 @@ class LiveSession {
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * Publish a player pronouns update
 | 
					   * Publish a player pronouns update
 | 
				
			||||||
   * @param player
 | 
					   * @param player
 | 
				
			||||||
   * @param pronouns
 | 
					   * @param value
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  sendPlayerPronouns({ player, pronouns }) {
 | 
					  sendPlayerPronouns({ player, value }) {
 | 
				
			||||||
    if (!this._isSpectator) return;
 | 
					    //send pronoun only for the current player
 | 
				
			||||||
 | 
					    if (this._store.state.session.playerId !== player.id) return;
 | 
				
			||||||
    const index = this._store.state.players.players.indexOf(player);
 | 
					    const index = this._store.state.players.players.indexOf(player);
 | 
				
			||||||
    this._send("pronouns", { index, pronouns });
 | 
					    this._send("pronouns", { index, value });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * Update a pronouns based on incoming data. Player only.
 | 
					   * Update a pronouns based on incoming data. Player only.
 | 
				
			||||||
   * @param index
 | 
					   * @param index
 | 
				
			||||||
   * @param pronouns
 | 
					   * @param value
 | 
				
			||||||
   * @private
 | 
					   * @private
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  _updatePlayerPronouns({ index, pronouns }) {
 | 
					  _updatePlayerPronouns({ index, value }) {
 | 
				
			||||||
    const player = this._store.state.players.players[index];
 | 
					    const player = this._store.state.players.players[index];
 | 
				
			||||||
    if (!player) return;
 | 
					    if (!player || this._store.state.session.playerId === player.id) return;
 | 
				
			||||||
    this._store.commit("players/setPronouns", { player, pronouns });
 | 
					    this._store.commit("players/update", {
 | 
				
			||||||
 | 
					      player,
 | 
				
			||||||
 | 
					      property: "pronouns",
 | 
				
			||||||
 | 
					      value
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
| 
						 | 
					@ -837,12 +842,13 @@ export default store => {
 | 
				
			||||||
        session.sendGamestate("", true);
 | 
					        session.sendGamestate("", true);
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
      case "players/update":
 | 
					      case "players/update":
 | 
				
			||||||
        session.sendPlayer(payload);
 | 
					        if (payload.property === "pronouns") {
 | 
				
			||||||
        break;
 | 
					 | 
				
			||||||
      case "players/setPronouns":
 | 
					 | 
				
			||||||
          session.sendPlayerPronouns(payload);
 | 
					          session.sendPlayerPronouns(payload);
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        session.sendPlayer(payload);
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // check for session Id in hash
 | 
					  // check for session Id in hash
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue