Advertisement
NokitaKaze

Движок Ачивок

Jan 18th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.29 KB | None | 0 0
  1. <?php
  2. /*
  3.  * Пришло время жрать оперативку
  4.  */
  5.  class AnimeAchievements{
  6.   var $data;
  7.  
  8.   function __construct($data_in=null){
  9.    $this->data =($data_in==null) ? (object)array() : json_decode(json_encode($data_in));
  10.   }
  11.  
  12.   /*
  13.    * Делаем запрос к found
  14.    */
  15.   function find($query){
  16.    $result=new AnimeAchievements();
  17.    
  18.    if ($this->length()==0){
  19.     // Если текущий список пустой, то даже разбирать не надо
  20.     return $result;
  21.    }
  22.    
  23.    // @todo not in через много пробелов
  24.    if (!preg_match('_^ *(type|id|status|score) *(\\<\\>|\\=|in|not in|\\>|\\<|\\>\\=|\\<\\=) *([0-9a-z, ]+)_i', $query, $a)){
  25.     return null;
  26.    }
  27.    
  28.    // Заполняем переменные
  29.    $q_variable   =strtolower($a[1]);
  30.    $q_comparision=strtolower($a[2]);
  31.    if ($q_comparision=='not in'){$q_comparision='<>';}
  32.    if ($q_comparision=='in'){$q_comparision='=';}
  33.    $q_values=$a[3];
  34.    //echo '<br />variable '.$q_variable.'; comparision '.$q_comparision.'; q_values '.$q_values;
  35.    
  36.    // Находим текущий value
  37.    switch ($q_variable){
  38.     case 'id': $key='anime_id'; break;
  39.     default:   $key=$q_variable;
  40.    }
  41.    //echo '<br/>key = '.$key;
  42.    
  43.    // Запоняем result
  44.    foreach($this->data as $datum){
  45.     $current_value=$datum->{$key};
  46.    
  47.     $a =preg_split('_[ ,]_', $q_values);
  48.     $u =false;
  49.     foreach ($a as $value){
  50.      $value=$this->const_to_value($value);
  51.      //echo '<br />'.$current_value.' '.$q_comparision.' '.$value;
  52.      if (
  53.          (($q_comparision=='=') and($value==$current_value))
  54.          or
  55.          (($q_comparision=='<>')and($value!=$current_value))
  56.          or
  57.          (($q_comparision=='>') and($value< $current_value))
  58.          or
  59.          (($q_comparision=='<') and($value> $current_value))
  60.          or
  61.          (($q_comparision=='>=')and($value<=$current_value))
  62.          or
  63.          (($q_comparision=='<=')and($value>=$current_value))
  64.      ){ $u=true; break; }
  65.     }
  66.    
  67.     if ($u){
  68.      $result->data->{$datum->anime_id}=(object)$datum;
  69.     }
  70.    }
  71.    
  72.    return $result;
  73.   }
  74.  
  75.   function const_to_value($value){
  76.    if (preg_match('|^([0-9]+)$|', $value, $a)){
  77.     return (int)$a[1];
  78.    }
  79.    
  80.    $value=strtolower($value);
  81.    // 1 — TV; 2 — OVA; 3 — Movie; 4 — Special; 5 — ONA; 6 — Music
  82.    switch($value){
  83.     case 'tv':      return 1;
  84.     case 'ova':     return 2;
  85.     case 'movie':   return 3;
  86.     case 'special': return 4;
  87.     case 'ona':     return 5;
  88.     case 'music':   return 6;
  89.    
  90.     case 'watching': return 1;
  91.     case 'completed':return 2;
  92.     case 'onhold':   return 3;
  93.     case 'dropped':  return 4;
  94.     case 'ptw':      return 6;
  95.    
  96.     case 'drop':     return 4;
  97.     case 'plan':     return 6;
  98.     case 'complete': return 2;
  99.    
  100.     default:  return null;
  101.    }
  102.   }
  103.  
  104.   function length(){
  105.    $count=0;
  106.    foreach ($this->data as $datum){$count++;}
  107.    return $count;
  108.   }
  109.  
  110.   function average_score(){
  111.    if ($this->length()==0){return 0;}
  112.    $sum=0;
  113.    $count=0;
  114.    foreach ($this->data as $datum){
  115.     if ($datum->score>0){$sum+=$datum->score;$count++;}
  116.    }
  117.    return ($count==0) ? 0 : $sum/$count;
  118.   }
  119.  
  120.  
  121.  }// class AnimeAchievements
  122.  
  123. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement