How to Make a Simple Instant Win Game

This sample demonstrates how to make a simple instant win game, where participants are given prizes according to the ordinal numbers of their messages.

Files used in this sample are located in the folder:

    C:\Program Files\CodeSegment\SMS Studio\Samples\InfoService\InstantWinGame

Upon receiving an incoming message, the script InstantWinGame.vbs checks the ordinal number of the message and informs the user if he won the prize. The script awards each 50th, 100th, 500th and 1,000th sender in each group of 1,000 messages.

How to run this sample

  1. Click the New button on the main toolbar and select the Info service option in the New Service dialog.
  2. Click the Setup button on the main toolbar to display the Info Setup dialog.
  3. In the General tab of the Info Setup dialog click the Add button to display the Add Rule dialog.
  4. In the General tab of the Add Rule dialog enter the following parameters:
    Name: Instant Win
    Pattern: Win
    Action: Execute external application
  5. In the Execute tab of the Add Rule dialog enter the following parameters:
    External application: InstantWinGame.vbs - use the selection button ( ... ) to locate and select the external application.
    Application parameters: "%PipeName%" "%MsgID%" "InstantWinGameData.txt"
  6. Click the Add button to close the Add Rule dialog.
  7. In the Advanced tab of the Info Setup dialog enter the name of the pipe:
    Pipe name: WinPipe
  8. Click the OK button to close the Info Setup dialog.
  9. Click the Start button on the main toolbar to start the Info service.
  10. Use Tools / Receive Message option in the main menu to display the Receive Message dialog. Fill in the Text field with the text Win and click the Receive button. The Info service will receive this message and run the InstantWinGame.vbs application which will inform the user if he won the prize by storing a reply in the Outbox.

Source code

The contents of the InstantWinGame.vbs file:

'-------------------------------------------------------------------------------------------------------------------------------
' This script demonstrates how to make a simple instant win game.
' WARNING: Error checking omitted for clarity.
'
' Use the following application parameters in the Info Rule dialog:
' "%PipeName%" "%MsgID%" "InstantWinGameData.txt"
'-------------------------------------------------------------------------------------------------------------------------------

if WScript.Arguments.Count = 3 then
    
    ' Collect arguments
    PipeName = WScript.Arguments(0)                         ' Pipe name
    MsgID = WScript.Arguments(1)                            ' Incoming message ID
    DataFileName = WScript.Arguments(2)                     ' Data file name

    if PipeName <> "" then                                  ' Pipe name must be specified in the Info Setup dialog

        ' Create file system object
        set fso = CreateObject("Scripting.FileSystemObject")

        ' Load the message count from the file
        set file = fso.OpenTextFile(DataFileName,1,true)
        MsgCount = 0
        if file.AtEndOfStream <> true then
            MsgCountTxt = file.ReadLine()
            if IsNumeric(MsgCountTxt) then
                MsgCount = CInt(MsgCountTxt)
                if MsgCount >= 1000 then                    ' Reset counter to 0 when it reaches 1000
                    MsgCount = 0
                end if
            end if
        end if
        file.Close
     
        ' Give the prize for the 50th, 100th, 500th and 1000th message
        MsgCount = MsgCount + 1
        ReplyText = "Sorry, no prize for you."    
        if MsgCount = 50 then
            ReplyText = "Congratulations, you have won the prize for 50."
        elseif MsgCount = 100 then
            ReplyText = "Congratulations, you have won the prize for 100."
        elseif MsgCount = 500 then
            ReplyText = "Congratulations, you have won the prize for 500."
        elseif MsgCount = 1000 then
            ReplyText = "Congratulations, you have won the prize for 1000."
        end if

        ' Save the message count to the file
        set file = fso.CreateTextFile(DataFileName,true)
        file.WriteLine(MsgCount)
        file.Close

        ' Send the reply back to the user via the Info pipe
        set pipe = fso.CreateTextFile("\\.\pipe\" & PipeName)
        pipe.WriteLine("ReplyToID:" & MsgID)
        pipe.WriteLine("Body:" & ReplyText)
        pipe.WriteLine("<Send>")
        pipe.Close
        
        WScript.Quit(0)                                     ' Success

    else

        MsgBox "Error: Pipe name is missing.", vbCritical, "SMS Studio Instant Win Game Sample"
        WScript.Quit(1)                                     ' Error

    end if
    
else
    
    MsgBox "Error: Wrong number of arguments.", vbCritical, "SMS Studio Instant Win Game Sample"
    WScript.Quit(1)                                         ' Error
    
end if

'-------------------------------------------------------------------------------------------------------------------------------
' Copyright (c) 2002-2009 CodeSegment. All rights reserved.                                          http://www.codesegment.com/
'-------------------------------------------------------------------------------------------------------------------------------

Related topics

Info Rules, Named Pipe

 

Copyright © 2002-2010 CodeSegment. All rights reserved.

   www.codesegment.com