start rewriting core

This commit is contained in:
Gnieark 2016-11-24 19:31:32 +01:00
parent f0ce5fd7de
commit 27cdeb549c
7 changed files with 320 additions and 14 deletions

View File

@ -1 +1 @@
1836 1865

View File

@ -13,9 +13,11 @@
@session_start(); @session_start();
require_once("../src/functions.php"); require_once("../src/functions.php");
//load classes
require_once ("../src/ARENAS.php");
require_once ("../src/BOTS.php");
$lnMysql=conn_bdd(); $lnMysql=conn_bdd();
$arenas=get_arenas_list();
$lang=get_language_array();
//check type of page //check type of page
//$_GET['arena'] -> an arena //$_GET['arena'] -> an arena
@ -24,6 +26,66 @@ $lang=get_language_array();
//$_GET['page'] -> a simple page like about page, legals etc... //$_GET['page'] -> a simple page like about page, legals etc...
//Nothing -> home page //Nothing -> home page
if(isset($_GET['arena'])){
$arenaId = $_GET['arena'];
}elseif(isset($_GET['doc'])){
$arenaId = $_GET['doc'];
}elseif(isset($_GET['scores'])){
$arenaId = $_GET['scores'];
}else{
$arenaId = "";
}
//hydrate
require_once ("../src/arenas_lists.php");
$exists = false;
foreach($arenas as $arena){
if($arenaId == "") break;
if($arena['id'] == $arenaId){
$currentArena = new ARENA($arenaId);
$currentArena->hydrate($arena);
//add Bots on arena
$rs=mysqli_query($lnMysql,
"SELECT id,name,url,description,ELO
FROM bots
WHERE game='".mysqli_real_escape_string($lnMysql,$currentArena->get_id())."'
AND active='1';"
);
while($r=mysqli_fetch_array($rs)){
$bot = new BOT("plop");
$bot->hydrate($r);
$currentArena->addBot($bot);
}
$exists = true;
break;
}
}
if((!$exists)&& ($arenaId <> ""))error(404,"Page not found ".$arenaId); //l'arene passée en url n'existe pass
if($arenaId == ""){
echo "accueil";
}else{
foreach($currentArena->bots as $bot){
echo $bot->name."\n";
}
}
die();
//**********************rewriting this file! ***************
//**********OLD code from here ************************************
$permitIndex=true; //will be set to false for pages that google or other bot must not index $permitIndex=true; //will be set to false for pages that google or other bot must not index
if(isset($_GET['arena'])){ if(isset($_GET['arena'])){

131
html/testBotScripts/tron.html Executable file
View File

@ -0,0 +1,131 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="author" content="Gnieark" />
<title>Trons's Ludus</title>
<style type="text/css">
body{width:100%; font-size:100%; line-height:140%; word-wrap:break-word; text-rendering:optimizelegibility;
margin:0 auto; font-family : "lucida grande", "gill sans", arial, sans-serif; left:auto;}
header{ background-color:#A60800; width: 100%; overflow: hidden; height: auto;}
header h1{display: block; font-size:300%; color:#FFF;float: left; margin-left: 5%;}
header nav{display: block; width: 45%; color:#FFF; float: right;}
#menus{ margin-left: 50px; width:100%; display: table;}
#menus a{color: #fff; display: table-cell; text-decoration: none;text-align: center;border-radius: 15px 15px 0px 0px;}
#menus a.selected{color:#202020; background-color:#fff;}
footer{height: 70px; display: block; color: #343638; font-size: 11pt; line-height: 15pt; margin: 0; padding-top: 15pt;
overflow-x: hidden; box-sizing: border-box; background-image: -webkit-linear-gradient(top, #f5f5f5,#e9e9e9);
border-top: 1px solid #bebebe; color: #999; font-size: 12px; line-height: 1.5em; text-align: center;width: 100%;}
footer a {margin:0px 15px 0px 15px; color: #666;text-decoration: none; font-weight: normal;}
#languages{float: right; text-align: right;}
section{margin: 0 auto; width: 90%;}
article{float: right; width:70%;}
aside{float:left;width:29%;}
.playerBox{}
.playerBox input, .playerBox select {width:100%;}
.hidden{display: none;}
#logs{font-size: 70%;}
</style>
<script><!--
function createElem(type,attributes){
var elem=document.createElement(type);
for (var i in attributes)
{elem.setAttribute(i,attributes[i]);}
return elem;
}
function changeRandom(index,newValue){
if (newValue == "random" ){
document.getElementById("posX" + index).className = "hidden";
document.getElementById("posY" + index).className = "hidden";
}else{
document.getElementById("posX" + index).className = "";
document.getElementById("posY" + index).className = "";
}
}
function changeTypePlayer(index,newValue){
if (newValue == "Human") document.getElementById("botUrl" + index).className = "hidden";
else document.getElementById("botUrl" + index).className = "";
}
function createPlayerForm(index){
var container = document.getElementById("playersSettings");
var divForm = createElem("div", {"id": "containerPlayer" + index,"class":"playerBox" } );
//Type of player
var select = createElem("select",{"id":"playerType" + index,"onchange":"changeTypePlayer('" + index + "', this.value);"});
var optionHuman = createElem("option",{"value":"Human"});
optionHuman.innerHTML = "Human";
var optionBot = createElem("option",{"value":"Bot"});
optionBot.innerHTML = "Bot";
select.appendChild(optionBot);
select.appendChild(optionHuman);
divForm.appendChild(select);
//url
var inputUrl = createElem("input", {"id","botUrl" + index, "type": "text", "value":"jljfhglfsghf", "placeholder":"http://Bot.url" });
divForm.appendChild(inputUrl);
//Starting cell
var selectRandomStartCell = createElem("select",{
"id": "randomOrNot" + index,
"onchange":"changeRandom('" + index + "', this.value);"
});
var optionRandom = createElem("option",{"value":"random","selected":"selected"});
optionRandom.innerHTML = "random";
var optionDefined = createElem("option",{"value":"defined"});
optionDefined.innerHTML = "Let me define it";
selectRandomStartCell.appendChild(optionRandom);
selectRandomStartCell.appendChild(optionDefined);
divForm.appendChild(selectRandomStartCell);
var inputX = createElem("input",{"type":"text","value": "0","id":"posX" + index,"class":"hidden"});
var inputY = createElem("input",{"type":"text","value": "0","id":"posY" + index,"class":"hidden"});
divForm.appendChild(inputX);
divForm.appendChild(inputY);
container.appendChild(divForm);
}
--></script>
</head>
<body>
<header>
<h1>Tron's Ludus</h1>
</header>
<section>
<p>Here you can test and fix your bot, against himself, against human or against any other bot if you know the URL.
<br/>No scoring here, it's a Ludus (gladiators training center).</p>
<aside id="playersSettings">
<h2>Configure the test</h2>
<script>
createPlayerForm(0);
</script>
</aside>
<article id="fightResult">
</article>
</section>
<footer>
<a href="/p/About">About</a><a href="https://github.com/gnieark/botsArena">Bots'Arena source code</a><a href="/p/legals">Mentions légales</a>
</footer>
</body>
</html>

77
src/ARENAS.php Normal file
View File

@ -0,0 +1,77 @@
<?php
class InvalidArenaException extends UnexpectedValueException{
}
class ARENA{
public $name;
public $bots;
private $path;
private $id;
private $url;
private $title;
private $metaDescription;
private $jsFile;
private $cssFile;
private $ludusUrl;
/*
'id' => "tictactoe",
'url' => "/tictactoe",
'title' => "Tic Tac Toe",
'metaDescription' => 'Affrontements de bots au TicTacToe, morpion',
'jsFile'=> "js.js",
'cssFile'=> "style.css",
'ludusUrl' => "/testBotScripts/tictactoe.html"
*/
public function get_id(){
return $this->id;
}
public function get_css_code(){
return file_get_contents($this->path.$this->cssFile);
}
public function get_js_code(){
return file_get_contents($this->path.$this->jsFile);
}
public function get_title(){
return $this->title;
}
public function get_meta_description(){
return $this->metaDescription;
}
public function get_ludus_url(){
return $this->ludusUrl;
}
public function get_doc($lang){
if(file_exists($this->path."doc-".$lang.".html")) return file_get_contents($this->path."doc-".$lang.".html");
if(file_exists($this->path."doc-fr.html")) return file_get_contents($this->path."doc-fr.html");
return "";
}
public function __construct($name){
$this->name = $name;
$this->path = __DIR__."/arenas/".$name."/";
if(!is_dir($this->path)){
throw new InvalidArenaException("No path containing arena ".$name." found ".__DIR__."/".$name."/");
}
$this->bots = new SplStack();
}
public function hydrate($arr){
foreach ($arr as $key => $value){
if (property_exists($this,$key)){
$this->$key = $value;
}else{
throw new InvalidArenaException("incorrect array key");
}
}
}
public function addBot(BOT $bot){
$this->bots->push($bot);
}
}

46
src/BOTS.php Normal file
View File

@ -0,0 +1,46 @@
<?php
class InvalidBotsException extends UnexpectedValueException{
}
class BOT{
/*
+---------------------+-------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+-------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | text | NO | | NULL | |
| game | varchar(10) | NO | | NULL | |
| url | text | NO | | NULL | |
| description | text | NO | | NULL | |
| unclean_description | text | NO | | NULL | |
| active | int(1) | NO | | NULL | |
| date_inscription | timestamp | NO | | CURRENT_TIMESTAMP | |
| validate_secret | varchar(8) | NO | | NULL | |
| author_email | text | NO | | NULL | |
| ELO | int(11) | NO | | 1500 | |
+---------------------+-------------+------+-----+-------------------+----------------+
*/
private $id;
public $name;
//don't link to game in this class
public $url;
public $description;
public $ELO;
public function __construct($name){
$this->name = $name;
}
public function hydrate ($arr){
foreach ($arr as $key => $value){
if (property_exists($this,$key)){
$this->$key = $value;
}elseif(is_numeric($key)){
//rien, on accepte mais prends pas en compte
}else{
throw new InvalidArenaException("incorrect array key".$key);
}
}
}
}

View File

@ -31,16 +31,6 @@ class DUEL{
} }
} }
class ELO class ELO
{ {
public $rank = 1500; //default rank public $rank = 1500; //default rank

View File

@ -1,6 +1,6 @@
<?php <?php
class InvalidDirectionException extends UnexpectedValueException{ class InvalidDirectionException extends UnexpectedValueException{
// throw new InvalidDirectionException("expected 'x+', 'x-', 'y+' or 'y-'". (string)$str."received.");
} }
class Direction class Direction
{ {