2
0

AIRepository.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from direct.distributed.ClientRepository import ClientRepository
  2. from panda3d.core import URLSpec, ConfigVariableInt, ConfigVariableString
  3. class AIRepository(ClientRepository):
  4. def __init__(self):
  5. """ The AI Repository usually lives on a server and is responsible for
  6. server side logic that will handle game objects """
  7. # List of all dc files that are of interest to this AI Repository
  8. dcFileNames = ['../direct.dc', 'sample.dc']
  9. # Initialize the repository. We pass it the dc files and as this is an
  10. # AI repository the dcSuffix AI. This will make sure any later calls to
  11. # createDistributedObject will use the correct version.
  12. # The connectMethod
  13. ClientRepository.__init__(
  14. self,
  15. dcFileNames = dcFileNames,
  16. dcSuffix = 'AI',
  17. threadedNet = True)
  18. # Set the same port as configured on the server to be able to connect
  19. # to it
  20. tcpPort = ConfigVariableInt('server-port', 4400).getValue()
  21. # Set the IP or hostname of the server we want to connect to
  22. hostname = ConfigVariableString('server-host', '127.0.0.1').getValue()
  23. # Build the URL from the server hostname and port. If your server
  24. # doesn't use http you should change it accordingly. Make sure to pass
  25. # the connectMethod to the ClientRepository.__init__ call too.
  26. # Available connection methods are:
  27. # self.CM_HTTP, self.CM_NET and self.CM_NATIVE
  28. url = URLSpec('http://{}:{}'.format(hostname, tcpPort))
  29. # Attempt a connection to the server
  30. self.connect([url],
  31. successCallback = self.connectSuccess,
  32. failureCallback = self.connectFailure)
  33. def connectFailure(self, statusCode, statusString):
  34. """ something went wrong """
  35. print("Couldn't connect. Make sure to run server.py first!")
  36. raise(StandardError, statusString)
  37. def connectSuccess(self):
  38. """ Successfully connected. But we still can't really do
  39. anything until we've got the doID range. """
  40. # The Client Repository will throw this event as soon as it has a doID
  41. # range and would be able to create distributed objects
  42. self.accept('createReady', self.gotCreateReady)
  43. def lostConnection(self):
  44. """ This should be overridden by a derived class to handle an
  45. unexpectedly lost connection to the gameserver. """
  46. exit()
  47. def gotCreateReady(self):
  48. """ Now we're ready to go! """
  49. # This method checks whether we actually have a valid doID range
  50. # to create distributed objects yet
  51. if not self.haveCreateAuthority():
  52. # Not ready yet.
  53. return
  54. # we are ready now, so ignore further createReady events
  55. self.ignore('createReady')
  56. # Create a Distributed Object by name. This will look up the object in
  57. # the dc files passed to the repository earlier
  58. self.timeManager = self.createDistributedObject(
  59. className = 'TimeManagerAI', # The Name of the Class we want to initialize
  60. zoneId = 1) # The Zone this Object will live in
  61. print("AI Repository Ready")
  62. def deallocateChannel(self, doID):
  63. """ This method will be called whenever a client disconnects from the
  64. server. The given doID is the ID of the client who left us. """
  65. print("Client left us: ", doID)