From 8ecb4aa01f9604b09992aab77183c558fd6e1324 Mon Sep 17 00:00:00 2001 From: Rackochad <33836535+Rackover@users.noreply.github.com> Date: Tue, 30 Apr 2019 21:13:02 +0200 Subject: [PATCH] Refactor api (#4) * Refactored API code * Added list and missile id functions to the API --- api.php | 203 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 125 insertions(+), 78 deletions(-) diff --git a/api.php b/api.php index ad1890e..1d4b024 100755 --- a/api.php +++ b/api.php @@ -1,6 +1,9 @@ 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){ @@ -110,6 +159,4 @@ function getTech($unit){ } return $unitTech; } - - ?> \ No newline at end of file