me: can you create a funny game

TSQL.APP AI: I'd be happy to create a funny game using TSQL.APP! Let's create a simple number guessing game where the user tries to guess a random number, with funny messages based on how close they are.


Here's a TSQL script for a "Guess the Number" game modal:

-- 1. Declare all variables at the start
DECLARE @TargetNumber NVARCHAR(MAX);
DECLARE @UserGuess NVARCHAR(MAX);
DECLARE @GuessButton NVARCHAR(MAX);
DECLARE @NewGameButton NVARCHAR(MAX);
DECLARE @Message NVARCHAR(MAX);
DECLARE @Title NVARCHAR(MAX);
DECLARE @Difference INT;
DECLARE @ParsedGuess INT;
DECLARE @ParsedTarget INT;
DECLARE @FunnyMessage NVARCHAR(MAX);
DECLARE @AttemptCount NVARCHAR(MAX);
DECLARE @ParsedAttemptCount INT;
DECLARE @GameState NVARCHAR(MAX);

-- 2. Synchronize modal values
EXEC sp_api_modal_get_value @name=N'@TargetNumber', @value=@TargetNumber OUT;
EXEC sp_api_modal_get_value @name=N'@UserGuess', @value=@UserGuess OUT;
EXEC sp_api_modal_get_value @name=N'@GuessButton', @value=@GuessButton OUT;
EXEC sp_api_modal_get_value @name=N'@NewGameButton', @value=@NewGameButton OUT;
EXEC sp_api_modal_get_value @name=N'@AttemptCount', @value=@AttemptCount OUT;
EXEC sp_api_modal_get_value @name=N'@GameState', @value=@GameState OUT;

-- 3. Initialize game if needed
IF @TargetNumber IS NULL OR @GameState = N'new'
BEGIN
    -- Generate random number between 1 and 100
    SET @TargetNumber = CAST(CAST(RAND() * 100 AS INT) + 1 AS NVARCHAR(MAX));
    SET @AttemptCount = N'0';
    SET @GameState = N'playing';
    
    -- Store values
    EXEC sp_api_modal_value @name=N'@TargetNumber', @value=@TargetNumber;
    EXEC sp_api_modal_value @name=N'@AttemptCount', @value=@AttemptCount;
    EXEC sp_api_modal_value @name=N'@GameState', @value=@GameState;
END

-- 4. Display game interface
SET @Title = N'🎮 Funny Number Guessing Game 🎲';
EXEC sp_api_modal_text @text=@Title, @class=N'h3';

-- Game instructions
IF @GameState = N'playing'
BEGIN
    EXEC sp_api_modal_text @text=N'I''m thinking of a number between 1 and 100. Can you guess it?', @class=N'text-muted';
    
    -- Display attempt count if any guesses made
    IF CAST(@AttemptCount AS INT) > 0
    BEGIN
        SET @Message = CONCAT(N'Attempts so far: ', @AttemptCount);
        EXEC sp_api_modal_text @text=@Message, @class=N'text-info mb-3';
    END
    
    -- Input for guess
    EXEC sp_api_modal_input 
        @name=N'@UserGuess',
        @value=@UserGuess OUT,
        @type=N'number',
        @placeholder=N'Enter your guess (1-100)',
        @class=N'mb-3',
        @number_min=1,
        @number_max=100,
        @number_step=1;
    
    -- Submit button
    EXEC sp_api_modal_button 
        @name=N'@GuessButton',
        @value=N'🤔 Submit Guess',
        @valueout=@GuessButton OUT,
        @class=N'btn-primary',
        @inline=1;
END
ELSE IF @GameState = N'won'
BEGIN
    -- Display victory message
    SET @Message = CONCAT(N'🎉 Congratulations! You guessed it in ', @AttemptCount, N' attempts!');
    EXEC sp_api_modal_text @text=@Message, @class=N'text-success h4 mb-3';
    
    -- Show the number
    SET @Message = CONCAT(N'The number was indeed ', @TargetNumber, N'!');
    EXEC sp_api_modal_text @text=@Message, @class=N'text-info mb-3';
END

-- Always show the new game button if game is over
IF @GameState = N'won'
BEGIN
    EXEC sp_api_modal_button 
        @name=N'@NewGameButton',
        @value=N'🎲 Play Again',
        @valueout=@NewGameButton OUT,
        @class=N'btn-success';
END

-- 5. Handle Guess Button Click
IF @GuessButton IS NOT NULL AND @GameState = N'playing'
BEGIN
    BEGIN TRY
        -- Parse and validate guess
        SET @ParsedGuess = TRY_CAST(@UserGuess AS INT);
        SET @ParsedTarget = TRY_CAST(@TargetNumber AS INT);
        
        IF @ParsedGuess IS NULL OR @ParsedGuess < 1 OR @ParsedGuess > 100
        BEGIN
            SET @Message = N'Please enter a valid number between 1 and 100';
            EXEC sp_api_toast @text=@Message, @class=N'btn-warning';
            RETURN;
        END
        
        -- Increment attempt counter
        SET @ParsedAttemptCount = CAST(@AttemptCount AS INT) + 1;
        SET @AttemptCount = CAST(@ParsedAttemptCount AS NVARCHAR(MAX));
        EXEC sp_api_modal_value @name=N'@AttemptCount', @value=@AttemptCount;
        
        -- Calculate difference
        SET @Difference = ABS(@ParsedGuess - @ParsedTarget);
        
        -- Check if correct
        IF @Difference = 0
        BEGIN
            SET @GameState = N'won';
            
            -- Prepare values to preserve in JSON format for winning
            DECLARE @WinValues NVARCHAR(MAX);
            SET @WinValues = (
                SELECT 
                    [@TargetNumber]=@TargetNumber,
                    [@AttemptCount]=@AttemptCount,
                    [@GameState]=@GameState
                FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
            );
            
            EXEC sp_api_modal_restart @values=@WinValues;
            RETURN;
        END
        
        -- Generate funny message based on how close the guess is
        IF @Difference >= 50
        BEGIN
            SET @FunnyMessage = N'Wow! You''re in another galaxy! 🚀';
        END
        ELSE IF @Difference >= 30
        BEGIN
            SET @FunnyMessage = N'Are you even trying? That''s way off! 🙈';
        END
        ELSE IF @Difference >= 20
        BEGIN
            SET @FunnyMessage = N'Getting warmer, but still quite chilly! ❄️';
        END
        ELSE IF @Difference >= 10
        BEGIN
            SET @FunnyMessage = N'You''re on the right continent at least! 🗺️';
        END
        ELSE IF @Difference >= 5
        BEGIN
            SET @FunnyMessage = N'So close yet so far! You can almost smell it! 👃';
        END
        ELSE IF @Difference >= 2
        BEGIN
            SET @FunnyMessage = N'HOT HOT HOT! You''re burning up! 🔥';
        END
        ELSE
        BEGIN
            SET @FunnyMessage = N'SCORCHING! Just a hair away! 🥵';
        END
        
        -- Direction hint
        IF @ParsedGuess > @ParsedTarget
        BEGIN
            SET @FunnyMessage = CONCAT(@FunnyMessage, N' (Try lower)');
        END
        ELSE
        BEGIN
            SET @FunnyMessage = CONCAT(@FunnyMessage, N' (Try higher)');
        END
        
        -- Prepare values to preserve in JSON format
        DECLARE @JsonValues NVARCHAR(MAX);
        SET @JsonValues = (
            SELECT 
                [@TargetNumber]=@TargetNumber,
                [@AttemptCount]=@AttemptCount,
                [@GameState]=@GameState
            FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
        );
        
        -- Display result
        EXEC sp_api_toast @text=@FunnyMessage, @class=N'btn-info';
        EXEC sp_api_modal_restart @values=@JsonValues;
    END TRY
    BEGIN CATCH
        SET @Message = ERROR_MESSAGE();
        EXEC sp_api_toast @text=@Message, @class=N'btn-danger';
    END CATCH
END

-- 6. Handle New Game Button Click
IF @NewGameButton IS NOT NULL
BEGIN
    SET @GameState = N'new';
    
    -- Prepare values to pass to modal restart
    DECLARE @NewGameValues NVARCHAR(MAX);
    SET @NewGameValues = (
        SELECT [@GameState]=@GameState
        FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
    );
    
    EXEC sp_api_modal_restart @values=@NewGameValues;
END

This script creates a fun number guessing game with the following features:

The system generates a random number between 1 and 100 The player tries to guess the number After each guess, they get a funny message with a hint about how close they are The game tracks the number of attempts When they correctly guess the number, they win and can play again The funny messages change based on how close the guess is, making it entertaining while also helping guide the player. The game also includes emoji to make it more visually engaging.

To play this game, you would run this SQL script as an action in TSQL.APP.