Commit 8d87d952 authored by Robin Borgogno's avatar Robin Borgogno

online

parent f5bf9b53
......@@ -57,7 +57,7 @@ class GameController extends Controller {
public function createGame($data)
{
if(array_key_exists("GAME_DESC", $data)){
$game = Game::createGame(array( ":GAME_DESC" => $data['GAME_DESC']));
$game = Game::createGame(array( ":GAME_DESC" => $data['GAME_DESC'], ":private" => $data['GAME_PRIVATE']));
$response = new Response(200,json_encode($game));
}else{
$response = Response::errorInParametersResponse("Parameters missing");
......
<?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 OnlineController 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->returnUserOnline($id);
case 'POST':
$id = $this->request->getURIParams()[0];
return $this->addUserOnline($id);
break;
case 'DELETE':
$id = $this->request->getURIParams()[0];
return $this->deleteUserOnline($id);
break;
case 'OPTIONS':
return Response::okresponse(json_encode("OPTIONS"));
}
return Response::errorResponse("unsupported parameters or method in game");
}
public function returnUserOnline($id)
{
$id = Online::returnUserOnline($id);
$res = json_encode($id);
return Response::okresponse($res);
}
public function addUserOnline($id)
{
Online::addUserOnline(array(":id" => $id));
return Response::okresponse(json_encode("It's ok bro"));
}
public function deleteUserOnline($id)
{
Online::deleteUserOnline(array(":id" => $id));
return Response::okresponse(json_encode("It's ok bro for delete"));
}
}
......@@ -16,7 +16,8 @@ class Game extends Model {
public static function getGame($id)
{
$stm = parent::exec('GAME_BY_ID', array(':game_id' => $id));
return $stm->fetchAll();
$res = $stm->fetchAll();
return $res;
}
public static function createGame($values)
......
<?php
class Online extends Model {
// ===========
// = Statics =
// ===========
protected static $table_name = 'ONLINE';
public static function addUserOnline($values)
{
$stm = parent::exec('ONLINE_ADD',$values);
return ;
}
public static function returnUserOnline($id)
{
$stm = parent::exec('ONLINE_IDMIN');
$res = $stm->fetchAll();
$idmin = $res[0]->USER_ID;
if($id == $idmin){
$game = Online::getIdGame();
// print_r($game);
// exit;
if(count($game) == 1){
Online::deleteUserOnline(array(":id" => $id));
Game::addUserGame(array(":GAME_ID" => $game[0]->GAME_ID, ":USER_ID" => $id));
return $game[0]->GAME_ID;
}
else {
$idgame = Game::createGame(array( ":GAME_DESC" => "Desc", ":private" => "0"));
Online::deleteUserOnline(array(":id" => $id));
// print_r($idgame);
// exit;
Game::addUserGame(array(":GAME_ID" => $idgame[0]->id, ":USER_ID" => $id));
return $idgame[0]->id;
}
}else {
return;
}
}
public static function deleteUserOnline($values)
{
$stm = parent::exec('ONLINE_DELETE',$values);
return;
}
public static function getIdGame()
{
$stm = parent::exec('ONLINE_IDGAME');
return $stm->fetchAll();
}
}
\ No newline at end of file
......@@ -7,7 +7,7 @@ Game::addSqlQuery('GAME_BY_ID',
"SELECT * FROM GAME WHERE GAME_ID=:game_id");
Game::addSqlQuery('GAME_CREATE',
"INSERT INTO GAME (GAME_ID, GAME_DESC) VALUES (NULL, :GAME_DESC)");
"INSERT INTO GAME (GAME_ID, GAME_DESC, GAME_PRIVATE) VALUES (NULL, :GAME_DESC, :private)");
Game::addSqlQuery('GAME_ID_MAX',
"SELECT MAX(GAME_ID) AS id FROM GAME");
......
<?php
Online::addSqlQuery('ONLINE_USERS',
"SELECT * FROM ONLINE");
Online::addSqlQuery('ONLINE_ADD',
"INSERT INTO ONLINE(USER_ID) VALUES (:id)");
Online::addSqlQuery('ONLINE_DELETE',
"DELETE FROM ONLINE WHERE ONLINE.USER_ID = :id");
Online::addSqlQuery('ONLINE_IDMIN',
"SELECT USER_ID FROM (SELECT MIN(ONLINE_ID) AS MINID FROM ONLINE) AS MIN, ONLINE WHERE ONLINE.ONLINE_ID=MIN.MINID");
Online::addSqlQuery('ONLINE_IDGAME',
"SELECT GAME_HISTO.* FROM (SELECT MAX(GAME_ID) AS MAXID FROM GAME WHERE GAME.GAME_PRIVATE = 0) AS MAXI, GAME_HISTO WHERE GAME_HISTO.GAME_ID=MAXI.MAXID");
\ No newline at end of file
......@@ -59,6 +59,11 @@ const routes = [
path: "/game/:id",
name: "Game",
component: () => import(/* webpackChunkName: "login" */ '../views/Game.vue')
},
{
path: "/online",
name: "Online",
component: () => import(/* webpackChunkName: "login" */ '../views/Online.vue')
}
]
......
......@@ -37,7 +37,8 @@ export default {
const url = "http://localhost/felten/projet-cdaw/backend/MVC/api.php/game"
let data = {
"GAME_DESC": "testHTML"
"GAME_DESC": "testHTML",
"GAME_PRIVATE": "1"
}
var params = {
......
......@@ -7,7 +7,7 @@
<div class="row">
<div class="column">
<tr class = "espaceBtn"><button class="partie">
<router-link to="/game">Rejoindre une partie publique</router-link>
<router-link to="/online">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>
......
......@@ -74,18 +74,16 @@ export default {
this.updateScore(result, value)
this.resetStyle()
havePlayed = false
setTimeout(async () => {this.resetGame(result)},2000)
setTimeout(async () => {this.resetGame(result)},700)
return
}
setTimeout(async () => {
await this.getResult(gameID,value)
}, 1000)
}, 250)
},
updateScore: function (result, value) {
console.log(result)
console.log(user.id)
let i = 0;
if(result[0]['USER_ID'] == user.id){
i = 1
......
<template>
<div class="page" onload="ajouterUser()">
<div class = "titre" >Veuillez patienter ...</div>
</div>
</template>
<script>
const user = require("../model/user.js")
const game = require("../model/game.js")
export default {
name: 'ChoixPartie',
methods: {
ajouterUser: async function() {
await fetch(
`http://localhost/felten/projet-cdaw/backend/MVC/api.php/online/${user.id}`,
{
method: "POST",
}
)
await this.findGame()
},
findGame: async function(){
console.log(user.id)
const res = await fetch(
`http://localhost/felten/projet-cdaw/backend/MVC/api.php/online/${user.id}`,
{
method: "GET"
}
)
console.log(res)
const result = await res.json()
console.log(result)
if (result.length >= 1) {
console.log(result)
this.goGame(result)
return
}
setTimeout(async () => {
await this.findGame()
}, 1000)
},
goGame: async function(gameID) {
let url = "http://localhost/felten/projet-cdaw/backend/MVC/api.php/gameuser/game"
console.log(gameID)
url += "/" + gameID;
var params = {
method: "GET"
}
let response = await fetch(url,params)
let result = await response.json()
if(result.length >= 2){
game.id = gameID
this.$router.push(`/game/${gameID}`)
return
}
setTimeout(() => {this.goGame(gameID)}, 2000)
}
},
beforeMount(){
this.ajouterUser();
},
}
</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