input('message'); $username = $request->input('username'); $gameSession = app(GameSessionController::class)->getCurrentGameSession(); Log::debug('TwitchController::parseChatMessage', [ 'message' => $message, 'username' => $username, 'gameSessionId' => $gameSession->id ]); if (!$message || !$username || !$gameSession) { Log::debug('TwitchController::parseChatMessage - Missing input or invalid game session', [ 'message' => $message, 'username' => $username, 'gameSession' => $gameSession ]); return response()->json(['error' => 'No message, username, or valid game session provided'], 400); } $user = TwitchUser::firstOrCreate(['username' => $username]); $parsedCommand = TwitchHelper::parseMessage($message); Log::debug('TwitchController::parseChatMessage - Parsed Command', ['parsedCommand' => $parsedCommand]); if ($parsedCommand) { $color = $this->getColorFromName($parsedCommand['color'], $gameSession->palette_id); if (!$color) { return response()->json(['error' => 'Invalid color'], 400); } CommandHistory::create([ 'twitch_user_id' => $user->id, 'command' => $parsedCommand['command'], 'x' => $parsedCommand['x'] ?? null, 'y' => $parsedCommand['y'] ?? null, 'palette_color_id' => $color->id, 'game_session_id' => $gameSession->id, ]); switch ($parsedCommand['command']) { case 'place': $this->handlePlaceCommand($parsedCommand, $user->id, $gameSession->id, $color->id); break; // Add cases for other commands like 'row', 'column', 'fill' } return response()->json([ 'command' => $parsedCommand, 'color' => [ 'hex_value' => $color->hex_value ] ]); } Log::debug('TwitchController::parseChatMessage - Invalid command', ['message' => $message]); return response()->json(['error' => 'Invalid command'], 400); } private function handlePlaceCommand($parsedCommand, $userId, $gameSessionId, $colorId) { Log::debug('TwitchController::handlePlaceCommand', ['command' => $parsedCommand]); $x = $parsedCommand['x']; $y = $parsedCommand['y']; $currentState = CurrentState::where('x', $x) ->where('y', $y) ->where('game_session_id', $gameSessionId) ->first(); Log::debug('TwitchController::handlePlaceCommand - Retrieved Current State', ['currentState' => $currentState]); if ($currentState) { $currentState->update(['palette_color_id' => $colorId, 'updated_by' => $userId, 'updated_at' => now()]); } else { CurrentState::create([ 'x' => $x, 'y' => $y, 'palette_color_id' => $colorId, 'updated_by' => $userId, 'updated_at' => now(), 'game_session_id' => $gameSessionId, ]); } } private function getColorFromName($colorName, $paletteId) { return PaletteColor::where('palette_id', $paletteId)->where('name', $colorName)->first(); } }