home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8vbsdk.exe / samples / multimedia / vbsamples / directplay / dxvbmessenger / server / moddbase.bas < prev    next >
Encoding:
BASIC Source File  |  2000-10-02  |  12.9 KB  |  298 lines

  1. Attribute VB_Name = "modDBase"
  2. Option Explicit
  3. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  4. '
  5. '  Copyright (C) 2000 Microsoft Corporation.  All Rights Reserved.
  6. '
  7. '  File:       modDBase.bas
  8. '
  9. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  10.  
  11. Private Type FriendOnlineType
  12.     sFriendName As String
  13.     fOnline As Boolean
  14.     fFriend As Boolean
  15. End Type
  16.  
  17. Public Enum LogonTypes
  18.     LogonSuccess
  19.     InvalidPassword
  20.     AccountDoesNotExist
  21. End Enum
  22.  
  23. Private Const msDoubleQuote As String = """"
  24. Public goConn As Connection
  25.  
  26. Public Sub OpenClientDatabase()
  27.  
  28.     On Error GoTo ErrOut
  29.     Dim sMedia As String
  30.     
  31.     sMedia = FindMediaDir("vbMsgSrv.mdb")
  32.     'Create a new ADO Connection
  33.     Set goConn = New Connection
  34.     'Open the database
  35.     goConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sMedia & "vbMsgSrv.mdb;Mode=Read|Write"
  36.     Exit Sub
  37. ErrOut:
  38.     MsgBox "Could not connect to the server database.  Exiting", vbOKOnly Or vbInformation, "Exiting."
  39.     End
  40. End Sub
  41.  
  42. Public Sub CloseDownDB()
  43.     goConn.Close
  44. End Sub
  45.  
  46. 'Check to see if this user already exists.  If they do, then we can't create a new account
  47. 'with this username.
  48. Public Function DoesUserExist(ByVal sUsername As String) As Boolean
  49.  
  50.     Dim myRs As New Recordset
  51.     
  52.     myRs.Open "Select * from ClientInfo where ClientName = " & msDoubleQuote & sUsername & msDoubleQuote, goConn
  53.     If Not myRs.EOF Then 'This user did exist
  54.         DoesUserExist = True
  55.         Exit Function
  56.     End If
  57.     DoesUserExist = False
  58.     
  59. End Function
  60.  
  61. Public Function LogonUser(ByVal sUsername As String, sPwd As String) As LogonTypes
  62.  
  63.     Dim myRs As New Recordset
  64.     Dim sPassword As String
  65.     
  66.     myRs.Open "Select * from ClientInfo where ClientName = " & msDoubleQuote & sUsername & msDoubleQuote, goConn
  67.     If myRs.EOF Then 'This user did not exist
  68.         LogonUser = AccountDoesNotExist
  69.         Exit Function
  70.     End If
  71.     'Ok, this user does exist.  First lets decrypt the password sent from the client
  72.     sPassword = EncodePassword(sPwd, glClientSideEncryptionKey)
  73.     'Now check this password against what's listed in the db.
  74.     If myRs.Fields("ClientPassword").Value = EncodePassword(sPassword, glServerSideEncryptionKey) Then
  75.         'The passwords match, logon was successful
  76.         LogonUser = LogonSuccess
  77.         Exit Function
  78.     Else
  79.         'Invalid password, let the user know
  80.         LogonUser = InvalidPassword
  81.         Exit Function
  82.     End If
  83. End Function
  84.  
  85. Public Sub AddUser(ByVal sUsername As String, sPwd As String, ByVal lCurrentDPlayID As Long)
  86.     Dim sPassword As String
  87.     Dim sSql As String
  88.     
  89.     'First decrypt the password
  90.     sPassword = EncodePassword(sPwd, glClientSideEncryptionKey)
  91.     'Add this user, and set the flag to show that we are currently logged on, and keep our current DPlay ID
  92.     sSql = "Insert Into ClientInfo (ClientName, ClientPassword, CurrentlyLoggedOn, CurrentDPlayID) Values "
  93.     sSql = sSql & " (" & msDoubleQuote & sUsername & msDoubleQuote & ", " & msDoubleQuote & EncodePassword(sPassword, glServerSideEncryptionKey) & msDoubleQuote
  94.     sSql = sSql & ", 1, " & CStr(lCurrentDPlayID) & ")"
  95.     'Now perform the action
  96.     goConn.Execute sSql
  97. End Sub
  98.  
  99. Public Sub UpdateDBToShowLogon(sPlayer As String, ByVal lCurrentDPlayID As Long)
  100.     'Set the flag to show that we are currently logged on, and keep our current DPlay ID
  101.     goConn.Execute "Update ClientInfo Set CurrentlyLoggedOn=1, CurrentDplayID=" & CStr(lCurrentDPlayID) & " where ClientName = " & msDoubleQuote & sPlayer & msDoubleQuote
  102. End Sub
  103.  
  104. Public Sub UpdateDBToShowLogoff(ByVal lCurrentDPlayID As Long)
  105.     'Set the flag to show that we are currently logged on, and keep our current DPlay ID
  106.     goConn.Execute "Update ClientInfo Set CurrentlyLoggedOn=0, CurrentDplayID=0 where CurrentDplayID = " & CStr(lCurrentDPlayID)
  107. End Sub
  108.  
  109. Public Sub NotifyFriendsImOnline(sPlayer As String)
  110.     Dim myRs As New Recordset, lMsg As Long
  111.     Dim oBuf() As Byte, lOffset As Long
  112.     Dim sMyClientID As String
  113.     Dim myTempRS As New Recordset
  114.     
  115.     'First we need to find out if I'm on anyones friends list
  116.     'Get my ClientID first
  117.     myRs.Open "Select * from ClientInfo where ClientName = " & msDoubleQuote & sPlayer & msDoubleQuote, goConn
  118.     'We need to get the clientid of this person
  119.     sMyClientID = myRs.Fields("ClientID").Value
  120.     myRs.Close
  121.     'Now see if I'm anyone's friends
  122.     myRs.Open "Select * from FriendList where FriendID = " & sMyClientID, goConn
  123.     Do While Not myRs.EOF 'Yup, I am.  Notify each of them that I just logged on
  124.         'First check to see if they are logged on
  125.         myTempRS.Open "Select * from ClientInfo where ClientID = " & CStr(myRs.Fields("ClientID").Value), goConn
  126.         If myTempRS.Fields("CurrentDPlayID").Value <> 0 Then 'Notify this person I'm online
  127.             lMsg = Msg_FriendLogon
  128.             lOffset = NewBuffer(oBuf)
  129.             AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  130.             AddStringToBuffer oBuf, sPlayer, lOffset
  131.             dps.SendTo CLng(myTempRS.Fields("CurrentDPlayID").Value), oBuf, 0, 0
  132.         End If
  133.         myTempRS.Close
  134.         myRs.MoveNext
  135.     Loop
  136.     
  137. End Sub
  138.  
  139. Public Sub NotifyFriendsImOffline(sPlayer As String)
  140.     Dim myRs As New Recordset, lMsg As Long
  141.     Dim oBuf() As Byte, lOffset As Long
  142.     Dim sMyClientID As String
  143.     Dim myTempRS As New Recordset
  144.     
  145.     'First we need to find out if I'm on anyones friends list
  146.     'Get my ClientID first
  147.     myRs.Open "Select * from ClientInfo where ClientName = " & msDoubleQuote & sPlayer & msDoubleQuote, goConn
  148.     'We need to get the clientid of this person
  149.     sMyClientID = myRs.Fields("ClientID").Value
  150.     myRs.Close
  151.     'Now see if I'm anyone's friends
  152.     myRs.Open "Select * from FriendList where FriendID = " & sMyClientID, goConn
  153.     Do While Not myRs.EOF 'Yup, I am.  Notify each of them that I just logged on
  154.         'First check to see if they are logged on
  155.         myTempRS.Open "Select * from ClientInfo where ClientID = " & CStr(myRs.Fields("ClientID").Value), goConn
  156.         If myTempRS.Fields("CurrentDPlayID").Value <> 0 Then 'Notify this person I'm online
  157.             lMsg = Msg_FriendLogoff
  158.             lOffset = NewBuffer(oBuf)
  159.             AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  160.             AddStringToBuffer oBuf, sPlayer, lOffset
  161.             dps.SendTo CLng(myTempRS.Fields("CurrentDPlayID").Value), oBuf, 0, 0
  162.         End If
  163.         myTempRS.Close
  164.         myRs.MoveNext
  165.     Loop
  166.     
  167. End Sub
  168.  
  169. Public Sub GetFriendsOfMineOnline(sPlayer As String)
  170.     Dim myRs As New Recordset, lMsg As Long
  171.     Dim oBuf() As Byte, lOffset As Long
  172.     Dim sMyClientID As String
  173.     Dim myTempRS As New Recordset
  174.     
  175.     Dim lSendID As Long
  176.     Dim oFriends() As FriendOnlineType
  177.     Dim lCount As Long
  178.     
  179.     'First we need to find out if I'm on anyones friends list
  180.     'Get my ClientID first
  181.     LogInfo "(GetFriends) SQL =Select * from ClientInfo where ClientName = " & msDoubleQuote & sPlayer & msDoubleQuote
  182.     myRs.Open "Select * from ClientInfo where ClientName = " & msDoubleQuote & sPlayer & msDoubleQuote, goConn
  183.     'We need to get the clientid of this person
  184.     sMyClientID = myRs.Fields("ClientID").Value
  185.     lSendID = CLng(myRs.Fields("CurrentDPlayID").Value)
  186.     myRs.Close
  187.     'Now see if I have any friends
  188.     LogInfo "(GetFriends) Friends SQL =Select * from FriendList where ClientID = " & sMyClientID
  189.     myRs.Open "Select * from FriendList where ClientID = " & sMyClientID, goConn
  190.     lMsg = Msg_SendClientFriends
  191.     lOffset = NewBuffer(oBuf)
  192.     AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  193.     ReDim oFriends(0)
  194.     Do While Not myRs.EOF 'Yup, I do.  Send me the list first
  195.         'First check to see if they are logged on
  196.         ReDim Preserve oFriends(UBound(oFriends) + 1)
  197.         myTempRS.Open "Select * from ClientInfo where ClientID = " & CStr(myRs.Fields("FriendID").Value), goConn
  198.         With oFriends(UBound(oFriends))
  199.             .sFriendName = myTempRS.Fields("ClientName").Value
  200.             .fOnline = (myTempRS.Fields("CurrentDPlayID").Value <> 0)
  201.             .fFriend = myRs.Fields("Friend").Value
  202.         End With
  203.         myTempRS.Close
  204.         myRs.MoveNext
  205.     Loop
  206.     myRs.Close
  207.     AddDataToBuffer oBuf, CLng(UBound(oFriends)), SIZE_LONG, lOffset
  208.     For lCount = 1 To UBound(oFriends)
  209.         AddDataToBuffer oBuf, oFriends(lCount).fFriend, LenB(oFriends(lCount).fFriend), lOffset
  210.         AddStringToBuffer oBuf, oFriends(lCount).sFriendName, lOffset
  211.     Next
  212.     dps.SendTo lSendID, oBuf, 0, 0
  213.     For lCount = 1 To UBound(oFriends)
  214.         If oFriends(lCount).fOnline Then
  215.             ReDim oBuf(0)
  216.             lMsg = Msg_FriendLogon
  217.             lOffset = NewBuffer(oBuf)
  218.             AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  219.             AddStringToBuffer oBuf, oFriends(lCount).sFriendName, lOffset
  220.             dps.SendTo lSendID, oBuf, 0, 0
  221.         End If
  222.     Next
  223.     
  224. End Sub
  225.  
  226. 'If fFriend is True, then this person is a friend.  If it is False, then the person is blocked
  227. Public Function AddFriend(ByVal lPlayerID As Long, sFriendName As String, ByVal fFriend As Boolean) As Boolean
  228.  
  229.     Dim myRs As New Recordset
  230.     Dim sFriendClient As String, sMyClientID As String
  231.     
  232.     AddFriend = False
  233.     LogInfo "(AddFriend) In Function: SQL= Select * from ClientInfo where ClientName = " & msDoubleQuote & sFriendName & msDoubleQuote
  234.     myRs.Open "Select * from ClientInfo where ClientName = " & msDoubleQuote & sFriendName & msDoubleQuote, goConn
  235.     'We need to get the clientid of this friend
  236.     sFriendClient = myRs.Fields("ClientID").Value
  237.     If myRs.Fields("CurrentDPlayID").Value <> 0 Then AddFriend = True
  238.     myRs.Close
  239.     LogInfo "(AddFriend) Got ClientID of Friend:"
  240.     
  241.     LogInfo "(AddFriend) Get ClientID of me:"
  242.     myRs.Open "Select * from ClientInfo where CurrentDPlayID = " & CStr(lPlayerID), goConn
  243.     'We need to get the clientid of this person
  244.     sMyClientID = myRs.Fields("ClientID").Value
  245.     myRs.Close
  246.     LogInfo "(AddFriend) Got ClientID of me:"
  247.     
  248.     'First we need to check if this user is already a friend or blocked, and if so, just update them
  249.     LogInfo "(AddFriend) Is this a friend already?"
  250.     myRs.Open "Select count(ClientID) from FriendList where ((ClientID = " & sMyClientID & ") and (FriendID = " & sFriendClient & "))"
  251.     If myRs.Fields(0).Value = 0 Then 'No one, add this one
  252.         'Now add this friend to the list
  253.         LogInfo "(AddFriend) No, add:"
  254.         goConn.Execute "Insert into FriendList values (" & sMyClientID & "," & sFriendClient & ", " & CStr(Abs(fFriend)) & ")"
  255.     Else
  256.         'Update the record that already exists
  257.         LogInfo "(AddFriend) Yes, update: SQL = Update FriendList Set Friend = " & CStr(Abs(fFriend)) & " where ((ClientID = " & sMyClientID & ") and (FriendID = " & sFriendClient & "))"
  258.         goConn.Execute "Update FriendList Set Friend = " & CStr(Abs(fFriend)) & " where ((ClientID = " & sMyClientID & ") and (FriendID = " & sFriendClient & "))"
  259.     End If
  260. End Function
  261.  
  262. Public Function GetCurrentDPlayID(sPlayer As String) As Long
  263.     Dim myRs As New Recordset
  264.     
  265.     'Get my ClientID first
  266.     myRs.Open "Select * from ClientInfo where ClientName = " & msDoubleQuote & sPlayer & msDoubleQuote, goConn
  267.     'We need to get the current dplay id of this person
  268.     GetCurrentDPlayID = CLng(myRs.Fields("CurrentDPlayID").Value)
  269.     myRs.Close
  270. End Function
  271.  
  272. Public Function AmIBlocked(sMe As String, sFriend As String) As Boolean
  273.     Dim myRs As New Recordset
  274.     Dim sMyClientID As String
  275.     Dim sFriendClientID As String
  276.     Dim myTempRS As New Recordset
  277.     
  278.     'Get my ClientID first
  279.     LogInfo "(AmIBlocked) SQL =Select * from ClientInfo where ClientName = " & msDoubleQuote & sMe & msDoubleQuote
  280.     myRs.Open "Select * from ClientInfo where ClientName = " & msDoubleQuote & sMe & msDoubleQuote, goConn
  281.     'We need to get the clientid of this person
  282.     sMyClientID = myRs.Fields("ClientID").Value
  283.     myRs.Close
  284.     'Now get my FriendsID
  285.     LogInfo "(AmIBlocked) SQL =Select * from ClientInfo where ClientName = " & msDoubleQuote & sFriend & msDoubleQuote
  286.     myRs.Open "Select * from ClientInfo where ClientName = " & msDoubleQuote & sFriend & msDoubleQuote, goConn
  287.     'We need to get the clientid of this person
  288.     sFriendClientID = myRs.Fields("ClientID").Value
  289.     myRs.Close
  290.     
  291.     LogInfo "(AmIBlocked) SQL = Select * from FriendList where ClientID = " & sMyClientID & " and FriendID = " & sFriendClientID
  292.     myRs.Open "Select * from FriendList where ClientID = " & sMyClientID & " and FriendID = " & sFriendClientID, goConn
  293.     On Error Resume Next
  294.     AmIBlocked = Not (myRs.Fields("Friend").Value)
  295.     myRs.Close
  296.     LogInfo "(AmIBlocked) Leaving: Retval = " & CStr(AmIBlocked)
  297. End Function
  298.