ClientRepository.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from direct.distributed.ClientRepository import ClientRepository
  2. from panda3d.core import URLSpec, ConfigVariableInt, ConfigVariableString
  3. from message import Message
  4. class GameClientRepository(ClientRepository):
  5. def __init__(self):
  6. dcFileNames = ['../direct.dc', 'sample.dc']
  7. ClientRepository.__init__(
  8. self,
  9. dcFileNames = dcFileNames,
  10. threadedNet = True)
  11. # Set the same port as configured on the server to be able to connect
  12. # to it
  13. tcpPort = ConfigVariableInt('server-port', 4400).getValue()
  14. # Set the IP or hostname of the server we want to connect to
  15. hostname = ConfigVariableString('server-host', '127.0.0.1').getValue()
  16. # Build the URL from the server hostname and port. If your server
  17. # uses another protocol then http you should change it accordingly.
  18. # Make sure to pass the connectMethod to the ClientRepository.__init__
  19. # call too. Available connection methods are:
  20. # self.CM_HTTP, self.CM_NET and self.CM_NATIVE
  21. self.url = URLSpec('http://{}:{}'.format(hostname, tcpPort))
  22. # Attempt a connection to the server
  23. self.connect([self.url],
  24. successCallback = self.connectSuccess,
  25. failureCallback = self.connectFailure)
  26. def lostConnection(self):
  27. ''' This should be overridden by a derived class to handle an
  28. unexpectedly lost connection to the gameserver. '''
  29. # Handle the disconnection from the server. This can be a reconnect,
  30. # simply exiting the application or anything else.
  31. exit()
  32. def connectFailure(self, statusCode, statusString):
  33. ''' Something went wrong '''
  34. # we could create a reconnect task to try and connect again.
  35. exit()
  36. def connectSuccess(self):
  37. ''' Successfully connected. But we still can't really do
  38. anything until we've got the doID range. '''
  39. # Make sure we have interest in the by the AIRepository defined
  40. # TimeManager zone, so we always see it even if we switch to
  41. # another zone.
  42. self.setInterestZones([1])
  43. # We must wait for the TimeManager to be fully created and
  44. # synced before we can enter another zone and wait for the
  45. # game object. The uniqueName is important that we get the
  46. # correct, our sync message from the TimeManager and not
  47. # accidentaly a message from another client
  48. self.acceptOnce(self.uniqueName('gotTimeSync'), self.syncReady)
  49. def syncReady(self):
  50. ''' Now we've got the TimeManager manifested, and we're in
  51. sync with the server time. Now we can enter the world. Check
  52. to see if we've received our doIdBase yet. '''
  53. # This method checks whether we actually have a valid doID range
  54. # to create distributed objects yet
  55. if self.haveCreateAuthority():
  56. # we already have one
  57. self.gotCreateReady()
  58. else:
  59. # Not yet, keep waiting a bit longer.
  60. self.accept(self.uniqueName('createReady'), self.gotCreateReady)
  61. def gotCreateReady(self):
  62. ''' Ready to enter the world. Expand our interest to include
  63. any other zones '''
  64. # This method checks whether we actually have a valid doID range
  65. # to create distributed objects yet
  66. if not self.haveCreateAuthority():
  67. # Not ready yet.
  68. return
  69. # we are ready now, so ignore further createReady events
  70. self.ignore(self.uniqueName('createReady'))
  71. # create a instance of the message class
  72. msg = Message(self)
  73. # and create the distributed Object with it
  74. self.createDistributedObject(
  75. distObj = msg, zoneId = 1)
  76. # save the created Distributed Object
  77. # in self.msg for later usage
  78. self.msg = msg
  79. def sendMessage(self, msgText):
  80. '''Function to call the send function of the message class,
  81. which sends the Message over the Network to the other users'''
  82. sentText = "{}: {}".format(self.doIdBase, msgText)
  83. self.msg.b_sendText(sentText)