Code: Select all
#NumPlayers=8
Global Dim Suite.S(3) ; string val of suite
Global Dim CardFace.S(12) ; string val of cards
;#Server="192.168.1.76"
;#Server="203.92.61.227"
#Server="127.0.0.1"
;#Server="alokdube.homeip.net"
;#Server="10.8.24.104"
#Port=6666
#DEBUG=0
Global timer_on=0 ; the timer is used to ensure the user plays in a specific amount of time
Global start_time ; the start time of the timer, the timer is in milliseconds
Global time_up=0 ;holds status if time is up or not
Global ConnectionID ; this keeps track of our connection to the server
Global Userid$
Global usercredits=0
Global playertomove$=""
Global status$=""
Global total_credits_on_table=0
Global GameCredits$="C:\Program Files\DAK\3-patti\1.jpg"
;our screen layout is such that the player with this view is on the bottom of the screen
;so all other orientations have to be arranged so that the player view is correct.
;we now set the value of the player id based on this player and the corresponding card
Global end_flag=0
Global raise_requested=0 ; keeps track if the player has asked for a table credit raise
Global GameCredits; value of table credits at any time
Global oldGameCredits;holds the initial value of the GameCredits prior to a raise
Global show_allowed=0
Global game_on=0
Global Dim player_name_from_table_position.S(#NumPlayers-1)
; this keeps track of postion_player(n)->player mapping ,position_player(position)->player
;so if player 5 is at position 0 in our view, we see position_player(0) as 5
; this keeps track of Player(n)->table mapping.
Global lastmessage$
;IncludeFile "dns-lookup.pb"
Procedure.s ReverseDNSQuery(IP.l)
Compiler = RunProgram("nslookup", IPString(IP), "", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
Output$ = ""
name$=""
If Compiler
While ProgramRunning(Compiler)
Output$=ReadProgramString(Compiler)
;Debug Output$
trim=FindString(Output$,"Name: ",1)
If trim
name$=name$+" "+Right(Output$,Len(output$)-trim+1-Len("Name: "))
;name$=RemoveString(Output$,"mail exchanger = ",1)
;Debug name$
EndIf
Wend
;MessageRequester("title",name$)
EndIf
ProcedureReturn (name$)
EndProcedure
; PureBasic Visual Designer v3.95 build 1485 (PB4Code)
#LoginWindow=1
#Cardview=0
#NumPlayers=8
#View_cards=28
#Pack=29
#Play=30
#Raise=31
#Show=32
#Text=33
#InputString=34
#ServerMessages=35
#SIgnOn=36
#UserCredits=37
#TableCredits=38
Global Dim Image(#NumPlayers-1,2)
; image id can be seen as player*3+card
; so if we have table postion 0, card 0, it will be imageid=0,
; and the image handle is image(0,0)
; we use the Image_id function to associate an image with its gadget, see below.
; note that imageid, is actually the gadget id,
; it is basically used by the front end to
; associate a front end gadget with a particular image
;- Fonts
Global FontID1
FontID1 = LoadFont(1, "Arial", 10, #PB_Font_Italic)
Global FontID2
FontID2 = LoadFont(2, "Book Antiqua", 12, #PB_Font_Bold)
Global FontID3
FontID3 = LoadFont(3, "Book Antiqua", 12)
Global FontID4
FontID4 = LoadFont(4, "Arial Black", 12, #PB_Font_Bold | #PB_Font_Italic)
Global FontID5
FontID5 = LoadFont(5, "Arial Black", 10)
Global FontID6
FontID6 = LoadFont(6, "Arial Black", 9)
Global FontID7
FontID7 = LoadFont(7, "Arial", 10, #PB_Font_Bold)
Global FontID8
FontID8 = LoadFont(8, "Arial", 12, #PB_Font_Bold)
Global FontID9
FontID9 = LoadFont(9, "Arial", 12)
Global FontID10
FontID10 = LoadFont(10, "Arial Black", 10, #PB_Font_Bold | #PB_Font_Italic)
;- Image Plugins
UsePNGImageDecoder()
UseJPEGImageDecoder()
;- Image Globals
Global Image24
Global Image25
;- Catch Images
Image(0,0) = LoadImage(0, "C:\Program Files\DAK\3-patti\null-null.png")
Image(0,1) = LoadImage(1, "C:\Program Files\DAK\3-patti\null-null.png")
Image(0,2) = LoadImage(2, "C:\Program Files\DAK\3-patti\null-null.png")
Image(1,0) = LoadImage(3, "C:\Program Files\DAK\3-patti\null-null.png")
Image(1,1) = LoadImage(4, "C:\Program Files\DAK\3-patti\null-null.png")
Image(1,2) = LoadImage(5, "C:\Program Files\DAK\3-patti\null-null.png")
Image(2,0) = LoadImage(6, "C:\Program Files\DAK\3-patti\null-null.png")
Image(2,1) = LoadImage(7, "C:\Program Files\DAK\3-patti\null-null.png")
Image(2,2) = LoadImage(8, "C:\Program Files\DAK\3-patti\null-null.png")
Image(3,0) = LoadImage(9, "C:\Program Files\DAK\3-patti\null-null.png")
Image(3,1) = LoadImage(10, "C:\Program Files\DAK\3-patti\null-null.png")
Image(3,2) = LoadImage(11, "C:\Program Files\DAK\3-patti\null-null.png")
Image(4,0) = LoadImage(12, "C:\Program Files\DAK\3-patti\null-null.png")
Image(4,1) = LoadImage(13, "C:\Program Files\DAK\3-patti\null-null.png")
Image(4,2) = LoadImage(14, "C:\Program Files\DAK\3-patti\null-null.png")
Image(5,0) = LoadImage(15, "C:\Program Files\DAK\3-patti\null-null.png")
Image(5,1) = LoadImage(16, "C:\Program Files\DAK\3-patti\null-null.png")
Image(5,2) = LoadImage(17, "C:\Program Files\DAK\3-patti\null-null.png")
Image(6,0) = LoadImage(18, "C:\Program Files\DAK\3-patti\null-null.png")
Image(6,1) = LoadImage(19, "C:\Program Files\DAK\3-patti\null-null.png")
Image(6,2) = LoadImage(20, "C:\Program Files\DAK\3-patti\null-null.png")
Image(7,0) = LoadImage(21, "C:\Program Files\DAK\3-patti\null-null.png")
Image(7,1) = LoadImage(22, "C:\Program Files\DAK\3-patti\null-null.png")
Image(7,2) = LoadImage(23, "C:\Program Files\DAK\3-patti\null-null.png")
Image24 = LoadImage(24, "C:\Program Files\DAK\3-patti\credits.JPG")
Image25 = LoadImage(25, GameCredits$)
Procedure Game_Screen()
OpenWindow(#LoginWindow, 25, 15, 1000, 690, "Teen Patti",#PB_Window_MinimizeGadget| #PB_Window_TitleBar| #PB_Window_MaximizeGadget )
SetWindowColor(#LoginWindow,RGB($00,$00,$00))
SetWindowState(#LoginWindow,#PB_Window_Minimize)
CreateGadgetList(WindowID(#LoginWindow))
TextGadget(#Text, 280, 120, 160, 20, "Enter your user name:", #PB_Text_Center)
SetGadgetFont(#Text,FontID2)
SetGadgetColor(#Text,#PB_Gadget_FrontColor,$33FFFF)
SetGadgetColor(#Text,#PB_Gadget_BackColor,$000000)
StringGadget(#InputString, 450, 120, 180, 20, "")
TextGadget(#ServerMessages, 450, 300, 180, 40, "",#PB_Text_Center)
SetGadgetFont(#ServerMessages,FontID2)
SetGadgetColor(#ServerMessages,#PB_Gadget_FrontColor,$33FFFF)
SetGadgetColor(#ServerMessages,#PB_Gadget_BackColor,$000000)
ButtonGadget(#SignOn, 500, 200, 60, 30, "Sign on")
SetGadgetFont(#SignOn, FontID6)
SetWindowState(#LoginWindow,#PB_Window_Maximize)
EndProcedure
Procedure Open_Cardview()
OpenWindow(#Cardview, 25, 15, 1000, 690, "Teen Patti", #PB_Window_MinimizeGadget | #PB_Window_TitleBar| #PB_Window_MaximizeGadget )
;keys
SetWindowColor(#Cardview,RGB($00,$00,$00))
CreateGadgetList(WindowID(#Cardview))
ButtonGadget(#View_cards, 300, 640, 80, 40, "View cards", #PB_Button_MultiLine)
SetGadgetFont(#View_cards, FontID6)
ButtonGadget(#Pack, 400, 640, 80, 40, "Pack")
SetGadgetFont(#Pack, FontID6)
ButtonGadget(#Raise, 500, 640, 80, 40, "Raise")
SetGadgetFont(#Raise, FontID6)
ButtonGadget(#Show, 600, 640, 80, 40, "Show")
SetGadgetFont(#Show, FontID6)
ButtonGadget(#Play, 460, 420, 80, 40, "Play")
SetGadgetFont(#Play, FontID6)
;player cards
ImageGadget(0, 360, 520, 71, 96, Image(0,0))
ImageGadget(1, 460, 520, 71, 96, Image(0,1))
ImageGadget(2, 560, 520, 71, 96, Image(0,2))
ImageGadget(3, 20, 420, 71, 96, Image(1,0))
ImageGadget(4, 120, 420, 71, 96, Image(1,1))
ImageGadget(5, 220, 420, 71, 96, Image(1,2))
ImageGadget(6, 20, 260, 71, 96, Image(2,0))
ImageGadget(7, 120, 260, 71, 96, Image(2,1))
ImageGadget(8, 220, 260, 71, 96, Image(2,2))
ImageGadget(9, 20, 80, 71, 96, Image(3,0))
ImageGadget(10, 120, 80, 71, 96, Image(3,1))
ImageGadget(11, 220, 80, 71, 96, Image(3,2))
ImageGadget(12, 380, 30, 71, 96, Image(4,0))
ImageGadget(13, 480, 30, 71, 96, Image(4,1))
ImageGadget(14, 580, 30, 71, 96, Image(4,2))
ImageGadget(15, 700, 80, 71, 96, Image(5,0))
ImageGadget(16, 800, 80, 71, 96, Image(5,1))
ImageGadget(17, 900, 80, 71, 96, Image(5,2))
ImageGadget(18, 700, 240, 71, 96, Image(6,0))
ImageGadget(19, 800, 240, 71, 96, Image(6,1))
ImageGadget(20, 900, 240, 71, 96, Image(6,2))
ImageGadget(21, 700, 400, 71, 96, Image(7,0))
ImageGadget(22, 800, 400, 71, 96, Image(7,1))
ImageGadget(23, 900, 400, 71, 96, Image(7,2))
ImageGadget(24, 400, 200, 194, 68, Image24)
ImageGadget(25, 440, 320, 123, 76, Image25)
EndProcedure
Procedure Change_image()
SetGadgetState(0,Image(0,0))
SetGadgetState(1,Image(0,1))
SetGadgetState(2,Image(0,2))
SetGadgetState(3,Image(1,0))
SetGadgetState(4,Image(1,1))
SetGadgetState(5,Image(1,2))
SetGadgetState(6,Image(2,0))
SetGadgetState(7,Image(2,1))
SetGadgetState(8,Image(2,2))
SetGadgetState(9,Image(3,0))
SetGadgetState(10,Image(3,1))
SetGadgetState(11,Image(3,2))
SetGadgetState(12,Image(4,0))
SetGadgetState(13,Image(4,1))
SetGadgetState(14,Image(4,2))
SetGadgetState(15,Image(5,0))
SetGadgetState(16,Image(5,1))
SetGadgetState(17,Image(5,2))
SetGadgetState(18,Image(6,0))
SetGadgetState(19,Image(6,1))
SetGadgetState(20,Image(6,2))
SetGadgetState(21,Image(7,0))
SetGadgetState(22,Image(7,1))
SetGadgetState(23,Image(7,2))
EndProcedure
Procedure Show_text()
TextGadget(100, 420, 480, 140, 20,player_name_from_table_position(0), #PB_Text_Center)
SetGadgetFont(100, FontID2)
If (#DEBUG)
PrintN("player 0:"+player_name_from_table_position(0))
EndIf
SetGadgetColor(100,#PB_Gadget_FrontColor,$33FFFF)
SetGadgetColor(100,#PB_Gadget_BackColor,$000000)
TextGadget(101, 80, 380, 140, 20, player_name_from_table_position(1), #PB_Text_Center)
SetGadgetFont(101, FontID2)
If (#DEBUG)
PrintN("player 1:"+player_name_from_table_position(1))
EndIf
SetGadgetColor(100,#PB_Gadget_FrontColor,$33FFFF)
SetGadgetColor(101,#PB_Gadget_BackColor,$000000)
TextGadget(102, 80, 220, 140, 20, player_name_from_table_position(2), #PB_Text_Center)
SetGadgetFont(102, FontID2)
If (#DEBUG)
PrintN("player 2:"+player_name_from_table_position(2))
EndIf
SetGadgetColor(100,#PB_Gadget_FrontColor,$33FFFF)
SetGadgetColor(102,#PB_Gadget_BackColor,$000000)
TextGadget(103, 80, 40, 140, 20, player_name_from_table_position(3), #PB_Text_Center)
SetGadgetFont(103, FontID2)
If (#DEBUG)
PrintN("player 3:"+player_name_from_table_position(3))
EndIf
SetGadgetColor(100,#PB_Gadget_FrontColor,$33FFFF)
SetGadgetColor(103,#PB_Gadget_BackColor,$000000)
TextGadget(104, 440, 0, 140, 20, player_name_from_table_position(4), #PB_Text_Center)
SetGadgetFont(104, FontID2)
If (#DEBUG)
PrintN("player 4:"+player_name_from_table_position(4))
EndIf
SetGadgetColor(100,#PB_Gadget_FrontColor,$33FFFF)
SetGadgetColor(104,#PB_Gadget_BackColor,$000000)
TextGadget(105, 760, 40, 140, 20,player_name_from_table_position(5), #PB_Text_Center)
SetGadgetFont(105, FontID2)
If (#DEBUG)
PrintN("player 5:"+player_name_from_table_position(5))
EndIf
SetGadgetColor(100,#PB_Gadget_FrontColor,$33FFFF)
SetGadgetColor(105,#PB_Gadget_BackColor,$000000)
TextGadget(106, 760, 200, 140, 20,player_name_from_table_position(6), #PB_Text_Center)
SetGadgetFont(106, FontID2)
If (#DEBUG)
PrintN("player 6:"+player_name_from_table_position(6))
EndIf
SetGadgetColor(100,#PB_Gadget_FrontColor,$33FFFF)
SetGadgetColor(106,#PB_Gadget_BackColor,$000000)
TextGadget(107, 760, 360, 140, 20,player_name_from_table_position(7), #PB_Text_Center)
SetGadgetFont(107, FontID2)
If (#DEBUG)
PrintN("player 7:"+player_name_from_table_position(7))
EndIf
SetGadgetColor(100,#PB_Gadget_FrontColor,$33FFFF)
SetGadgetColor(107,#PB_Gadget_BackColor,$000000)
TextGadget(#ServerMessages, 40, 540, 240, 120, "")
SetGadgetFont(#ServerMessages,FontID2)
SetGadgetColor(#ServerMessages,#PB_Gadget_FrontColor,$33FFFF)
SetGadgetColor(#ServerMessages,#PB_Gadget_BackColor,$000000)
TextGadget(#UserCredits, 720, 580, 200, 20, "Available Credits:"+Str(usercredits), #PB_Text_Center)
SetGadgetFont(#UserCredits,FontID2)
SetGadgetColor(#UserCredits,#PB_Gadget_FrontColor,$33FFFF)
SetGadgetColor(#UserCredits,#PB_Gadget_BackColor,$000000)
TextGadget(#TableCredits, 720, 610, 200, 20, "Total Credits on table:"+Str(total_credits_on_table), #PB_Text_Center)
SetGadgetFont(#TableCredits,FontID2)
SetGadgetColor(#TableCredits,#PB_Gadget_FrontColor,$33FFFF)
SetGadgetColor(#TableCredits,#PB_Gadget_BackColor,$000000)
EndProcedure
Procedure Change_GameCredits()
Image25 = LoadImage(25, GameCredits$)
SetGadgetState(25,Image25)
EndProcedure
Procedure Table_credits_change()
SetGadgetText(#TableCredits,"Total Credits on table:"+Str(total_credits_on_table))
EndProcedure
Procedure Change_user_credits()
SetGadgetText(#UserCredits,"Available Credits:"+Str(usercredits))
EndProcedure
Procedure Image_id(x,y)
ProcedureReturn (x*3+y)
EndProcedure
Procedure Change_text()
SetGadgetText(100,player_name_from_table_position(0))
If (#DEBUG)
PrintN("player 0:"+player_name_from_table_position(0))
EndIf
If (playertomove$=player_name_from_table_position(0))
SetGadgetColor(100,#PB_Gadget_FrontColor,$00FF00)
Else
SetGadgetColor(100,#PB_Gadget_FrontColor,$33FFFF)
EndIf
SetGadgetColor(100,#PB_Gadget_BackColor,$000000)
SetGadgetText(101,player_name_from_table_position(1))
SetGadgetFont(101, FontID2)
If (#DEBUG)
PrintN("player 1:"+player_name_from_table_position(1))
EndIf
If (playertomove$=player_name_from_table_position(1))
SetGadgetColor(101,#PB_Gadget_FrontColor,$00FF00)
Else
SetGadgetColor(101,#PB_Gadget_FrontColor,$33FFFF)
EndIf
SetGadgetColor(101,#PB_Gadget_BackColor,$000000)
SetGadgetText(102,player_name_from_table_position(2))
SetGadgetFont(102, FontID2)
If (#DEBUG)
PrintN("player 2:"+player_name_from_table_position(2))
EndIf
If (playertomove$=player_name_from_table_position(2))
SetGadgetColor(102,#PB_Gadget_FrontColor,$00FF00)
Else
SetGadgetColor(102,#PB_Gadget_FrontColor,$33FFFF)
EndIf
SetGadgetColor(102,#PB_Gadget_BackColor,$000000)
SetGadgetText(103,player_name_from_table_position(3))
SetGadgetFont(103, FontID2)
If (#DEBUG)
PrintN("player 3:"+player_name_from_table_position(3))
EndIf
If (playertomove$=player_name_from_table_position(3))
SetGadgetColor(103,#PB_Gadget_FrontColor,$00FF00)
Else
SetGadgetColor(103,#PB_Gadget_FrontColor,$33FFFF)
EndIf
SetGadgetColor(103,#PB_Gadget_BackColor,$000000)
SetGadgetText(104,player_name_from_table_position(4))
SetGadgetFont(104, FontID2)
If (#DEBUG)
PrintN("player 4:"+player_name_from_table_position(4))
EndIf
If (playertomove$=player_name_from_table_position(4))
SetGadgetColor(104,#PB_Gadget_FrontColor,$00FF00)
Else
SetGadgetColor(104,#PB_Gadget_FrontColor,$33FFFF)
EndIf
SetGadgetColor(104,#PB_Gadget_BackColor,$000000)
SetGadgetText(105,player_name_from_table_position(5))
SetGadgetFont(105, FontID2)
If (#DEBUG)
PrintN("player 5:"+player_name_from_table_position(5))
EndIf
If (playertomove$=player_name_from_table_position(5))
SetGadgetColor(105,#PB_Gadget_FrontColor,$00FF00)
Else
SetGadgetColor(105,#PB_Gadget_FrontColor,$33FFFF)
EndIf
SetGadgetColor(105,#PB_Gadget_BackColor,$000000)
SetGadgetText(106,player_name_from_table_position(6))
SetGadgetFont(106, FontID2)
If (#DEBUG)
PrintN("player 6:"+player_name_from_table_position(6))
EndIf
If (playertomove$=player_name_from_table_position(6))
SetGadgetColor(106,#PB_Gadget_FrontColor,$00FF00)
Else
SetGadgetColor(106,#PB_Gadget_FrontColor,$33FFFF)
EndIf
SetGadgetColor(106,#PB_Gadget_BackColor,$000000)
SetGadgetText(107,player_name_from_table_position(7))
SetGadgetFont(107, FontID2)
If (#DEBUG)
PrintN("player 7:"+player_name_from_table_position(7))
EndIf
If (playertomove$=player_name_from_table_position(7))
SetGadgetColor(107,#PB_Gadget_FrontColor,$00FF00)
Else
SetGadgetColor(107,#PB_Gadget_FrontColor,$33FFFF)
EndIf
SetGadgetColor(107,#PB_Gadget_BackColor,$000000)
EndProcedure
;Open_Cardview()
;File$="C:\Program Files\DAK\3-patti\"+Suite(Player(0,0)\suite)+"-"+Cardface(Player(0,0)\cardface)
;Image0=LoadImage(0,File$)
;SetGadgetState(#Image_0,Image0)
;PrintN (File$)
;Image0=LoadImage(0,"C:\Program Files\DAK\3-patti\clubs-ace.png")
;SetGadgetState(#Image_1,Image0)
;Repeat ; Start of the event loop
; Event = WaitWindowEvent() ; This line waits until an event is received from Windows
;WindowID = EventWindow() ; The Window where the event is generated, can be used in the gadget procedures
; GadgetID = EventGadget() ; Is it a gadget event?
;EventType = EventType() ; The event type
;You can place code here, and use the result as parameters for the procedures
;If Event = #PB_Event_Gadget
; If GadgetID = #Image_1
;ElseIf GadgetID = #Image_0
;ElseIf GadgetID = #Image_2
;ElseIf GadgetID = #Button_0
;ElseIf GadgetID = #Button_1
;ElseIf GadgetID = #Button_2
;ElseIf GadgetID = #Image_6
;ElseIf GadgetID = #Image_7
;ElseIf GadgetID = #Image_8
;ElseIf GadgetID = #Image_9
;ElseIf GadgetID = #Image_10
;ElseIf GadgetID = #Image_13
;ElseIf GadgetID = #Image_14
;ElseIf GadgetID = #Image_15
;ElseIf GadgetID = #Image_16
;ElseIf GadgetID = #Image_17
;ElseIf GadgetID = #Image_18
;ElseIf GadgetID = #Image_19
;ElseIf GadgetID = #Image_20
;ElseIf GadgetID = #Image_21
;ElseIf GadgetID = #Image_22
;ElseIf GadgetID = #Image_23
;ElseIf GadgetID = #Image_24
;ElseIf GadgetID = #Image_25
;ElseIf GadgetID = #Image_26
;ElseIf GadgetID = #Image_28
;ElseIf GadgetID = #Image_29
;ElseIf GadgetID = #Image_31
;ElseIf GadgetID = #Image_33
;ElseIf GadgetID = #Button_3
;EndIf
;EndIf
;Until Event = #PB_Event_CloseWindow ; End of the event loop
;End
;we need to keep the structure called Card so that we can easily pick up info
; based on what is passed over the network
Structure Card
suite.s
cardface.s
EndStructure
Global Dim Player.Card(#NumPlayers-1,2)
Structure Player_data
Name.s
Table_position.l
EndStructure
Global Dim PlayerInfo.Player_data(#NumPlayers-1)
;We define the string to array maps
;Suite(3) = "spades"
;Suite(2) = "diamonds"
;Suite(1) = "clubs"
;Suite(0) = "hearts"
;CardFace(0)="two"
;CardFace(1)="three"
;CardFace(2)="four"
;CardFace(3)="five"
;CardFace(4)="six"
;CardFace(5)="seven"
;CardFace(6)="eight"
;CardFace(7)="nine"
;CardFace(8)="ten"
;CardFace(9)="jack"
;CardFace(10)="queen"
;CardFace(11)="king"
;CardFace(12)="ace" ; ace is highest, 2 is lowest
If (#DEBUG)
OpenConsole()
EndIf
Procedure Connect_to_server()
status=1
ConnectionID = OpenNetworkConnection(#Server, #Port)
If ConnectionID
SetGadgetText(#ServerMessages, "Client connected To server...")
SendNetworkString(ConnectionID, "event:newuser\r"+Userid$+"\r\e")
;If (#DEBUG)
;MessageRequester("PureBasic - Client", "A string has been sent to the server, please check it before quit...", 0)
;EndIf
;we now check if the username has been accepted
Repeat
CEvent=NetworkClientEvent(ConnectionID)
Until CEvent=#PB_NetworkEvent_Data
Buffer = AllocateMemory(1000)
Message$=""
Event$=""
ReceiveNetworkData(ConnectionID, Buffer, 1000)
Message$=PeekS(Buffer)
FreeMemory(Buffer)
;we need to seperate on "\r" boundaries here
If (#Debug)
PrintN("Server:"+ReverseDnsQuery(GetClientIP(ConnectionID))+" has sent a packet !")
PrintN("Message:"+Message$)
EndIf
Dim Attribute.s(500)
attr_cnt=0; this holds the number of attributes, attribute(0) is the event:<event>
msg_cnt=0
Repeat
msg_cnt=msg_cnt+1
If Mid(Message$,msg_cnt,2)="\r"
Attribute(attr_cnt)=Left(Message$,msg_cnt-1)
attr_cnt=attr_cnt+1
remainingmessage$=Right(Message$,Len(Message$)-(msg_cnt+1))
Message$=remainingmessage$
msg_cnt=0
; If (#Debug)
; PrintN("Message:"+Message$)
; PrintN("Attribute: "+Attribute(attr_cnt-1))
; EndIf
EndIf
Until Mid(Message$,msg_cnt,2)="\e"
Event$=RemoveString(Attribute(0),"event:",1)
;If (#Debug)
; PrintN("Event:"+Event$)
;EndIf
Select Event$
Case "usernamereject"
SetGadgetText(#ServerMessages,"Error!:"+Attribute(1))
status=0
;if the username is acceptable, we will receive a tableupdate
Case "tableupdate"
If (#DEBUG)
PrintN("Table update:"+Message$)
EndIf
status=1
attr_cnt=1
For i=0 To #NumPlayers-1
PlayerInfo(i)\Name=Attribute(attr_cnt)
attr_cnt=attr_cnt+1
;Debug PlayerInfo(i)\Name
Player(i,0)\suite=Attribute(attr_cnt)
attr_cnt=attr_cnt+1
;Debug Player(i,0)\suite
Player(i,0)\cardface=Attribute(attr_cnt)
attr_cnt=attr_cnt+1
;Debug Player(i,0)\cardface
Player(i,1)\suite=Attribute(attr_cnt)
attr_cnt=attr_cnt+1
;Debug Player(i,1)\suite
Player(i,1)\cardface=Attribute(attr_cnt)
attr_cnt=attr_cnt+1
;Debug Player(i,1)\cardface
Player(i,2)\suite=Attribute(attr_cnt)
attr_cnt=attr_cnt+1
;Debug Player(i,2)\suite
Player(i,2)\cardface=Attribute(attr_cnt)
attr_cnt=attr_cnt+1
;Debug Player(i,2)\cardface
Next i
EndSelect
Else
SetGadgetText(#ServerMessages,"Error! Cannot connect to server")
status=0
EndIf;If ConnectionID ends here
ProcedureReturn status
EndProcedure
Procedure User_Login()
Game_Screen()
If InitNetwork() = 0
SetGadgetText(#ServerMessages,"Error! Cannot connect to network..")
Else
connected=0
Repeat ; Start of the event loop
Event = WaitWindowEvent() ; This line waits until an event is received from Windows
If Event=#PB_Event_Gadget
GadgetID = EventGadget()
If (GadgetID=#SignOn And connected=0)
Userid$=GetGadgetText(#InputString)
If (FindString (LCase(Userid$),"\r",1)) Or (FindString(LCase(Userid$),"\e",1))Or(FindString(LCase(Userid$),"null",1))Or(FindString(LCase(Userid$),"pack",1))
Userid$=""
EndIf
If Userid$=""
SetGadgetText(#ServerMessages,"Please Enter a valid user name")
Else
SetGadgetText(#ServerMessages,"")
connected=Connect_to_Server()
;Debug Str(connected)
;user signon client server code goes here
EndIf
EndIf
;Debug "Signon clicked"
;Debug UserID$
ElseIf Event=#PB_Event_CloseWindow
;Debug "window close event"
End
EndIf
Until connected=1
EndIf
CloseWindow(#LoginWindow)
EndProcedure
;note that from here we will have to look at client server relationships
;Userid$="alok"
;PlayerInfo(0)\Name="null"
;PlayerInfo(1)\Name="null"
;PlayerInfo(2)\Name="null"
;PlayerInfo(3)\Name="null"
;PlayerInfo(4)\Name="null"
;PlayerInfo(5)\Name="null"
;PlayerInfo(6)\Name="null"
;PlayerInfo(7)\Name="null"
;"seen" if user has seen cards, "pack" if user has packed
;"" if user is new (no cards so far) and "blind" is user is blind.
;playername="null" indicates no player at this position
;PlayerInfo(0)\Status=""
;PlayerInfo(1)\Status=""
;PlayerInfo(2)\Status=""
;PlayerInfo(3)\Status=""
;PlayerInfo(4)\Status=""
;PlayerInfo(5)\Status=""
;PlayerInfo(6)\Status=""
;PlayerInfo(7)\Status=""
;note that till this player's status is "" any command he sends to the server
;has no meaning
Procedure Check_null()
filename$=""
;we check the player status and change cards accordingly
For players=0 To #NumPlayers-1
If PlayerInfo(players)\Name="null"
PlayerInfo(players)\Name=""
EndIf
Next players
EndProcedure
;the procedure below can used to get the playerid of a particular player, given his name
;Playerid is actually the position of the individual as seen from the server
Procedure Player_id_from_name (playername.s)
For i=0 To #NumPlayers-1
If UCase(PlayerInfo(i)\Name)=UCase(playername)
Break
EndIf
Next i
ProcedureReturn(i)
EndProcedure
;the reason we are trying to map things to names is that we can pass player updates by their name
;example an event could be "event:pack\rplayer:foo"
Procedure Table_position_from_name (playername.s)
For i=0 To #NumPlayers-1
If UCase(PlayerInfo(i)\Name)=UCase(playername)
Break
EndIf
Next i
ProcedureReturn(PlayerInfo(i)\Table_position)
EndProcedure
;we now wait for the server to throw us the userlist
Procedure Change_cards()
;Debug Userid$
This_player_id=Player_id_from_name(Userid$)
;Debug Str(This_player_id)
; so now our image(0,card) is actually this_player_id's cards and his view of things around and so on
; hence Image(position_player(0),card) is what we have to keep track off after this step.
If #DEBUG
PrintN ("inside change_cards")
EndIf
table_position=0
Repeat
player_name_from_table_position(table_position)=PlayerInfo(This_player_id)\Name; position 0 has this player
;this is useful to find the player name, given the player position on the screen
PlayerInfo(This_player_id)\Table_position=table_position
;Debug Player(This_player_id,card)\suite
;Debug Player(This_player_id,card)\cardface
For card=0 To 2
File$="C:\Program Files\DAK\3-patti\"+Player(This_player_id,card)\suite+"-"+Player(This_player_id,card)\cardface+".png"
If #DEBUG
PrintN (File$)
EndIf
Image(table_position,card)=LoadImage(Image_id(table_position,card),File$)
; SetGadgetState(Image_id(i,card),Image(i,card))
Next card
If This_player_id=7
This_player_id=0
Else
This_player_id=This_player_id+1
EndIf
table_position=table_position+1
Until table_position>7
EndProcedure
User_Login()
Open_Cardview()
Check_null()
;PrintN("check_null done")
Change_cards()
Change_image()
Show_text()
Procedure Receive_server_data()
If #DEBUG
PrintN ("inside receive server Data")
EndIf
Buffer = AllocateMemory(5000)
Message$=""
message_len=0
Event$=""
ReceiveNetworkData(ConnectionID, Buffer, 5000)
Message$=PeekS(Buffer)
message_len=Len(Message$)
FreeMemory(Buffer)
;we need to seperate on "\r" boundaries here
If (#Debug)
PrintN("Client:"+ReverseDnsQuery(GetClientIP(ConnectionID))+" has sent a packet !")
PrintN("Message:"+Message$)
EndIf
Dim Attribute.s(500)
remainingmessage$=""
Repeat
attr_cnt=0; this holds the number of attributes, attribute(0) is the event:<event>
msg_cnt=0
Repeat
msg_cnt=msg_cnt+1
If Mid(Message$,msg_cnt,2)="\r"
Attribute(attr_cnt)=Left(Message$,msg_cnt-1)
attr_cnt=attr_cnt+1
remainingmessage$=Right(Message$,Len(Message$)-(msg_cnt+1))
Message$=remainingmessage$
msg_cnt=0
;If (#Debug)
;PrintN("Message:"+Message$)
;PrintN("Attribute: "+Attribute(attr_cnt-1))
;EndIf
EndIf
Until Mid(Message$,msg_cnt,2)="\e"
If (remainingmessage$="\e")
remainingmessage$=""
Else
remainingmessage$=Right(Message$,Len(Message$)-(msg_cnt+2))
EndIf
Event$=RemoveString(Attribute(0),"event:",1)
If (#Debug)
PrintN("Event:"+Event$)
PrintN("Remaining_message:"+remainingmessage$)
EndIf
Select Event$
;if the username is acceptable, we will receive a tableupdate
Case "tableupdate"
If (#DEBUG)
PrintN("Table update:"+Message$)
EndIf
status=1
attr_cnt=1
For i=0 To #NumPlayers-1
PlayerInfo(i)\Name=Attribute(attr_cnt)
attr_cnt=attr_cnt+1
;Debug PlayerInfo(i)\Name
Player(i,0)\suite=Attribute(attr_cnt)
attr_cnt=attr_cnt+1
;Debug Player(i,0)\suite
Player(i,0)\cardface=Attribute(attr_cnt)
attr_cnt=attr_cnt+1
;Debug Player(i,0)\cardface
Player(i,1)\suite=Attribute(attr_cnt)
attr_cnt=attr_cnt+1
;Debug Player(i,1)\suite
Player(i,1)\cardface=Attribute(attr_cnt)
attr_cnt=attr_cnt+1
;Debug Player(i,1)\cardface
Player(i,2)\suite=Attribute(attr_cnt)
attr_cnt=attr_cnt+1
;Debug Player(i,2)\suite
Player(i,2)\cardface=Attribute(attr_cnt)
attr_cnt=attr_cnt+1
;Debug Player(i,2)\cardface
Next i
Check_null()
Change_cards()
Change_image()
Change_text()
Case "servermessage"
SetGadgetText(#ServerMessages,Attribute(1))
lastmessage$=Attribute(1)
Change_text()
Case "playertomovechange"
playertomove$=Attribute(1)
If playertomove$=Userid$
start_time=ElapsedMilliseconds()
timer_on=1
time_up=0
Else
timer_on=0
time_up=0
EndIf
Change_text()
If (#DEBUG)
PrintN("player to move change:"+playertomove$)
EndIf
Case "playercreditschange"
usercredits=Val(Attribute(1))
Change_user_credits()
If (#DEBUG)
PrintN("usercredits change:"+Str(usercredits))
EndIf
Case "tablecreditschange"
GameCredits=Val(Attribute(1))
oldGameCredits=GameCredits
GameCredits$="C:\Program Files\DAK\3-patti\"+Str(GameCredits)+".jpg"
Change_GameCredits()
If (#DEBUG)
PrintN("table credits change:"+GameCredits$)
EndIf
Case "newgame"
status$=""
If (#DEBUG)
PrintN("newgame starts")
EndIf
Case "showallowed"
show_allowed=1
If (#DEBUG)
PrintN("show allowed")
EndIf
Case "totalcreditsontablechange"
total_credits_on_table=Val(Attribute(1))
Table_credits_change()
If (#DEBUG)
PrintN("total_credits_on_table"+Str(total_credits_on_table))
EndIf
Case "game_on"
game_on=Val(Attribute(1))
If (#DEBUG)
PrintN("game_on="+Str(game_on))
EndIf
If game_on=0
SetGadgetText(#ServerMessages,"Awaiting game to start..")
status$="blind"
EndIf
If game_on=2
SetGadgetText(#ServerMessages,"Game over..")
EndIf
Case "userleaveaccepted"
end_flag=1
EndSelect
Message$=remainingmessage$
If (#Debug)
PrintN("Message left:"+Message$)
EndIf
Until remainingmessage$=""
EndProcedure
status$="blind"
;we can assume that the status is blind because if the player is watching,
;then his turn will never come
Repeat ; Start of the event loop
If (timer_on=1)
elapsed_time=ElapsedMilliseconds()-start_time
If elapsed_time>5*60*1000
time_up=1
EndIf
EndIf
Event = WindowEvent() ; This line waits until an event is received from Windows
If (playertomove$=Userid$)And(time_up=0)And(game_on=1) ;..this player can send data/press buttons only when it is his turn
;note that the player's turn is controlled by the server
;we can safely assume the player starts with a blind because if the player is not in the game
;his turn will never come
;WindowID = EventWindow() ; The Window where the event is generated, can be used in the gadget procedures
GadgetID = EventGadget() ; Is it a gadget event?
EventType = EventType() ; The event type
;You can place code here, and use the result as parameters for the procedures
If Event = #PB_Event_Gadget
If (GadgetID = #View_cards)And( Not( (status$="seen")Or(status$="pack") ) )
If EventType=#PB_EventType_LeftClick
SendNetworkString(ConnectionID,"event:viewcardsrequest\r"+Userid$+"\r\e")
If #DEBUG
PrintN("Viewcardsrequest sent")
EndIf
status$="seen"
EndIf
ElseIf (GadgetID = #Pack)And(Not(status$="pack"))
If EventType=#PB_EventType_LeftClick
SendNetworkString(ConnectionID,"event:pack\r"+Userid$+"\r\e")
If #DEBUG
PrintN("Pack sent")
EndIf
status$="pack"
EndIf
ElseIf (GadgetID = #Play)And(Not(status$="pack"))And(EventType=#PB_EventType_LeftClick)
If raise_requested=0
SendNetworkString(ConnectionID,"event:play\r"+Userid$+"\r\e")
If #DEBUG
PrintN("Play sent")
EndIf
ElseIf raise_requested=1
If (status$="seen")
GameCredits=GameCredits/2
EndIf
SendNetworkString(ConnectionID,"event:creditsraise\r"+Userid$+"\r"+Str(GameCredits)+"\r\e")
SendNetworkString(ConnectionID,"event:play\r"+Userid$+"\r\e")
If #DEBUG
PrintN("CreditRaise and Play sent")
EndIf
raise_requested=0 ; we set this to zero as there is no raise requested now
EndIf
ElseIf (GadgetID = #Raise)And(Not(status$="pack"))And(EventType=#PB_EventType_LeftClick)
raise_requested=1
oldGameCredits=GameCredits
;raising credits is handled locally, the player sees the credits raised locally
If (status$="seen")And(GameCredits<16)
GameCredits=GameCredits+2
ElseIf (GameCredits<8)
GameCredits=GameCredits+1
EndIf
;and only when he clicks play is the server notified about the raise
GameCredits$="C:\Program Files\DAK\3-patti\"+Str(GameCredits)+".jpg"
Change_GameCredits()
ElseIf (GadgetID = #Show)And(Not(status$="pack"))And(EventType=#PB_EventType_LeftClick)And(show_allowed=1)
;if there are 2 players on the table the server will send a showallowed message
If raise_requested=1
SetGadgetText(#ServerMessages,"You cannot raise credits during a show,reverting to old credits")
GameCredits$="C:\Program Files\DAK\3-patti\"+Str(oldGameCredits)+".jpg"
Change_GameCredits()
raise_requested=0
Delay(2000)
EndIf
SendNetworkString(ConnectionID,"event:show\r"+Userid$+"\r\e")
; ElseIf GadgetID = #Image_4
; ElseIf GadgetID = #Image_5
; ElseIf GadgetID = #Image_6
; ElseIf GadgetID = #Image_7
; ElseIf GadgetID = #Image_8
; ElseIf GadgetID = #Image_9
; ElseIf GadgetID = #Image_10
; ElseIf GadgetID = #Image_11
; ElseIf GadgetID = #Image_12
; ElseIf GadgetID = #Image_13
; ElseIf GadgetID = #Image_14
; ElseIf GadgetID = #Image_15
; ElseIf GadgetID = #Image_16
; ElseIf GadgetID = #Image_17
; ElseIf GadgetID = #Image_18
; ElseIf GadgetID = #Image_19
; ElseIf GadgetID = #Image_20
; ElseIf GadgetID = #Image_21
; ElseIf GadgetID = #Image_22
; ElseIf GadgetID = #Image_23
; ElseIf GadgetID = #Image_24
; ElseIf GadgetID = #Image_25
; ElseIf GadgetID = #Button_3
EndIf ;gadgetid ends here
EndIf;event ends
;Delay(1)
EndIf ;playertomove$=userid$ ends here
If NetworkClientEvent(ConnectionID)=#PB_NetworkEvent_Data
Receive_server_data()
EndIf
Until (Event = #PB_Event_CloseWindow ) Or (time_up=1); If the user has pressed on the close button
If (time_up=1)
SetGadgetText(#ServerMessages,"Your time is up, your credits already in this game will be forfeited.")
Delay (10000)
EndIf
SendNetworkString(ConnectionID, "event:userleaves\r"+Userid$+"\r\e")
Repeat
If NetworkClientEvent(ConnectionID)=#PB_NetworkEvent_Data
Receive_server_data()
EndIf
Until end_flag=1
If (time_up=1)
CloseWindow(0)
EndIf
CloseNetworkConnection(ConnectionID)
End
;EndIf