botsArena/src/arenas/tron/act.php

92 lines
2.1 KiB
PHP
Raw Normal View History

2016-06-29 16:30:39 +02:00
<?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");
2016-06-29 16:56:24 +02:00
2016-06-29 16:30:39 +02:00
switch ($_POST['act']){
case "initGame":
2016-06-29 16:56:24 +02:00
//check if bots exists
2016-06-30 14:53:38 +02:00
$botsArrayTemp = json_decode($_POST['bots']);
2016-06-30 14:57:08 +02:00
$bots = array();
2016-06-30 14:53:38 +02:00
$positions = array();
$botCount = 0;
foreach($botsArrayTemp as $botId){
do{
$x = rand(1,999);
$y = rand(1,999);
}while(in_array($x.",".$y,$positions));
$positions[] = $x.",".$y;
2016-06-30 14:59:02 +02:00
$bots[$botCount] = new TronPlayer($botId,$x,$y,'y+');
2016-06-30 14:53:38 +02:00
2016-06-30 14:57:08 +02:00
if ($bots[$botCount]->getStatus() === false){
unset($bots[$botCount]);
2016-06-30 14:53:38 +02:00
}else{
$botCount++;
}
2016-06-29 16:56:24 +02:00
}
2016-07-01 07:19:00 +02:00
$players = $botCount;
2016-06-30 14:53:38 +02:00
if ($botCount < 2){
error (500,"missing bots");
2016-06-29 16:56:24 +02:00
}
2016-07-01 07:43:20 +02:00
$logs="";
2016-06-29 16:56:24 +02:00
2016-07-01 07:19:00 +02:00
//send init message
$gameId = get_unique_id();
$responses = array();
2016-07-01 07:43:20 +02:00
2016-07-01 07:22:58 +02:00
for ($botCount = 0; $botCount < count($bots); $botCount ++){
2016-07-01 07:19:00 +02:00
$messageArr = array(
'game-id' => "".$gameId,
'action' => 'init',
2016-07-01 07:20:48 +02:00
'game' => 'tron',
2016-07-01 07:19:00 +02:00
'board' => '',
'players' => $players,
'player-index' => $botCount
);
2016-07-01 07:43:20 +02:00
$resp = get_IA_Response($bots[$botCount]->getURL(),$messageArr);
if($_POST['fullLogs'] == "true"){
$logs.='Arena send to '.$bots[$botCount]->getName().'<em>'.htmlentities($resp['messageSend']).'</em><br/>
HTTP status: <em>'.htmlentities($resp['httpStatus']).'</em><br/>
Bot anwser: <em>'.htmlentities($resp['response']).'</em><br/>';
}else{
$logs.="Init message send to ".$bots[$botCount]->getName()."<br/>";
}
2016-07-01 07:19:00 +02:00
}
2016-07-01 07:50:35 +02:00
2016-07-03 12:35:55 +02:00
//save bots on session var
$_SESSION['bots'] = serialize($bots);
$_SESSION['gameId'] = $gameId;
2016-07-01 07:56:22 +02:00
echo json_encode(array(
2016-07-01 07:50:35 +02:00
'status' => 'OK',
2016-07-03 12:35:55 +02:00
'logs' => $logs,
'gameId' => $gameId
2016-07-01 07:50:35 +02:00
));
2016-07-01 07:29:56 +02:00
die;
2016-07-03 12:35:55 +02:00
break;
case "play":
print_r($_SESSION);
2016-06-29 16:30:39 +02:00
break;
default:
break;
}