<?php
class DungeonsCommand extends CConsoleCommand {
    public function run($args) {
        for ($i = 1; $i < 10; $i++) {
        $dungeons = BattlesDungeons::model()->with('bots')->findAll(array('condition' => 'bots.health_now>0 AND t.time_end = 0'));
            foreach ($dungeons as $dungeon) {
                $bot = Users::getRandomBotInDungeonForAttack($dungeon->id_battle);
                if ($bot) {
                    if (count($dungeon->warriors)>0 || count($dungeon->healers)>0) {
                        foreach ($dungeon->warriors as $enemy) {
                            $result = BattleHelper::damageUser($bot, $enemy);
                            /* Если был удар - идет кол-во в участие */
                            if ($result) {
                                $log = new BattleLog();
                                $log->battle_type = 2;
                                $log->battle_id = $bot->battle_id;
                                $log->id_user = $bot->id_user;
                                $log->id_target = $enemy->id_user;
                                $log->type = 1;
                                $log->value = $result['damage'];
                                $log->info = ($result['isCritical'] ? 'крит' : '');
                                $log->users = Users::getUsersForLog($enemy->id_user);
                                $log->save(false);
                            }
                            unset($enemy);
                        }
                        foreach ($dungeon->healers as $enemy) {
                            $result = BattleHelper::damageUser($bot, $enemy);
                            /* Если был удар - идет кол-во в участие */
                            if ($result) {
                                $log = new BattleLog();
                                $log->battle_type = 2;
                                $log->battle_id = $bot->battle_id;
                                $log->id_user = $bot->id_user;
                                $log->id_target = $enemy->id_user;
                                $log->type = 1;
                                $log->value = $result['damage'];
                                $log->info = ($result['isCritical'] ? 'крит' : '');
                                $log->users = Users::getUsersForLog($enemy->id_user);
                                $log->save(false);
                            }
                            unset($enemy);
                        }
                    }
                    else {
                        if (count($dungeon->users) > 0) {
                            $users_ids = CHtml::listData($dungeon->users, 'id_user', 'id_user');
                            $log = new BattleLog();
                            $log->battle_type = 2;
                            $log->battle_id = $bot->battle_id;
                            $log->id_user = $bot->id_user;
                            $log->type = 9;
                            $log->info = 'Вашему отряду не удалось пройти пещеру.';
                            $log->users = $users_ids;
                            $log->save(false);
                        }
                        Users::model()->updateByPk($bot->id_user, array('last_visit'=>time()));
                        Users::model()->updateByPk($dungeon->id_battle, array('time_end'=>time()));
                    }
                }
            }
            sleep(6);
        }
    }
}

