gameserverdemo.py
上传用户:ghyvgy
上传日期:2009-05-26
资源大小:547k
文件大小:3k
源码类别:

其他游戏

开发平台:

Python

  1. # gameserverdemo.py
  2. #
  3. # Main driver program for a demo game server created
  4. # for the purpose of demonstrating the programming principles
  5. # illustrated in the articles,
  6. #   Precise Game Event Broadcasting with Python
  7. #   Creating a "Safe Sandbox" for Game Scripting
  8. #
  9. # In the book, "Massively Multiplayer Game Development".
  10. #
  11. # Implements server initialization and the main server loop.
  12. # Author: Matthew Walker
  13. #         mwalker@softhome.net
  14. #
  15. import gameserver.gameserver
  16. import gameserver.deferred
  17. import gameserver.objmgr
  18. import gameserver.gameobs
  19. def InitGame():
  20.   """
  21.   Perform any necessary startup functions.
  22.   """
  23.   # init the safe sandbox classes
  24.   import gameserver.safesandbox
  25.   sandboxclasses = []
  26.   sandboxclasses.append(('gameserver.sandboxmodules.thetrap', 'TheTrap'))
  27.   sandboxclasses.append(('gameserver.sandboxmodules.battle', 'Battle'))
  28.   sandboxclasses.append(('gameserver.sandboxmodules.town', 'Town'))
  29.   gameserver.safesandbox._InitSandboxClasses(sandboxclasses)
  30.   # create the "world" areas; i.e. the sandboxes
  31.   gameserver.gameobs.CreateRoom('TheTrap')
  32.   gameserver.gameobs.CreateRoom('Battle')
  33.   gameserver.gameobs.CreateRoom('Town')
  34. def ProcessRequests():
  35.   """
  36.   Handle all waiting calls whose time has come.
  37.   Return the time until our next iteration.
  38.   """
  39.   waitTime = None
  40.   try:
  41.     waitTime = gameserver.deferred.ExecuteDeferredCalls()
  42.   except:
  43.     # anything can happen here
  44.     import traceback
  45.     traceback.print_exc()
  46.   return waitTime
  47. def GameLoop (timeout=1.0, use_poll=0, map=None):
  48.   """
  49.   Main server loop for asynchronous processing.
  50.   Steal the functionality straight from asyncore and
  51.   modify it for our own purposes.
  52.   This function handles both client requests and 
  53.   deferred calls.
  54.   """
  55.   import asyncore
  56.   if map is None:
  57.     map=asyncore.socket_map
  58.   # pick the asyncore polling function to use
  59.   if use_poll:
  60.     if hasattr (select, 'poll'):
  61.       WaitForRequests = asyncore.poll3
  62.     else:
  63.       WaitForRequests = asyncore.poll2
  64.   else:
  65.     WaitForRequests = asyncore.poll
  66.   # loop forever, or until we are done
  67.   waitTime = timeout
  68.   while map:
  69.     # receive client requests via asyncore
  70.     WaitForRequests(waitTime, map)
  71.     # fire waiting deferred calls
  72.     waitTime = ProcessRequests()
  73.     if not waitTime:
  74.       waitTime = timeout
  75. #
  76. # command-line interface:
  77. # usage: python gameserverdemo.py <port>
  78. if __name__ == '__main__':
  79.   import sys
  80.   import string
  81.   if len(sys.argv) < 2:
  82.     print 'Usage: %s <server-port>' % sys.argv[0]
  83.   else:
  84.     game = gameserver.gameserver.GameServer(string.atoi (sys.argv[1]))
  85.     InitGame()
  86.     GameLoop()