<?php

class m141223_224844_actions extends CDbMigration {

    public function up() {
        $this->execute("
            CREATE TABLE `actions` (
  `id_action` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `type_action` tinyint(1) unsigned NOT NULL,
  `short_name_action` varchar(100) NOT NULL,
  `name_action` varchar(512) NOT NULL,
  `descr_action` varchar(2054) NOT NULL,
  `bonus` tinyint(2) unsigned NOT NULL,
  `time` int(11) unsigned NOT NULL,
  PRIMARY KEY (`id_action`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
ALTER TABLE `mland`.`actions` 
CHANGE COLUMN `bonus` `bonus` TINYINT(3) UNSIGNED NOT NULL ,
ADD COLUMN `min_bonus` TINYINT(3) UNSIGNED NOT NULL AFTER `descr_action`;
ALTER TABLE `mland`.`actions` 
ADD COLUMN `time_active` TINYINT(3) UNSIGNED NOT NULL AFTER `time`;
ALTER TABLE `mland`.`actions` 
CHANGE COLUMN `time_active` `time_active` TINYINT(3) UNSIGNED NOT NULL AFTER `bonus`;
INSERT INTO `mland`.`shop_sets` (`id_set`, `name`, `min`, `max`, `shop`) VALUES ('10', 'Комплект Лоа', '25', '25', '0');
UPDATE `mland`.`shop_sets` SET `min`='25', `max`='25' WHERE `id_set`='9';
UPDATE `mland`.`shop_sets` SET `max`='25' WHERE `id_set`='8';
UPDATE `mland`.`shop_sets` SET `max`='25' WHERE `id_set`='7';
UPDATE `mland`.`shop_sets` SET `max`='25' WHERE `id_set`='6';
INSERT INTO `shop_items` VALUES (109,'Шлем Лоа',10,1,25,0,24,96,0,24),(110,'Амулет Лоа',10,2,25,24,0,0,96,24),(111,'Наплечник Лоа',10,3,25,24,24,48,0,24),(112,'Бурка Лоа',10,4,25,24,24,24,24,24),(113,'Кираса Лоа',10,5,25,24,96,24,0,0),(114,'Серьга Лоа',10,6,25,24,24,24,24,24),(115,'Штаны Лоа',10,7,25,24,48,0,24,24),(116,'Браслет Лоа',10,8,25,0,24,24,24,24),(117,'Наручи Лоа',10,9,25,48,24,0,24,24),(118,'Кольцо Лоа',10,10,25,0,0,24,24,96),(119,'Молот Лоа',10,11,25,96,0,24,24,0),(120,'Сапоги Лоа',10,12,25,24,24,48,48,0);
UPDATE `mland`.`shop_items` SET `name`='Шапка Рождества' WHERE `id_item`='109';
UPDATE `mland`.`shop_items` SET `name`='Ожерелье Рождества' WHERE `id_item`='110';
UPDATE `mland`.`shop_items` SET `name`='Щит Рождества' WHERE `id_item`='112';
UPDATE `mland`.`shop_items` SET `name`='Посох Рождества' WHERE `id_item`='119';
UPDATE `mland`.`shop_items` SET `name`='Сапоги Рождества' WHERE `id_item`='120';
UPDATE `mland`.`shop_items` SET `name`='Кольцо Рождества' WHERE `id_item`='118';
UPDATE `mland`.`shop_items` SET `name`='Руковицы Рождества' WHERE `id_item`='117';
UPDATE `mland`.`shop_items` SET `name`='Браслет Рождества' WHERE `id_item`='116';
UPDATE `mland`.`shop_items` SET `name`='Штаны Рождества' WHERE `id_item`='115';
UPDATE `mland`.`shop_items` SET `name`='Серьги Рождества' WHERE `id_item`='114';
UPDATE `mland`.`shop_items` SET `name`='Кираса Рождества' WHERE `id_item`='113';
UPDATE `mland`.`shop_items` SET `name`='Наплечник Рождества' WHERE `id_item`='111';
ALTER TABLE `mland`.`user_daily` 
ADD COLUMN `bonus` TINYINT(1) UNSIGNED NOT NULL AFTER `ironmaster`;

INSERT INTO `battles_locations` VALUES ('25', 'Снежный Утёс', '25', '35', '2', '2', '4000', '10000', '2', '4000', '100');
INSERT INTO `battles_locations` VALUES ('26', 'Обрыв', '25', '35', '3', '2', '4000', '10000', '2', '4000', '100');
INSERT INTO `battles_locations` VALUES ('27', 'Цитадель Севера', '25', '35', '3', '2', '4000', '10000', '2', '4000', '100');
INSERT INTO `battles_locations` VALUES ('28', 'Туманная Гора', '25', '35', '3', '2', '4000', '10000', '2', '4000', '100');
INSERT INTO `battles_locations` VALUES ('29', 'Корни Горы', '25', '35', '3', '1', '4000', '10000', '1', '4000', '100');
INSERT INTO `battles_locations` VALUES ('30', 'Хвойные Леса', '25', '35', '3', '1', '4000', '10000', '1', '4000', '100');
INSERT INTO `battles_locations` VALUES ('31', 'Снежная Равнина', '25', '35', '2', '1', '4000', '10000', '1', '4000', '100');
INSERT INTO `battles_navigator` VALUES ('123', '2', '25', '1', '2');
INSERT INTO `battles_navigator` VALUES ('124', '25', '2', '2', '2');
INSERT INTO `battles_navigator` VALUES ('125', '25', '26', '1', '2');
INSERT INTO `battles_navigator` VALUES ('126', '25', '27', '1', '2');
INSERT INTO `battles_navigator` VALUES ('127', '26', '25', '2', '2');
INSERT INTO `battles_navigator` VALUES ('128', '27', '25', '2', '2');
INSERT INTO `battles_navigator` VALUES ('129', '27', '28', '1', '2');
INSERT INTO `battles_navigator` VALUES ('130', '26', '28', '1', '2');
INSERT INTO `battles_navigator` VALUES ('131', '28', '27', '2', '2');
INSERT INTO `battles_navigator` VALUES ('132', '28', '26', '2', '2');
INSERT INTO `battles_navigator` VALUES ('133', '28', '29', '1', '2');
INSERT INTO `battles_navigator` VALUES ('134', '28', '30', '1', '2');
INSERT INTO `battles_navigator` VALUES ('135', '30', '28', '2', '2');
INSERT INTO `battles_navigator` VALUES ('136', '29', '28', '2', '2');
INSERT INTO `battles_navigator` VALUES ('138', '29', '31', '1', '2');
INSERT INTO `battles_navigator` VALUES ('139', '30', '31', '1', '2');
INSERT INTO `battles_navigator` VALUES ('140', '31', '29', '2', '2');
INSERT INTO `battles_navigator` VALUES ('141', '31', '30', '2', '2');
INSERT INTO `battles_navigator` VALUES ('142', '1', '31', '2', '1');
INSERT INTO `battles_navigator` VALUES ('143', '31', '1', '1', '1');
INSERT INTO `battles_navigator` VALUES ('144', '31', '30', '2', '1');
INSERT INTO `battles_navigator` VALUES ('145', '31', '29', '2', '1');
INSERT INTO `battles_navigator` VALUES ('146', '29', '31', '1', '1');
INSERT INTO `battles_navigator` VALUES ('147', '30', '31', '1', '1');
INSERT INTO `battles_navigator` VALUES ('148', '29', '28', '2', '1');
INSERT INTO `battles_navigator` VALUES ('149', '30', '28', '2', '1');
INSERT INTO `battles_navigator` VALUES ('150', '28', '29', '1', '1');
INSERT INTO `battles_navigator` VALUES ('151', '28', '30', '1', '1');
INSERT INTO `battles_navigator` VALUES ('152', '28', '26', '2', '1');
INSERT INTO `battles_navigator` VALUES ('153', '28', '27', '2', '1');
INSERT INTO `battles_navigator` VALUES ('154', '27', '28', '1', '1');
INSERT INTO `battles_navigator` VALUES ('155', '26', '28', '1', '1');
INSERT INTO `battles_navigator` VALUES ('156', '26', '25', '2', '1');
INSERT INTO `battles_navigator` VALUES ('157', '27', '25', '2', '1');
INSERT INTO `battles_navigator` VALUES ('158', '25', '26', '1', '1');
INSERT INTO `battles_navigator` VALUES ('159', '25', '27', '1', '1');


                ");

        if (Yii::app()->hasComponent('cache')) {
            Yii::app()->getComponent('cache')->flush();
            echo "Cache flused\n";
        }

        $this->clearAssets();
    }

    private function clearAssets() {
        $path = Yii::app()->getComponent('assetManager')->getBasePath();
        $this->clearDir($path);
        echo "Assets clear\n";
    }

    private function clearDir($folder, $main = true) {
        if (is_dir($folder)) {
            $handle = opendir($folder);
            while ($subfile = readdir($handle)) {
                if ($subfile == '.' || $subfile == '..')
                    continue;
                if (is_file($subfile))
                    unlink("{$folder}/{$subfile}");
                else
                    $this->clearDir("{$folder}/{$subfile}", false);
            }
            closedir($handle);
            if (!$main)
                rmdir($folder);
        } else
            unlink($folder);
    }

    public function down() {
        echo "m141223_224844_actions does not support migration down.\n";
        return false;

        $this->execute("");
    }

    /*
      // Use safeUp/safeDown to do migration with transaction
      public function safeUp()
      {
      }

      public function safeDown()
      {
      }
     */
}
