Пример #1
0
 public static function getTank($name, $lname = null, $level = 0, $nation = null, $class = null, $imageUrl = null)
 {
     if (preg_match("/#(.*?):(.*)/", $lname, $mathes)) {
         $lname = $mathes[2];
     }
     if (empty(self::$_models)) {
         self::$_models = self::model()->findAll(array('index' => 'tank_name'));
     }
     if (!isset(self::$_models[$name])) {
         $tankNation = WotTankNation::model()->findByPk($nation);
         if (empty($tankNation)) {
             $tankNation = new WotTankNation();
             $tankNation->tank_nation_id = $tankNation->tank_nation_name = $nation;
             $tankNation->save(false);
         }
         $tankClass = WotTankClass::model()->findByPk($class);
         if (empty($tankClass)) {
             $tankClass = new WotTankClass();
             $tankClass->tank_class_id = $tankClass->tank_class_name = $class;
             $tankClass->save(false);
         }
         $tank = new WotTank();
         $tank->tank_class_id = $class;
         $tank->tank_nation_id = $nation;
         $tank->tank_name = $name;
         $tank->tank_localized_name = $lname;
         $tank->tank_level = $level;
         $tank->tank_image = $imageUrl;
         $tank->save(false);
         self::$_models = self::model()->findAll(array('index' => 'tank_name'));
     } else {
         $tank = self::$_models[$name];
         if ($tank->tank_level != $level || $tank->tank_nation_id != $nation || $tank->tank_class_id != $class || $tank->tank_localized_name != $lname || $tank->tank_image != $imageUrl) {
             $tank->tank_nation_id = $nation;
             $tank->tank_name = $name;
             $tank->tank_localized_name = $lname;
             $tank->tank_level = $level;
             $tank->tank_image = $imageUrl;
             $tank->save(false);
         }
     }
     return self::$_models[$name];
 }
Пример #2
0
 public static function updateTanks()
 {
     $jsonString = self::getContent(str_replace('{applicationId}', self::getApplicationId(), self::$wotApiTanks));
     if ($jsonString != false) {
         $jsonData = json_decode($jsonString, true);
         if ($jsonData['status'] == 'ok') {
             $tran = Yii::app()->db->beginTransaction();
             $tanks = WotTank::model()->findAll(array('index' => 'tank_id'));
             $nations = WotTankNation::model()->findAll(array('index' => 'tank_nation_id'));
             $classes = WotTankClass::model()->findAll(array('index' => 'tank_class_id'));
             foreach ($jsonData['data'] as $data) {
                 if (!isset($nations[$data['nation']])) {
                     $nation = new WotTankNation();
                     $nation->tank_nation_id = $data['nation'];
                     $nation->tank_nation_name = $data['nation_i18n'];
                     $nation->save(false);
                     $nations[$data['nation']] = $nation;
                 }
                 if (!isset($classes[$data['type']])) {
                     $class = new WotTankClass();
                     $class->tank_class_id = $data['type'];
                     $class->tank_class_name = $data['type_i18n'];
                     $class->save(false);
                     $classes[$data['type']] = $class;
                 }
                 if (!isset($tanks[$data['tank_id']])) {
                     $tank = new WotTank();
                 } else {
                     $tank = $tanks[$data['tank_id']];
                 }
                 $tank->tank_id = $data['tank_id'];
                 $tank->tank_class_id = $data['type'];
                 $tank->tank_nation_id = $data['nation'];
                 $tank->tank_level = $data['level'];
                 $tank->is_premium = $data['is_premium'];
                 if (preg_match("/#(.*?):(.*)/", $data['name'], $mathes)) {
                     $tankName = $mathes[2];
                 } else {
                     $tankName = $data['name'];
                 }
                 $tank->tank_name = $tankName;
                 $tank->tank_name_i18n = $data['name_i18n'];
                 $tank->save(false);
             }
             $tran->commit();
         }
     }
 }