public function postUpdate($tournament) { if ($tournament->user != Auth::user()->id) { return Redirect::to('tournaments'); } $tournament->name = Input::get('name'); $maps = array_keys(Input::get('maps', [])); $players = array_keys(Input::get('players.ids', [])); $names = array_keys(Input::get('players.names', [])); if (is_null($tournament->name)) { return Redirect::back(); } $total = count($names) + count($players); if (ceil($total / 2) != count($maps)) { return Redirect::back(); } $tournament->save(); $tournament->maps()->detach(); foreach ($maps as $map) { $tournament->maps()->attach($map); } $tournament->players()->detach(); foreach ($players as $player) { $tournament->players()->attach($player); } $scenarii = new Collection(); $tournament->maps->each(function ($map) use(&$scenarii, $tournament) { if ($scenarii->count() < 2) { $scenarii = Scenario::where('season', Carbon::now()->year)->get()->shuffle(); } $map->scenarii($tournament)->attach($scenarii->pop()->id, ['tournament' => $tournament->id]); $map->scenarii($tournament)->attach($scenarii->pop()->id, ['tournament' => $tournament->id]); }); foreach ($names as $name) { $player = new Player(); $player->name = $name; $player->user = Auth::user()->id; $player->save(); $tournament->players()->attach($player->id); } if ($total % 2 != 0) { $tournament->players()->attach(User::fantom()->id); } return Redirect::to('tournaments/' . $tournament->id); }
function playerprogress_get() { if (!$this->get('pfsnumber') || !$this->get('type')) { $this->response(NULL, 400); } $scenario = new Scenario(); $scenario->select('season, type')->distinct()->get(); $types = array(); foreach ($scenario as $s) { $types[] = $s->season; } $types[] = 'mod'; $types[] = 'ap'; $response = array(); $playerprogress = new Person(); $scen = new Scenario(); $playerprogress->where('pfsnumber', $this->get('pfsnumber'))->get(); foreach ($scenario as $s) { if ($s->type == 'mod') { // Modules $response['mod'] = array('season' => 'Modules', 'total' => 0, 'completed' => 0); $response['mod']['completed'] = $playerprogress->scenarios->where_join_field('players', $this->get('type') . ' IS NOT NULL', NULL)->where('type', 'mod')->where('archived IS NULL', NULL)->get()->result_count(); $response['mod']['total'] = $scen->where('type', 'mod')->where('archived IS NULL', NULL)->count(); } elseif ($s->type == 'ap') { // Adventure paths $response['ap'] = array('season' => 'APs', 'total' => 0, 'completed' => 0); $response['ap']['completed'] = $playerprogress->scenarios->where_join_field('players', $this->get('type') . ' IS NOT NULL', NULL)->where('type', 'ap')->where('archived IS NULL', NULL)->get()->result_count(); $response['ap']['total'] = $scen->where('type', 'ap')->where('archived IS NULL', NULL)->count(); } else { // Seasons $response[$s->season] = array('season' => $s->season, 'total' => 0, 'completed' => 0, 'contenttype' => $s->type); $response[$s->season]['completed'] = $playerprogress->scenarios->where_join_field('players', $this->get('type') . ' IS NOT NULL', NULL)->where('season', $s->season)->where('archived IS NULL', NULL)->get()->result_count(); $response[$s->season]['total'] = $scen->where('season', $s->season)->where('archived IS NULL', NULL)->count(); } } $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; }