Game.vue 5.28 KB
<template>
  <div class="espace">
    <div class="separation">
      <span class="scoreboard">You: {{myScore}} - Oponent: {{oponentScore}}</span>
    </div>
    <div>
      <div v-on:click="select('rock')">
        <img id="rock" src="https://www.flaticon.com/svg/static/icons/svg/838/838023.svg" alt="rock">
      </div>
      <div v-on:click="select('paper')">
        <img id="paper" src="https://www.flaticon.com/svg/static/icons/svg/3039/3039835.svg"  alt="paper">
      </div>
      <div v-on:click="select('scissors')">
        <img id="scissors" src="https://www.flaticon.com/svg/static/icons/svg/760/760911.svg" alt="scissors">
      </div>
    </div>
  </div>
</template>

<style scoped>
    img {
        width: 64px;
        height: 64px;
    }
    .selected {
      border: 4px solid red;
      border-radius: 10px;
      padding: 15px;

    }
    
    tr {height: 20%;}

    .espace {
      padding: 5%;
    }

    .separation {
      padding: 5%;
    }

    .scoreboard {
      margin-bottom: 20px;
      font-size: larger;
    }
</style>

<script>
let havePlayed = false
const user = require("../model/user.js")

export default {
    name: "RockPaperScissors",
    data: function () {
      return { oponentScore: 0, myScore: 0 , rock : 0, paper : 0, scissors: 0}
    },
    methods: {
        select: async function (value) {
          const gameID = window.location.href.split("/").slice(-1)[0]
          
          if (havePlayed)
          return
          this.resetStyle()
          havePlayed = true
          document.getElementById(value).classList.add("selected")
          await fetch(
            sessionStorage.getItem('APIURL') + `/result/${gameID}`,
            {
              method: "POST",
              body: JSON.stringify({
                value,
                USER_ID: user.id
                })
            })
          await this.getResult(gameID,value)
            
        },
        getResult: async function(gameID,value) {
          const res = await fetch(
              sessionStorage.getItem('APIURL') + `/result/${gameID}`,
                {
                  method: "GET"
                }
              )
              const result = await res.json()
              if (result.length >= 2) {
                this.updateScore(result, value)
                this.resetStyle()
                havePlayed = false
                setTimeout(async () => {this.resetGame(result)},700)
                return
              }
              setTimeout(async () => {
                await this.getResult(gameID,value)
              
            }, 250)
        },

        updateScore: function (result, value) {
          let i = 0;
          if(result[0]['USER_ID'] == user.id){
            i = 1
          }
          const valueAgainst = result[i]['RESULT_AWNSER']
          if(value === "paper"){
            this.paper += 1
            if(valueAgainst === "rock"){
              this.myScore +=1
            }
            else if(valueAgainst === "scissors"){
              this.oponentScore +=1
            }
          }else if(value === "rock"){
            this.rock +=1
            if(valueAgainst === "paper"){
              this.oponentScore+=1
            }
            else if(valueAgainst === "scissors"){
              this.myScore +=1
            }
          }else{
            this.scissors+=1
            if(valueAgainst === "paper"){
              this.myScore +=1
            }
            else if(valueAgainst === "rock"){
              this.oponentScore +=1
            }
          }
          if(this.myScore >= 3){
            alert("Vous avez gagné !")
            console.log(user.token)
            let win=1
            let lost=0
            this.updateUser(win, lost)
            this.$router.push("/")
          }
          else if(this.oponentScore >= 3){
            alert("Vous avez perdu ...")
            let win=0
            let lost=1
            this.updateUser(win, lost)
            this.$router.push("/")
          }
        },
        updateUser: async function(win, lost){
          if(user.id<1000000){
            let rock = this.rock
            let paper = this.paper
            let scissors = this.scissors
            await fetch(
                  sessionStorage.getItem('APIURL') + `/user/${user.id}`,
                  {
                    method: "PUT",
                    headers: {
                        'Authorization': 'Bearer '+ user.token,
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({USER_PAPER: paper, USER_ROCK: rock, USER_SCISSORS: scissors, USER_LOST: lost, USER_WIN:win}),
                  }
            )
          }
        }
        ,

        resetGame: async function(result) {
          let i = 0;
          if(result[0]['USER_ID'] == user.id){
            i = 1
          }
          await fetch(
                sessionStorage.getItem('APIURL') + '/result',
                {
                  method: "DELETE",
                  body: JSON.stringify({USER_ID: result[i]['USER_ID']})
                }
          )
        },

        resetStyle: function () {
          document.getElementById("rock").classList.remove("selected")
          document.getElementById("paper").classList.remove("selected")
          document.getElementById("scissors").classList.remove("selected")
        }
    }
}
</script>