Jump to content

Научите передавать данные между функциями


TemKa_SD
 Share

Recommended Posts

Здравствуйте.

 

У меня есть две функции:

 

Основная (лишний код удалил) :

 

   public function getGamelist($ActiveGamesWidgetType, $ActiveGamesWidgetBots, $ActiveGamesWidgetSortBy, $ActiveGamesWidgetPagination, $ActiveGamesWidgetPerPage, $uniqueKey)
   {
       $where = array();
       $botIds = array();
       $ids = array();

       // Фильтр по botid

       if ( isset( \IPS\Request::i()->botId ) )
       {
           $ids = explode( ',', \IPS\Request::i()->botId );
       }

       else if ( isset( \IPS\Request::i()->cookie['runningGames_botIds'] ) )
       {
           $ids = explode( ',', \IPS\Request::i()->cookie['runningGames_botIds'] );
       }

       if ( isset( $ActiveGamesWidgetBots ) AND (!isset( \IPS\Request::i()->botId ) AND !isset( \IPS\Request::i()->cookie['runningGames_botIds'] ) ) )
       {
           $ids = explode( ',', $ActiveGamesWidgetBots );
       }

       if ( \count( $ids ) )
       {
           foreach( $ids as $id )
           {
               $botIds = array_filter( $ids, 'intval' );
           }

           if ( \count( $botIds ) )
           {
               $where[] = array( \IPS\Db::i()->in( 'botid', array_filter( $botIds ) ) );
           }
       }
   }

 

Это передается из настроек виджета:

 

public function getGamelist($ActiveGamesWidgetType, $ActiveGamesWidgetBots, $ActiveGamesWidgetSortBy, $ActiveGamesWidgetPagination, $ActiveGamesWidgetPerPage, $uniqueKey)

 

ВТОРАЯ ФУНКЦИЯ СО СТАТИСТИКОЙ:

 

   public function getStats()
   {

       $where = array();
       $botIds = array();
       $ids = array();

       // Фильтр по botid
       if ( isset( \IPS\Request::i()->botId ) )
       {
           $ids = explode( ',', \IPS\Request::i()->botId );
       }
       else if ( isset( \IPS\Request::i()->cookie['runningGames_botIds'] ) )
       {
           $ids = explode( ',', \IPS\Request::i()->cookie['runningGames_botIds'] );
       }

       if ( \count( $ids ) )
       {
           foreach( $ids as $id )
           {
               $botIds = array_filter( $ids, 'intval' );
           }

           if ( \count( $botIds ) )
           {
               $where[] = array( \IPS\Db::i()->in( 'botid', array_filter( $botIds ) ) );
           }
       }

       $cg = new \IPS\Patterns\ActiveRecordIterator(\IPS\Db::i('wc3')->select('*', static::$databaseTable, $where, 'lobby DESC'), '\IPS\wc3\ActiveGames\ActiveGames');
       $lobbies = $games = $players = 0;
       foreach($cg as $g) {
           if($g->getLobby()) {
               $lobbies++;
           } else {
               $games++;
           }

           $players += $g->getSlotstaken();
           foreach( static::getUsers($g->getUsernames()) as $realm )
           {
               isset( $realms[ $realm['realm'] ] ) ? $realms[ $realm['realm'] ]++ : $realms[ $realm['realm'] ] = 1;
           }
       }
       return [
           'lobbies' => $lobbies,
           'games' => $games,
           'players' => $players,
           'realms' => isset( $realms ) ? $realms : ''
       ];
   }

 

Этот код в статистике дублируется:

 

       $botIds = array();
       $ids = array();

       // Фильтр по botid
       if ( isset( \IPS\Request::i()->botId ) )
       {
           $ids = explode( ',', \IPS\Request::i()->botId );
       }
       else if ( isset( \IPS\Request::i()->cookie['runningGames_botIds'] ) )
       {
           $ids = explode( ',', \IPS\Request::i()->cookie['runningGames_botIds'] );
       }

       if ( \count( $ids ) )
       {
           foreach( $ids as $id )
           {
               $botIds = array_filter( $ids, 'intval' );
           }

           if ( \count( $botIds ) )
           {
               $where[] = array( \IPS\Db::i()->in( 'botid', array_filter( $botIds ) ) );
           }
       }

 

Как правильно передать значения из первой функции во вторую, чтобы не дублировать код?

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...