home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectPlay / DXVBMessenger / Server / modDBase.bas < prev    next >
Encoding:
BASIC Source File  |  2001-10-08  |  18.5 KB  |  458 lines

  1. Attribute VB_Name = "modDBase"
  2. Option Explicit
  3. Option Compare Text
  4. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  5. '
  6. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  7. '
  8. '  File:       modDBase.bas
  9. '
  10. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  11.  
  12. Private Type FriendOnlineType
  13.     sFriendName As String
  14.     fOnline As Boolean
  15.     fFriend As Boolean
  16. End Type
  17.  
  18. Public Enum LogonTypes
  19.     LogonSuccess
  20.     InvalidPassword
  21.     AccountDoesNotExist
  22. End Enum
  23.  
  24. Public goDOM As DOMDocument 'The main xml document
  25.  
  26. Public Sub OpenClientDatabase()
  27.  
  28.     On Error GoTo ErrOut
  29.     Dim sMedia As String
  30.     
  31.     sMedia = AddDirSep(GetDXSampleFolder) & "vbsamples\media\vbMsgSrv.xml"
  32.     Set goDOM = New DOMDocument
  33.     'Create a new XML Doc
  34.     goDOM.async = False
  35.     goDOM.Load sMedia 'This will load the xml document, if it's available, otherwise it will create a new one
  36.     If Not goDOM.hasChildNodes Then 'This is an empty XML file
  37.         MsgBox "The default XML data structure could not be found.  Creating a new one.", vbOKOnly Or vbInformation, "New XML."
  38.         'Create our default file
  39.         CreateDefaultXMLStructure
  40.     End If
  41.     Exit Sub
  42. ErrOut:
  43.     MsgBox "There was an error trying to load the XML file.  Creating a new one.", vbOKOnly Or vbInformation, "New XML."
  44.     Set goDOM = Nothing
  45.     Set goDOM = New DOMDocument
  46.     'Create our default file
  47.     CreateDefaultXMLStructure
  48. End Sub
  49.  
  50. Public Sub CloseDownDB()
  51.     MarkEveryoneLoggedOff
  52.     SaveXMLStructure
  53.     Set goDOM = Nothing
  54. End Sub
  55.  
  56. 'Check to see if this user already exists.  If they do, then we can't create a new account
  57. 'with this username.
  58. Public Function DoesUserExist(ByVal sUserName As String) As Boolean
  59.     'Let's see if this user exists
  60.     Dim oNode As IXMLDOMNode
  61.     Dim oNodes As IXMLDOMNodeList
  62.     
  63.     'Get a list of all client names
  64.     Set oNodes = goDOM.selectNodes("MessengerServerDB/ClientInfo/Row/ClientName")
  65.     For Each oNode In oNodes
  66.         'See if we are in that list
  67.         'We use the childnodes(0) since the only member of the 'ClientName' node
  68.         'is the text that contains the name
  69.         If oNode.childNodes(0).nodeTypedValue = sUserName Then
  70.             DoesUserExist = True
  71.             Exit Function
  72.         End If
  73.     Next
  74.     DoesUserExist = False
  75. End Function
  76.  
  77. Public Function LogonUser(ByVal sUserName As String, ByVal sPwd As String) As LogonTypes
  78.  
  79.     Dim oNode As IXMLDOMNode
  80.     Dim sPassword As String
  81.     
  82.     Set oNode = ReturnUserNode(sUserName)
  83.     If oNode Is Nothing Then
  84.         LogonUser = AccountDoesNotExist
  85.         Exit Function
  86.     End If
  87.     'Ok, this user does exist.  First lets decrypt the password sent from the client
  88.     sPassword = EncodePassword(sPwd, glClientSideEncryptionKey)
  89.     'Now check this password against what's listed in the db.
  90.     'The next sibling of the 'ClientName' node is the password node.
  91.     'The first child of that node is the actual text value of the password
  92.     If oNode.nextSibling.childNodes(0).nodeTypedValue = EncodePassword(sPassword, glServerSideEncryptionKey) Then
  93.         'The passwords match, logon was successful
  94.         LogonUser = LogonSuccess
  95.         Exit Function
  96.     Else
  97.         'Invalid password, let the user know
  98.         LogonUser = InvalidPassword
  99.         Exit Function
  100.     End If
  101. End Function
  102.  
  103. Public Sub AddUser(ByVal sUserName As String, ByVal sPwd As String, ByVal lCurrentDPlayID As Long)
  104.     Dim sPassword As String
  105.     
  106.     'First decrypt the password
  107.     sPassword = EncodePassword(sPwd, glClientSideEncryptionKey)
  108.     'Now add this user to our xml structure
  109.     AddUserXML sUserName, EncodePassword(sPassword, glServerSideEncryptionKey), True, lCurrentDPlayID
  110. End Sub
  111.  
  112. Public Sub UpdateDBToShowLogon(ByVal sPlayer As String, ByVal lCurrentDPlayID As Long)
  113.     'Set the flag to show that we are currently logged on, and keep our current DPlay ID
  114.     Dim oNode As IXMLDOMNode
  115.     Dim oUpdate As IXMLDOMNode
  116.     
  117.     Set oNode = ReturnUserNode(sPlayer)
  118.     Set oUpdate = GetNode(oNode, "CurrentlyLoggedIn")
  119.     oUpdate.childNodes(0).nodeTypedValue = -1
  120.     Set oUpdate = GetNode(oNode, "CurrentDPlayID")
  121.     oUpdate.childNodes(0).nodeTypedValue = lCurrentDPlayID
  122. End Sub
  123.  
  124. Public Sub UpdateDBToShowLogoff(ByVal lCurrentDPlayID As Long)
  125.     'Set the flag to show that we are currently logged off, and lose our current DPlay ID
  126.     Dim oNode As IXMLDOMNode
  127.     Dim oUpdate As IXMLDOMNode
  128.     
  129.     Set oNode = ReturnUserNodeFromDPlayID(lCurrentDPlayID)
  130.     Set oUpdate = GetNode(oNode, "CurrentlyLoggedIn")
  131.     oUpdate.childNodes(0).nodeTypedValue = 0
  132.     Set oUpdate = GetNode(oNode, "CurrentDPlayID")
  133.     oUpdate.childNodes(0).nodeTypedValue = 0
  134. End Sub
  135.  
  136. Public Sub NotifyFriends(ByVal sPlayer As String, ByVal lNotifyMsg As Long)
  137.     Dim lMsg As Long
  138.     Dim oBuf() As Byte, lOffset As Long
  139.     Dim oNodes As IXMLDOMNodeList, oTemp As IXMLDOMNode
  140.     Dim oNode As IXMLDOMNode, oNodeFriend As IXMLDOMNode
  141.     
  142.     'See if I'm anyone's friends
  143.     Set oNodes = goDOM.selectNodes("MessengerServerDB/FriendList/Row/FriendName")
  144.     For Each oNode In oNodes
  145.         If oNode.childNodes(0).nodeTypedValue = sPlayer Then 'Yup, I am.  Notify each of them that I just logged on
  146.             'First check to see if they are logged on
  147.             Set oNodeFriend = ReturnUserNode(oNode.previousSibling.childNodes(0).nodeTypedValue)
  148.             Set oTemp = GetNode(oNodeFriend, "CurrentDPlayID")
  149.             If Not (oTemp Is Nothing) Then
  150.                 lMsg = lNotifyMsg
  151.                 lOffset = NewBuffer(oBuf)
  152.                 AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  153.                 AddStringToBuffer oBuf, sPlayer, lOffset
  154.                 dps.SendTo CLng(oTemp.childNodes(0).nodeTypedValue), oBuf, 0, 0
  155.             End If
  156.             Set oNodeFriend = Nothing
  157.             Set oTemp = Nothing
  158.         End If
  159.     Next
  160.     
  161. End Sub
  162.  
  163. Public Sub GetFriendsOfMineOnline(ByVal sPlayer As String)
  164.     Dim lMsg As Long
  165.     Dim oBuf() As Byte, lOffset As Long
  166.     Dim oNodes As IXMLDOMNodeList
  167.     Dim oNode As IXMLDOMNode, oNodeFriend As IXMLDOMNode
  168.     
  169.     Dim lSendID As Long
  170.     Dim oFriends() As FriendOnlineType
  171.     Dim lCount As Long
  172.     'First we need to find out if I have any friends in my list
  173.     Set oNodes = goDOM.selectNodes("MessengerServerDB/FriendList/Row/ClientName")
  174.     ReDim oFriends(0)
  175.     For Each oNode In oNodes
  176.         If oNode.childNodes(0).nodeTypedValue = sPlayer Then 'Yup, I do.  Lets save them so I can notify them
  177.             'Get the friend node
  178.             Set oNodeFriend = ReturnUserNode(oNode.nextSibling.childNodes(0).nodeTypedValue)
  179.                 'First check to see if they are logged on
  180.                 ReDim Preserve oFriends(UBound(oFriends) + 1)
  181.                 With oFriends(UBound(oFriends))
  182.                     .sFriendName = oNodeFriend.childNodes(0).nodeTypedValue
  183.                     .fOnline = GetNode(oNodeFriend, "CurrentDPlayID").childNodes(0).nodeTypedValue <> 0
  184.                     .fFriend = oNode.nextSibling.nextSibling.childNodes(0).nodeTypedValue
  185.                 End With
  186.             Set oNodeFriend = Nothing
  187.         End If
  188.     Next
  189.     
  190.     'Get my DPlayID
  191.     lSendID = GetCurrentDPlayID(sPlayer)
  192.     'Now see if I have any friends
  193.     lMsg = Msg_SendClientFriends
  194.     lOffset = NewBuffer(oBuf)
  195.     AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  196.     AddDataToBuffer oBuf, CLng(UBound(oFriends)), SIZE_LONG, lOffset
  197.     For lCount = 1 To UBound(oFriends)
  198.         AddDataToBuffer oBuf, oFriends(lCount).fFriend, LenB(oFriends(lCount).fFriend), lOffset
  199.         AddStringToBuffer oBuf, oFriends(lCount).sFriendName, lOffset
  200.     Next
  201.     dps.SendTo lSendID, oBuf, 0, 0
  202.     For lCount = 1 To UBound(oFriends)
  203.         If oFriends(lCount).fOnline Then
  204.             ReDim oBuf(0)
  205.             lMsg = Msg_FriendLogon
  206.             lOffset = NewBuffer(oBuf)
  207.             AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  208.             AddStringToBuffer oBuf, oFriends(lCount).sFriendName, lOffset
  209.             dps.SendTo lSendID, oBuf, 0, 0
  210.         End If
  211.     Next
  212.     
  213. End Sub
  214.  
  215. 'If fFriend is True, then this person is a friend.  If it is False, then the person is blocked
  216. Public Function AddFriend(ByVal lPlayerID As Long, ByVal sFriendName As String, ByVal fFriend As Boolean) As Boolean
  217.     Dim oMyNode As IXMLDOMNode
  218.     Dim oFriendNode As IXMLDOMNode
  219.     Dim oNodeList As IXMLDOMNodeList, oNode As IXMLDOMNode, fFoundNode As Boolean
  220.     
  221.     AddFriend = False
  222.     fFoundNode = False
  223.     Set oFriendNode = ReturnUserNode(sFriendName)
  224.     'Is this friend currently logged in?
  225.     If GetNode(oFriendNode, "CurrentDPlayID").childNodes(0).nodeTypedValue <> 0 Then AddFriend = True
  226.     Set oMyNode = ReturnUserNodeFromDPlayID(lPlayerID)
  227.     
  228.     Set oNodeList = goDOM.selectNodes("MessengerServerDB/FriendList/Row/ClientName")
  229.     For Each oNode In oNodeList
  230.         If (oNode.childNodes(0).nodeTypedValue = oMyNode.childNodes(0).nodeTypedValue) And _
  231.             oNode.nextSibling.childNodes(0).nodeTypedValue = sFriendName Then
  232.             fFoundNode = True
  233.             'We found the node, update it
  234.             oNode.nextSibling.nextSibling.childNodes(0).nodeTypedValue = fFriend
  235.             Exit For
  236.         End If
  237.     Next
  238.     If Not fFoundNode Then    'We need to add this node
  239.         AddFriendXML oMyNode.childNodes(0).nodeTypedValue, sFriendName, fFriend
  240.     End If
  241. End Function
  242.  
  243. Public Function AmIBlocked(ByVal sMe As String, ByVal sFriend As String) As Boolean
  244.     Dim oMyNode As IXMLDOMNode
  245.     Dim oFriendNode As IXMLDOMNode
  246.     Dim oNodeList As IXMLDOMNodeList, oNode As IXMLDOMNode
  247.     
  248.     AmIBlocked = False
  249.     Set oFriendNode = ReturnUserNode(sFriend)
  250.     'Is this friend currently logged in?
  251.     Set oMyNode = ReturnUserNode(sMe)
  252.     
  253.     Set oNodeList = goDOM.selectNodes("MessengerServerDB/FriendList/Row/ClientName")
  254.     For Each oNode In oNodeList
  255.         If (oNode.childNodes(0).nodeTypedValue = oMyNode.childNodes(0).nodeTypedValue) And _
  256.             oNode.nextSibling.childNodes(0).nodeTypedValue = sFriend Then
  257.             'We found the node, update it
  258.             AmIBlocked = (oNode.nextSibling.nextSibling.childNodes(0).nodeTypedValue = False)
  259.             Exit For
  260.         End If
  261.     Next
  262. End Function
  263.  
  264. Public Function GetCurrentDPlayID(ByVal sPlayer As String) As Long
  265.     Dim oNode As IXMLDOMNode
  266.     
  267.     Set oNode = ReturnUserNode(sPlayer)
  268.     'We need to get the current dplay id of this person
  269.     GetCurrentDPlayID = CLng(GetNode(oNode, "CurrentDPlayID").childNodes(0).nodeTypedValue)
  270. End Function
  271.  
  272. Private Sub CreateDefaultXMLStructure()
  273.   Dim root As IXMLDOMNode
  274.   Dim clientNode As IXMLDOMNode, friendNode As IXMLDOMNode
  275.   'Create a 'root' node
  276.   Set root = goDOM.createNode(NODE_ELEMENT, "MessengerServerDB", vbNullString)
  277.   goDOM.appendChild root
  278.   Set clientNode = goDOM.createNode(NODE_ELEMENT, "ClientInfo", vbNullString)
  279.   root.appendChild clientNode
  280.   Set friendNode = goDOM.createNode(NODE_ELEMENT, "FriendList", vbNullString)
  281.   root.appendChild friendNode
  282.  
  283. End Sub
  284.  
  285. Private Sub AddUserXML(ByVal sUserName As String, ByVal sPassword As String, ByVal fLoggedOn As Boolean, ByVal lCurrentDPlayID As Long)
  286.     Dim oClientNode As IXMLDOMNode, lCount As Long
  287.     Dim oRowNode As IXMLDOMNode, oTableField As IXMLDOMNode
  288.     Dim oText As IXMLDOMNode
  289.     Dim oNodes As IXMLDOMNodeList
  290.     
  291.     'Ok, we need to add a user, first check to see if there are any nodes in our xml doc, if so, add them to those
  292.     'if not, create new ones (we already did this when we created the file, just in case tho)
  293.     If Not goDOM.hasChildNodes Then  'This is an empty XML file
  294.         'Create our default file
  295.         CreateDefaultXMLStructure
  296.     End If
  297.     'We know the first item is the main node
  298.     Set oNodes = goDOM.selectNodes("MessengerServerDB/ClientInfo")
  299.     For Each oClientNode In oNodes
  300.         If oClientNode.nodeName = "ClientInfo" Then
  301.             Set oRowNode = goDOM.createNode(NODE_ELEMENT, "Row", vbNullString)
  302.             'now the Client name
  303.             Set oText = Nothing
  304.             Set oTableField = Nothing
  305.             Set oTableField = goDOM.createNode(NODE_ELEMENT, "ClientName", vbNullString)
  306.             Set oText = goDOM.createNode(NODE_TEXT, vbNullString, vbNullString)
  307.             oText.nodeTypedValue = sUserName
  308.             oTableField.appendChild oText
  309.             oRowNode.appendChild oTableField
  310.             'now the Client pwd
  311.             Set oText = Nothing
  312.             Set oTableField = Nothing
  313.             Set oTableField = goDOM.createNode(NODE_ELEMENT, "ClientPassword", vbNullString)
  314.             Set oText = goDOM.createNode(NODE_TEXT, vbNullString, vbNullString)
  315.             oText.nodeTypedValue = sPassword
  316.             oTableField.appendChild oText
  317.             oRowNode.appendChild oTableField
  318.             'now the currently logged in state
  319.             Set oText = Nothing
  320.             Set oTableField = Nothing
  321.             Set oTableField = goDOM.createNode(NODE_ELEMENT, "CurrentlyLoggedIn", vbNullString)
  322.             Set oText = goDOM.createNode(NODE_TEXT, vbNullString, vbNullString)
  323.             oText.nodeTypedValue = fLoggedOn
  324.             oTableField.appendChild oText
  325.             oRowNode.appendChild oTableField
  326.             'now the currently logged in state
  327.             Set oText = Nothing
  328.             Set oTableField = Nothing
  329.             Set oTableField = goDOM.createNode(NODE_ELEMENT, "CurrentDPlayID", vbNullString)
  330.             Set oText = goDOM.createNode(NODE_TEXT, vbNullString, vbNullString)
  331.             oText.nodeTypedValue = lCurrentDPlayID
  332.             oTableField.appendChild oText
  333.             oRowNode.appendChild oTableField
  334.             'Now actually add the row
  335.             oClientNode.appendChild oRowNode
  336.         End If
  337.     Next
  338. End Sub
  339.  
  340. Private Sub AddFriendXML(ByVal sUserName As String, ByVal sFriend As String, ByVal fFriend As Boolean)
  341.     Dim oFriendNode As IXMLDOMNode, lCount As Long
  342.     Dim oRowNode As IXMLDOMNode, oTableField As IXMLDOMNode
  343.     Dim oText As IXMLDOMNode
  344.     Dim oNodes As IXMLDOMNodeList
  345.     
  346.     'Ok, we need to add a user, first check to see if there are any nodes in our xml doc, if so, add them to those
  347.     'if not, create new ones (we already did this when we created the file, just in case tho)
  348.     If Not goDOM.hasChildNodes Then  'This is an empty XML file
  349.         'Create our default file
  350.         CreateDefaultXMLStructure
  351.     End If
  352.     
  353.     Set oNodes = goDOM.selectNodes("MessengerServerDB/FriendList")
  354.     For Each oFriendNode In oNodes
  355.         If oFriendNode.nodeName = "FriendList" Then
  356.             Set oRowNode = goDOM.createNode(NODE_ELEMENT, "Row", vbNullString)
  357.             'now the Client name
  358.             Set oText = Nothing
  359.             Set oTableField = Nothing
  360.             Set oTableField = goDOM.createNode(NODE_ELEMENT, "ClientName", vbNullString)
  361.             Set oText = goDOM.createNode(NODE_TEXT, vbNullString, vbNullString)
  362.             oText.nodeTypedValue = sUserName
  363.             oTableField.appendChild oText
  364.             oRowNode.appendChild oTableField
  365.             'now the friend name
  366.             Set oText = Nothing
  367.             Set oTableField = Nothing
  368.             Set oTableField = goDOM.createNode(NODE_ELEMENT, "FriendName", vbNullString)
  369.             Set oText = goDOM.createNode(NODE_TEXT, vbNullString, vbNullString)
  370.             oText.nodeTypedValue = sFriend
  371.             oTableField.appendChild oText
  372.             oRowNode.appendChild oTableField
  373.             'now the friend state
  374.             Set oText = Nothing
  375.             Set oTableField = Nothing
  376.             Set oTableField = goDOM.createNode(NODE_ELEMENT, "Friend", vbNullString)
  377.             Set oText = goDOM.createNode(NODE_TEXT, vbNullString, vbNullString)
  378.             oText.nodeTypedValue = fFriend
  379.             oTableField.appendChild oText
  380.             oRowNode.appendChild oTableField
  381.             'Now actually add the row
  382.             oFriendNode.appendChild oRowNode
  383.         End If
  384.     Next
  385. End Sub
  386.  
  387.  
  388. Private Function ReturnUserNode(ByVal sUserName As String) As IXMLDOMNode
  389.     Dim oNode As IXMLDOMNode
  390.     Dim oNodes As IXMLDOMNodeList
  391.     
  392.     'Get a list of all client names
  393.     Set oNodes = goDOM.selectNodes("MessengerServerDB/ClientInfo/Row/ClientName")
  394.     For Each oNode In oNodes
  395.         'See if we are in that list
  396.         'We use the childnodes(0) since the only member of the 'ClientName' node
  397.         'is the text that contains the name
  398.         If oNode.childNodes(0).nodeTypedValue = sUserName Then
  399.             Set ReturnUserNode = oNode
  400.             Exit Function
  401.         End If
  402.     Next
  403.     Set ReturnUserNode = Nothing
  404. End Function
  405.  
  406. Private Function ReturnUserNodeFromDPlayID(ByVal lID As Long) As IXMLDOMNode
  407.     Dim oNode As IXMLDOMNode
  408.     Dim oNodes As IXMLDOMNodeList
  409.     
  410.     'Get a list of all client names
  411.     Set oNodes = goDOM.selectNodes("MessengerServerDB/ClientInfo/Row/CurrentDPlayID")
  412.     For Each oNode In oNodes
  413.         'See if we are in that list
  414.         'We use the childnodes(0) since the only member of the 'CurrentDPlayID' node
  415.         'is the text that contains the id
  416.         If oNode.childNodes(0).nodeTypedValue = lID Then
  417.             'The user node is 3 siblings before the DPlayID node
  418.             Set ReturnUserNodeFromDPlayID = oNode.previousSibling.previousSibling.previousSibling
  419.             Exit Function
  420.         End If
  421.     Next
  422.     Set ReturnUserNodeFromDPlayID = Nothing
  423. End Function
  424.  
  425. Private Function GetNode(ByVal oNode As IXMLDOMNode, ByVal sNodeName As String) As IXMLDOMNode
  426.     Dim oUpdate As IXMLDOMNode
  427.     
  428.     On Error GoTo ExitFunc
  429.     Set oUpdate = oNode.nextSibling
  430.     Do
  431.         If oUpdate.nodeName = sNodeName Then
  432.             Set GetNode = oUpdate
  433.             Exit Function
  434.         End If
  435.         Set oUpdate = oUpdate.nextSibling
  436.     Loop
  437. ExitFunc:
  438. Set GetNode = Nothing
  439. End Function
  440.  
  441. Private Function MarkEveryoneLoggedOff()
  442.     Dim oNode As IXMLDOMNode
  443.     Dim oNodes As IXMLDOMNodeList
  444.     
  445.     'Get a list of all client names
  446.     Set oNodes = goDOM.selectNodes("MessengerServerDB/ClientInfo/Row/ClientName")
  447.     For Each oNode In oNodes
  448.         'Mark everyone as logged off
  449.         oNode.nextSibling.nextSibling.childNodes(0).nodeTypedValue = 0
  450.         oNode.nextSibling.nextSibling.nextSibling.childNodes(0).nodeTypedValue = 0
  451.     Next
  452. End Function
  453.  
  454. Public Sub SaveXMLStructure()
  455.     'Save the XML structure out to a file
  456.     goDOM.Save AddDirSep(GetDXSampleFolder) & "vbsamples\media\vbMsgSrv.xml"
  457. End Sub
  458.