PixelPlacerBot/app/Http/Controllers/AdministrationController.php

62 lines
2.0 KiB
PHP
Raw Permalink Normal View History

2024-07-28 18:06:05 -04:00
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\GameSession;
use App\Models\Palette;
use App\Models\CurrentState;
class AdministrationController extends Controller
{
public function index()
{
$palettes = Palette::all();
$currentGame = GameSession::whereNull('ended_at')->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.');
}
}