Beispiel #1
0
 def Run(self, svraddr, renderer, keyboard, user_conf, tries, resend,
         timeout):
     '''Run the game as a client.
     This method connects to the server at svraddr, performs a handshake,
     synchronizes the clock, and starts the game.
     Argument:
     svraddr    -- The address of the server.
     renderer  -- The renderer to use.
     keyboard  -- The keyboard to use.
     user_conf -- A GameConfig provided by the user.
     tries     -- Number of tries before failing.
     resend    -- The number of duplicate messages to send.
     timeout   -- The timeout for Handshake().
     Return value:
     True if a game was completed successfully and False otherwise.
     '''
     e = UDPGameEngine()
     sock = UDPSocket()
     sock.Open()
     for i in range(0, tries):
         logger.info('Connecting to server.')
         if not sock.Connect(svraddr, 1):
             logger.info('Connection failed.')
             continue
         logger.info('Connected as {0}.'.format(sock.sock.getsockname()))
         svr = UDPEventSocket(sock)
         # Allow server to make clock measurements.
         if user_conf.do_sync:
             logger.info('Syncing clock with server.')
             svr.RecvSync(user_conf.sync_timeout * 3)
         if not self.Handshake(svr, resend, timeout):
             logger.info('Handshake failed.')
             continue
         self.conf.Apply(e)
         logger.info('Starting game as player {0}.'.format(e.player_id))
         logger.debug('delay={0}, cool_down={1}'.format(e.buffer_delay,
             e.key_cool_down_time))
         e.server = svr
         e.is_client = True
         e.is_server = True
         e.renderer = renderer
         e.keyboard = keyboard
         # To do: Apply user_conf without overriding server config.
         e.PlayAs(e.state, self, self.conf.start_time / 1000.0)
         logger.info('Game ended.')
         sock.Close()
         return True
     logger.info('Failed to start game.')
     sock.Close()
     return False