From 7599202cbb7b750a8c963fafc8c15a5017b84ea5 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Wed, 29 Jun 2016 16:30:39 +0200 Subject: [PATCH] class playersTron --- src/arenas/tron/act.php | 22 +++++++++++ src/arenas/tron/functions.php | 71 +++++++++++++++++++++++++++++++++++ src/arenas/tron/js.js | 3 +- src/functions.php | 2 +- 4 files changed, 96 insertions(+), 2 deletions(-) diff --git a/src/arenas/tron/act.php b/src/arenas/tron/act.php index e69de29..e825e0c 100644 --- a/src/arenas/tron/act.php +++ b/src/arenas/tron/act.php @@ -0,0 +1,22 @@ +direction == "x-")) + || (($newDir == "x-") && ($this->direction == "x+")) + || (($newDir == "y+") && ($this->direction == "y-")) + || (($newDir == "y-") && ($this->direction == "y+")) + ){ + return false; + } + $this->direction = $newdir; + return true; + } + + public function grow($dir=""){ + if($dir == ""){ + $dir = $this->direction; + } + + $headCoords = end($this->tail); + + + switch $dir{ + case "y+": + $targetCoords = array($headCoords[0],$headCoords[1]++); + break; + case "y-": + $targetCoords = array($headCoords[0],$headCoords[1]--); + break; + case "x+": + $targetCoords = array($headCoords[0]++,$headCoords[1]); + break; + case "x-": + $targetCoords = array($headCoords[0]--,$headCoords[1]); + break; + default: + return false; + + } + + } + + + public function __construct($id,$initialX0,$initialY0,$initialDirection){ + $lnBdd = conn_bdd(); + $rs = mysqli_query($lnBdd, + "SELECT name,url FROM bots WHERE game='tron' AND id='".mysqli_real_escape_string($lnBdd, $id)."';" + ); + if(($r=mysqli_fetch_row($rs)) && in_array($initialDirection,array('x-','x+','y-','y+'))){ + $this->name = $r[0]; + $this->url = $r[1]; + $this->tail = array(array($initialX,$initialY)); + $this->direction = $initialDirection; + $this->state= true; + }else{ + $this->state = false; + } + + + + } +} \ No newline at end of file diff --git a/src/arenas/tron/js.js b/src/arenas/tron/js.js index fea8610..9c0f9be 100644 --- a/src/arenas/tron/js.js +++ b/src/arenas/tron/js.js @@ -13,9 +13,10 @@ function createElemNS(type,attributes){ } function applyInitMessage(req){ + //callback function when init game request if(req.readyState == 4){ if(req.status == 200) { - alert ("ok"); + alert (req.responseTxt); }else{ alert ('error ' + xhr.status); diff --git a/src/functions.php b/src/functions.php index 45cc289..e289bf2 100644 --- a/src/functions.php +++ b/src/functions.php @@ -151,7 +151,7 @@ function conn_bdd(){ } mysqli_select_db($linkMysql,$mysqlParams['database']); mysqli_set_charset($linkMysql, 'utf8'); - return $linkMysql; //does PHP can do that? + return $linkMysql; } function get_battles_history($game){