public function getIndex()
 {
     // Get the list
     $list = Session::get('list', []);
     foreach ($list as $k => &$l) {
         // $l starts as the amount integer and we're transforming it to an array
         $l = ['amount' => $l, 'item' => Item::with(array('recipe' => function ($query) {
             $query->limit(1);
         }, 'name'))->find($k)];
         if (count($l['item']->recipe) == 0) {
             unset($list[$k]);
         }
     }
     unset($l);
     $saved_link = [];
     if ($list) {
         foreach ($list as $id => $info) {
             $saved_link[] = $id . ',' . $info['amount'];
         }
     }
     $saved_link = implode(':', $saved_link);
     $job_list = ClassJob::get_name_abbr_list();
     $active = 'list';
     return view('pages.list', compact('active', 'list', 'saved_link', 'job_list'));
 }
 public function getSearch(Request $request)
 {
     $input = $request->all();
     $name = $input['name'];
     $min = $input['min'] ?: 1;
     $max = $input['max'] ?: 90;
     $class = $input['class'] ?: 'all';
     $per_page = isset($input['per_page']) ? $input['per_page'] : 10;
     $sorting = $input['sorting'] ?: 'name_asc';
     if (!in_array($per_page, array(10, 25, 50))) {
         $per_page = 10;
     }
     if (!is_numeric($min)) {
         $min = 1;
     }
     if (!is_numeric($max)) {
         $max = 90;
     }
     if ($min > $max) {
         list($max, $min) = [$min, $max];
     }
     $job_list = ClassJob::get_name_abbr_list();
     if ($class && $class != 'all') {
         $classjob = ClassJob::get_by_abbr($class);
     }
     $sorting = explode('_', $sorting);
     $order_by = 'name';
     $sort = 'asc';
     if (count($sorting) == 2) {
         // Only overwrite if need-be (i.e. don't test for "name" or "asc")
         if ($sorting[0] == 'level') {
             $order_by = $sorting[0];
         }
         if ($sorting[1] == 'desc') {
             $sort = $sorting[1];
         }
     }
     $query = Recipes::with('item', 'item.name')->select('*', 'recipes.id AS recipe_id', 'recipes.level AS level')->join('items AS i', 'i.id', '=', 'recipes.item_id')->join('translations AS t', 't.id', '=', 'i.name_' . Config::get('language'));
     $query->orderBy($order_by == 'name' ? 't.term' : 'recipes.' . $order_by, $sort);
     if ($name) {
         $query->whereHas('item', function ($query) use($name) {
             $query->whereHas('name', function ($query) use($name) {
                 $query->where('term', 'like', '%' . $name . '%');
             });
         });
     }
     if ($min && $max) {
         $query->whereBetween('recipes.level', [$min, $max]);
     }
     if (isset($classjob)) {
         $query->where('recipes.classjob_id', $classjob->id);
     }
     $list = $query->paginate($per_page);
     $crafting_list_ids = array_keys(Session::get('list', []));
     view()->share(compact('list', 'per_page', 'crafting_list_ids'));
     $output = ['tbody' => view('recipes.results')->render(), 'tfoot' => view('recipes.results_footer')->render()];
     return $output;
 }
 public function getList()
 {
     // All Jobs
     $job_list = ClassJob::get_name_abbr_list();
     view()->share('job_list', $job_list);
     $include_quests = TRUE;
     if (!\Request::all()) {
         return redirect()->back();
     }
     // Get Options
     $options = explode(':', array_keys(\Request::except('_token'))[0]);
     // Parse Options              						// Defaults
     $desired_job = isset($options[0]) ? $options[0] : 'CRP';
     $start = isset($options[1]) ? $options[1] : 1;
     $end = isset($options[2]) ? $options[2] : 5;
     $self_sufficient = isset($options[3]) ? $options[3] : 1;
     $misc_items = isset($options[4]) ? $options[4] : 0;
     $component_items = isset($options[5]) ? $options[5] : 0;
     $special = isset($options[6]) ? $options[6] : 0;
     $item_ids = $item_amounts = array();
     $top_level = TRUE;
     if ($desired_job == 'List') {
         $start = $end = null;
         $include_quests = FALSE;
         // Get the list
         $item_amounts = Session::get('list', array());
         $item_ids = array_keys($item_amounts);
         if (empty($item_ids)) {
             return redirect('/list');
         }
         view()->share(compact('item_ids', 'item_amounts'));
         $top_level = $item_amounts;
     }
     if ($desired_job == 'Item') {
         $item_id = $special;
         $start = $end = null;
         $include_quests = FALSE;
         // Get the list
         $item_amounts = array($item_id => 1);
         $item_ids = array_keys($item_amounts);
         if (empty($item_ids)) {
             return redirect('/list');
         }
         $item_special = true;
         view()->share(compact('item_ids', 'item_amounts', 'item_special'));
         $top_level = $item_amounts;
     }
     if (!$item_ids) {
         // Jobs are capital
         $desired_job = strtoupper($desired_job);
         // Make sure it's a real job, jobs might be multiple
         $job = [];
         foreach (explode(',', $desired_job) as $ds) {
             $job[] = ClassJob::get_by_abbr($ds);
         }
         // If the job isn't real, error out
         if (!isset($job[0])) {
             // Check for DOL quests
             $quests = array();
             foreach (array('MIN', 'BTN', 'FSH') as $job) {
                 $job = ClassJob::get_by_abbr($job);
                 $quests[$job] = QuestItem::where('classjob_id', $job->id)->orderBy('level')->with('item')->get();
             }
             $error = true;
             return view('crafting', compact('error', 'quests'));
         }
         $job_ids = [];
         foreach ($job as $j) {
             $job_ids[] = $j->id;
         }
         $full_name_desired_job = false;
         if (count($job) == 1) {
             $job = $job[0];
             $full_name_desired_job = $job->name->term;
         }
         // Starting maximum of 1
         if ($start < 0) {
             $start = 1;
         }
         if ($start > $end) {
             $end = $start;
         }
         if ($end - $start > 9) {
             $end = $start + 9;
         }
         // Check for quests
         $quest_items = QuestItem::with('classjob', 'classjob.abbr')->whereBetween('level', array($start, $end))->whereIn('classjob_id', $job_ids)->orderBy('level')->with('item')->get();
         view()->share(compact('job', 'start', 'end', 'quest_items', 'desired_job', 'full_name_desired_job'));
     }
     // Gather Recipes and Reagents
     $query = Recipes::with('name', 'classjob', 'classjob.name', 'classjob.abbr', 'item', 'item.name', 'item.quest', 'item.leve', 'item.vendors', 'item.beasts', 'item.clusters', 'item.clusters.classjob', 'item.clusters.classjob.abbr', 'reagents', 'reagents.vendors', 'reagents.beasts', 'reagents.clusters', 'reagents.clusters.classjob', 'reagents.clusters.classjob.abbr', 'reagents.recipe', 'reagents.recipe.name', 'reagents.recipe.item', 'reagents.recipe.item.name', 'reagents.recipe.item.vendors', 'reagents.recipe.item.beasts', 'reagents.recipe.item.clusters', 'reagents.recipe.item.clusters.classjob', 'reagents.recipe.item.clusters.classjob.abbr', 'reagents.recipe.classjob', 'reagents.recipe.classjob.abbr')->groupBy('recipes.item_id')->orderBy('rank');
     if ($item_ids) {
         $query->whereIn('recipes.item_id', $item_ids);
     } else {
         $query->whereHas('classjob', function ($query) use($job_ids) {
             $query->whereIn('classjob.id', $job_ids);
         })->whereBetween('level', array($start, $end));
     }
     $recipes = $query->get();
     // Do we not want miscellaneous items?
     if ($misc_items == 0 && $desired_job != 'List') {
         foreach ($recipes as $key => $recipe) {
             // Remove any Furnishings, Dyes and "Other"
             // ItemCategory 14 == 'Furnishing', 15 == 'Dye', 16 == 'Miscellany'
             // Miscellany is special, we want to keep any Miscellany if it's in a range of UI
             // ItemUICategory 12 to 32 are Tools (Primary/Secondary), keep those
             // Technically this only solves a Secondary tool issue.  Primary tools aren't part of 16/Miscellany
             if (in_array($recipe->item->itemcategory_id, [14, 15]) || $recipe->item->itemcategory_id == 16 && !in_array($recipe->item->itemuicategory_id, range(12, 32))) {
                 unset($recipes[$key]);
             }
         }
     }
     // Do we not want component items?
     if ($component_items == 0 && $desired_job != 'List') {
         foreach ($recipes as $key => $recipe) {
             // Remove any Miscellany
             // ItemUICategory of 63, as that's "Other"
             if ($recipe->item->itemuicategory_id == 63) {
                 unset($recipes[$key]);
             }
         }
     }
     // Fix the amount of the top level to be evenly divisible by the amount the recipe yields
     if (is_array($top_level)) {
         foreach ($recipes as $recipe) {
             $tl_item =& $top_level[$recipe->item_id];
             // If they're not evently divisible
             if ($tl_item % $recipe->yields != 0) {
                 // Make it so
                 $tl_item = ceil($tl_item / $recipe->yields) * $recipe->yields;
             }
         }
         unset($tl_item);
         view()->share('item_amounts', $top_level);
     }
     $reagent_list = $this->_reagents($recipes, $self_sufficient, 1, $include_quests, $top_level);
     // Look through the list.  Is there something we're already crafting?
     // Subtract what's being made from needed reagents.
     //  Example, culinary 11 to 15, you need olive oil for Parsnip Salad (lvl 13)
     //   But you make 3 olive oil at level 11.  We don't want them crafting another olive oil.
     foreach ($recipes as $recipe) {
         if (!isset($reagent_list[$recipe->item_id])) {
             continue;
         }
         $reagent_list[$recipe->item_id]['both_list_warning'] = TRUE;
         $reagent_list[$recipe->item_id]['make_this_many'] += 1;
     }
     // Look through the reagent list, make sure the reagents are evently divisible by what they yield
     foreach ($reagent_list as &$reagent) {
         // If they're not evently divisible
         if ($reagent['make_this_many'] % $reagent['yields'] != 0) {
             // Make it so
             $reagent['make_this_many'] = ceil($reagent['make_this_many'] / $reagent['yields']) * $reagent['yields'];
         }
     }
     unset($reagent);
     // Let's sort them further, group them by..
     // Gathered, Then by Level
     // Other (likely mob drops)
     // Crafted, Then by level
     // Bought, by price
     $sorted_reagent_list = ['Gathered' => [], 'Bought' => [], 'Other' => [], 'Pre-Requisite Crafting' => [], 'Crafting List' => []];
     $gathering_class_abbreviations = ClassJob::get_abbr_list(Config::get('site.job_ids.gathering'));
     foreach ($reagent_list as $reagent) {
         $section = 'Other';
         $level = 0;
         // Section
         if (in_array($reagent['self_sufficient'], $gathering_class_abbreviations)) {
             $section = 'Gathered';
             $level = $reagent['item']->level;
         } elseif ($reagent['self_sufficient']) {
             $section = 'Pre-Requisite Crafting';
             if (!isset($reagent['item']->recipe[0])) {
                 dd($reagent['item']);
             }
             $level = $reagent['item']->recipe[0]->level;
         } elseif (count($reagent['item']->vendors)) {
             $section = 'Bought';
             $level = $reagent['item']->min_price;
         }
         if (!isset($sorted_reagent_list[$section][$level])) {
             $sorted_reagent_list[$section][$level] = array();
         }
         $sorted_reagent_list[$section][$level][$reagent['item']->id] = $reagent;
         ksort($sorted_reagent_list[$section][$level]);
     }
     foreach ($sorted_reagent_list as $section => $list) {
         ksort($sorted_reagent_list[$section]);
     }
     // Sort the pre-requisite crafting by rank
     // We don't need to sort them by level, just make sure it's in the proper structure
     // The keys don't matter either
     $prc =& $sorted_reagent_list['Pre-Requisite Crafting'];
     $new_prc = array('1' => array());
     foreach ($prc as $vals) {
         foreach ($vals as $v) {
             $new_prc['1'][] = $v;
         }
     }
     // Sort them by rank first
     usort($new_prc['1'], function ($a, $b) {
         return $a['item']->rank - $b['item']->rank;
     });
     // Then by classjob
     usort($new_prc['1'], function ($a, $b) {
         return $a['item']->recipe[0]->classjob_id - $b['item']->recipe[0]->classjob_id;
     });
     $prc = $new_prc;
     $reagent_list = $sorted_reagent_list;
     return view('crafting.list', compact('recipes', 'reagent_list', 'self_sufficient', 'misc_items', 'component_items', 'include_quests'));
 }
 public function postLoad(Request $request)
 {
     $inputs = $request->all();
     $job = $request['job'];
     $level = $request['level'];
     $craftable_only = $request['craftable_only'];
     $rewardable_too = $request['rewardable_too'];
     // All Jobs
     $job_list = ClassJob::get_name_abbr_list();
     // Jobs are capital
     $desired_job = strtoupper($job);
     $job = ClassJob::get_by_abbr($desired_job);
     // What stats do the class like?
     $stat_ids_to_focus = Stat::get_ids(Stat::focus($job->abbr->term));
     view()->share(compact('job_list', 'job', 'stat_ids_to_focus', 'level'));
     $equipment = Item::calculate($job->id, $level, 0, $craftable_only, $rewardable_too);
     return $this->getOutput($equipment);
 }