/**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     // Read the database of allied awards
     $csv = Reader::createFromPath(storage_path('app/csv/issued_allied_awards.csv'));
     // This will read the CSV as set of data into the listed array keys
     $data = $csv->fetchAssoc(['playerName', 'playerLabel', 'awardName', 'campaign_id', 'awardedAt', 'submitterName', 'cincName', 'forumSource'], function ($row) {
         // manipulate the incoming data as it is read. This is turn it into
         // a set of data which can be immediately added to our awards table
         $drow = [];
         $drow['playerName'] = trim(strtolower($row[1]));
         $drow['playerLabel'] = trim($row[1]);
         // case as it appears in csv
         $drow['awardName'] = snake_case(strtolower(trim($row[2])));
         $drow['campaign_id'] = trim($row[3]) == '' ? null : trim($row[3]);
         $drow['awardedAt'] = trim($row[6]);
         $drow['submitterName'] = trim(strtolower($row[7]));
         $drow['cincName'] = trim(strtolower($row[8]));
         $drow['forumSource'] = trim(strtolower($row[9]));
         return $drow;
     });
     Player::unguard();
     foreach ($data as $item) {
         $award = Award::where('key', $item['awardName'])->first();
         if ($award == null) {
             Log::info(sprintf('Could not find an award with key %s', $item['awardName']));
             continue;
         }
         $submitter = Player::where('playerName', $item['submitterName'])->first();
         if ($submitter == null) {
             $submitter = Player::create(['playerName' => $item['submitterName'], 'isActive' => true]);
         }
         $cinc = Player::where('playerName', $item['cincName'])->first();
         if ($cinc == null) {
             $cinc = Player::create(['playerName' => $item['cincName'], 'isActive' => true]);
         }
         // check the rype of award we are presenting. If it's a squad award, find the squad and apply it
         // also, if there is a space in the name, its not a player name
         if ($award->awardedTo == 'squad' || preg_match('/\\s/', $item['playerName']) > 0) {
             $squad = Squad::where('key', snake_case($item['playerName']))->first();
             if ($squad == null) {
                 $squad = Squad::create(['key' => snake_case($item['playerName']), 'label' => $item['playerLabel'], 'isActive' => true]);
             }
             $squad->awards()->save($award, ['awardedBy_id' => $submitter->id, 'cinc_id' => $cinc->id, 'campaign_id' => $item['campaign_id'], 'awardedAt' => Carbon\Carbon::createFromFormat('d/m/Y', $item['awardedAt']), 'forumLink' => $item['forumSource']]);
         } else {
             $player = Player::where('playerName', $item['playerName'])->first();
             if ($player == null) {
                 $player = Player::create(['playerName' => $item['playerName'], 'isActive' => true]);
             }
             $player->awards()->save($award, ['awardedBy_id' => $submitter->id, 'cinc_id' => $cinc->id, 'campaign_id' => $item['campaign_id'], 'awardedAt' => Carbon\Carbon::createFromFormat('d/m/Y', $item['awardedAt']), 'forumLink' => $item['forumSource']]);
         }
     }
     Player::reguard();
 }
Example #2
0
 /**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Closure  $next
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     // Получить все отряды, которым уже должны штурмовать вражеский замок...
     $toAssault = Squad::readyToAssault()->get();
     foreach ($toAssault as $s) {
         try {
             $s->assault();
         } catch (GameException $exc) {
             // Нужно обрабатывать?
             Log::error($exc->getMessage());
         }
     }
     // Получить все отряды, которые пора уже рассформировать...
     $toDisband = Squad::readyToDisband()->get();
     foreach ($toDisband as $s) {
         try {
             $s->disband();
         } catch (GameException $exc) {
             // Нужно обрабатывать?
             Log::error($exc->getMessage());
         }
     }
     return $next($request);
 }
Example #3
0
 public function getActiveSpyDetected()
 {
     $ownSpies = $this->ownSpies()->getResults();
     $arrayActiveReports = [];
     foreach ($ownSpies as $oneOwnSpy) {
         $shForThatSpy = SpyHistory::where('spy_id', $oneOwnSpy->id)->where('detect', 1)->get()->all();
         foreach ($shForThatSpy as $oneSpyHistory) {
             if (!empty($oneSpyHistory)) {
                 $squad = Squad::find($oneSpyHistory->squads_id);
                 if (!empty($squad)) {
                     if ($squad->battle_at > \Carbon\Carbon::now()) {
                         $arrayActiveReports[] = [$oneOwnSpy, $squad];
                     }
                 }
             }
         }
     }
     return $arrayActiveReports;
 }
Example #4
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;
 }