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 # TODO: need to log this crap
53 print len(muffvars.userlist),"connection(s)"
55 # iterate over the connected users
56 for each_user in muffvars.userlist:
58 # show the user a menu as needed
61 # check for some input
62 # TODO: make a separate function for this
64 input_data = each_user.connection.recv(1024)
70 # tack this on to any previous partial input
71 each_user.partial_input += input_data
73 # the held input ends in a newline
74 if each_user.partial_input[-1] == "\n":
76 # filter out non-printable characters
77 each_user.partial_input = filter(lambda x: x>=' ' and x<='~', each_user.partial_input)
79 # strip off leading/trailing whitespace
80 each_user.partial_input = string.strip(each_user.partial_input)
82 # move it to the end of the input queue
83 each_user.input_queue.append(each_user.partial_input)
85 # reset the held partial input
86 each_user.partial_input = ""
88 # pass the first item in the input
89 # queue to the main handler
90 muffcmds.handle_user_input(each_user, each_user.input_queue[0])
92 # remove the first item from the queue
93 each_user.input_queue.remove(each_user.input_queue[0])
95 # the loop has terminated, so tear down all sockets
96 # TODO: move the save from command_halt() to here
97 muffsock.destroy_all_sockets()
100 # TODO: need a logging function for this kind of stuff
101 print "Shutting down now."