2
0

AIRepository.py 3.6 KB

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