-
Notifications
You must be signed in to change notification settings - Fork 13
/
tworld.py
88 lines (67 loc) · 2.37 KB
/
tworld.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3
"""
tworld: Copyright (c) 2013, Andrew Plotkin
(Available under the MIT License; see LICENSE file.)
This is the top-level script which acts as Tworld's core server.
Players do not connect directly to this process. The tweb server (the web
server process) connects to this; players connect to tweb. All game
commands are relayed to us through tweb.
"""
import sys
import types
import logging
import tornado.options
import tornado.ioloop
import tornado.autoreload
# Set up all the options. (Generally found in the config file.)
# Clever hack to parse a config file off the command line.
tornado.options.define(
'config', type=str,
help='configuration file',
callback=lambda path: tornado.options.parse_config_file(path, final=False))
tornado.options.define(
'python_path', type=str,
help='Python modules directory (optional)')
tornado.options.define(
'debug', type=bool,
help='application debugging (see Tornado docs)')
tornado.options.define(
'show_stack_traces', type=bool,
help='show stack traces for errors that are probably a player\'s fault')
tornado.options.define(
'log_level', type=str, default=None,
help='logging threshold (default usually WARNING)')
tornado.options.define(
'log_file_tworld', type=str, default=None,
help='log file to write to (default is stdout)')
tornado.options.define(
'tworld_port', type=int, default=4001,
help='port number for communication between tweb and tworld')
tornado.options.define(
'mongo_database', type=str, default='tworld',
help='name of mongodb database')
# Parse 'em up.
tornado.options.parse_command_line()
opts = tornado.options.options
if opts.python_path:
sys.path.insert(0, opts.python_path)
# Set up the logging configuration.
logconf = {
'format': '[%(levelname).1s %(asctime)s: %(module)s:%(lineno)d] %(message)s',
'datefmt': '%b-%d %H:%M:%S',
}
if opts.log_level:
logconf['level'] = opts.log_level
if opts.log_file_tworld:
logconf['filename'] = opts.log_file_tworld
else:
logconf['stream'] = sys.stdout
logging.basicConfig(**logconf)
# Now that we have a python_path, we can import the tworld-specific modules.
import twcommon.autoreload
import two.app
app = two.app.Tworld(opts)
if opts.debug:
twcommon.autoreload.sethandler(app.autoreload_handler)
tornado.autoreload.start()
tornado.ioloop.IOLoop.instance().start()