PixelPlacerBot/app/Http/Controllers/GameSessionController.php
2024-07-28 18:06:05 -04:00

57 lines
1.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\GameSession;
use App\Models\Palette;
use App\Models\WinningState;
class GameSessionController extends Controller
{
public function start(Request $request)
{
$request->validate([
'session_name' => 'required|string|max:255',
'palette_id' => 'required|exists:palettes,id',
'image_path' => 'required|string'
]);
$gameSession = GameSession::create([
'session_name' => $request->session_name,
'palette_id' => $request->palette_id,
'image_path' => $request->image_path,
]);
// Logic to initialize the winning state based on the provided image
// ...
return response()->json($gameSession);
}
public function end(Request $request, $id)
{
$gameSession = GameSession::findOrFail($id);
$gameSession->update(['ended_at' => now()]);
return response()->json($gameSession);
}
public function current()
{
$currentGame = GameSession::whereNull('ended_at')->latest()->first();
return response()->json($currentGame);
}
public function getCurrentGameSession()
{
$gameSession = GameSession::whereNull('ended_at')->latest()->first();
if (!$gameSession) {
$gameSession = GameSession::create(['session_name' => 'Session ' . now(), 'palette_id' => 1, 'image_path' => '']);
}
return $gameSession;
}
}