public function validateGameMode($attr, $params)
 {
     $value = substr($this->{$attr}, 1);
     $isExist = !!GameMode::findOne(['key' => $value]);
     if (!$isExist) {
         $this->addError($attr, Yii::t('yii', '{attribute} is invalid.', ['attribute' => $this->getAttributeLabel($attr)]));
     }
 }
 public function up()
 {
     $this->createTable('game_mode', ['id' => $this->primaryKey(), 'key' => $this->string(16)->notNull()->unique(), 'name' => $this->string(32)->notNull()->unique()]);
     $this->createTable('rule', ['id' => $this->primarykey(), 'mode_id' => $this->integer()->notNull(), 'key' => $this->string(16)->notNull()->unique(), 'name' => $this->string(32)->notNull()->unique()]);
     $this->addForeignKey('fk_rule_1', 'rule', 'mode_id', 'game_mode', 'id');
     $this->batchInsert('game_mode', ['key', 'name'], [['regular', 'Regular Battle'], ['gachi', 'Ranked Battle']]);
     $modeRegular = GameMode::findOne(['key' => 'regular'])->id;
     $modeGachi = GameMode::findOne(['key' => 'gachi'])->id;
     $this->batchInsert('rule', ['key', 'mode_id', 'name'], [['nawabari', $modeRegular, 'Turf War'], ['area', $modeGachi, 'Splat Zones'], ['yagura', $modeGachi, 'Tower Control'], ['hoko', $modeGachi, 'Rainmaker']]);
 }
 private function mapUpdateGachi()
 {
     echo "gachi...\n";
     $gameMode = GameMode::findOne(['key' => 'gachi']);
     $latestPeriod = $this->getLatestPeriod($gameMode);
     $currntPeriod = \app\components\helpers\Battle::calcPeriod(time());
     $futureOnly = $latestPeriod >= $currntPeriod;
     $json = array_filter(array_map(function ($item) {
         $item->period = \app\components\helpers\Battle::calcPeriod(strtotime($item->start));
         return $item;
     }, $this->queryJson($futureOnly ? 'http://splapi.retrorocket.biz/gachi/next_all' : 'http://splapi.retrorocket.biz/gachi')->result), function ($item) use($latestPeriod) {
         return $item->period > $latestPeriod;
     });
     if (empty($json)) {
         echo "no data updated.\n";
         return;
     }
     printf("count(new_data) = %d\n", count($json));
     usort($json, function ($a, $b) {
         return $a->period - $b->period;
     });
     echo "Converting to insert data...\n";
     $map = $this->getMapTable();
     $rule = $this->getRuleTable($gameMode);
     $insert = [];
     foreach ($json as $item) {
         if (!isset($rule[$item->rule])) {
             echo "Unknown rule name: {$item->rule}\n";
             continue;
         }
         foreach ($item->maps as $mapName) {
             if (isset($map[$mapName])) {
                 $insert[] = [$item->period, $rule[$item->rule], $map[$mapName]];
             } else {
                 echo "Unknown map name: {$mapName}\n";
             }
         }
     }
     echo "inserting...\n";
     Yii::$app->db->createCommand()->batchInsert(PeriodMap::tableName(), ['period', 'rule_id', 'map_id'], $insert)->execute();
     echo "done.\n";
 }