Jump to content
Дизайн и модификация IPS Community IPBSkinsBETA
Search In
  • More options...
Find results that contain...
Find results in...
Sign in to follow this  
TemKa_SD

Помогите с условием

Recommended Posts

Здравствуйте. У в функции выбираются последние игры игрока, есть два типа игроков dotaPlayer и w3mmdPlayer.

 

     /**
        * STEP 2: Selet Game
        */
       $steps['sharedstats_report_select_game'] = function ($data) use ($names) {
           $games = $gameIds = [];
           $i = 0;

           $acc = isset($data['account']) ? $data['account'] : $names[0];


           foreach ((\IPS\DB::i('stats')->select('*', 'stats_gameplayers', array('name=?', $acc['name']), 'id DESC', \IPS\Settings::i()->report_max_last_games)) as $gameplayer) {
               try {
                   $game = \IPS\DB::i('stats')->select('*', 'stats_games', array('id=?', $gameplayer['gameid']))->first();
                   $dotaPlayer = \IPS\DB::i('stats')->select('*', 'stats_dotaplayers', array('newcolour=? and gameid=?', $gameplayer['colour'], $game['id']))->first();
                   $w3mmdPlayer = \IPS\DB::i('stats')->select('*', 'stats_w3mmdplayers', array('pid=? and gameid=?', $gameplayer['colour'], $game['id']))->first();
                   $hero = mb_strtoupper($dotaPlayer['hero']);
                   $gameIds[$i] = $game['id'];
                   $games[] = $game['gamename'] /*"<br>
                           <div class='gc_u_flex_row'><span class='hero hero-${hero} hero--small hero--round'></span> 
                           <strong>K/D/A:</strong>   <span style='color: #0F0;'>${dotaPlayer['kills']}</span>/<span style='color: #00F;'>${dotaPlayer['assists']}</span>/<span style='color: #F00;'>${dotaPlayer['deaths']}</span>
                              <strong>C/D/N:</strong> <span>${dotaPlayer['creepkills']}</span>/<span>${dotaPlayer['creepdenies']}</span>/<span>${dotaPlayer['neutralkills']}</span></div>
                   "*/;
               } catch(\UnderflowException $e) {}
               ++$i;
           }
           $form = new \IPS\Helpers\Form('', 'continue');
           $form->addHeader('sharedstats_report_select_game');
           $form->add(new \IPS\Helpers\Form\Radio('sharedstats_report_select_recent_game', null, true, array('options' => $games)));
           if ($values = $form->values()) {
               $data['gameid'] = $gameIds[$values['sharedstats_report_select_recent_game']];
               return $data;
           }
           return $form;
       };

 

Изначально код имеет вид без строк, так как заточено под доту, мне нужно вывести игры второго типа игрока.

 

$w3mmdPlayer = \IPS\DB::i('stats')->select('*', 'stats_w3mmdplayers', array('pid=? and gameid=?', $gameplayer['colour'], $game['id']))->first();

 

Игры не отображаются, если убрать

$dotaPlayer = \IPS\DB::i('stats')->select('*', 'stats_dotaplayers', array('newcolour=? and gameid=?', $gameplayer['colour'], $game['id']))->first();

то всё в порядке.

Edited by TemKa_SD

Share this post


Link to post
Share on other sites

Вроде понял, нужно в catch добавить условие для второго типа игроков. Правильно?

Share this post


Link to post
Share on other sites

Я же уже писал про first() и try/catch

 

Если у Вас есть

                try {
                   $game = \IPS\DB::i('stats')->select('*', 'stats_games', array('id=?', $gameplayer['gameid']))->first();
                   $dotaPlayer = \IPS\DB::i('stats')->select('*', 'stats_dotaplayers', array('newcolour=? and gameid=?', $gameplayer['colour'], $game['id']))->first();
                   $w3mmdPlayer = \IPS\DB::i('stats')->select('*', 'stats_w3mmdplayers', array('pid=? and gameid=?', $gameplayer['colour'], $game['id']))->first();
               } catch(\UnderflowException $e) {}

и в

                     $game = \IPS\DB::i('stats')->select('*', 'stats_games', array('id=?', $gameplayer['gameid']))->first();

будет выброшено исключение, то ниже идущий код не выполнится

То же справедливо для других выборок

Share this post


Link to post
Share on other sites

Я думал об этом, но изначальный код имеет вид:

 

                try {
                   $game = \IPS\DB::i('stats')->select('*', 'stats_games', array('id=?', $gameplayer['gameid']))->first();
                   $dotaPlayer = \IPS\DB::i('stats')->select('*', 'stats_dotaplayers', array('newcolour=? and gameid=?', $gameplayer['colour'], $game['id']))->first();
               } catch(\UnderflowException $e) {}

 

И всё работает, первая и вторая переменная выбирают результаты.

Share this post


Link to post
Share on other sites

Правильно?

            foreach ((\IPS\DB::i('stats')->select('*', 'stats_gameplayers', array('name=?', $acc['name']), 'id DESC', \IPS\Settings::i()->report_max_last_games)) as $gameplayer) {
               try {
                   $game = \IPS\DB::i('stats')->select('*', 'stats_games', array('id=?', $gameplayer['gameid']))->first();
                       try {
                           $dotaPlayer = \IPS\DB::i('stats')->select('*', 'stats_dotaplayers', array('newcolour=? and gameid=?', $gameplayer['colour'], $game['id']))->first();
                           $hero = mb_strtoupper($dotaPlayer['hero']);
                           $gameIds[$i] = $game['id'];
                           $games[] = $game['gamename'] . "<br>
                                   <div class='gc_u_flex_row'><span class='hero hero-${hero} hero--small hero--round'></span> 
                                   <strong>K/D/A:</strong>   <span style='color: #0F0;'>${dotaPlayer['kills']}</span>/<span style='color: #00F;'>${dotaPlayer['assists']}</span>/<span style='color: #F00;'>${dotaPlayer['deaths']}</span>
                                      <strong>C/D/N:</strong> <span>${dotaPlayer['creepkills']}</span>/<span>${dotaPlayer['creepdenies']}</span>/<span>${dotaPlayer['neutralkills']}</span></div>
                           ";
                       } catch(\UnderflowException $e) {}
                       try {
                           $w3mmdPlayer = \IPS\DB::i('stats')->select('*', 'stats_w3mmdplayers', array('pid=? and gameid=?', $gameplayer['colour'], $game['id']))->first();
                           $gameIds[$i] = $game['id'];
                           $games[] = $game['gamename'];
                       } catch(\UnderflowException $e) {}
               } catch(\UnderflowException $e) {}
               ++$i;
           }

Share this post


Link to post
Share on other sites

Ну да, всё отлично. Я понял примерно, в catch можно еще ошибку выводить или другие какие-нибудт данные, когда try не срабатывает.

 

Помогите еще вывести иконку героя.

 

У меня есть готовые функции, которые выполняют это, их надо просто внедрить сюда.

 

Собирает все данные, которые, передает карта в БД:

    protected function collectPlayerVars($gameId) {
       $playerVars = \IPS\DB::i('stats')->select('*', ['stats_w3mmdvars', 'w3v'], array('w3v.gameid=?', $gameId));
       return $playerVars;
   }

 

Собирает выше упомянутые данные с игроком:

    protected function mergePlayerVars($players, $vars) {
       $p = [];
       foreach($players as $player) {
           $v = [];
           foreach($vars as $var) {
               if($player['pid'] === $var['pid']) {
                   $varName = $var['varname'];
                   if($varName !== 'name') {
                       $v[$varName] = intval($var['value_int']);
                   } else {
                       $v['name'] = $var['value_string'];
                   }
               }
           }
           $winner = $player['flag'] === 'winner';
           if($winner) {
               $this->hasWinner = true;
           }

           $p[$player['pid']] = [
               'name' => $player['name'],
               'team' => $player['team'],
               'ip' => $player['ip'],
               'left' => intval($player['left']),
               'leftreason' => $player['leftreason'],
               'colour' => intval($player['colour']),
               'server' => $player['spoofedrealm'],
               'winner' => $winner,
               'looser' => !$winner,
               'leaver' => intval($player['leaver']),
               'vars' => $v,
               'scoreId' => isset($player['scoreId']) ? $player['scoreId'] : 0
           ];
       }

       return $p;
   }

Сюда нужно вставить результат:

                        try {
                           $w3mmdPlayer = \IPS\DB::i('stats')->select('*', 'stats_w3mmdplayers', array('pid=? and gameid=?', $gameplayer['colour'], $game['id']))->first();
                           $playerVars = $this->collectPlayerVars($game['id']);
                           $mergedPlayerVars = $this->mergePlayerVars($w3mmdPlayer, $playerVars);
                           $hero = $mergedPlayerVars;
                           $gameIds[$i] = $game['id'];
                           $games[] = $game['gamename'] . "<br>
                           ${hero}
                           ";
                       } catch(\UnderflowException $e) {}

Share this post


Link to post
Share on other sites

Сейчас такая ошибка: Whoops\Exception\ErrorException: Illegal string offset 'pid' (2)

Share this post


Link to post
Share on other sites

Сейчас такая ошибка: Whoops\Exception\ErrorException: Illegal string offset 'pid' (2)

 

Используйте isset()

if isset($var['pid'])

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...