Skip to content

Commit

Permalink
Refactor api (#4)
Browse files Browse the repository at this point in the history
* Refactored API code

* Added list and missile id functions to the API
  • Loading branch information
Rackover authored Apr 30, 2019
1 parent 105b586 commit 8ecb4aa
Showing 1 changed file with 125 additions and 78 deletions.
203 changes: 125 additions & 78 deletions api.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
header('Content-Type: application/json');


///////
// Loading data
//
$dataString = file_get_contents("data/blueprints.json");
$dataFull = json_decode($dataString);
$dataUnits = [];
Expand All @@ -15,81 +18,127 @@
else if ($thisUnit->BlueprintType == "ProjectileBlueprint"){
$dataMissiles[]=$thisUnit;
}
}


if (isset($_GET['id']) || isset($_GET['searchunit'])){

$requested = $_GET["id"];
$searchName = $_GET["searchunit"];
$uOI = null;

for ($i = 0; $i < sizeOf($dataUnits); $i++){
$element = $dataUnits[$i];
$id = $element->Id;
$name = $id .= " ";

/*
//Nicknames
switch ($id){
case "XSL0401 ":
$name .= "chicken ";
break;
case "UAL0401 ":
$name .= "gc ";
break;
case "UAA0310 ":
$name .= "donut ";
}
//Endof
*/
$name .= ($element->General->FactionName).' '.getTech($element);

if (property_exists($element->General, 'UnitName')){
$name .= ' "'.($element->General->UnitName).'" ';
}
if (property_exists($element, 'Description')){
$name .= ($element->Description);
}
/*
//All the names
if (strpos(strtolower($name), "extractor") !== false){
$name .= " mex mexes ";
}
if (strpos(strtolower($name), "strategic missile launcher") !== false){
$name .= " nuke SML ";
}
if (strpos(strtolower($name), "strategic missile defense") !== false){
$name .= " SMD ";
}
if (strpos(strtolower($name), "tactical missile launcher") !== false){
$name .= " TML ";
}
if (strpos(strtolower($name), "tactical missile defense") !== false){
$name .= " TMD ";
}
if (strpos(strtolower($name), "power generator") !== false && getTech($element) == "T1 "){
$name .= " pgen ";
}
//endof
*/
if ($id == $requested || strpos(strtolower($name), strtolower($searchName)) !== false){
$uOI = $element;
$uOI = (array)$uOI;
$uOI['ApiName'] = $name;
$uOI = (object)$uOI;
break;
}
}

echo json_encode($uOI);
exit;

}

}

//
//////

$commands = array();

//////////////////////////////////
///
/// Search for a specific unit
///
$commands["searchunit"] = function($data){
$uOI = null;
$units = $GLOBALS["dataUnits"];
for ($i = 0; $i < sizeOf($units); $i++){
$element = $units[$i];
$id = $element->Id;
$name = $id .= " " .($element->General->FactionName).' '.getTech($element);

if (property_exists($element->General, 'UnitName')){
$name .= ' "'.($element->General->UnitName).'" ';
}
if (property_exists($element, 'Description')){
$name .= ($element->Description);
}

if ($id == $data || strpos(strtolower($name), strtolower($data)) !== false){
$uOI = $element;
$uOI = (array)$uOI;
$uOI['ApiName'] = $name;
$uOI = (object)$uOI;
break;
}
}

return json_encode($uOI);
};
// alias
$commands["id"] = $commands["searchunit"];
///
///
//////////////////////////////////

//////////////////////////////////
///
/// Get missile by id
///
$commands["projectileid"] = function($data){

$projs = $GLOBALS["dataMissiles"];
foreach($projs as $proj){
if ($proj->Id == $data){

return json_encode($proj);

}

}

};
///
///
//////////////////////////////////

//////////////////////////////////
///
/// List units
///
$commands["listunits"] = function($data){
$list = array();
$units = $GLOBALS["dataUnits"];
foreach($units as $unit){
$list []= $unit->Id;
}
return json_encode($list);
};
///
///
//////////////////////////////////

//////////////////////////////////
///
/// List data
///
$commands["listdata"] = function($data){
$list = array("units"=>[], "missiles"=>[]);
$units = $GLOBALS["dataUnits"];
foreach($units as $unit){
$list ["units"] []= $unit->Id;
}
foreach($GLOBALS["dataMissiles"] as $miss){
$list ["missiles"] []= $miss->Id;
}
return json_encode($list);
};
///
///
//////////////////////////////////




//////
// Main execution of commands
//

foreach($commands as $name=>$function){
if (isset($_GET[$name])){
$data = $function($_GET[$name]);
echo $data;
exit;
}
}
//
//////


//////////////////////////////////
///
/// Utilitary functions
///

function getTech($unit){

Expand All @@ -110,6 +159,4 @@ function getTech($unit){
}
return $unitTech;
}


?>

0 comments on commit 8ecb4aa

Please sign in to comment.