Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. export default {
  3.   name: 'ScoreBoard',
  4.   data() {
  5.     return {
  6.       state: 'gameStarted',
  7.       addPlayerName: '',
  8.       gameStarted: false,
  9.       gameEnded: false,
  10.       turnCounter: 0,
  11.       selectedScore: null,
  12.       winningPlayer: null,
  13.       players: [
  14.         {
  15.           name: 'Rainbird',
  16.           score: 0,
  17.           strikes: 0,
  18.           id: 0,
  19.         },
  20.         {
  21.           name: 'George',
  22.           score: 0,
  23.           strikes: 0,
  24.           id: 1,
  25.         },
  26.         {
  27.           name: 'Balloo',
  28.           score: 0,
  29.           strikes: 0,
  30.           id: 2,
  31.         },
  32.         {
  33.           name: 'Greg',
  34.           score: 0,
  35.           strikes: 0,
  36.           id: 3,
  37.         },
  38.         {
  39.           name: 'Peter',
  40.           score: 0,
  41.           strikes: 0,
  42.           id: 4,
  43.         },
  44.         {
  45.           name: 'Captain',
  46.           score: 0,
  47.           strikes: 0,
  48.           id: 5,
  49.         }
  50.       ],
  51.       addPlayerCounter: 0,
  52.     };
  53.   },
  54.   methods: {
  55.     updateSelectedScore: function(value) {
  56.       this.selectedScore = value;
  57.     },
  58.     addPlayer: function() {
  59.       var playerName = this.addPlayerName;
  60.       this.players.push({
  61.         name: playerName,
  62.         score: 0,
  63.         strikes: 0,
  64.         id: this.addPlayerCounter,
  65.       })
  66.       this.addPlayerName = '';
  67.       this.addPlayerCounter++;
  68.     },
  69.     initAddPlayers: function() {
  70.       this.state = 'addPlayers';
  71.     },
  72.     startGame: function() {
  73.       this.state = 'gameStarted';
  74.     },
  75.     removePlayer: function(player) {
  76.       for (let i = 0; i < this.players.length; i++) {
  77.         if(this.players[i].id === player.id) {
  78.           this.players.splice(i, 1);
  79.         }
  80.       }
  81.     },
  82.     shufflePlayers: function() {
  83.       var players = this.players;
  84.       function shuffle(array) {
  85.           let counter = array.length;
  86.           // While there are elements in the array
  87.           while (counter > 0) {
  88.               // Pick a random index
  89.               let index = Math.floor(Math.random() * counter);
  90.  
  91.               // Decrease counter by 1
  92.               counter--;
  93.  
  94.               // And swap the last element with it
  95.               let temp = array[counter];
  96.               array[counter] = array[index];
  97.               array[index] = temp;
  98.  
  99.           }
  100.           return array;
  101.       }
  102.       shuffle(players);
  103.       for (let i = 0; i < players.length; i++) {
  104.         players[i].id = i;
  105.       }
  106.       this.players = players.splice(0);
  107.     },
  108.     selectThis(val, index) {
  109.       this.activeScoreButton = index;
  110.     },
  111.     endGame: function() {
  112.       this.state = 'gameEnded';
  113.       for (let i = 0; i < this.players.length; i++) {
  114.         this.players[i].score = '-';
  115.         this.players[i].strikes = 0;
  116.         this.players[i].eliminated = false;
  117.       }
  118.     },
  119.     saveScore: function() {
  120.  
  121.       var currentPlayer = this.current_player;
  122.  
  123.       //reset dash as we are going to get a result
  124.       if(currentPlayer.score === '-') {
  125.         currentPlayer.score = 0;
  126.       }
  127.  
  128.       //if the score is 0, update the player's strike
  129.       if(this.selectedScore === 0) {
  130.         currentPlayer.strikes += 1;
  131.         if(currentPlayer.strikes === 3) {
  132.           currentPlayer.eliminated = true;
  133.         }
  134.       } else {
  135.         //reset the players strike since they have scored
  136.         currentPlayer.strikes = 0;
  137.         //add the selected score to the current player, then reset it
  138.         currentPlayer.score += this.selectedScore;
  139.       }
  140.  
  141.       //test if player has gone over 50
  142.       if(currentPlayer.score > 50) {
  143.         currentPlayer.score = 25;
  144.       }
  145.  
  146.       //reset score
  147.       this.selectedScore = null;
  148.  
  149.       //check if player has won, otherwise next player
  150.       if(currentPlayer.score === 50) {
  151.         this.winningPlayer = currentPlayer;
  152.         this.endGame();
  153.       } else {
  154.         //set the turnCounter to the next player;
  155.         this.turnCounter = this.next_player.id;
  156.       }
  157.  
  158.  
  159.  
  160.     }
  161.   },
  162.   computed : {
  163.     current_player() {
  164.       var currPlayer;
  165.       var currTurn= this.turnCounter;
  166.       for (let i = 0; i < this.players.length; i++) {
  167.         if(this.players[i].id === currTurn) {
  168.           currPlayer = this.players[i];
  169.         }
  170.       }
  171.       return currPlayer;
  172.     },
  173.     next_player() {
  174.       var currPlayer;
  175.       var players = this.players;
  176.       function getNextPlayer(index) {
  177.         if (!players[index].eliminated) {
  178.           return players[index];
  179.         }
  180.         index++;
  181.         var newIndex = (index) % players.length;
  182.         return getNextPlayer(newIndex);
  183.       }
  184.       var index = (this.turnCounter + 1) % players.length;
  185.       return getNextPlayer(index);
  186.     },
  187.     sorted_players() {
  188.         var clone = this.players.slice(0);
  189.         return clone.sort((a, b) => {
  190.           return a.eliminated - b.eliminated || b.score - a.score;
  191.         });
  192.     }
  193.   }
  194. };
  195. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement