scoring class

This commit is contained in:
Gnieark 2016-10-18 20:18:32 +02:00
parent 7939a6a911
commit 87d2c4cf1e
3 changed files with 42 additions and 85 deletions

View File

@ -1 +1 @@
1716 1726

View File

@ -3,29 +3,36 @@
class ScoreLap class ScoreLap
{ {
private $playersIdsByOrder; //array containing id's of alive players at the beginning of the match, Put only alive bots in! private $playersIdsByOrder; //array containing id's of alive players at the beginning of the match, Put only alive bots in!
private $looserList; //only containing theirs orders private $looserList; //idem but only loosers
public function addBotOnLap($order,$id){ public function addBotOnLap($order,$id){
$this->playersIdsByOrder[$order] = $id; $this->playersIdsByOrder[$order] = $id;
} }
public function addLoser($order){ public function addLoser($order,$id){
$this->looserList[] = $order; $this->looserList[$order] = $order;
} }
public function __construct() { public function __construct() {
$this->playersIdsByOrder = array(); $this->playersIdsByOrder = array();
$this->looserList = array(); $this->looserList = array();
} }
public function make($botsList){
//$botsList must be like array[{botOrder:BotId},{botOrder:BotId}] public function getLoosersList(){
$this->looserList = $botsList; return $this->looserList;
} }
//NO!
// public function make($botsList){
// //$botsList must be like array[{botOrder:BotId},{botOrder:BotId}]
// $this->looserList = $botsList;
// }
private function ApplyDraws(){ private function ApplyDraws(){
//apply draw match to all losers //apply draw match to all losers
if(count($this->looserList) > 1){ //no draw if only 0 or one user at this lap if(count($this->looserList) > 1){ //no draw if only 0 or one user at this lap
foreach($looserList as $looser1){ foreach($looserList as $order1 => $looser1){
foreach($looserList as $looser2){ foreach($looserList as $order2 => $looser2){
if($order1 <> $order2){
save_battle('tron', save_battle('tron',
$this->playersIdsByOrder[$looser1], $this->playersIdsByOrder[$looser1],
$this->playersIdsByOrder[$looser2], $this->playersIdsByOrder[$looser2],
@ -35,8 +42,16 @@ class ScoreLap
} }
} }
} }
}
private function ApplyWins(){ private function ApplyWins(){
foreach($this->looserList as $looser){
//need to make losers List. simply array of orders
$loosersOrdersArr = array();
foreach($this->looserList as $order => $looser){
$loosersOrdersArr[] = $order;
}
foreach($this->looserList as $looserOrder => $looserId){
foreach($playersIdsByOrder as $order=>$player){ foreach($playersIdsByOrder as $order=>$player){
if(!in_array($order,$this->looserList)){ if(!in_array($order,$this->looserList)){
save_battle('tron', save_battle('tron',

View File

@ -20,58 +20,6 @@ class TronGame
} }
} }
private function apply_looses($loosersArr){
//$loosersArr est construit comme ça: [{"order":1,"id":54"},{"order":3,"id":54"}]
//save draws
if( count($loosersArr) > 0 ){
$loosersById = array();
foreach($loosersArr as $bot){
//error_log('apply lose '.$bot['id']);
$loosersById[] = $bot['id'];
}
}
if( count($loosersArr) > 1 ){ //...que si au moins deux joueurs ont perdu sur le meme tour
$this->save_draw_bots($loosersById);
}
//save victories
if( count($loosersArr) > 0 ){
//make victorous bots array
$vbots = array();
for ($botCount = 0; $botCount < count($this->bots); $botCount++){
if($this->bots[$botCount]->isAlive){
$vbots[] = $this->bots[$botCount]->id;
}
}
$this->save_losers_winers($loosersById,$vbots);
}
}
private function save_draw_bots($arr){
/*
* Recursive function who save all combionaisons of draw matches
*/
if(count($arr) < 2){
return;
}else{
$a = $arr[0];
array_shift($arr);
foreach($arr as $bot){
save_battle('tron',$a,$bot,0,'id');
}
$this->save_draw_bots($arr);
}
}
private function save_losers_winers($arrLoosers,$arrWiners){
foreach($arrWiners as $winner){
foreach($arrLoosers as $looser){
save_battle('tron',$winner,$looser,1,'id');
}
}
}
public function get_trails(){ public function get_trails(){
//return all trails for draw svg //return all trails for draw svg
$trailsArr = array(); $trailsArr = array();
@ -96,7 +44,6 @@ class TronGame
$paramToSend = array(); $paramToSend = array();
$board = $this->get_trails(); $board = $this->get_trails();
//$board = $this->get_lasts_trails(); //$board = $this->get_lasts_trails();
$loosers = array();
$lastsCells = array(); $lastsCells = array();
$scoring = new ScoreLap(); $scoring = new ScoreLap();
@ -105,7 +52,7 @@ class TronGame
for ($botCount = 0; $botCount < $nbeBots; $botCount++){ for ($botCount = 0; $botCount < $nbeBots; $botCount++){
if ($this->bots[$botCount]->isAlive){ if ($this->bots[$botCount]->isAlive){
$scoreLap->addBotOnLap($botCount,$this->bots[$botCount]->id); $scoring->addBotOnLap($botCount,$this->bots[$botCount]->id);
$urls[$botCount] = $this->bots[$botCount]->url; $urls[$botCount] = $this->bots[$botCount]->url;
$paramsToSend[$botCount] = array( $paramsToSend[$botCount] = array(
@ -127,14 +74,15 @@ class TronGame
if(!$dir = Direction::make($responses[$botCount]['responseArr']['play'])){ if(!$dir = Direction::make($responses[$botCount]['responseArr']['play'])){
//he loses , non conform response //he loses , non conform response
$loosers[] = $botCount; $scoring-> addLoser($botCount,$this->bots[$botCount]->id);
$this->bots[$botCount]->loose(); $this->bots[$botCount]->loose();
}else{ }else{
$lastsCells[$botCount] = $this->bots[$botCount]->grow($dir); $lastsCells[$botCount] = $this->bots[$botCount]->grow($dir);
if($lastsCells[$botCount] === false){ if($lastsCells[$botCount] === false){
$loosers[] = $botCount; //$loosers[] = $botCount;
$scoring-> addLoser($botCount,$this->bots[$botCount]->id);
$this->bots[$botCount]->loose(); $this->bots[$botCount]->loose();
} }
@ -150,25 +98,19 @@ class TronGame
if(($botCount <> $botCount2) if(($botCount <> $botCount2)
&& ($this->bots[$botCount2]->trail->contains($lastsCells[$botCount])) && ($this->bots[$botCount2]->trail->contains($lastsCells[$botCount]))
){ ){
$loosers[] = $botCount;
$scoring-> addLoser($botCount,$this->bots[$botCount]->id);
$this->bots[$botCount]->loose(); $this->bots[$botCount]->loose();
break; break;
} }
} }
} }
} }
//loosers list (in order to pass their id)
$loosersList = array(); //$this->apply_looses($loosersList);
foreach($loosers as $looser){
$loosersList[] = array(
'order' => $looser,
'id' => $this->bots[$looser]->id
);
}
$this->apply_looses($loosersList);
return array( return array(
'last_points' => $this->get_lasts_trails(), 'last_points' => $this->get_lasts_trails(),
'loosers' => $loosersList 'loosers' => $scoring->getLoosersList()
); );
} }
private function get_multi_IAS_Responses($iasUrls, $postParams){ private function get_multi_IAS_Responses($iasUrls, $postParams){