Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sleavely/945939aec743ebe1b54ddc89b861493b to your computer and use it in GitHub Desktop.
Save Sleavely/945939aec743ebe1b54ddc89b861493b to your computer and use it in GitHub Desktop.
<?php
use Carbon\Carbon;
class ApiV1Controller extends BaseController {
public $endpoints = array(
'items' => 'https://storage.googleapis.com/nacleanopenworldprodshards/ItemTemplates_cleanopenworldprodeu2.json',
'nations' => 'https://storage.googleapis.com/nacleanopenworldprodshards/Nations_cleanopenworldprodeu2.json',
'ports' => 'https://storage.googleapis.com/nacleanopenworldprodshards/Ports_cleanopenworldprodeu2.json',
'shops' => 'https://storage.googleapis.com/nacleanopenworldprodshards/Shops_cleanopenworldprodeu2.json',
);
/**
* Helper for caching and whatnot. Decides the virtual server
* date based on whether maintenance has already ocurred.
*
* @param Carbon|string $carbondate
* Optional Carbon date or date string to verify against.
* @return string
* Y-m-d
*/
public function _ServerDate($carbondate = '')
{
// Handle the Carbon argument first
if(!($carbondate instanceof Carbon))
{
if(empty($carbondate))
{
$carbondate = Carbon::now('UTC');
}
else
{
$carbondate = new Carbon($carbondate);
}
}
// Make sure its UTC
if(!$carbondate->utc)
{
// This will update so that when asking for ->hour and ->minute it will say the correct numbers.
$carbondate->timezone('UTC');
}
// Now lets figure out how the Carbon date compares to server maintenance.
// For simplicitys sake, lets assume that by 07:50 UTC the data files have been updated.
if($carbondate->hour >= 8 || ($carbondate->hour == 7 && $carbondate->minute >= 50))
{
$outputdate = Carbon::today('UTC');
}
else
{
$outputdate = Carbon::yesterday('UTC');
}
return $outputdate->toDateString();
}
public function getServerDate()
{
$date = $this->_ServerDate();
return Response::JSON($date);
}
public function _DownloadEndpoint($url)
{
$content = file_get_contents($url);
if($content !== null && is_string($content))
{
// Skip the Javascript stuff and extract the JSON-like data.
$equalPos = strpos($content, '=');
if($equalPos!==false)
$content = trim(print_r(substr($content, $equalPos+1), true), ' ;');
// Decode it into associative arrays
$JSON = json_decode($content, TRUE);
return $JSON;
}
// If we get this far we're pretty fucked.
throw new Exception('Could not parse data file');
}
public function _GetData($endpoint)
{
$serverdate = $this->_ServerDate();
$cache_key = 'apiv1-datafile-'.$serverdate.md5($endpoint);
$cache_minutes = 1440; // 24h
$cache_value = Cache::remember($cache_key, $cache_minutes, function() use ($endpoint)
{
$data = $this->_DownloadEndpoint($this->endpoints[$endpoint]);
return $data;
});
return $cache_value;
}
public function getItems()
{
$output = $this->_GetData('items');
return Response::JSON($output);
}
public function getNations()
{
$output = $this->_GetData('nations');
return Response::JSON($output);
}
public function getPorts()
{
$output = $this->_GetData('ports');
return Response::JSON($output);
}
public function getShops()
{
$output = $this->_GetData('shops');
return Response::JSON($output);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment