-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.min.js
1 lines (1 loc) · 3.42 KB
/
game.min.js
1
const app=angular.module("app",[]);app.controller("GameController",(function($scope){function generateGame(){let colors;return angular.copy($scope.colors).sort(()=>.5-Math.random()).slice(0,4)}function prepareShare(){let emojis="",tries=$scope.game.currentBoard+1,text;return 10==tries&&(tries="X"),$scope.game.answer.forEach(color=>{switch(color){case"blue":emojis+="🔵";break;case"red":emojis+="🔴";break;case"purple":emojis+="🟣";break;case"green":emojis+="🟢";break;case"brown":emojis+="🟤";break;case"yellow":emojis+="🟡"}}),`Joguei Colorama! #${$scope.stats.wins+$scope.stats.loss}\n${tries}/10\n${emojis}\n\nhttps://gabrielsilva.dev.br/colorama`}$scope.colors=["blue","red","purple","green","brown","yellow"],window.localStorage.getItem("game")?$scope.game=JSON.parse(window.localStorage.getItem("game")):($scope.game={answer:generateGame(),currentBoard:0,currentBall:0,boards:Array.from({length:10},()=>({guesses:["","","",""],tips:["","","",""]}))},window.localStorage.setItem("game",JSON.stringify($scope.game))),window.localStorage.getItem("stats")?$scope.stats=JSON.parse(window.localStorage.getItem("stats")):($scope.stats={wins:0,loss:0},window.localStorage.setItem("stats",JSON.stringify($scope.stats))),$scope.$watch("game",(function(newValue,oldValue){$scope.victory||$scope.fail||newValue==oldValue||window.localStorage.setItem("game",JSON.stringify($scope.game))}),!0),$scope.$watch("stats",(function(newValue,oldValue){newValue!=oldValue&&window.localStorage.setItem("stats",JSON.stringify($scope.stats))}),!0),$scope.setGuess=function(color){$scope.game.boards[$scope.game.currentBoard].guesses.includes("")&&!$scope.colorUsed(color)&&($scope.game.boards[$scope.game.currentBoard].guesses[$scope.game.currentBall]=color,$scope.game.currentBall++)},$scope.colorUsed=function(color){return $scope.game.boards[$scope.game.currentBoard].guesses.includes(color)},$scope.clearGuess=function(){0!=$scope.game.currentBall&&$scope.game.currentBall--,$scope.game.boards[$scope.game.currentBoard].guesses[$scope.game.currentBall]=""},$scope.submitGuess=function(){if($scope.game.boards[$scope.game.currentBoard].guesses.includes(""))return void alert("Escolha todas as cores antes de fazer um chute!");let tips=[];for($scope.game.boards[$scope.game.currentBoard].guesses.forEach((guess,key)=>{$scope.game.answer[key]==guess?tips.push("correct"):$scope.game.answer.includes(guess)&&tips.push("incorrect")}),tips=tips.sort(()=>.5-Math.random());tips.length<4;)tips.push("");return $scope.game.boards[$scope.game.currentBoard].tips=tips,tips.every(elem=>"correct"===elem)?($scope.victory=!0,$scope.stats.wins++,void window.localStorage.removeItem("game")):9==$scope.game.currentBoard?($scope.fail=!0,$scope.stats.loss++,void window.localStorage.removeItem("game")):($scope.game.currentBall=0,void $scope.game.currentBoard++)},$scope.playAgain=function(){window.location.reload()},window.localStorage.getItem("help")||($scope.help=!0),$scope.showHelp=function(){$scope.help=!0},$scope.showStats=function(){$scope.status=!0},$scope.closeHelp=function(){window.localStorage.setItem("help",!0),$scope.help=!1},$scope.closeStats=function(){$scope.status=!1},$scope.share=function(social){let text=prepareShare(),url="";switch(social){case"twitter":url="https://twitter.com/intent/tweet?text="+encodeURIComponent(text);break;case"copy":return navigator.clipboard.writeText(text),void alert("Resultado copiado para a área de transferência!");default:return}window.open(url,"_blank")}}));