1 """Main loop for the MUFF Engine"""
3 # Copyright (c) 2005 MUDpy, The Fungi <fungi@yuggoth.org>, all rights reserved.
4 # Licensed per terms in the LICENSE file distributed with this software.
6 # string.strip is used to clean up leading/trailing whitespace in user input
9 # time.sleep is used in the loop to save cpu and provide crude pulse timing
12 # hack to load all modules in the muff package
14 for module in muff.__all__:
15 exec("import " + module)
20 # loop indefinitely while the world is not flagged for termination
21 while not muffvars.terminate_world:
23 # open the listening socket if it hasn't been already
24 if not muffvars.newsocket: muffsock.initialize_server_socket()
26 # pause for a configurable amount of time (decimal seconds)
27 time.sleep(muffconf.config_data.getfloat("general", "increment"))
29 # the world was flagged for a reload of all code/data
30 if muffvars.reload_modules:
32 # reload the muff package
35 # reload all modules listed in the muff package
36 for module in muff.__all__:
37 exec("reload(muff." + module + ")")
39 # reset the reload flag
40 muffvars.reload_modules = False
42 # assign a user if a new connection is waiting
43 user = muffsock.check_for_connection(muffvars.newsocket)
45 # there was a new connection
48 # welcome to the user list
49 muffvars.userlist.append(user)
52 muffmisc.log(str(len(muffvars.userlist)) + " connection(s)")
54 # iterate over the connected users
55 for each_user in muffvars.userlist:
57 # show the user a menu as needed
60 # disconnect users with the appropriate state
61 if each_user.state == "disconnecting":
63 # save to cold storage
66 # close the connection
67 each_user.connection.close()
69 # remove from the list
74 # check for some input
75 # TODO: make a separate function for this
77 input_data = each_user.connection.recv(1024)
83 # tack this on to any previous partial
84 each_user.partial_input += input_data
86 # the held input ends in a newline
87 if each_user.partial_input[-1] == "\n":
89 # filter out non-printables
90 each_user.partial_input = filter(lambda x: x>=' ' and x<='~', each_user.partial_input)
92 # strip off extra whitespace
93 each_user.partial_input = string.strip(each_user.partial_input)
95 # put on the end of the queue
96 each_user.input_queue.append(each_user.partial_input)
98 # reset the held partial input
99 each_user.partial_input = ""
101 # pass first item in the input
102 # queue to the main handler
103 muffcmds.handle_user_input(each_user, each_user.input_queue[0])
105 # then remove it from the queue
106 each_user.input_queue.remove(each_user.input_queue[0])
108 # the loop has terminated, so tear down all sockets
109 # TODO: move the save from command_halt() to here
110 muffsock.destroy_all_sockets()
112 # log a final message
113 muffmisc.log("Shutting down now.")