/** * Responds to requests to POST /high-scores * * @param $request request object */ public function postIndex(Request $request) { // store form data $puzzle_title = $request->puzzle; $sort_criteria = $request->criteria; // get list of created puzzles $puzzles = \App\Puzzle::where("created", "=", "1")->get(); // get selected puzzle and its id $puzzle = $puzzles->where("title", $puzzle_title)->first(); $puzzle_id = $puzzle["id"]; // get high scores based on form data $gamesessions = \App\Gamesession::with("puzzle")->orderBy($sort_criteria, "ASC")->where("puzzle_id", "=", $puzzle_id)->take(20)->get(); $headers = []; // set up table headers based on form data if ($sort_criteria == "total_time") { $headers = ["Time", "Moves"]; } else { $headers = ["Moves", "Time"]; } // get selected list of puzzles $puzzle_titles = $this->returnTitles($puzzles); // beginning rank must be 0 for index reasons in view $ranking = 0; //return "Profile controller -- by POST!"; return view("scores.index")->with(["gamesessions" => $gamesessions, "ranking" => $ranking, "usernames" => $this->returnUsernames($gamesessions), "puzzle_titles" => $puzzle_titles, "headers" => $headers, "data" => $this->returnDropdownData($request)]); }
/** * Run the database seeds. * * @return void */ public function run() { // get all gamesessions $gamesessions = \App\Gamesession::all(); // for every gamesession foreach ($gamesessions as $session) { // insert data into table DB::table('gamesession_user')->insert(['created_at' => $session->created_at, 'updated_at' => $session->updated_at, 'gamesession_id' => $session->id, 'user_id' => $session->user_id]); } }
/** * Responds to requests to POST /the-river-crossing-puzzle * * @param $request request object */ public function postIndex(Request $request) { // if user not logged in, do not store data if (!Auth::check()) { return; } // array to save data in $data = []; // put all request data in array foreach ($request->all() as $key => $value) { $data[$key] = $value; } // remove "T" from start and end times $start_time = str_replace("T", " ", $data["start_time"]); $end_time = str_replace("T", " ", $data["end_time"]); // remove last 5 characters from start and end time strings (milliseconds) $start_time = substr($start_time, 0, 19); $end_time = substr($end_time, 0, 19); // find total time spent on game in minutes $datetime1 = strtotime($start_time); $datetime2 = strtotime($end_time); $difference = abs($datetime2 - $datetime1); $total_time = round($difference / 60, 2); // get puzzle id $puzzle = \App\Puzzle::where("title", "=", "The Endangered Miners")->first(); $puzzle_id = $puzzle["id"]; // get and set last attempt for this puzzle $last_attempt = \App\Gamesession::where("user_id", "=", \Auth::id())->max("attempt_num"); $last_attempt++; // enter gamesession into database $gamesession = new \App\Gamesession(); $gamesession->user_id = \Auth::id(); $gamesession->puzzle_id = $puzzle_id; $gamesession->attempt_num = $last_attempt; $gamesession->start_time = $start_time; $gamesession->end_time = $end_time; $gamesession->total_time = $total_time; $gamesession->moves = $data["moves"]; $gamesession->save(); // save data in gamesession_user $user = array(\Auth::id()); $gamesession->users()->sync($user); }
/** * Responds to requests to POST /stats * * @param $request request object */ public function postIndex(Request $request) { // get the current user $user = Auth::user(); // get list of created puzzles $puzzles = \App\Puzzle::where("created", "=", "1")->get(); // either sort or delete the data if ($request->input("sort")) { // get all gamesessions for this user $gamesessions = \App\Gamesession::with("puzzle")->where("user_id", "=", $user->id)->get(); // store at a glance stats $glance_stats = array("puzzles_solved" => count($gamesessions), "min_time" => $gamesessions->min("total_time"), "min_moves" => $gamesessions->min("moves")); // store form data $puzzle_title = $request->puzzle; $sort_criteria = $request->criteria; // get selected puzzle and its id $puzzle = $puzzles->where("title", $puzzle_title)->first(); $puzzle_id = $puzzle["id"]; $stats; // sort based on form specifications if ($request->criteria == "first") { $stats = \App\Gamesession::with("puzzle")->orderBy("id", "ASC")->where("puzzle_id", "=", $puzzle_id)->where("user_id", "=", $user->id)->get(); } elseif ($request->criteria == "last") { $stats = \App\Gamesession::with("puzzle")->orderBy("id", "DESC")->where("puzzle_id", "=", $puzzle_id)->where("user_id", "=", $user->id)->get(); } elseif ($request->criteria == "fast") { $stats = \App\Gamesession::with("puzzle")->orderBy("total_time", "ASC")->where("puzzle_id", "=", $puzzle_id)->where("user_id", "=", $user->id)->get(); } elseif ($request->criteria == "slow") { $stats = \App\Gamesession::with("puzzle")->orderBy("total_time", "DESC")->where("puzzle_id", "=", $puzzle_id)->where("user_id", "=", $user->id)->get(); } elseif ($request->criteria == "least") { $stats = \App\Gamesession::with("puzzle")->orderBy("moves", "ASC")->where("puzzle_id", "=", $puzzle_id)->where("user_id", "=", $user->id)->get(); } elseif ($request->criteria == "most") { $stats = \App\Gamesession::with("puzzle")->orderBy("moves", "DESC")->where("puzzle_id", "=", $puzzle_id)->where("user_id", "=", $user->id)->get(); } return view("stats.index")->with(['stats' => $stats, 'user' => $user, 'puzzle_titles' => $this->returnTitles($puzzles), 'data' => $this->returnDropdownData($request), 'glance_stats' => $glance_stats, 'delete_data' => $this->returnDeleteData($request)]); } else { if ($request->input("delete")) { // store form data $puzzle_title = $request->delete_stats; // get selected puzzle and its id $puzzle = $puzzles->where("title", $puzzle_title)->first(); $puzzle_id = $puzzle["id"]; // delete stats from pivot table $user->gamesessions()->where("puzzle_id", "=", $puzzle_id)->detach(); // get stats from gamesessions table $stats = \App\Gamesession::where("user_id", "=", $user->id)->where("puzzle_id", "=", $puzzle_id)->get(); // delete stats from gamesessions table foreach ($stats as $stat) { $stat->delete(); } // get all gamesessions for this user $gamesessions = \App\Gamesession::with("puzzle")->where("user_id", "=", $user->id)->get(); // store at a glance stats $glance_stats = array("puzzles_solved" => count($gamesessions), "min_time" => $gamesessions->min("total_time"), "min_moves" => $gamesessions->min("moves")); return view("stats.index")->with(['user' => $user, 'puzzle_titles' => $this->returnTitles($puzzles), 'data' => $this->returnDropdownData($request), 'glance_stats' => $glance_stats, 'delete_data' => $this->returnDeleteData($request), 'delete_message' => "Your stats for " . $puzzle["title"] . " were deleted."]); } } }