validate([ 'username' => 'required|string', 'x' => 'required|string', 'y' => 'required|string', 'color' => 'required|string', ]); $user = TwitchUser::firstOrCreate(['username' => $request->username]); $currentGame = GameSession::whereNull('ended_at')->latest()->first(); if (!$currentGame) { return response()->json(['error' => 'No active game session'], 404); } $color = PaletteColor::where('hex_value', $request->color)->first(); if (!$color) { return response()->json(['error' => 'Invalid color'], 400); } $command = CommandHistory::create([ 'twitch_user_id' => $user->id, 'game_session_id' => $currentGame->id, 'x' => $request->x, 'y' => $request->y, 'palette_color_id' => $color->id, 'timestamp' => now(), ]); CurrentState::updateOrCreate( ['game_session_id' => $currentGame->id, 'x' => $request->x, 'y' => $request->y], ['palette_color_id' => $color->id, 'updated_by' => $user->id, 'timestamp' => now()] ); // Logic to check if the current state matches the winning state // ... return response()->json($command); } }