teen-patti

Advanced game related topics
alokdube
Enthusiast
Enthusiast
Posts: 148
Joined: Fri Nov 02, 2007 10:55 am
Location: India
Contact:

teen-patti

Post by alokdube »

well this is an indian version of the game called "brag" (somewhat like poker)

http://en.wikipedia.org/wiki/Teen_Patti

Sources at:
http://www.4shared.com/dir/10765619/8d0 ... aring.html

To make it work over the network:
the server side is 3 files in 3-patti-server
the client side is images (cards) and 2 files 3-patti.pb and main.pb
the server address is in the 3-patti.pb file, that can be changed to point to where you run your server.

You can use the precompiled msi client on the site too...

Rules mentioned in the 3-patti-engine.pb
simply put
2 or more players:
1. each calls a bluff either as a blind (unseen cards )or seen cards
2. a blind move is x credits then a seen move is 2x credits
3. keeps going till one calls a "show"
4. initially both are blind, and one can see his cards
5. one can simply "play" or raise the stakes
6. winner simply is: trio>sequence+color>sequence>color>pair>highest card
i know most people other than indians may not get the game, but any ways feel free to try it and play around
Last edited by alokdube on Tue Feb 24, 2009 9:41 am, edited 2 times in total.
alokdube
Enthusiast
Enthusiast
Posts: 148
Joined: Fri Nov 02, 2007 10:55 am
Location: India
Contact:

source code server main

Post by alokdube »

Code: Select all

#NumPlayers=8; number of players
#DEBUG =0;set to one for debug
#Port=6666
#MinPlayersperGame=2
Global Actual_players=0 ; keeps track of number of players actually present
Global GameCredits=1;credits per move in this game, i.e. the credits deducted from the player
Global game_on=0;becomes 1 when the game is on
Global show_allowed=0; becomes 1 when a show is allowed in the game
Global show_called=0 ; this value becomes 1 when a "show" is called in the game
Global players_in_game=0;keeps track of people playing the game
Global Winner$="" ;holds the name of the winner of the game
Global total_credits_in_game=0;holds the total credits on the table in this game, the winner takes this
Global Winner_reason$="";holds the reason why Winner$ has won

Structure Player_data
Name.s
;"null" implies no player at this position
Status.s 
;"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.
Credits.l
ClientID.l
DomainName.s
EndStructure

Global ServerEvent
Global firsttomove_player_id=0
Global playertomove_id=0

Global Dim PlayerInfo.Player_data(#NumPlayers-1)



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"
;PlayerInfo(8)\Name="null"

PlayerInfo(0)\Status=""
PlayerInfo(1)\Status=""
PlayerInfo(2)\Status=""
PlayerInfo(3)\Status=""
PlayerInfo(4)\Status=""
PlayerInfo(5)\Status=""
PlayerInfo(6)\Status=""
PlayerInfo(7)\Status=""
;PlayerInfo(8)\Status=""


esc$=""
If (#DEBUG) 
OpenConsole()
EndIf


; This is the server side of the code
; it listens on a tcp port and expects incoming connections
; this in turn calls functions in the main engine to make the decisions
; this has to keep track of the number of clients (#NumPlayers)
; it has to keep track of how many clients are connected (min 3 for a game)
; it has to keep track of how many players are still playing and how many have packed
; it has to keep track of number of credits
; it has to keep track of blind and seen players
; if a player calls a show, make sure only 2 players left
; it has to keep track of the winner among the packed
; 

;the procedure below returns the player id from the name.
;it however cannot be used to check for pack or null.
Procedure Player_id_from_name (playername.s)
matchposition=-1
For i=0 To #NumPlayers-1
  If UCase(PlayerInfo(i)\name)=UCase(playername)
  matchposition=i
  Break
  EndIf
  Next i  
ProcedureReturn(matchposition)
EndProcedure

Procedure Find_empty_player_position()
freeposition=-1
For i=0 To #NumPlayers-1
If PlayerInfo(i)\Name="null"
 freeposition=i
Break
EndIf
Next i
ProcedureReturn(freeposition)
EndProcedure

Procedure User_name_exists(name.s)
user_exists=0
For i=0 To #NumPlayers-1
If LCase(name)=LCase(PlayerInfo(i)\Name)
 user_exists=1
EndIf
Next i
ProcedureReturn(user_exists)
EndProcedure

Procedure Player_id_from_ClientID (clientid.l)
matchposition=-1
For i=0 To #NumPlayers-1
  If PlayerInfo(i)\ClientID=clientid
  matchposition=i
  Break
  EndIf
  Next i  
ProcedureReturn(matchposition)
EndProcedure

Procedure Change_player_status_to_blind()
For i=0 To #NumPlayers-1
 If Not(PlayerInfo(i)\Name="null")
 PlayerInfo(i)\Status="blind"
 EndIf
Next i
EndProcedure

Procedure Increment_playertomove_id(playertomove_id)
playertomove_id=playertomove_id + 1
 While (PlayerInfo(playertomove_id)\Name="null")Or(PlayerInfo(playertomove_id)\Status="pack")Or(PlayerInfo(playertomove_id)\Status="")
 playertomove_id=playertomove_id+1
  If playertomove_id> (#NumPlayers -1)
  playertomove_id=0
  EndIf
 Wend
ProcedureReturn playertomove_id
EndProcedure

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




IncludeFile "3-patti-engine.pbi"
IncludeFile "3-patti-network-server.pb"



Start_Network_Server()
;we start the server to listen for incoming connections
;the moment we have #MinPlayersperGame players we can deal
;players present have a string value
;players not present have a "null" as a string value in player_name
;any new player will have to wait till the present game is over
;players not playing,null, will have their cards shown as Pack
;players who have packed will have a string value as "Name\rpack"




Repeat
;****
If NetworkServerEvent()
  Check_message_received()
  If (#DEBUG)
  PrintN("awaiting players, actual players="+Str(Actual_players))
  EndIf
EndIf
;*****

;if the actual_players > 3 we start the game
;Actual_players=3

If (Actual_players>=#MinPlayersperGame)
  
 ;Delay(5)
 Winner$=""
 Winner_reason$=""
 dealt=0 ;keeps track of how many cards are dealt
 Trio_winner=-1 ; this holds the value of a trio winner -1 indicates no trio
 Trio_count=0 ; this holds the number of people with trios
 Sequence_winner=-1 ; holds the value of the sequence winner -1 indicates no sequence
 Sequence_count=0 ; this holds the number of people with a sequence
 Sequence_color_count=0 ; this tracks if we have a sequence and color
 Sequence_color_winner=-1 ; this tracks the sequence color winner
 Color_winner=-1 ;this holds the color winner
 Color_count=0; this tracks the number of people with a color
 Pair_count=0 ; holds track of number of people with a pair
 Pair_winner=-1 ;holds the pair winner
 Highest_winner=-1 ;holds the highest card winner
 Highest_count=0 ;holds highest count
 show_called=0 ; we set this to 0
 show_allowed=0 ; this becomes 1 when a show is allowed in the game
 
 ;now we deal the cards
 total_credits_in_game=0
 
 game_on=1 ; the game has started
 
 If Not(Actual_players=0)
 Sendgame_on(game_on)
 EndIf
 
 GameCredits=1
 players_in_game=Actual_players
 total_credits_in_game=total_credits_in_game + Actual_players
 
If Not(Actual_players=0)
  TotalCreditsonTableChange()
  Start_new_game()
 ;we 1st shuffle the cards
  Shuffle_cards() 
 ;We now deal cards
  Deal_cards()
 
 ; we need to sort the cards
  Sort_cards()
EndIf
 
While PlayerInfo(firsttomove_player_id)\Name="null"
  firsttomove_player_id=firsttomove_player_id+1
  If (firsttomove_player_id>(#NumPlayers-1))
    firsttomove_player_id=0
  EndIf
Wend
 
servermessage$="Last Event: Game starts, " +PlayerInfo(firsttomove_player_id)\Name+" to play"
 
 
If #DEBUG
  PrintN ("First to move player id: "+PlayerInfo(firsttomove_player_id)\Name)
EndIf
 playertomove_id=firsttomove_player_id
 ;the dealer in the game rotates from 0 to 7 and the valid guy at any times becomes the dealer
 ;we increment the dealer at the end of each game
 ;the playertomove changes till the show is called
 
 Change_player_status_to_blind()
 ;all players already in the game now will be treated as blind
 ;any new player joining will be "" which means he is not in the game and sees a null
 
 
If Not(Actual_players=0) And (game_on=1)
 Table_update()
 ;cards have been dealt we now need to work on the network events till a show is called
 ;****
 Reduce_player_credits()
 ;each player has to pay one credit to play the game
 PlayerCreditsChange()
 ;credits change
 Tablecreditschange() 
 Playertomovechange(PlayerInfo(playertomove_id)\Name)
 ;changing the player to move, this will now be changed everytime a player clicks on "play"
 ServerMessage(servermessage$);ensure that servermessage$ always is after Playertomovechange
EndIf
 
If (players_in_game=2)And(show_allowed=0)
 If Not(Actual_players=0)
  Sendshowallowed()
 EndIf
 show_allowed=1
EndIf
 
Repeat

If (NetworkServerEvent())And(game_on=1)
  Check_message_received()
;we now check if there are 2 players in the game, if yes we can send a showallowed
 If (players_in_game=2)And(show_allowed=0)

  If Not(Actual_players=0)
   Sendshowallowed()
  EndIf

 show_allowed=1
  If (#DEBUG)
    PrintN("show_allowed=1")
  EndIf

 EndIf
EndIf
Until ((show_called=1)Or(players_in_game=1))


;********************

If #DEBUG
   PrintN ("show_called")
EndIf 
 
 
 ;once the game starts we need to keep track of server side events till we call "findwinner"
 ;we now find the winner
Find_winner()
 ;If (#DEBUG)
 ;Input()
 ;EndIf
 ;PrintN (File$)
PlayerInfo(Player_id_from_name(Winner$))\Credits=PlayerInfo(Player_id_from_name(Winner$))\Credits+total_credits_in_game 

If (players_in_game=1)
  servermessage$=Winner$+" wins this game , reason:All others have packed"
    Table_show() ; show the cards
  total_credits_in_game =0
  TotalCreditsonTableChange()
  PlayerCreditsChange()
  ServerMessage(servermessage$)
  ;credits change, as the winner has got more now
  game_on=2
  Delay(3000)
  Sendgame_on(game_on)
  Delay (3000)
  game_on=0
  Sendgame_on(game_on)
  
 Else
  servermessage$=Winner$+" wins this game , reason: "+Winner_reason$
  ServerMessage(servermessage$)
 
EndIf

If (#DEBUG)
  PrintN("winner credits:"+Str(PlayerInfo(Player_id_from_name(Winner$))\Credits))
EndIf

;Delay (3000)
If Not(Actual_players=0) And (game_on=1)
  Table_show() ; show the cards
  total_credits_in_game =0
  TotalCreditsonTableChange()
  PlayerCreditsChange()
  ;credits change, as the winner has got more now
   game_on=2
  Delay(3000)
  Sendgame_on(game_on)
  game_on=0
  Sendgame_on(game_on)
  
EndIf


 firsttomove_player_id=firsttomove_player_id+1
If (firsttomove_player_id>#NumPlayers-1)
 firsttomove_player_id=0
EndIf
;increment the dealer id

;Delay(1000)


servermessage$="..awaiting at least "+Str(#minPlayersperGame)+" players to start game"
game_on=0
Sendgame_on(game_on)
If Not(Actual_players=0)
 ServerMessage(servermessage$)
EndIf
;the game is done

If (#DEBUG)
 Esc$=Inkey()
EndIf


EndIf;actual_players>=#minplayerspergame


Until 1=0

If (#DEBUG)
CloseConsole()
EndIf
Last edited by alokdube on Tue Feb 24, 2009 9:18 am, edited 4 times in total.
alokdube
Enthusiast
Enthusiast
Posts: 148
Joined: Fri Nov 02, 2007 10:55 am
Location: India
Contact:

client side code

Post by alokdube »

again clubbed into 1 file

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
alokdube
Enthusiast
Enthusiast
Posts: 148
Joined: Fri Nov 02, 2007 10:55 am
Location: India
Contact:

3-patti-engine.pb

Post by alokdube »

Code: Select all

Global Dim Suite.S(3) ; string val of suite
Global Dim CardFace.S(12) ; string val of cards

Global dealt ;keeps track of how many cards are dealt
Global  Trio_winner=-1 ; this holds the value of a trio winner -1 indicates no trio
Global  Trio_count=0 ; this holds the number of people with trios
Global Sequence_winner=-1 ; holds the value of the sequence winner -1 indicates no sequence
Global Sequence_count=0 ; this holds the number of people with a sequence
Global Sequence_color_count=0 ; this tracks if we have a sequence and color
Global Sequence_color_winner=-1 ; this tracks the sequence color winner
Global Color_winner=-1 ;this holds the color winner
Global Color_count=0; this tracks the number of people with a color
Global Pair_count=0 ; holds track of number of people with a pair
Global Pair_winner=-1 ;holds the pair winner
Global Highest_winner=-1 ;holds the highest card winner
Global Highest_count=0 ;holds highest count


; a card is defined as card\suite , card\cardface
Structure Card
suite.b
cardface.b
EndStructure

Global Dim Player.Card(#NumPlayers-1,2)
;2D array of cards of the form Player(player,card)\suite and Player(player,card)\cardface

Global Dim Dealt(51)
;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

Procedure Shuffle_cards()
For i=0 To 51
  Dealt(i)=Int(Random(51))
  j=0
  While (j<i)
   If Dealt(i)=Dealt(j)
     Dealt(i)=Int(Random(51))
     j=0
   Else
   j=j+1
   EndIf
   Wend
        If (#DEBUG)  
        PrintN (Str(i)+":::"+Str(Dealt(i)))
        EndIf
 Next i
EndProcedure

Procedure Deal_cards()

     If (#DEBUG) 
     PrintN ("=====================================================================")
     EndIf
For i=0 To #NumPlayers-1 ; number of players
    If (#DEBUG)
    PrintN ("Player:"+PlayerInfo(i)\Name)
    EndIf
    
  For j=0 To 2 ; 3 cards per player
  ; simply put 52 cards if card number =X Quotient(X/13)=card\suite
  ; Remainder(X/13)=card\cardface, so card number 13 is suite=1 and cardface=0
  ; from the arracy of suites and cardface the card is ace of diamonds
   suite = Int(Dealt(dealt) / 13) 
   cardface= Dealt(dealt) -suite*13
   Player(i,j)\suite=suite
   Player(i,j)\cardface=cardface
   dealt=dealt+1
    If (#DEBUG) 
    Print ("Card "+Str(j)+" :")
    EndIf
   
    If (#DEBUG) 
    PrintN (Cardface(Player(i,j)\cardface)+" of "+Suite(Player(i,j)\suite))
    EndIf
   
   Next j

    If (#DEBUG)
    PrintN ("=====================================================================")
    EndIf
 Next i
 EndProcedure

Procedure Sort_cards()
For players=0 To #NumPlayers-1
Dim sorted.card(2) 
sorted(0)\cardface=Player(players,0)\cardface
sorted(0)\suite=Player(players,0)\suite
sorted(1)\cardface=Player(players,1)\cardface
sorted(1)\suite=Player(players,1)\suite
sorted(2)\cardface=Player(players,2)\cardface
sorted(2)\suite=Player(players,2)\suite
  For card=0 To 2
  For card1=card To 2
  ;to sort the cards we need to check the cardface
  ;we compare each card with every other card
  ;the card with the highest cardface is 1st, the lowest is last
  If sorted(card)\cardface<sorted(card1)\cardface
      temp=sorted(card)\cardface
      tempsuite=sorted(card)\suite
      sorted(card)\cardface=sorted(card1)\cardface
      sorted(card)\suite=sorted(card1)\suite
      sorted(card1)\cardface=temp
      sorted(card1)\suite=tempsuite
  EndIf
  Next card1
  Next card
Player(players,0)\cardface=sorted(0)\cardface
Player(players,0)\suite=sorted(0)\suite
Player(players,1)\cardface=sorted(1)\cardface
Player(players,1)\suite=sorted(1)\suite
Player(players,2)\cardface=sorted(2)\cardface
Player(players,2)\suite=sorted(2)\suite
If (#DEBUG)
 If Not((PlayerInfo(players)\Status="pack")Or(PlayerInfo(players)\Status="")Or(PlayerInfo(players)\Name="null"))
  PrintN("Player " +PlayerInfo(players)\Name + " sorted cards: ")
  PrintN (Cardface(Player(players,0)\cardface)+" of " +Suite(Player(players,0)\suite))
  PrintN (Cardface(Player(players,1)\cardface)+" of " +Suite(Player(players,1)\suite))
  PrintN (Cardface(Player(players,2)\cardface)+" of " +Suite(Player(players,2)\suite))
  PrintN ("")
  PrintN ("")
  EndIf;findstring ends here
 EndIf
Next players
EndProcedure

Procedure Check_trio()
For players=0 To #NumPlayers-1
If Not((PlayerInfo(players)\Status="pack")Or(PlayerInfo(players)\Status="")Or(PlayerInfo(players)\Name="null"))

If (Player(players,0)\cardface = Player(players,1)\cardface)And(Player(players,1)\cardface=Player(players,2)\cardface)
Trio_count=Trio_count+1 ; we have a trio increase trio count
 If Trio_count>1 
; we have a second trio! 1st trio would make this 1
; we now need to check if the new trio is bigger than the older
; the old trio winner is in Trio_winner
  If (Player(players,0)\cardface > Player(Trio_winner,0)\cardface)
  Trio_winner=players ; if this player has a higher trio, he wins
  ;there is no other Case here either one is higher in a trio Or lower 
  ; only 4 cards of a cardface
  Else
  Trio_winner=Trio_winner ; else let the old guy continue as the winner
  EndIf
 Else; if no trio seen before make this guy the winner
 Trio_winner=players
 EndIf
  If (#DEBUG)
  PrintN("Trio found for player "+Str(players))
  EndIf
EndIf

EndIf;player name invalid ends here

Next players
EndProcedure

Procedure Check_sequence ()

Dim Winning_sequence(2); this holds the winning sequence
For players=0 To #NumPlayers-1
;we first sort the cards for each player
If Not((PlayerInfo(players)\Status="pack")Or(PlayerInfo(players)\Status="")Or(PlayerInfo(players)\Name="null"))

Dim sorted(2)
is_color=0
sorted(0)=Player(players,0)\cardface
sorted(1)=Player(players,1)\cardface
sorted(2)=Player(players,2)\cardface
;check for sequence, if tie then sequence+color > sequence,else highest sequence
;else higest spade>highest diamond>higest club >highest hearts
If (Player(players,0)\suite=Player(players,1)\suite)And(Player(players,1)\suite=Player(players,2)\suite)
is_color=1
EndIf

   If (#DEBUG)
   PrintN("==============================================================")
   PrintN("Player Name: "+PlayerInfo(players)\Name)
   PrintN("Sequence_count:" +Str(Sequence_count))
   PrintN("Sequence_color_count:" +Str(Sequence_color_count))
   EndIf

  ;note that 12,1,0 And 12,11,0 are valid sequences
  ;we now check for sequences , if we already have a sequence and color, we have to make sure this is also sequence and color
 If (((sorted(0)-1=sorted(1))And(sorted(1)-1=sorted(2)))Or((sorted(0)=12)And(sorted(1)=1)And(sorted(2)=0))Or((sorted(0)=12)And(sorted(1)=11)And(sorted(2)=0)))And(((Sequence_color_count>0)And(is_color=1))Or(Sequence_color_count=0))
  ;so we have a sequence or we have a sequence and color (if we already had a color)
 Sequence_count=Sequence_count+1
  If (is_color=1) ; we have a sequence +color
  Sequence_color_count=Sequence_color_count+1
  EndIf

  If (#DEBUG)
  PrintN("Player " +Str(players) + " has sequence==: ")
  PrintN (Cardface(sorted(0))+" "+ Cardface(sorted(1)) +" " + Cardface(sorted(2)))
  EndIf

  ;if we already have a color and sequence and this is not a color we cannot proceed
  ;if we dont have a color and sequence we can check
  ; if this is a color, then too we can check
   ;if this value is more than 1 this is a second sequence
   
   If ((Sequence_count>1)And(Sequence_color_count=0))Or((Sequence_color_count>1)And(is_color=1))
   ; simple truth table
   ;   sequence count    sequence_color_count is_color
   ;      0                   0                  0  ; we cannot get here as we have to be a sequence
   ;      0                   0                  1  ; ok this is the 1st sequence and color, we short this path and this guy wins
   ;      0                   1                  0  ; not possible as sequence color and sequence go hand in hand
   ;      0                   1                  1  ; not possible same as above
   ;      1                   0                  0  ; we have the 1st sequence, short this path
   ;      1                   0                  1  ; the 1st sequence and color, short this path (though sequence_color_count should be 1)
   ;      1                   1                  0  ; 1st sequence and color, short this path (though is color should be 1)
   ;      1                   1                  1  ; ideally 1st sequence which is also color
   ;      >1                  0                  0  ; 2nd sequence, no color so far, compare
   ;      >1                  0                  1  ; not possible as both sequence_color_count and is_color should be 1
   ;      >1                  1                  0  ; we already have a color and this is not color, dont modify what we have
   ;      >1                  1                  1  ; we already have a seq, we now have a seq+color, short this bit
   ;      >1                  >1                 0  ; we wont get here
   ;      >1                  >1                 1   ; we have a new seq+color, run the check
   ;if we already have a color and sequence and this is not a color we cannot proceed
   ;if we dont have a color and sequence we can check
   ;if this is a color, then too we can check
   ;we can now check this against the Winning_sequence
   
   ;Simple truth table
   ;winning sequence(0)<sorted(0) so the new one is a higher sequence
   ;winning sequence(0)=sorted(0) winning sequence(1)<sorted(1) the new one is a higher sequence
   ;winning sequence(0)=sorted(0) winning sequence(1)=sorted(1) winning_sequence(2)<sorted(2) the new one is a higher sequence
   ;if all 3 cards are the same, highest suite ones
    If Winning_sequence(0)<sorted(0)
   ;if we are here it means we have a higher cardface sequence
    Winning_sequence(0)=sorted(0)
    Winning_sequence(1)=sorted(1)
    Winning_sequence(2)=sorted(2)
    Sequence_winner=players
    If (is_color=1) ; if he has a color he is the winner of sequence+color
    Sequence_color_winner=players
    EndIf
     If (#DEBUG)
     PrintN("Player " +Str(players) + " has displaced player: "+Str(Sequence_winner))
     PrintN (Cardface(sorted(0))+" "+ Cardface(sorted(1)) +" " + Cardface(sorted(2)))
     If (Is_color=1)
     PrintN("and is also a color")
     EndIf
     EndIf

    ElseIf (Winning_sequence(0)=sorted(0))And(Winning_sequence(1)<sorted(1))
   ;if we are here we have a 12,1,0 and a 12,11,0, and we have a 12,11,0
    Winning_sequence(0)=sorted(0)
    Winning_sequence(1)=sorted(1)
    Winning_sequence(2)=sorted(2)

     If (#DEBUG)
     PrintN("Player " +Str(players) + " has displaced player: "+Str(Sequence_winner))
     PrintN (Cardface(sorted(0))+" "+ Cardface(sorted(1)) +" " + Cardface(sorted(2)))
     If (Is_color=1)
     PrintN("and is also a color")
     EndIf
     EndIf

    Sequence_winner=players
    If (is_color=1) ; if he has a color he is the winner of sequence+color
    Sequence_color_winner=players
    EndIf
    
    ElseIf (Winning_sequence(0)=sorted(0))And(Winning_sequence(1)=sorted(1))And(Winning_sequence(2)<sorted(2))
    ;If we are here we have a 12,11,10 And a 12,11,0, And we have a 12,11,10
    Winning_sequence(0)=sorted(0)
    Winning_sequence(1)=sorted(1)
    Winning_sequence(2)=sorted(2)

    If (#DEBUG)
    PrintN("Player " +Str(players) + " has displaced player: "+Str(Sequence_winner))
    PrintN (Cardface(sorted(0))+" "+ Cardface(sorted(1)) +" " + Cardface(sorted(2)))
    If (Is_color=1)
    PrintN("and is also a color")
    EndIf    
    EndIf

    Sequence_winner=players
    If (is_color=1) ; if he has a color he is the winner of sequence+color
    Sequence_color_winner=players
    EndIf
    
    ;if there is a tie in sequence then the highest suite wins
    ElseIf (Winning_sequence(0)=sorted(0))And(Winning_sequence(1)=sorted(1))And(Winning_sequence(2)=sorted(2))
    If Player(Sequence_winner,0)\suite < Player(players,0)\suite
    Winning_sequence(0)=sorted(0)
    Winning_sequence(1)=sorted(1)
    Winning_sequence(2)=sorted(2)

    If (#DEBUG)
    PrintN("Player " +Str(players) + " has displaced player: "+Str(Sequence_winner)+" for higher suite")
    PrintN (Cardface(sorted(0))+" "+ Cardface(sorted(1)) +" " + Cardface(sorted(2))+" of: "+Suite(Player(players,0)\suite)+" versus "+Suite(Player(Sequence_winner,0)\suite))
    If (Is_color=1)
    PrintN("and is also a color")
    EndIf    
    EndIf ; #debug ends

    Sequence_winner=players
    If (is_color=1) ; if he has a color he is the winner of sequence+color
    Sequence_color_winner=players
    EndIf
    
    EndIf ;suite matching ends   
    EndIf ;tie on sequence ends   
   Else ;we get here when sequence_count=1 or sequence_color_count=1; the 1st sequence or the 1st color and sequence
   Winning_sequence(0)=sorted(0)
   Winning_sequence(1)=sorted(1)
   Winning_sequence(2)=sorted(2)  
   Sequence_winner=players
   If (is_color=1) ; if he has a color he is the winner of sequence+color
   Sequence_color_winner=players
   EndIf

   If (#DEBUG)
   PrintN (Cardface(sorted(0))+" "+ Cardface(sorted(1)) +" " + Cardface(sorted(2)))
   If (Is_color=1)
   PrintN("and is also a color")
   EndIf
   EndIf

  EndIf ; sequence comparisions end here
  EndIf ;if sequence ends here
  
  
 EndIf ; player name invalid ends here
 Next players

EndProcedure

Procedure Check_color()
;check for color, if tie then highest card color, if tie then spade>diamond>club>hearts
For players=0 To #NumPlayers-1
If Not((PlayerInfo(players)\Status="pack")Or(PlayerInfo(players)\Status="")Or(PlayerInfo(players)\Name="null"))
  If (#DEBUG)
  PrintN("==============================================================")
  PrintN("Inside check_color")
  EndIf

If (Player(players,0)\suite=Player(players,1)\suite)And(Player(players,1)\suite=Player(players,2)\suite)
;we have a color
;we now need to check if there are other colors and if any other color is higher
Color_count=Color_count+1
  If (Color_count>1)
  ;this is the second color so we check if the older color_winner had a highest 1st card
  ;if tie then 2nd card
  ;if tie then 3rd card
  ;if tie then spade>diamon>club>hearts
  If Player(Color_winner,0)\cardface < Player(players,0)\cardface
  Color_winner=players
  ElseIf (Player(Color_winner,0)\cardface = Player(players,0)\cardface)And(Player(Color_winner,1)\cardface < Player(players,1)\cardface)
  Color_winner=players
  ElseIf (Player(Color_winner,0)\cardface = Player(players,0)\cardface)And(Player(Color_winner,1)\cardface = Player(players,1)\cardface)And(Player(Color_winner,1)\cardface = Player(players,1)\cardface)And(Player(Color_winner,2)\cardface < Player(players,2)\cardface)
  Color_winner=players
  ElseIf (Player(Color_winner,0)\cardface = Player(players,0)\cardface)And(Player(Color_winner,1)\cardface = Player(players,1)\cardface)And(Player(Color_winner,1)\cardface = Player(players,1)\cardface)And(Player(Color_winner,2)\cardface = Player(players,2)\cardface)And(Player(Color_winner,0)\suite<Player(players,0)\suite)
  Color_winner=players
  EndIf
 Else
 ;this is the first color
 Color_winner=players
 EndIf; color_count<1 ends here
EndIf ; is color ends here

EndIf ;player name invalid
Next players

  If (#DEBUG)
  PrintN("==============================================================")
  PrintN("Check_color done")
  EndIf

EndProcedure  

Procedure Check_pair()
Dim Winning_pair(2)
Winning_pair_suite=-1  
;this holds the pair winner cards as paircard1, paircard2, thirdcard and the cardface of the pair
For players=0 To #NumPlayers-1

If Not((PlayerInfo(players)\Status="pack")Or(PlayerInfo(players)\Status="")Or(PlayerInfo(players)\Name="null"))
  If (#DEBUG)
  PrintN("==============================================================")
  PrintN(" Inside Check_pair")
  EndIf
Dim Pair(2)
Pair_suite=-1
;this holds the pair cards as paircard1, paircard2, thirdcard
is_pair=0
If (Player(players,0)\cardface=Player(players,1)\cardface)
  Pair(0)=Player(players,0)\cardface
  Pair(1)=Player(players,1)\cardface
  Pair(2)=Player(players,2)\cardface
  Pair_suite=Player(players,0)\suite
  is_pair=1
ElseIf(Player(players,1)\cardface=Player(players,2)\cardface)
  Pair(0)=Player(players,1)\cardface
  Pair(1)=Player(players,2)\cardface
  Pair(2)=Player(players,0)\cardface
  Pair_suite=Player(players,1)\suite
  is_pair=1
ElseIf(Player(players,0)\cardface=Player(players,2)\cardface)
  Pair(0)=Player(players,0)\cardface
  Pair(1)=Player(players,2)\cardface
  Pair(2)=Player(players,1)\cardface
  Pair_suite=Player(players,0)\suite
  is_pair=1
EndIf;check for pair ends here  
If is_pair=1
;we have a pair
Pair_count=Pair_count+1
  If Pair_count>1
  ;this is the second pair
  ;check if this pair is higher
    If (Winning_pair(0)<Pair(0))
     Winning_pair(0)=Pair(0)
     Winning_pair(1)=Pair(1)
     Winning_pair(2)=Pair(2)
     Winning_pair_suite=Pair_suite
     Pair_winner=players
    ;check 2nd card of pair if 1st is tie
    ElseIf (Winning_pair(0)=Pair(0))And(Winning_pair(1)<Pair(1))
     Winning_pair(0)=Pair(0)
     Winning_pair(1)=Pair(1)
     Winning_pair(2)=Pair(2)
     Winning_pair_suite=Pair_suite
     Pair_winner=players  
    ;else check for higher third card
    ElseIf (Winning_pair(0)=Pair(0))And(Winning_pair(1)=Pair(1))And(Winning_pair(2)<Pair(2))
     Winning_pair(0)=Pair(0)
     Winning_pair(1)=Pair(1)
     Winning_pair(2)=Pair(2)
     Winning_pair_suite=Pair_suite      
     Pair_winner=players
    ;else check for highest card suite spade pair>diamond pair>club pair>hearts pair
    ElseIf (Winning_pair(0)=Pair(0))And(Winning_pair(2)=Pair(2))And(Winning_pair_suite<Pair_suite)
     Winning_pair(0)=Pair(0)
     Winning_pair(1)=Pair(1)
     Winning_pair(2)=Pair(2)
     Winning_pair_suite=Pair_suite      
     Pair_winner=players
   EndIf;check for winning pair ends here
  Else
  ;this is the 1st pair
  Winning_pair(0)=Pair(0)
  Winning_pair(1)=Pair(1)
  Winning_pair(2)=Pair(2)
  Winning_pair_suite=Pair_suite
  Pair_winner=players
  EndIf ; pair_count>1  ends here
EndIf ;is_pair ends here

EndIf;player name check ends here
Next players

  If (#DEBUG)
  PrintN("==============================================================")
  PrintN("Check_pair done")
  EndIf
  
EndProcedure

Procedure Check_highest()
;we start with the assumption that there is no highest winner 
;we set highest_winner=-1
;we then check if the value is -1
;and make the 1st check the winner
;the lowest denomination wins
Highest_winner=-1

For players=0 To #NumPlayers-1
If Not((PlayerInfo(players)\Status="pack")Or(PlayerInfo(players)\Status="")Or(PlayerInfo(players)\Name="null"))
  If (#DEBUG)
  PrintN("==============================================================")
  PrintN("Inside check_highest")
  PrintN(Str(Highest_winner))
  EndIf
 If Highest_winner=-1
 Highest_count=Highest_count+1
 Highest_winner=players
 Else;we already have a higest winner
 If Player(players,0)\cardface > Player(Highest_winner,0)\cardface
 Highest_winner=players
 Highest_count=Highest_count+1
 ElseIf (Player(players,0)\cardface = Player(Highest_winner,0)\cardface)And(Player(players,1)\cardface > Player(Highest_winner,1)\cardface)
 Highest_winner=players
 Highest_count=Highest_count+1
 ElseIf (Player(players,0)\cardface = Player(Highest_winner,0)\cardface)And(Player(players,1)\cardface = Player(Highest_winner,1)\cardface)And(Player(players,2)\cardface>Player(Highest_winner,2)\cardface)
 Highest_winner=players
 Highest_count=Highest_count+1
 ElseIf (Player(players,0)\cardface = Player(Highest_winner,0)\cardface)And(Player(players,1)\cardface = Player(Highest_winner,1)\cardface)And(Player(players,2)\cardface=Player(Highest_winner,2)\cardface)And(Player(players,0)\suite>Player(Highest_winner,0)\suite)
 Highest_winner=players
 Highest_count=Highest_count+1
 EndIf
 EndIf ; Highest winner=-1 ends here

EndIf;player name check ends here 
Next players

  If (#DEBUG)
  PrintN("==============================================================")
  PrintN("Check_highest done")
  PrintN("Highest_count="+Str(Highest_count))
  EndIf
EndProcedure

Procedure Find_winner()
Check_trio()
If Trio_count>0
  
  If (#DEBUG)
  PrintN("Player "+PlayerInfo(Trio_winner)\Name+ " wins trio")
  EndIf
Winner$=PlayerInfo(Trio_winner)\Name
Winner_reason$="Trio"
;check for sequence, if tie then sequence+color > sequence,else highest sequence
;else higest spade>highest diamond>higest club >highest hearts
Else
  Check_sequence()
  If Sequence_count>0
   
   If (#DEBUG)
   PrintN("Player "+PlayerInfo(Sequence_winner)\Name+ " wins sequence")
   EndIf
   
Winner$=PlayerInfo(Sequence_winner)\Name
Winner_reason$="Sequence"
   
;check for color, if tie then highest card color, if tie then spade>diamond>club>hearts
  Else
     Check_color()
     If Color_count>0
     
     If (#DEBUG)
     PrintN("Player "+PlayerInfo(Color_winner)\Name+ " wins color")
     EndIf  
  
Winner$=PlayerInfo(Color_winner)\Name
Winner_reason$="Color"
;now we check for pair
;highest card pair wins
;else if pair is the same cardface, highest third card wins
;else spade pair>diamond pair>club pair>hearts pair
     Else
       Check_pair()
       If Pair_count>0
       
       If (#DEBUG)
       PrintN("Player "+PlayerInfo(Pair_winner)\Name+ " wins pair")
       EndIf
Winner$=PlayerInfo(Pair_winner)\Name         
Winner_reason$="Pair"
;check for highest card/cards i.e. if highest is a tie check 2nd etc., if all are a tie, spade > diamond > club >hearts
       Else
           Check_highest()

           If Highest_count>0
           
           If (#DEBUG)
           PrintN("Player "+PlayerInfo(Highest_winner)\Name+ " wins Highest card")
           EndIf
Winner$=PlayerInfo(Highest_winner)\Name           
Winner_reason$="Highest Card"
           EndIf
       EndIf    
     EndIf  
   EndIf
EndIf
;Endif

EndProcedure


alokdube
Enthusiast
Enthusiast
Posts: 148
Joined: Fri Nov 02, 2007 10:55 am
Location: India
Contact:

3-patti-network-server.pb

Post by alokdube »

Code: Select all

;1. client connects To server
;2. server checks If user id exists
;3. If it exists it tells the client To change the user id
;4. user logs in
;5. other users are updated
;6. other users are passed back To the user
;7. If this is Not a new game at the time of log in, user is in watch mode
;8. at the time of a new deal the player comes in play mode
;9. each event is passed To other users As a change in credits Or cards

;event strings: 
;\r End of this string
;\e End of this event

;sent from server To clients:

;broadcast:


;event:tablecreditschange\r<newvalue>\r\e;credits per move
;event:tableupdate\r
;      <username>\r<suite1>\r<cardface1>\r<suite2>\r<cardface2>\r<suite3>\r<cardface3>\r
;      <username>\r<suite1>\r<cardface1>\r<suite2>\r<cardface2>\r<suite3>\r<cardface3>\r
;      <username>\r<suite1>\r<cardface1>\r<suite2>\r<cardface2>\r<suite3>\r<cardface3>\r
;      <username>\r<suite1>\r<cardface1>\r<suite2>\r<cardface2>\r<suite3>\r<cardface3>\r 
;      <username>\r<suite1>\r<cardface1>\r<suite2>\r<cardface2>\r<suite3>\r<cardface3>\r
;      <username>\r<suite1>\r<cardface1>\r<suite2>\r<cardface2>\r<suite3>\r<cardface3>\r
;      <username>\r<suite1>\r<cardface1>\r<suite2>\r<cardface2>\r<suite3>\r<cardface3>\r
;      <username>\r<suite1>\r<cardface1>\r<suite2>\r<cardface2>\r<suite3>\r<cardface3>\r\e
;each user sees his "view" of the cards of the other players, not everyone else's cards.
;until a show is called when the function Table_show() is executed and everyone sees everyone's cards
;      
;event:userleaves\r<username>\r\e <---this is followed by tableupdate
;event:game_on\r<value>\r\e
;event:servermessage\r<text>\r\e
;event:playercreditschange\r<username>\r<credits>\r\e
;event:playertomovechange\r<username>\r\e
;event:showallowed\r\e
;event:newgame\r\e ; this is used by the clients to initialize themselves
;event:totalcreditsontablechange\r<value>\r\e
;event:awaitingplayers\r\e


;unicast:
;event:usernamereject\r<reason>\r\e
;event:usernameaccept\r\e


;sent from clients To server:

;event:newuser\r<username>\r\e
;event:viewcardsrequest\r<username>\r\e
;event:pack\r<username>\r\e
;event:play\r<username>\r\e
;event:show\r<username>\r\e
;event:creditsraise\r<username>\r<value>\r\e
;event:userleaves\r<username>\r\e
;State Machine
;0. wait For 3 users, game_on=0
;1. user logs in
;2. user list update sent across,cardschange sent across to reflect new users null cards
;3. user count >3 If yes Goto 4, If no Goto 0
;4. start game server message sent, game_on=1
;5. cardschange sent across
;6. If new user joins && game_on=1, send userlist update, user state pack, cardschange
;7. detect events on network: credit raise, pack,play,viewcards, And act accordingly
;8. wait For show, show can be done only If activeplayers=2
;9. If activeplayers=1 player wins, check everytime a user packs/user leaves If activeplayer=1
;10.when show called run find_winner
;11.Declare winner, increase player credits,Goto 4

Procedure Sendgame_on(game_status)
For i=0 To #NumPlayers-1
If Not(PlayerInfo(i)\Name="null")
SendNetworkString(PlayerInfo(i)\ClientID,"event:game_on\r"+Str(game_status)+"\r\e")
EndIf
Next i
EndProcedure

Procedure Reduce_player_credits()
;this is called in the begining of the game only
For i=0 To #NumPlayers-1
If Not(PlayerInfo(i)\Name="null")
PlayerInfo(i)\Credits=PlayerInfo(i)\Credits-(GameCredits)
EndIf
Next i
EndProcedure

Procedure SendShowallowed()
For i=0 To #NumPlayers-1
If Not(PlayerInfo(i)\Name="null")
SendNetworkString(PlayerInfo(i)\ClientID,"event:showallowed\r\e")
EndIf
Next i
EndProcedure

Procedure TotalCreditsonTableChange()
For i=0 To #NumPlayers-1
If Not(PlayerInfo(i)\Name="null")
SendNetworkString(PlayerInfo(i)\ClientID,"event:totalcreditsontablechange\r"+Str(total_credits_in_game)+"\r\e")
EndIf
Next i
EndProcedure

Procedure Start_new_game()
For i=0 To #NumPlayers-1
If Not(PlayerInfo(i)\Name="null")
SendNetworkString(PlayerInfo(i)\ClientID,"event:newgame\r\e")
EndIf
Next i
EndProcedure

Procedure Tablecreditschange()
For i=0 To #NumPlayers-1
If (Not(PlayerInfo(i)\Name="null")And(GameCredits<=8))
 If ((PlayerInfo(i)\Status="seen") Or (PlayerInfo(i)\Status="pack"))
 ;for the players who have seen their cards, the credits per move are twice the blind
 SendNetworkString(PlayerInfo(i)\ClientID,"event:tablecreditschange\r"+Str(GameCredits*2)+"\r\e")
 
 If #DEBUG
  PrintN ("sending tablecreditschange::"+Str(GameCredits*2)+" to player:"+PlayerInfo(i)\Name)
 EndIf
 
 Else
 SendNetworkString(PlayerInfo(i)\ClientID,"event:tablecreditschange\r"+Str(GameCredits)+"\r\e")
 
 If #DEBUG
  PrintN ("sending tablecreditschange::"+Str(GameCredits)+" to player:"+PlayerInfo(i)\Name)
 EndIf 
 
 EndIf
EndIf 
Next i
EndProcedure

Procedure ServerMessage(Servermessage$)
For i=0 To #NumPlayers-1
If Not(PlayerInfo(i)\Name="null")
SendNetworkString(PlayerInfo(i)\ClientID,"event:servermessage\r"+Servermessage$+"\r\e")
EndIf
Next i
EndProcedure

Procedure PlayerCreditsChange()
For i=0 To #NumPlayers-1
If Not(PlayerInfo(i)\Name="null")
SendNetworkString(PlayerInfo(i)\ClientID,"event:playercreditschange\r"+Str(PlayerInfo(i)\Credits)+"\r\e")
EndIf
Next i
EndProcedure

Procedure Playertomovechange(name$)
For i=0 To #NumPlayers-1
If Not(PlayerInfo(i)\Name="null")
SendNetworkString(PlayerInfo(i)\ClientID,"event:playertomovechange\r"+name$+"\r\e")
EndIf
Servermessage(name$+" to play")
Next i
EndProcedure

Procedure Table_show()
For i=0 To #NumPlayers-1
;i is the client to send the string to
;we have to ensure that each player who is seen can see only his cards and sees the rest as 
;blind, unseen of null, so the variable j is used to check the rest of the players w.r.t this player i
If Not(PlayerInfo(i)\Name="null")
tableupdate$="event:tableupdate\r"
 For j=0 To #NumPlayers-1
   ;player name is null no player here
   If (PlayerInfo(j)\Name="null")
   tableupdate$=tableupdate$+PlayerInfo(j)\Name+"\rnull\rnull\rnull\rnull\rnull\rnull\r"
   ;this player is in the table but not playing
   ElseIf (PlayerInfo(j)\Status="")
   tableupdate$=tableupdate$+PlayerInfo(j)\Name+"\rnull\rnull\rnull\rnull\rnull\rnull\r" 
   ElseIf (PlayerInfo(j)\Status="seen")Or(PlayerInfo(j)\Status="blind")Or(PlayerInfo(j)\Status="pack")
   tableupdate$=tableupdate$+PlayerInfo(j)\Name+"\r"
   tableupdate$=tableupdate$+Suite(Player(j,0)\suite)+"\r"+Cardface(Player(j,0)\cardface)+"\r"
   tableupdate$=tableupdate$+Suite(Player(j,1)\suite)+"\r"+Cardface(Player(j,1)\cardface)+"\r"
   tableupdate$=tableupdate$+Suite(Player(j,2)\suite)+"\r"+Cardface(Player(j,2)\cardface)+"\r"
   EndIf
 Next j
 tableupdate$=tableupdate$+"\e"
 SendNetworkString(PlayerInfo(i)\ClientID,tableupdate$) 
EndIf
Next i

EndProcedure

Procedure Table_update()
;1st we compose the message

For i=0 To #NumPlayers-1
;i is the client to send the string to
;we have to ensure that each player who is seen can see only his cards and sees the rest as 
;blind, unseen of null, so the variable j is used to check the rest of the players w.r.t this player i
If Not(PlayerInfo(i)\Name="null")
tableupdate$="event:tableupdate\r"
 For j=0 To #NumPlayers-1
  If Not(j=i)
   ;player name is null no player here
   If (PlayerInfo(j)\Name="null")
   tableupdate$=tableupdate$+PlayerInfo(j)\Name+"\rnull\rnull\rnull\rnull\rnull\rnull\r"
   
   
   
   ;this player is in the table but not playing
   ElseIf (PlayerInfo(j)\Status="")
   tableupdate$=tableupdate$+PlayerInfo(j)\Name+"\rnull\rnull\rnull\rnull\rnull\rnull\r"  
   
   ;this player is in the table but has packed.
   ElseIf (PlayerInfo(j)\Status="pack")
   tableupdate$=tableupdate$+PlayerInfo(j)\Name+"\rpack\rpack\rpack\rpack\rpack\rpack\r"  
   
   ;this player is in the table but is blind
   ElseIf (PlayerInfo(j)\Status="blind")
   tableupdate$=tableupdate$+PlayerInfo(j)\Name+"\rblind\rblind\rblind\rblind\rblind\rblind\r"
   
   ;this player has seen his cards but we cant show it to player(i)
   ElseIf (PlayerInfo(j)\Status="seen")
   tableupdate$=tableupdate$+PlayerInfo(j)\Name+"\rseen\rseen\rseen\rseen\rseen\rseen\r"
   EndIf
  Else ;(if j=i ) 
  
   
   ;this player is in the table but not playing
   If (PlayerInfo(j)\Status="")
   tableupdate$=tableupdate$+PlayerInfo(i)\Name+"\rnull\rnull\rnull\rnull\rnull\rnull\r"  
   
   ;this player is in the table but has packed.
   ElseIf (PlayerInfo(j)\Status="pack")
   tableupdate$=tableupdate$+PlayerInfo(i)\Name+"\rpack\rpack\rpack\rpack\rpack\rpack\r"  
   
   ;this player is in the table but is blind
   ElseIf (PlayerInfo(j)\Status="blind")
   tableupdate$=tableupdate$+PlayerInfo(i)\Name+"\rblind\rblind\rblind\rblind\rblind\rblind\r"
   ElseIf (PlayerInfo(j)\Status="seen")
   tableupdate$=tableupdate$+PlayerInfo(i)\Name+"\r"
   tableupdate$=tableupdate$+Suite(Player(i,0)\suite)+"\r"+Cardface(Player(i,0)\cardface)+"\r"
   tableupdate$=tableupdate$+Suite(Player(i,1)\suite)+"\r"+Cardface(Player(i,1)\cardface)+"\r"
   tableupdate$=tableupdate$+Suite(Player(i,2)\suite)+"\r"+Cardface(Player(i,2)\cardface)+"\r"
   EndIf
  EndIf
  Next j

;now once we have the tableupdate ready we send it to client(i)
tableupdate$=tableupdate$+"\e"
If (#DEBUG)
 PrintN("Name:"+PlayerInfo(i)\Name)
 PrintN("tableupdate"+tableupdate$)
EndIf

 SendNetworkString(PlayerInfo(i)\ClientID,tableupdate$)
EndIf; if username is null we dont care to send anything 
Next i
 
EndProcedure

Procedure Start_Network_Server()
If InitNetwork() = 0

   If (#DEBUG)
    PrintN ("Error: Can't initialize the network !")
   End
  EndIf

EndIf;InitNetwork ends here


If CreateNetworkServer(0, #Port)

 If (#DEBUG)
  PrintN("Server created (Port "+Str(Port)+").") 
 EndIf

Else

 If (#DEBUG)
  PrintN("Could not create server on Port:"+Str(Port))
 EndIf

EndIf
 



  EndProcedure
  
Procedure Check_message_received()
     
    SEvent = NetworkServerEvent()
If SEvent
     ClientID = EventClient()
  
Select SEvent
Case #PB_NetworkEvent_Connect
       If (#Debug)
        PrintN("A new client has connected :"+ReverseDnsQuery(GetClientIP(ClientID)))
       EndIf
       
Case #PB_NetworkEvent_Data
        Buffer = AllocateMemory(5000)
        Message$=""
        message_len=0
        Event$=""
        ReceiveNetworkData(ClientID, Buffer, 5000)
        Message$=PeekS(Buffer)
        FreeMemory(Buffer)
        message_len=Len(Message$)
        ;we need to seperate on "\r" boundaries here
        If (#Debug)
          PrintN("Client:"+ReverseDnsQuery(GetClientIP(ClientID))+" has sent a message !")
          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
                






If Event$="newuser"
             Username$=Attribute(1)
             If (#DEBUG)
               PrintN("New user name "+Username$+" has opened a connexion from:"+ReverseDnsQuery(GetClientIP(ClientID)))
             EndIf
             player_position=Find_empty_player_position()
           If (User_name_exists(Username$)Or(player_position=-1))
             SendNetworkString(ClientID,"event:usernamereject\r"+Username$+" already exists\r\e")
              If (#Debug)
               PrintN("username:"+Username$+" rejected")
              EndIf  
           Else
             PlayerInfo(player_position)\Name=Username$
             PlayerInfo(player_position)\Status=""
             PlayerInfo(player_position)\Credits=100
             PlayerInfo(player_position)\ClientID=ClientID
             PlayerInfo(player_position)\DomainName=ReverseDnsQuery(GetClientIP(ClientID))
             Actual_players=Actual_players+1
             If #DEBUG
             PrintN("Name:"+username$+"   ClientID"+Str(ClientID)+ " player_position"+Str(player_position))
             EndIf
        If Not(Actual_players=0)
             Table_update()
             PlayerCreditsChange()
             Tablecreditschange() 
             TotalCreditsonTableChange()
             ;this user does not exist so let us add this user to the table and
             ;send a tableupdate
            If (game_on=0)
             servermessage$="Last Event: "+username$+" has connected from "+ReverseDnsQuery(GetClientIP(ClientID))
             If Actual_players<#MinPlayersperGame
              servermessage$=servermessage$+"..awaiting at least "+Str(#minPlayersperGame)+" players to start game"
             EndIf
                         
             ServerMessage (servermessage$)
            Else
            SendNetworkString(PlayerInfo(player_position)\ClientID,"event:servermessage\rThe game is on, please wait till the next game\r\e")
            EndIf
           EndIf
        EndIf
   
   
   
   
             
             
             
ElseIf Event$="userleaves"
          username$=Attribute(1)
            If (#DEBUG)
             PrintN(username$+" has closed the connexion...")
            EndIf
          playerid=Player_id_from_name(username$)
          SendNetworkString(PlayerInfo(playerid)\ClientID,"event:userleaveaccepted\r\e")  
          servermessage$="Last Event: "+username$+" has disconnected from "+PlayerInfo(playerid)\DomainName
          CloseNetworkConnection(PlayerInfo(playerid)\ClientID)

          
          ;if this player is playing the game, 
          ;we have To deduct the number of players in the game
           If Not(PlayerInfo(playerid)\Status="")
              players_in_game=players_in_game-1
           ;   If players_in_game=1
           ;   show_called=1
           ;   EndIf              
           EndIf                    
          PlayerInfo(playerid)\Name="null"
          PlayerInfo(playerid)\Status=""
          PlayerInfo(playerid)\Credits=0
          PlayerInfo(playerid)\DomainName=""
          Actual_players=Actual_players-1
        If Not(Actual_players=0)
          If playertomove_id=playerid
             playertomove_id=Increment_playertomove_id(playertomove_id)
          EndIf
          Playertomovechange(PlayerInfo(playertomove_id)\Name)
          If (#DEBUG)
             PrintN("player to move change: "+PlayerInfo(playertomove_id)\Name)
          EndIf              
          
          
             Table_update()
             PlayerCreditsChange()
             Tablecreditschange() 
             TotalCreditsonTableChange()
             ServerMessage (servermessage$)
        EndIf       
   
   
   
   
   
   
   
   
   
   
          
ElseIf Event$="viewcardsrequest"
          username$=Attribute(1)
          playerid=Player_id_from_name(username$)
          ;PlayerInfo(playerid)\Status="seen"
            If (#DEBUG)
             PrintN(username$+" has sent a viewcardsrequest...")
            EndIf    
           
       If Not(Actual_players=0) And (game_on=1)
           PlayerInfo(playerid)\Status="seen"
            servermessage$="Last Event: "+username$+" has seen the cards" 
             Table_update()
             PlayerCreditsChange()
             Tablecreditschange() 
             TotalCreditsonTableChange()
             TotalCreditsonTableChange()
             ServerMessage (servermessage$)
       EndIf





          
          
ElseIf Event$="pack"
          username$=Attribute(1)
          playerid=Player_id_from_name(username$)
          PlayerInfo(playerid)\Status="pack"
            If (#DEBUG)
             PrintN(username$+" has packed...")
            EndIf
          ;user has packed, now reduce number of players in game
          ;increment the player to move
          players_in_game=players_in_game-1
          If playertomove_id=playerid
             playertomove_id=Increment_playertomove_id(playertomove_id)
          EndIf
          ;If players_in_game=1
          ;show_called=1
          ;EndIf
        If Not(Actual_players=0) And (game_on=1)
          Playertomovechange(PlayerInfo(playertomove_id)\Name)
          
          servermessage$="Last Event: "+username$+" has packed the cards, "+PlayerInfo(playertomove_id)\Name+" to play"  
             Table_update()
             PlayerCreditsChange()
             Tablecreditschange() 
             TotalCreditsonTableChange()             
             ServerMessage (servermessage$)
        EndIf




          
          
ElseIf Event$="play"
          username$=Attribute(1)
          playerid=Player_id_from_name(username$)
            If (#DEBUG)
              PrintN(username$+"has clicked play")
            EndIf          
          ;we have to reduce this users credits by GameCredits*2 or GameCredits
          ;depending on if he is blind or seen
          ;we have to increase total_credits_in_game  
      If Not(Actual_players=0) And (game_on=1)                            
         If PlayerInfo(playerid)\Status="seen"
             PlayerInfo(playerid)\Credits=PlayerInfo(playerid)\Credits-(GameCredits*2)
             total_credits_in_game=total_credits_in_game+GameCredits*2
         ElseIf PlayerInfo(playerid)\Status="blind"
             PlayerInfo(playerid)\Credits=PlayerInfo(playerid)\Credits-(GameCredits)
             total_credits_in_game=total_credits_in_game+GameCredits
         EndIf             
           ;we have to update the user to move
         If playertomove_id=playerid
             playertomove_id=Increment_playertomove_id(playertomove_id)
         EndIf
          Playertomovechange(PlayerInfo(playertomove_id)\Name)
          ;update the table, not needed just good housekeeping
          servermessage$="Last Event: "+username$+" has finished playing, "+PlayerInfo(playertomove_id)\Name+" to play  now"
             Table_update()
             PlayerCreditsChange()
             Tablecreditschange() 
             TotalCreditsonTableChange()
             ServerMessage (servermessage$)
       EndIf      




          
        
ElseIf Event$="creditsraise"
         username$=Attribute(1)
         ;GameCredits=Val(Attribute(2))
           If (#DEBUG)
              PrintN("credits raised by "+username$+" to "+Str(GameCredits))
           EndIf
         If Not(Actual_players=0) And (game_on=1)
             GameCredits=Val(Attribute(2))
             Table_update()
             PlayerCreditsChange()
             Tablecreditschange() 
             TotalCreditsonTableChange()
             ServerMessage (servermessage$)
         EndIf    







             
ElseIf Event$="show"
         username$=Attribute(1)
         playerid=Player_id_from_name(username$)
             If (#DEBUG)
               PrintN("User: "+username$+" has asked for a show")
             EndIf           
           If (show_allowed=1)
           show_called=1
         If PlayerInfo(playerid)\Status="seen"
             PlayerInfo(playerid)\Credits=PlayerInfo(playerid)\Credits-(GameCredits*2)
             total_credits_in_game=total_credits_in_game+GameCredits*2
         ElseIf PlayerInfo(playerid)\Status="blind"
             PlayerInfo(playerid)\Credits=PlayerInfo(playerid)\Credits-(GameCredits)
             total_credits_in_game=total_credits_in_game+GameCredits
         EndIf
           servermessage$="Last Event: "+username$+" has requested a show"
       If Not(Actual_players=0) And (game_on=1)
           ServerMessage (servermessage$)
           PlayerCreditsChange()
           Tablecreditschange() 
           TotalCreditsonTableChange()
           Else
           servermessage$="Last Event: "+username$+" has requested a show, however a show is allowed when there are only 2 players left"            
           EndIf
           ServerMessage (servermessage$)
       EndIf
         EndIf;this is where event$ demuxing ends
        ;Case #PB_NetworkEvent_File
        ;If (#DEBUG)
        ;  MessageRequester("PureBasic - Server", "Client "+Str(ClientID)+" has send a file via the network !", 0)
        ;  ReceiveNetworkFile(ClientID, "C:\TEST_Network.ftp3")
        ;EndIf
      Message$=remainingmessage$ 
       If (#Debug)
        PrintN("Message left:"+Message$)
       EndIf
Until remainingmessage$=""



        
Case #PB_NetworkEvent_Disconnect
        playerid=Player_id_from_ClientID(ClientID)
        username$=PlayerInfo(playerid)\Name
        If (#DEBUG)
          PrintN(username$+" has closed the connexion...")
        EndIf
          ;if this player is playing the game, 
          ;we have To deduct the number of players in the game        
           If Not(PlayerInfo(playerid)\Status="")
              players_in_game=players_in_game-1
              ;If players_in_game=1
              ;show_called=1
              ;EndIf
           EndIf
          PlayerInfo(playerid)\Name="null"
          PlayerInfo(playerid)\Status=""
          PlayerInfo(playerid)\Credits=0
          PlayerInfo(playerid)\DomainName=""
          Actual_players=Actual_players-1
        
        If Not(Actual_players=0)
          Table_update()
          servermessage$="Last Event: "+username$+" has disconnected from "+ReverseDnsQuery(GetClientIP(ClientID))
          If playertomove_id=playerid
             playertomove_id=Increment_playertomove_id(playertomove_id)
          EndIf
          Playertomovechange(PlayerInfo(playertomove_id)\Name)
          If (#DEBUG)
             PrintN("player to move change: "+PlayerInfo(playertomove_id)\Name)
          EndIf              
          ServerMessage (servermessage$)
        EndIf
         
EndSelect
EndIf

EndProcedure
alokdube
Enthusiast
Enthusiast
Posts: 148
Joined: Fri Nov 02, 2007 10:55 am
Location: India
Contact:

Post by alokdube »

images are on the links posted in the 1st post
Post Reply