with('palette')->latest()->first(); $currentState = $currentGame ? CurrentState::where('game_session_id', $currentGame->id)->with('color')->get() : []; return view('administration.index', compact('palettes', 'currentGame', 'currentState')); } public function changePalette(Request $request) { $request->validate([ 'palette_id' => 'required|exists:palettes,id', ]); $currentGame = GameSession::whereNull('ended_at')->latest()->first(); if ($currentGame) { $currentGame->palette_id = $request->palette_id; $currentGame->save(); return redirect()->route('administration.index')->with('success', 'Palette changed successfully.'); } return redirect()->route('administration.index')->with('error', 'No active game session.'); } public function createGame(Request $request) { $request->validate([ 'session_name' => 'required|string|max:255', 'palette_id' => 'required|exists:palettes,id', ]); // End the current game session if one is active $currentGame = GameSession::whereNull('ended_at')->latest()->first(); if ($currentGame) { $currentGame->update(['ended_at' => now()]); } // Create the new game session $newGame = GameSession::create([ 'session_name' => $request->session_name, 'palette_id' => $request->palette_id, ]); // Reset the board for the new game session CurrentState::where('game_session_id', $newGame->id)->delete(); return redirect()->route('administration.index')->with('success', 'New game session created and board reset.'); } }