Пример #1
0
 /**
  * Вернуться обратно в свой замок если это возможно.
  *
  * @throws GameException
  */
 public function comeback()
 {
     $this->continueOrException();
     $goal = $this->goal;
     $castle = $this->army->castle;
     // Поход уже закончился или еще не начался...
     if (isset($this->crusade_end_at) || !(isset($this->crusade_at) && isset($this->battle_at))) {
         throw new GameException('Отряд уже закончил поход или похода не существует.');
     }
     // Рассчитать время возвращения домой отряда...
     $minutes = Location::howMuchTime($castle, $goal);
     $minutes = intval($minutes * 1.15);
     // С учетом усталости отряда...
     $end = $this->crusade_end_at = $this->battle_at->addMinutes($minutes);
     // дата возвращения отряда.
     $this->save();
     $now = Carbon::now();
     Log::info('---------------------------------------------------------------------------------------------------');
     Log::info("({$now}) Отряд id={$this->id} '{$this->name}' вернется в замок {$this->crusade_end_at}.");
     return $end;
 }
Пример #2
0
 /**
  * Совершить поход на вражеский замок.
  *
  * @param {string} $name имя отряда
  * @param {int} $count количество в отряде
  * @param Castle $goal замок
  * @return Squad
  * @throws GameException
  * @throws \Exception
  */
 public function crusade($name, $count, Castle $goal)
 {
     if ($goal->id === $this->id) {
         throw new GameException('Нельзя отправить отряд на собственный замок.');
     }
     // Есть ли возможность создать отряд?
     $diff = $this->size - $count;
     if ($count <= 0 || $diff < 0) {
         throw new GameException('Нельзя создать отряд для похода. Не хватает храбрых воинов.');
     }
     $squad = new Squad(['name' => $name, 'size' => $count]);
     $squad->crusade_at = Carbon::now();
     // Начало похода
     // Время на поход...
     $minutes = Location::howMuchTime($this->castle, $goal);
     $squad->battle_at = Carbon::now()->addMinutes($minutes);
     // Конец похода
     DB::beginTransaction();
     try {
         $this->update(['size' => $diff]);
         $squad->goal()->associate($goal);
         // Вражеский замок
         // Сохранить отряд...
         $this->squads()->save($squad);
         // Так как отряд сохранен, внедряем сюда шпионов
         // Получаем их всех
         $enemySpies = $this->castle()->first()->enemySpies()->getResults();
         foreach ($enemySpies as $oneSpy) {
             $spyHistory = new SpyHistory();
             $spyHistory->spy_id = $oneSpy->id;
             $spyHistory->squads_id = $squad->id;
             // Пытаемся обнаружить атаку
             $spyHistory->detect = $oneSpy->canDetectedAttack($this->level, $squad->size) ? true : false;
             $spyHistory->save();
         }
         //
         $now = Carbon::now();
         Log::info('---------------------------------------------------------------------------------------------------');
         Log::info("({$now}) Создан новый отряд - id={$squad->id} '{$squad->name}' ({$squad->size} в)...");
         Log::info("Поход на вражеский замок - id={$goal->id} '{$goal->name}");
         Log::info("Начало похода {$squad->crusade_at}, сражение состоится {$squad->battle_at}");
     } catch (\Exception $ex) {
         DB::rollBack();
         throw $ex;
         // next...
     }
     DB::commit();
     return $squad;
 }