public function challenges()
 {
     $startd = DB::table('Games')->select('start')->first();
     $endd = DB::table('Games')->select('stop')->first();
     if ($startd->start > Carbon::now()) {
         return view("pages.countdown");
     } elseif ($endd->stop < Carbon::now()) {
         return view('pages.closed');
     }
     $num_users = User::count();
     $completed = Submitted_flag::count();
     $num_challenges = Challenge::count();
     $average_c = $completed / $num_users / $num_challenges * 100;
     $stats = array('num_users' => $num_users, 'completed' => $completed, 'average' => $average_c);
     $mycompleted = Submitted_flag::where('user_id', Auth::user()->id)->get()->toArray();
     $game = Game::first();
     $categories = Category::get();
     $categories = $categories->toArray();
     foreach ($categories as $category) {
         $challenges = Challenge::where('category_id', $category['id'])->orderby('point_value', 'ASC')->get()->toArray();
         $categories[(int) $category['id'] - 1]['challenges'] = $challenges;
     }
     $directory = base_path() . '/public/Challenges_Repo/';
     $files = scandir($directory);
     $startd = DB::table('Games')->select('start')->first();
     if ($startd->start > Carbon::now()) {
         $start = true;
     } else {
         $start = false;
     }
     $data = array('user' => Auth::user(), 'game' => $game, 'categories' => $categories, 'stats' => $stats, 'files' => $files, 'completed' => $mycompleted, 'start' => $start);
     return view("pages.challenges")->with('data', $data);
 }
 public function challenges()
 {
     /*$game = Game::first();
             $categories = Category::get();
             $categories = $categories->toArray();
     
             foreach($categories as $category)
             {
                 $challenges = Challenge::where('category_id', $category['id'])->get()->toArray();
                 $categories[(int)$category['id'] - 1]['challenges'] =  $challenges;
             }
     
     
             $data = array('game' => $game, 'categories' => $categories);*/
     $num_users = User::count();
     $completed = Submitted_flag::count();
     $num_challenges = Challenge::count();
     $average_c = $completed / $num_users / $num_challenges * 100;
     $stats = array('num_users' => $num_users, 'completed' => $completed, 'average' => $average_c);
     $game = Game::first();
     $categories = Category::get();
     $categories = $categories->toArray();
     foreach ($categories as $category) {
         $challenges = Challenge::where('category_id', $category['id'])->get()->toArray();
         $categories[(int) $category['id'] - 1]['challenges'] = $challenges;
     }
     $directory = base_path() . '/public/Challenges_Repo/';
     $files = scandir($directory);
     $data = array('user' => Auth::user(), 'game' => $game, 'categories' => $categories, 'stats' => $stats, 'files' => $files);
     return view("pages.challenges")->with('data', $data);
 }
 public function store(SubmitFlagRequest $request)
 {
     $startd = DB::table('Games')->select('start')->first();
     $endd = DB::table('Games')->select('stop')->first();
     if ($startd->start > Carbon::now()) {
         return view("pages.countdown");
     } elseif ($endd->stop < Carbon::now()) {
         return view('pages.closed');
     }
     $request = $request->all();
     $flags = Flag::where('flag', $request['flag'])->first();
     if (!empty($flags)) {
         $previously = Submitted_flag::select('user_id')->where('text', $request['flag'])->where('user_id', Auth::user()->id)->get();
         $previously = $previously->toArray();
         var_dump($previously);
         if (empty($previously)) {
             $flags = $flags->toArray();
             $submission['created_at'] = Carbon::now()->toDateTimeString();
             $submission['updated_at'] = Carbon::now()->toDateTimeString();
             $submission['user_id'] = Auth::user()->id;
             $submission['challenge_id'] = $flags['challenge_id'];
             //to do yo
             $submission['text'] = $request['flag'];
             Submitted_flag::create($submission);
             return redirect('flags/submit')->with('message', 'Correct!');
         }
         $message = 'You have previously submitted this flag!';
         return redirect('flags/submit')->with('message', $message);
     }
     $filename = "../storage/failed.csv";
     $content = Auth::user()->name . "," . $request['flag'] . "\n\r";
     $bytesWritten = File::append($filename, $content);
     if ($bytesWritten === false) {
         die("Couldn't write to the file.");
     }
     return redirect('flags/submit')->with('message', 'Incorrect, sorry try again!');
 }
 public function store(SubmitFlagRequest $request)
 {
     $request = $request->all();
     $flags = Flag::where('flag', $request['flag'])->first();
     if (!empty($flags)) {
         $previously = Submitted_flag::select('user_id')->where('text', $request['flag'])->where('user_id', Auth::user()->id)->get();
         $previously = $previously->toArray();
         var_dump($previously);
         if (empty($previously)) {
             $flags = $flags->toArray();
             $submission['created_at'] = Carbon::now()->toDateTimeString();
             $submission['updated_at'] = Carbon::now()->toDateTimeString();
             $submission['user_id'] = Auth::user()->id;
             $submission['challenge_id'] = $flags['challenge_id'];
             //to do yo
             $submission['text'] = $request['flag'];
             Submitted_flag::create($submission);
             return redirect('flags/submit')->with('message', 'Correct!');
         }
         $message = 'You have previously submitted this flag!';
         return redirect('flags/submit')->with('message', $message);
     }
     return redirect('flags/submit')->with('message', 'Incorrect, sorry try again!');
 }