-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathmain.py
74 lines (61 loc) · 2.06 KB
/
main.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
import random
import socket
import logging
import argparse
import string
from twisted.internet import reactor
from twisted.web import server, static
from autobahn.twisted.resource import WebSocketResource
from rose.common import config
from . import game, net
log = logging.getLogger("main")
def main():
logging.basicConfig(level=logging.INFO, format=config.logger_format)
parser = argparse.ArgumentParser(description="ROSE Server")
parser.add_argument(
"--track_definition",
"-t",
dest="track_definition",
default="random",
choices=["random", "same"],
help="Definition of driver tracks: random or same."
"If not specified, random will be used.",
)
parser.add_argument(
"--seed",
"-s",
dest="seed",
default="",
help="Optional, use a custom seed for the map generation",
)
args = parser.parse_args()
"""
If the argument is 'same', the track will generate the obstacles in the
same place for both drivers, otherwise, the obstacles will be genrated in
random locations for each driver.
"""
if args.track_definition == "same":
config.is_track_random = False
else:
config.is_track_random = True
if args.seed:
seed = args.seed
else:
seed = generate_seed(config.seed_length)
log.info(f"Seed for map: {seed}")
g = game.Game(seed=seed)
log.info("starting server")
h = net.Hub(g)
reactor.listenTCP(config.game_port, net.PlayerFactory(h))
root = static.File(config.web_root)
wsuri = "ws://%s:%s" % (socket.gethostname(), config.web_port)
watcher = net.WatcherFactory(wsuri, h)
root.putChild(b"ws", WebSocketResource(watcher))
root.putChild(b"res", static.File(config.res_root))
root.putChild(b"admin", net.WebAdmin(g))
root.putChild(b"rpc2", net.CliAdmin(g))
site = server.Site(root)
reactor.listenTCP(config.web_port, site)
reactor.run()
def generate_seed(seed_length=5):
return "".join(random.choice(string.ascii_lowercase) for _ in range(seed_length))