Advertisement
Guest User

stocks.php

a guest
Jun 7th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.30 KB | None | 0 0
  1. <?php
  2.  
  3. const WORLD_URL_REGEX = '/(?P<country>[A-Za-z]+)(?P<worldId>\d+)/';
  4. const STOCKS_REGEX = '/(?P<id>\d+)\s+(?P<name>.+?)\s+(?P<value>\d+)/';
  5. const SECOND_REFRESH = 3600;
  6.  
  7. require_once('Option.php');
  8.  
  9. function arrayGet($array, $key)
  10. {
  11.     return COption::of(isset($array[$key]) ? $array[$key] : null);
  12. }
  13.  
  14. function getWorldInfo($worldUrl)
  15. {
  16.     return (preg_match_all(WORLD_URL_REGEX, $worldUrl, $matches, PREG_SET_ORDER, 0) == true ? COption::of([ 'country' => $matches[0]['country'], 'id' => intval($matches[0]['worldId']) ]) : COption::of(null));
  17. }
  18.  
  19. function validateWorldInfo($worldInfo)
  20. {
  21.     $id = $worldInfo['id'];
  22.     switch($worldInfo['country'])
  23.     {
  24.     case 'de': return (1 <= $id && $id <= 14);
  25.     case 'uk': return (1 <= $id && $id <= 1);
  26.     default: return False;
  27.     }
  28. }
  29.  
  30. function getUrl($worldInfo)
  31. {
  32.     function getUrlForCountry($country)
  33.     {
  34.         switch($country)
  35.         {
  36.         case 'uk': return 'http://world%d.freewar.com/freewar/';
  37.         case 'de': return 'http://welt%d.freewar.de/freewar/';
  38.         }
  39.     }
  40.  
  41.     function getUrl_($country, $id)
  42.     {
  43.         return sprintf(getUrlForCountry($country), $id);
  44.     }
  45.  
  46.     return getUrl_($worldInfo['country'], $worldInfo['id']);
  47. }
  48.  
  49. function parseStocksFile($content)
  50. {
  51.     preg_match_all(STOCKS_REGEX, $content, $matches, PREG_SET_ORDER, 0);
  52.     $stocks = array();
  53.     foreach($matches as $match)
  54.     {
  55.         $id = intval($match['id']);
  56.         $name = $match['name'];
  57.         $value = intval($match['value']);
  58.         $stocks[$id] = array('name' => $name, 'value' => $value);
  59.     }
  60.     return $stocks;
  61. }
  62.  
  63.  
  64. $res = arrayGet($_GET, 'world')->flatMap(getWorldInfo)->filter(validateWorldInfo)->flatMap(function($info)
  65. {
  66.     $file = sprintf('freewar/sotcks_%s%d.json', $info['country'], $info['id']);
  67.     if(!file_exists($file) || time() - filemtime($file) >= SECOND_REFRESH)  // print out old file
  68.     {
  69.         $url = getUrl($info);
  70.         if(($urlContent = @file_get_contents($url . 'list_stocks.php')) != false)
  71.         {
  72.             $stocks = parseStocksFile($urlContent);
  73.             file_put_contents($file, json_encode([ 'status' => 'success', 'data' => $stocks ], JSON_PRETTY_PRINT));
  74.         }
  75.         else
  76.         {
  77.             return COption::of(NULL);
  78.         }
  79.     }
  80.     return COption::of(file_get_contents($file));
  81. })->getOrElse(json_encode([ 'status' => 'error', 'message' => 'an error occured' ], JSON_PRETTY_PRINT));
  82.  
  83. header('Content-Type: application/json');
  84. echo $res;
  85.    
  86. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement