class playersTron

This commit is contained in:
Gnieark 2016-06-29 16:30:39 +02:00
parent 06610e9646
commit 7599202cbb
4 changed files with 96 additions and 2 deletions

View File

@ -0,0 +1,22 @@
<?php
#- BEGIN LICENSE BLOCK ---------------------------------------
#
# This file is part of botsArena.
#
# Copyright (C) Gnieark et contributeurs
# Licensed under the GPL version 3.0 license.
# See LICENSE file or
# http://www.gnu.org/licenses/gpl-3.0-standalone.html
#
# -- END LICENSE BLOCK -----------------------------------------
require_once(__DIR__."/functions.php");
switch ($_POST['act']){
case "initGame":
break;
default:
break;
}

View File

@ -0,0 +1,71 @@
<?php
class TronPlayer{
private $url;
private $name;
private $tail = array();
private $direction;
public $state;
private function set_direction($newDir){
//can't be the opposite of the previous direction
if(
(($newDir == "x+") && ($this->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;
}
}
}

View File

@ -13,9 +13,10 @@ function createElemNS(type,attributes){
} }
function applyInitMessage(req){ function applyInitMessage(req){
//callback function when init game request
if(req.readyState == 4){ if(req.readyState == 4){
if(req.status == 200) { if(req.status == 200) {
alert ("ok"); alert (req.responseTxt);
}else{ }else{
alert ('error ' + xhr.status); alert ('error ' + xhr.status);

View File

@ -151,7 +151,7 @@ function conn_bdd(){
} }
mysqli_select_db($linkMysql,$mysqlParams['database']); mysqli_select_db($linkMysql,$mysqlParams['database']);
mysqli_set_charset($linkMysql, 'utf8'); mysqli_set_charset($linkMysql, 'utf8');
return $linkMysql; //does PHP can do that? return $linkMysql;
} }
function get_battles_history($game){ function get_battles_history($game){