示例#1
0
 function scenarios_get()
 {
     // Set a limit, could be expanded later on
     $limit = 15;
     $scenarios = new Scenario();
     $i = 0;
     while ($i < 2) {
         if ($this->get('scenarios') || $this->get('modules') || $this->get('aps') || $this->get('quests')) {
             $scenarios->group_start();
         }
         // Filter: Scenarios
         if ($this->get('scenarios')) {
             $scenarios->or_where('type', 'scenario');
         }
         // Filter: Modules
         if ($this->get('modules')) {
             $scenarios->or_where('type', 'mod');
         }
         // Filter: Adventure paths
         if ($this->get('aps')) {
             $scenarios->or_where('type', 'ap');
         }
         // Filter: Quests
         if ($this->get('quests')) {
             $scenarios->or_where('type', 'quest');
         }
         if ($this->get('scenarios') || $this->get('modules') || $this->get('aps') || $this->get('quests')) {
             $scenarios->group_end();
         }
         if (!$this->get('scenarios') && !$this->get('modules') && !$this->get('aps') && !$this->get('quests')) {
             // Dirty, but works
             $scenarios->where('type', 'nonexistent');
         }
         // Filter: Season
         if ($this->get('season')) {
             $scenarios->where_in('season', $this->get('season'));
         }
         // Filter: Retired
         if (!$this->get('retired')) {
             $scenarios->where('archived IS NULL', NULL);
         }
         // Filter: Specials
         if (!$this->get('specials')) {
             $scenarios->not_like('name', '%Special: %');
         }
         // Filter: Search
         if ($this->get('search')) {
             $scenarios->like('name', $this->get('search'));
         }
         // Filter: Author
         if ($this->get('author')) {
             $scenarios->like_related_authors('name', $this->get('author'))->distinct();
         }
         // Filter: Level range
         if ($this->get('levels')) {
             $levels = $this->get('levels');
             foreach ($levels as $level) {
                 $scenarios->like('levelrange', $level);
             }
         }
         if ($this->get('levelRangeMin') && $this->get('levelRangeMax')) {
             $scenarios->group_start();
             $levelrange = range($this->get('levelRangeMin'), $this->get('levelRangeMax'));
             foreach ($levelrange as $key => $value) {
                 $value = str_pad($value, 2, '0', STR_PAD_LEFT);
                 $scenarios->or_like('levelrange', $value);
             }
             $scenarios->group_end();
         }
         // Filter: Evergreen
         if ($this->get('evergreen')) {
             $scenarios->where('evergreen', 1);
         }
         // Filter: Players
         if ($this->get('player') && $this->get('campaign')) {
             $pfsnumbers = array();
             $subq_players = new Scenario();
             $subq_players->select('id')->where_join_field('players', $this->get('campaign') . ' IS NOT NULL', null)->where_in_related_players('pfsnumber', $this->get('player'));
             $scenarios->where_not_in_subquery('id', $subq_players);
         }
         // Sorting
         if ($this->get('sorting')) {
             if ($this->get('sorting') == 'name_asc') {
                 $scenarios->order_by('name', 'asc');
             } elseif ($this->get('sorting') == 'name_desc') {
                 $scenarios->order_by('name', 'desc');
             } elseif ($this->get('sorting') == 'season_asc') {
                 $scenarios->order_by('season', 'asc');
                 $scenarios->order_by('cast(number as unsigned)', 'asc');
                 $scenarios->order_by('name', 'asc');
             } elseif ($this->get('sorting') == 'season_desc') {
                 $scenarios->order_by('season', 'desc');
                 $scenarios->order_by('cast(number as unsigned)', 'desc');
                 $scenarios->order_by('name', 'desc');
             }
         }
         // Pagination
         if ($i == 0) {
             $scenarios->get();
             $response['count'] = $scenarios->result_count();
         } else {
             // First time through, just count
             if ($this->get('currentPage')) {
                 $page = $limit * ($this->get('currentPage') - 1);
                 $scenarios->limit($limit, $page);
             } else {
                 // Just limit it to X scenarios
                 $scenarios->limit($limit);
             }
             $scenarios->get();
             //$scenarios->check_last_query();
         }
         $i++;
     }
     if ($scenarios->exists()) {
         $scenarios_array = $scenarios->all_to_array();
         foreach ($scenarios_array as $key => $value) {
             foreach ($scenarios as $scenario) {
                 if ($scenario->id == $value['id']) {
                     $scenarios_array[$key]['authors'] = $scenario->authors->all_to_single_array('name');
                     //$scenarios_array[$key]['subtiers'] = $scenario->subtiers->all_to_single_array('name');
                 }
             }
         }
         $response['scenarios'] = $scenarios_array;
         $this->response($response, 200);
         // 200 being the HTTP response code
     } else {
         $response['scenarios'] = array();
         $response['total'] = 0;
         $this->response($response, 200);
     }
 }
 public function generate($type = 0)
 {
     $scenario_options = array('played_most', 'played_least', 'season', 'evergreen');
     $person_options = array('player_complete_pfs', 'player_complete_core', 'gm_complete_pfs', 'gm_complete_core');
     if (in_array($type, $scenario_options)) {
         // This is a scenario based statistic
         $scenarios = new Scenario();
         $scenarios->where('archived IS NULL', NULL);
         if ($type == 'evergreen') {
             $scenarios->where('evergreen', '1');
         }
         $scenarios->get();
         $options = array();
         foreach ($scenarios as $scenario) {
             $options[$scenario->id] = $scenario->players->count();
         }
         // Sort
         if ($type == 'played_least') {
             asort($options);
         } else {
             arsort($options);
         }
         // Select only top 5
         $options = array_slice($options, 0, 10, true);
         // Delete old data
         $this->where('type', $type)->get()->delete_all();
         $number = 1;
         foreach ($options as $key => $option) {
             $this->clear();
             $this->type = $type;
             $this->number = $number;
             $this->scenario_id = $key;
             $this->comment = $option;
             $this->created_on = date("Y-m-d H:i:s");
             $this->save();
             $number++;
         }
     } else {
         // This is a person based statistic
         $people = new Person();
         $people->get();
         $number = 1;
         $options = array();
         foreach ($people as $person) {
             if ($type == 'player_complete_pfs') {
                 $scenarios = $person->scenarios->where_join_field('players', 'pfs IS NOT NULL', NULL, FALSE)->where('archived IS NULL', NULL)->get();
                 $options[$person->id] = $scenarios->result_count();
             } elseif ($type == 'gm_complete_pfs') {
                 $scenarios = $person->scenarios->where_join_field('players', 'pfs_gm IS NOT NULL', NULL, FALSE)->where('archived IS NULL', NULL)->get();
                 $options[$person->id] = $scenarios->result_count();
             } elseif ($type == 'player_complete_core') {
                 $scenarios = $person->scenarios->where_join_field('players', 'core IS NOT NULL', NULL, FALSE)->where('archived IS NULL', NULL)->get();
                 $options[$person->id] = $scenarios->result_count();
             } elseif ($type == 'gm_complete_core') {
                 $scenarios = $person->scenarios->where_join_field('players', 'core_gm IS NOT NULL', NULL, FALSE)->where('archived IS NULL', NULL)->get();
                 $options[$person->id] = $scenarios->result_count();
             }
         }
         // Sort
         arsort($options);
         // Select only top 5
         $options = array_slice($options, 0, 10, true);
         // Delete old data
         $this->where('type', $type)->get()->delete_all();
         // Get total amount of scenarios
         $scenarios = new Scenario();
         $total = $scenarios->where('archived IS NULL', NULL)->count();
         foreach ($options as $key => $option) {
             $this->clear();
             $this->type = $type;
             $this->number = $number;
             $this->person_id = $key;
             $this->comment = $option . '/' . $total;
             $this->save();
             $number++;
         }
     }
     return TRUE;
 }