Commit ba60d466 authored by Robin Borgogno's avatar Robin Borgogno

shifumi

parent f3958386
<?php
include_once __ROOT_DIR . '/libs/php-jwt/src/BeforeValidException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/ExpiredException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/SignatureInvalidException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/JWT.php';
use \Firebase\JWT\JWT;
class ResultController extends Controller {
public function __construct($name, $request) {
parent::__construct($name, $request);
}
// ==============
// Actions
// ==============
public function processRequest()
{
switch ($this->request->getHttpMethod()) {
case 'GET':
$id = $this->request->getURIParams()[0];
return $this->returnResultGameID($id);
case 'POST':
$id = $this->request->getURIParams()[0];
$data = json_decode(file_get_contents("php://input"),TRUE);
return $this->saveAwnser($id, $data);
break;
case 'DELETE':
$data = json_decode(file_get_contents("php://input"),TRUE);
return $this->deleteLine($data);
break;
case 'OPTIONS':
return Response::okresponse(json_encode("OPTIONS"));
// $data = json_decode(file_get_contents("php://input"),TRUE);
// return $this->deleteLine($data);
// break;
}
return Response::errorResponse("unsupported parameters or method in game");
}
public function saveAwnser($id, $data)
{
Result::saveAwnser(array(":id" => $id, ":user_id" => $data['USER_ID'], ":awn" => $data['value']));
return Response::okresponse(json_encode("Tout va bien"));
}
public function returnResultGameID($id)
{
$result = Result::returnResultGameID(array(":id" => $id));
return Response::okresponse(json_encode($result));
}
public function deleteLine($data)
{
Result::deleteLigne(array(":id" => $data['USER_ID']));
return Response::okresponse(json_encode("Theo"));
}
}
<?php
class Result extends Model {
// ===========
// = Statics =
// ===========
protected static $table_name = 'RESULT';
public static function saveAwnser($values)
{
$stm = parent::exec('RESULT_SAVE',$values);
}
public static function returnResultGameID($values)
{
$stm = parent::exec('RESULT_GET',$values);
return $stm->fetchAll();
}
public static function deleteLigne($values)
{
$stm = parent::exec('RESULT_DELETE',$values);
return;
}
}
\ No newline at end of file
......@@ -19,7 +19,7 @@ Game::addSqlQuery('GAME_DELETE',
"DELETE FROM GAME WHERE GAME_ID=:GAME_ID");
Game::addSqlQuery('GAME_GET_USERS',
"SELECT USER_ID FROM GAME_HISTO WHERE GAME_ID=:GAME_ID");
"SELECT * FROM GAME_HISTO WHERE GAME_ID=:GAME_ID");
Game::addSqlQuery('GAME_ADD_USER',
"INSERT INTO GAME_HISTO (GAME_HISTO_ID, GAME_ID, USER_ID) VALUES (NULL, :GAME_ID, :USER_ID)");
......
<?php
Result::addSqlQuery('RESULT_SAVE',
"INSERT INTO RESULT(USER_ID, RESULT_AWNSER, GAME_ID) VALUES (:user_id , :awn, :id)");
Result::addSqlQuery('RESULT_GET',
"SELECT * FROM RESULT WHERE RESULT.GAME_ID = :id");
Result::addSqlQuery('RESULT_DELETE',
"DELETE FROM RESULT WHERE RESULT.USER_ID = :id");
\ No newline at end of file
......@@ -3,10 +3,10 @@
<button class="btn-login">
<router-link to="/">Accueil</router-link>
</button>
<button class="btn-login" v-if="user.id==0">
<button class="btn-login" v-if="user.id >= 1000000">
<router-link to="/inscription">Inscription</router-link>
</button>
<button class="btn-login" v-if="user.id==0">
<button class="btn-login" v-if="user.id >= 1000000">
<router-link to="/login">Connection</router-link>
</button>
<button class="btn-user" v-if="user.id!==0">
......
let user = {
token: "",
id: 0,
name: ""
name: "",
id: Math.floor(Math.random() * 1000000 + 1000000)
}
module.exports = user
\ No newline at end of file
......@@ -44,6 +44,21 @@ const routes = [
path: "/updateUser",
name: "UpdateUser",
component: () => import(/* webpackChunkName: "login" */ '../views/UpdateUser.vue')
},
{
path: "/joinPrivate",
name: "JoinPrivate",
component: () => import(/* webpackChunkName: "login" */ '../views/JoinPrivate.vue')
},
{
path: "/playerPrivate",
name: "PlayerPrivate",
component: () => import(/* webpackChunkName: "login" */ '../views/PlayerPrivate.vue')
},
{
path: "/game/:id",
name: "Game",
component: () => import(/* webpackChunkName: "login" */ '../views/Game.vue')
}
]
......
......@@ -7,8 +7,7 @@
<h1 class="titre"> Code de la partie :</h1>
<h1 class="titre" id="codeGame">{{gameID()}}</h1>
</div>
<div class="joueurs">
{{JoueurPresent()}}
<div class="joueurs" id="joueurs">
</div>
</div>
</div>
......@@ -30,10 +29,11 @@
<script>
const user = require("../model/user.js")
const game = require("../model/game.js")
export default {
name: 'AdminPrivate',
methods: {
createIdGame: function(){
createIdGame: async function(){
const url = "http://localhost/felten/projet-cdaw/backend/MVC/api.php/game"
let data = {
......@@ -46,17 +46,17 @@ export default {
// console.log(user)
fetch(url, params)
.then(response=>response.json())
.then(data=>{
document.getElementById("codeGame").innerHTML = data[0].id;
game.id = data[0].id;
console.log(game.id)
})
let response = await fetch(url, params)
let result = await response.json()
console.log(result)
document.getElementById("codeGame").innerHTML = result[0].id;
game.id = result[0].id;
// console.log(game.id)
},
gameID: function(){
this.createIdGame()
this.ajoutAdminGame()
gameID: async function(){
await this.createIdGame()
await this.ajoutAdminGame()
await this.JoueurPresent()
},
returnUserID: function(){
return user.id
......@@ -64,46 +64,48 @@ export default {
returnGameID: function(){
return game.id
},
ajoutAdminGame: function(){
JoueurPresentAppel: async function(){
await this.JoueurPresent()
},
ajoutAdminGame: async function(){
const url = "http://localhost/felten/projet-cdaw/backend/MVC/api.php/gameuser"
console.log(this.returnUserID())
console.log(this.returnGameID())
console.log(game.id)
let data = {
"GAME_ID": this.returnGameID(),
"USER_ID": this.returnUserID()
"GAME_ID": game.id,
"USER_ID": user.id,
}
var params = {
method: 'POST',
body : JSON.stringify(data) };
fetch(url,params)
.then(response=>response.json())
await fetch(url,params)
},
JoueurPresent: function(){
JoueurPresent: async function(){
let url = "http://localhost/felten/projet-cdaw/backend/MVC/api.php/gameuser/game"
console.log(game.id)
url += "/" + game.id;
var params = {
method: "GET"
}
fetch(url,params)
.then(response=>response.json())
.then(data=>{
console.log(data)
let text;
data.forEach(user => {
text += "<h2 class='joueur' id='joueur'>" + user.USER_ID + "</h2>"
});
document.getElementById("joueurs").innerHTML = text
})
let response = await fetch(url,params)
let result = await response.json()
let text = "";
result.forEach(user => {
text += `<tr><h2 class='joueur' id='joueur'>${user.USER_ID}</h2></tr>`
});
document.getElementById("joueurs").innerHTML = text
if(result.length >= 2){
this.$router.push(`/game/${game.id}`)
return
}
setTimeout(() => {if(game.id){this.JoueurPresent()}}, 3000)
// setInterval(() => {if(game.id){this.JoueurPresent()}}, 3000)
}
},
components:{
},
components: {
LoginNavbar
}
},
}
import LoginNavbar from '../components/LoginNavbar.vue'
</script>
\ No newline at end of file
......@@ -7,13 +7,13 @@
<div class="row">
<div class="column">
<tr class = "espaceBtn"><button class="partie">
<router-link to="/choixPartie">Rejoindre une partie publique</router-link>
<router-link to="/game">Rejoindre une partie publique</router-link>
</button></tr>
<tr class = "espaceBtn"><button class="partie">
<router-link to="/adminPrivate">Créer une partie privée</router-link>
</button></tr>
<tr class = "espaceBtn"><button class="partie">
<router-link to="/choixPartie">Rejoindre une partie privée</router-link>
<router-link to="/joinPrivate">Rejoindre une partie privée</router-link>
</button></tr>
</div>
</div>
......
<template>
<div>
<span class="scoreboard">You: {{myScore}} - Oponent: {{oponentScore}}</span>
<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;
}
.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 }
},
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(
`http://localhost/felten/projet-cdaw/backend/MVC/api.php/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(
`http://localhost/felten/projet-cdaw/backend/MVC/api.php/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)},2000)
return
}
setTimeout(async () => {
await this.getResult(gameID,value)
}, 1000)
},
updateScore: function (result, value) {
console.log(result)
console.log(user.id)
let i = 0;
if(result[0]['USER_ID'] == user.id){
i = 1
}
const valueAgainst = result[i]['RESULT_AWNSER']
if(value === "paper"){
if(valueAgainst === "rock"){
this.myScore +=1
}
else if(valueAgainst === "scissors"){
this.oponentScore +=1
}
}else if(value === "rock"){
if(valueAgainst === "paper"){
this.oponentScore+=1
}
else if(valueAgainst === "scissors"){
this.myScore +=1
}
}else{
if(valueAgainst === "paper"){
this.myScore +=1
}
else if(valueAgainst === "rock"){
this.oponentScore +=1
}
}
if(this.myScore >= 3){
alert("Vous avez gagné !")
this.$router.push("/")
}
else if(this.oponentScore >= 3){
alert("Vous avez perdu ...")
this.$router.push("/")
}
},
resetGame: async function(result) {
let i = 0;
if(result[0]['USER_ID'] == user.id){
i = 1
}
await fetch(
'http://localhost/felten/projet-cdaw/backend/MVC/api.php/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>
\ No newline at end of file
......@@ -138,7 +138,11 @@ export default {
},
call: function(data){
const url = "http://localhost/projet-cdaw/backend/MVC/api.php/user"
// Felten config
// const url = "http://localhost/projet-cdaw/backend/MVC/api.php/user"
// Robin config
const url = "http://localhost/felten/projet-cdaw/backend/MVC/api.php/user"
var params = {
method: 'POST',
......
<template>
<div class="inscription">
<LoginNavbar></LoginNavbar>
<div class="row _centered">
<div class="column _600">
<form>
<tr><label class="label">Code de la partie :</label></tr>
<tr><input v-model="idgame" class="codeGame" id="codeGame" required></tr>
<tr><button @click="rejoindre()" type="submit" id="btn">Rejoindre</button></tr>
</form>
</div>
</div>
</div>
</template>
<script>
const user = require("../model/user.js")
const game = require("../model/game.js")
export default {
name: 'JoinPrivate',
data(){
return this.idgame= "";
},
methods: {
rejoindre: async function(){
if((Number(this.idgame) != this.idgame || Number(this.idgame) < 0 )){
console.log(Number(this.idgame) != this.idgame)
console.log(Number(this.idgame) < 0)
return
}
const url = "http://localhost/felten/projet-cdaw/backend/MVC/api.php/gameuser"
let data = {
"GAME_ID": this.idgame,
"USER_ID": user.id
}
var params = {
method: 'POST',
body : JSON.stringify(data) };
game.id=this.idgame;
let response = await fetch(url, params)
console.log(response)
this.$router.push("/playerPrivate")
}
},
components: {
LoginNavbar
},
}
import LoginNavbar from '../components/LoginNavbar.vue'
</script>
\ No newline at end of file
......@@ -96,10 +96,11 @@ export default {
},
call: function(login, password){
const url = "http://localhost/projet-cdaw/backend/MVC/api.php/login"
// Felten config
// const url = "http://localhost/projet-cdaw/backend/MVC/api.php/login"
// Robin config
// const url = "http://localhost/felten/projet-cdaw/backend/MVC/api.php/login"
const url = "http://localhost/felten/projet-cdaw/backend/MVC/api.php/login"
let data = {
login: login,
......@@ -116,7 +117,7 @@ export default {
.then(response => {
user.token = response.jwt_token
user.id = response.data.id
user.name = response.data.name
user.login = response.data.login
})
.then(this.$router.push("/"))
}
......
<template>
<div class="inscription">
<LoginNavbar></LoginNavbar>
<div class="row _centered">
<div class="column _600">
<div class="codeGame">
<h1 class="titre"> Code de la partie :</h1>
<h1 class="titre" id="codeGame">{{gameID()}}</h1>
</div>
<div class="joueurs" id="joueurs">
{{JoueurPresent()}}
</div>
</div>
</div>
</div>
</template>
<style scoped>
.inscription{
height: 100%;
margin: 0 auto;
max-width: 1200px;
}
.titre{
height: 90%;
}
</style>
<script>
// const user = require("../model/user.js")
const game = require("../model/game.js")
export default {
name: 'PlayerPrivate',
methods: {
gameID: function(){
return game.id
},
JoueurPresent: async function(){
let url = "http://localhost/felten/projet-cdaw/backend/MVC/api.php/gameuser/game"
console.log(game.id)
url += "/" + game.id;
var params = {
method: "GET"
}
let response = await fetch(url,params)
let result = await response.json()
console.log(result)
let text = "";
result.forEach(user => {
text += `<tr><h2 class='joueur' id='joueur'>${user.USER_ID}</h2></tr>`
});
console.log(text)
document.getElementById("joueurs").innerHTML = text
if(result.length >= 2){
this.$router.push(`/game/${game.id}`)
return
}
setTimeout(() => {if(game.id){this.JoueurPresent()}}, 3000)
}
},
components: {
LoginNavbar
},
}
import LoginNavbar from '../components/LoginNavbar.vue'
</script>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment