def run(self):
        """

        """
        global _Server_Queue
        global _CMD_Queue
        global game_engine
        global _World_list

        done = False
        while not done:
            command = ""
            try:
                command = _Server_Queue.get()
            except:
                pass

            if command != "":  # We got something
                logger.write_line("Got server command: %s" % command)
                if command.lower() == "quit":
                    print "Got quit, shutting down server and all engines."
                    done = True
                    for engine in _World_list:  # For each engine...
                        this_engine = _World_list[engine]  # Get the engine.
                        logger.write_line("Shutting down engine %s" % engine)
                        print "Shutting down engine %s" % engine
                        this_engine.shutdown_game()
                    break

                elif "start" in command.lower():  # command syntax: start engine_name <save_state#>
                    cmd = command.split()  # Split it up
                    save_state = None
                    if len(cmd) > 3 or len(cmd) < 2:  # We got too much or too few
                        print "Error, command syntax for start is: start engine_name [save_state]"
                    else:
                        if cmd[0].lower() == "start":  # Command is a start command, we will start an engine.
                            eng_name = cmd[1]  # Get engine name.
                            if len(cmd) == 3:  # We got a save state too.
                                save_state = cmd[2]
                            if eng_name in _World_list:  # This is a valid engine to start up.
                                engine = _World_list[eng_name]  # Get the engine
                                if not engine._IsRunning:  # The engine has not been started yet, we can start it.
                                    if save_state != None:  # We got a value for this, start the engine with this number
                                        logger.write_line(
                                            "Starting engine %s with save state number %s" % (eng_name, save_state)
                                        )
                                        print "Starting engine %s with save state number %s" % (eng_name, save_state)
                                        engine.init_game(int(save_state))
                                    else:  # We did not get a save state
                                        engine.init_game()  # Start without save state.
                                        logger.write_line("Starting engine %s without save state" % eng_name)
                                        print "Starting engine %s without save state" % eng_name
                                else:  # This engine is already running, we don't want to start it again.
                                    print "Engine %s is already running, you cannot start it again." % eng_name
                                    logger.write_line(
                                        "Starting of engine %s declined, engine already running" % eng_name
                                    )

                elif "stop" in command.lower():  # command syntax: stop engine_name
                    cmd = command.split()
                    if len(cmd) > 2 or len(cmd) < 2:  # We did not get enough
                        print "Error, command syntax for stop is: stop engine_name"
                    else:  # Valid
                        eng_name = cmd[1]  # Get the name of it
                        if eng_name in _World_list:  # This is a valid engine we can shut down
                            engine = _World_list[eng_name]
                            if engine._IsRunning:
                                engine.shutdown_game()
                                logger.write_line("Got command to shutdown %s engine. Proceeding." % eng_name)
                                print "Shutting down engine <%s>" % eng_name
                            else:  # This engine is already shut down.
                                print "This engine is already shut down <%s>" % eng_name
                        else:  # This is not an engine we know of.
                            print "Unrecognized engine name <%s>" % eng_name

                else:  # No other commands presently.
                    print "Got command: %s" % command
            time.sleep(0.05)
        return True
示例#2
0
    def run(self):
        """

        """
        global _Server_Queue
        global _CMD_Queue
        global game_engine
        global _World_list
        global _Shutdown_Lock
        global _Shutdown
        global _Player_Loc_Lock
        global _Player_Locations
        global _Player_OQueues_Lock
        global _Player_OQueues
        
        _Shutdown_Lock.acquire()
        done = _Shutdown
        _Shutdown_Lock.release()
        while not done:
            command = ''
            try:
                command = _Server_Queue.get()
            except:
                pass

            if command != '': #We got something
                logger.write_line("Got server command: %s" % command)
                if command.lower() == 'quit':
                    print 'Got quit, shutting down server and all engines.'
                    _Shutdown_Lock.acquire()
                    _Shutdown = True
                    _Shutdown_Lock.release()
                    for engine in _World_list: #For each engine...
                        this_engine = _World_list[engine] #Get the engine.
                        logger.write_line("Shutting down engine %s" % engine)
                        print "Shutting down engine %s" % engine
                        this_engine.shutdown_game()
                        text = "Shutting down game server, please close your client as it will no longer be able to reach the game."
                        for person in _Player_OQueues:
                            _Player_OQueues_Lock.acquire()
                            _Player_OQueues[person].put(text)
                            _Player_OQueue_Lock.release()
                        
                    break
                    
                elif "start" in command.lower(): #command syntax: start engine_name <save_state#>
                    cmd = command.split() #Split it up
                    save_state = None
                    if len(cmd) > 3 or len(cmd) < 2: #We got too much or too few
                        print "Error, command syntax for start is: start engine_name [save_state]"
                    else:
                        if cmd[0].lower() == 'start': #Command is a start command, we will start an engine.
                            eng_name = cmd[1] #Get engine name.
                            if len(cmd) == 3: #We got a save state too.
                                save_state = cmd[2]
                            if eng_name in _World_list: #This is a valid engine to start up.
                                engine = _World_list[eng_name] #Get the engine
                                if not engine._IsRunning: #The engine has not been started yet, we can start it.
                                    if save_state != None: #We got a value for this, start the engine with this number
                                        logger.write_line("Starting engine %s with save state number %s" % (eng_name, save_state))
                                        print "Starting engine %s with save state number %s" % (eng_name, save_state)
                                        engine.init_game(int(save_state))
                                    else: #We did not get a save state
                                        engine.init_game() #Start without save state.
                                        logger.write_line("Starting engine %s without save state" % eng_name)
                                        print "Starting engine %s without save state" % eng_name
                                else: #This engine is already running, we don't want to start it again.
                                    print "Engine %s is already running, you cannot start it again." % eng_name
                                    logger.write_line("Starting of engine %s declined, engine already running" % eng_name)
                                
                elif "stop" in command.lower(): #command syntax: stop engine_name
                    cmd = command.split()
                    if len(cmd) > 2 or len(cmd) < 2: #We did not get enough
                        print "Error, command syntax for stop is: stop engine_name"
                    else: #Valid
                        eng_name = cmd[1] #Get the name of it
                        if eng_name in _World_list: #This is a valid engine we can shut down
                            engine = _World_list[eng_name]
                            if engine._IsRunning:
                                engine.shutdown_game()
                                logger.write_line("Got command to shutdown %s engine. Proceeding." % eng_name)
                                print "Shutting down engine <%s>" % eng_name
                                for person in _Player_Locations:
                                    _Player_Loc_Lock.acquire()
                                    location = _Player_Locations[person]
                                    _Player_Loc_Lock.release()
                                    
                                    if location == eng_name: #This is the same name as the engine that is shutting down, tell them to close their client.
                                        _Player_OQueues_Lock.acquire()
                                        _Player_OQueues[person].put("The engine %s is going to shut down, please close your client as it will no longer connect to this engine." % eng_name)
                                        _Player_OQueues_Lock.release()
                            else: #This engine is already shut down.
                                print "This engine is already shut down <%s>" % eng_name
                        else: #This is not an engine we know of.
                            print "Unrecognized engine name <%s>" % eng_name
                    
             
                else: #No other commands presently.
                    print 'Got command: %s' % command
            _Shutdown_Lock.acquire()
            done = _Shutdown
            _Shutdown_Lock.release()
            time.sleep(0.05)
        return True