Umer Pasha Blogs

www.umerpasha.com

MS SQL Source code for Las vegas Slot Machine game

on October 31, 2014

Sometimes while working you feel like running away and to take a break…. maybe a drive to Vegas.

Now with this script, you will not have to. You will bring Vegas to your MS-SQL Server Query Window!

Just change the DB name (UmerDB) in the script below to any of your DB where you want to host. Maybe name it your favorite casino! and run the script.

To play all you have to do is execute the following command repeatedly in the query window with the DB selected:
USAGE: sp_LVG ‘login’,’password’,
EXAMPLE: sp_LVG ‘Umer’,’pwd123′,15.50

Different symbols have different winning factors. Every double or triple wins! It’s that easy!
Even if you have a $ show up in one of the slots, you don’t lose! See… told you it was easy!

Winnings go up in this order. $$$ is a jackpot:

‘#’
‘€’
‘£’
‘†’
‘A’
‘7’
‘¤’
‘$’

Enjoy!


USE [UmerDB]
GO

/****** Object: StoredProcedure [dbo].[sp_LVG] Script Date: 02/08/2010 15:06:53 ******/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_LVG]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_LVG]
GO

USE [UmerDB]
GO

/****** Object: StoredProcedure [dbo].[sp_LVG] Script Date: 02/08/2010 15:06:53 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

--USAGE: sp_LVG 'login','password',
--EXAMPLE: sp_LVG 'lgn','pwd',15.50

Create Procedure [dbo].[sp_LVG] 
 @Login varchar(10),
 @Password varchar(10),
 @Money Money

AS
BEGIN
SET NOCOUNT ON;

Declare @Count As int
Declare @StartingBalance As Money
Declare @Slot1 As char(1)
Declare @Slot2 As char(1)
Declare @Slot3 As char(1)
Declare @Temp As char(1)
Declare @Slots varchar(3)
Declare @Factor As Float
Declare @Tries Int


Select @StartingBalance = 100
Select @Count = 0
Select @Tries = 0

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[LVG]') AND type in (N'U'))
BEGIN
 CREATE TABLE [dbo].[LVG]
 (
 vLogin varchar(10),
 vPassword varchar(10),
 mMoney Money,
 iTries Int 
 )
END

Select @Count = Count(vLogin) From LVG Where vLogin = @Login and vPassword = @Password 
If @Count > 0
Begin
 Select vLogin As Your_Login, mMoney as Your_Balance, iTries as Tries From LVG Where vLogin = @Login and vPassword = @Password

 Select @StartingBalance = mMoney From LVG Where vLogin = @Login and vPassword = @Password
 If @StartingBalance > 0 AND @StartingBalance >= @Money
 Begin

 Create Table #Slots
 (
 iInt int,
 cASCII tinyint 
 )

 Insert into #Slots values (1,35)
 Insert into #Slots values (2,36)
 Insert into #Slots values (3,55)
 Insert into #Slots values (4,65)
 Insert into #Slots values (5,128)
 Insert into #Slots values (6,134)
 Insert into #Slots values (7,163)
 Insert into #Slots values (8,164)

 Create Table #Winnings
 (
 vSymbol varchar(1),
 iOccurance tinyint,
 fFactor float
 )

 Insert into #Winnings values ('#',2,0)
 Insert into #Winnings values ('€',2,0.25)
 Insert into #Winnings values ('£',2,0.5)
 Insert into #Winnings values ('†',2,0.75)
 Insert into #Winnings values ('A',2,1.75)
 Insert into #Winnings values ('7',2,1.50)
 Insert into #Winnings values ('¤',2,1.25)
 Insert into #Winnings values ('$',2,2.0)
 Insert into #Winnings values ('#',3,1.25)
 Insert into #Winnings values ('€',3,1.5)
 Insert into #Winnings values ('£',3,1.75)
 Insert into #Winnings values ('†',3,2.0)
 Insert into #Winnings values ('A',3,2.25)
 Insert into #Winnings values ('7',3,2.50)
 Insert into #Winnings values ('¤',3,2.75)
 Insert into #Winnings values ('$',3,3.0)



 Select @Slot1 = char(cASCII) from #Slots Where iInt = CEILING(8 * RAND()) 
 Select @Slot2 = char(cASCII) from #Slots Where iInt = CEILING(8 * RAND()) 
 Select @Slot3 = char(cASCII) from #Slots Where iInt = CEILING(8 * RAND()) 

 Select @Slot1 As Slot_1, @Slot2 As Slot_2, @Slot3 As Slot_3 

 If @Slot1 = @Slot2 Or @Slot1 = @Slot3 Or @Slot2 = @Slot3
 Begin
 Select @Slots=@Slot1 + @Slot2 + @Slot3
 DECLARE temp_cursor CURSOR FORWARD_ONLY FOR 
 Select distinct vSymbol, fFactor from #winnings where 
 ((3 - LEN(REPLACE(@Slots, @Slot1, ''))) =iOccurance and vSymbol = @Slot1)
 OR
 ((3 - LEN(REPLACE(@Slots, @Slot2, ''))) =iOccurance and vSymbol = @Slot2)
 OR
 ((3 - LEN(REPLACE(@Slots, @Slot3, ''))) =iOccurance and vSymbol = @Slot3)

 OPEN temp_cursor
 FETCH NEXT FROM temp_cursor INTO @Slots, @Factor
 CLOSE temp_cursor
 DEALLOCATE temp_cursor

 Update LVG Set mMoney = (mMoney + (@Money * @Factor)) ,
 iTries = iTries + 1
 Where vLogin = @Login 
 End
 Else
 Begin
 --Select @Count = max(iInt) from #Slots 
 Select @Count = 2 --For $ 
 Select @Temp = char(cASCII) from #Slots where iInt = @Count

 If @Temp <> @Slot1 And @Temp <> @Slot2 And @Temp <> @Slot3
 Update LVG Set mMoney = (mMoney - @Money) ,iTries = iTries + 1 Where vLogin = @Login 
 Else
 Update LVG Set iTries = iTries + 1 Where vLogin = @Login 
 
 End
 
 

 Drop Table #Slots
 Drop Table #winnings

 Select @Login As Your_Login, mMoney as Your_Balance, iTries as Tries From LVG Where vLogin=@Login
 End
End
Else
Begin
 Insert into LVG values (@Login, @Password, @StartingBalance, 0)
 Select @Login As Your_Login, @Password as Your_Password, @StartingBalance as Your_Balance 
End

--Drop Table LVG
--Drop proc sp_lvg
--sp_LVG 'up2','up2',10


END
GO


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: