1 | #!/usr/bin/env python2 |
||
2 | |||
3 | # |
||
4 | # Copyright 2001 - 2016 Ludek Smid [http://www.ospace.net/] |
||
5 | # |
||
6 | # This file is part of Outer Space. |
||
7 | # |
||
8 | # Outer Space is free software; you can redistribute it and/or modify |
||
9 | # it under the terms of the GNU General Public License as published by |
||
10 | # the Free Software Foundation; either version 2 of the License, or |
||
11 | # (at your option) any later version. |
||
12 | # |
||
13 | # Outer Space is distributed in the hope that it will be useful, |
||
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | # GNU General Public License for more details. |
||
17 | # |
||
18 | # You should have received a copy of the GNU General Public License |
||
19 | # along with Outer Space; if not, write to the Free Software |
||
20 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
||
21 | # |
||
22 | |||
23 | import argparse |
||
24 | import multiprocessing |
||
25 | import os, os.path |
||
26 | import sys |
||
27 | import time |
||
28 | |||
29 | relative_path = os.path.dirname(os.path.realpath(__file__)) |
||
30 | # tweak PYTHONPATH |
||
31 | sys.path.insert(0, os.path.join(relative_path, "client")) |
||
32 | sys.path.insert(0, os.path.join(relative_path, "server")) |
||
33 | sys.path.insert(0, os.path.join(relative_path, "server", "data")) |
||
34 | sys.path.insert(0, os.path.join(relative_path, "client-ai")) |
||
35 | sys.path.insert(0, os.path.join(relative_path, "server","lib")) |
||
36 | |||
37 | |||
38 | parser = argparse.ArgumentParser() |
||
39 | |||
40 | common_parser = argparse.ArgumentParser(add_help=False) |
||
41 | common_parser.add_argument("--configdir", dest = "configDir", |
||
42 | metavar = "DIRECTORY", |
||
43 | default = os.path.join(os.path.expanduser("~"), ".outerspace"), |
||
44 | help = "Override default configuration directory", |
||
45 | ) |
||
46 | common_parser.add_argument("--server", dest = "server", |
||
47 | metavar = "HOSTNAME:PORT", |
||
48 | default = "pichu.dahaic.net:9080", |
||
49 | help = "Outer Space server location" |
||
50 | ) |
||
51 | common_parser.add_argument("--local", dest = "local", |
||
52 | action = "store_true", |
||
53 | default = False, |
||
54 | help = "Setting on local mode (connections are made to localhost)" |
||
55 | ) |
||
56 | common_parser.add_argument("--profile", dest = "profile", |
||
57 | default=False, |
||
58 | help = "Run with profiling enabled" |
||
59 | ) |
||
60 | subparsers = parser.add_subparsers(help='Subcommands: client (default), server, ai, ai-pool') |
||
61 | |||
62 | parser_client = subparsers.add_parser('client', help='Game client of Outer Space', parents=[common_parser]) |
||
63 | parser_server = subparsers.add_parser('server', help='Dedicated server', parents=[common_parser]) |
||
64 | parser_ai = subparsers.add_parser('ai', help='Run one AI worker', parents=[common_parser]) |
||
65 | parser_ai_pool = subparsers.add_parser('ai-pool', help='Batch run of AI players defined in configuration', parents=[common_parser]) |
||
66 | |||
67 | # unfortunately, argparser does not support default subcommand (maybe it is |
||
68 | # messy approach? :( ) so we push 'client' when default should apply |
||
69 | if len(sys.argv) == 1 or sys.argv[1] not in ['client', 'server', 'ai', 'ai-pool', '--help', '-h']: |
||
70 | sys.argv = [sys.argv[0]] + ['client'] + sys.argv[1:] |
||
71 | |||
72 | subcommand = sys.argv[1] |
||
73 | |||
74 | # common stuff |
||
75 | |||
76 | # client |
||
77 | parser_client.add_argument("--configfilename", dest = "configFilename", |
||
78 | metavar = "FILENAME", |
||
79 | default = "osci.ini", |
||
80 | help = "Override default configuration file name", |
||
81 | ) |
||
82 | parser_client.add_argument("--login", dest = "login", |
||
83 | metavar = "HOSTNAME:PORT", |
||
84 | default = None, |
||
85 | help = "Account login" |
||
86 | ) |
||
87 | parser_client.add_argument("--password", dest = "password", |
||
88 | metavar = "HOSTNAME:PORT", |
||
89 | default = None, |
||
90 | help = "Account password" |
||
91 | ) |
||
92 | parser_client.add_argument("--heartbeat", dest = "heartbeat", |
||
93 | type = int, |
||
94 | metavar = "SECONDS", |
||
95 | default = 60, |
||
96 | help = "Heartbeat for server connection" |
||
97 | ) |
||
98 | |||
99 | # server |
||
100 | parser_server.add_argument("--configfilename", dest = "configFilename", |
||
101 | metavar = "FILENAME", |
||
102 | default = "osci.ini", |
||
103 | help = "Override default configuration file name", |
||
104 | ) |
||
105 | parser_server.add_argument("--restore", dest = "restore", |
||
106 | metavar = "STRING", |
||
107 | default = None, |
||
108 | help = "Restore from backup files beginning with STRING", |
||
109 | ) |
||
110 | parser_server.add_argument("--reset", dest = "reset", |
||
111 | action = "store_true", default=False, |
||
112 | help = "Server resets itself before starting up" |
||
113 | ) |
||
114 | parser_server.add_argument("--upgrade", dest = "upgrade", |
||
115 | action = "store_true", default=False, |
||
116 | help = "Server will undergo upgrade routine" |
||
117 | ) |
||
118 | parser_server.add_argument("--mode", dest = "mode", |
||
119 | type=int, |
||
120 | metavar = "MODE", |
||
121 | default=1, |
||
122 | help = "Server mode: 0 - debug, 1 - normal", |
||
123 | ) |
||
124 | |||
125 | # ai |
||
126 | parser_ai.add_argument("--login", dest = "login", |
||
127 | metavar = "LOGIN", |
||
128 | default = None, |
||
129 | help = "Login name of the AI player.", |
||
130 | ) |
||
131 | parser_ai.add_argument("--password", dest = "password", |
||
132 | metavar = "PASSWORD", |
||
133 | default = None, |
||
134 | help = "Corresponding password of the AI player.", |
||
135 | ) |
||
136 | parser_ai.add_argument("--ai", dest = "ai", |
||
137 | metavar = "AI", |
||
138 | default = None, |
||
139 | help = "Type of the AI applied." |
||
140 | ) |
||
141 | parser_ai.add_argument("--game", dest = "game", |
||
142 | metavar = "NAME", |
||
143 | default = 'Alpha', |
||
144 | help = "Name of game to which the AI belongs", |
||
145 | ) |
||
146 | parser_ai.add_argument("--test-connection", dest = "test", |
||
147 | action = "store_true", default=False, |
||
148 | help = argparse.SUPPRESS |
||
149 | ) |
||
150 | parser_ai.add_argument("--galaxy", dest = "galaxies", |
||
151 | metavar = "NAME", |
||
152 | action = "append", |
||
153 | default = [], |
||
154 | help = "Name of galaxy to enable AI for, no argument means all galaxies" |
||
155 | ) |
||
156 | |||
157 | |||
158 | # ai-pool |
||
159 | parser_ai_pool.add_argument("--procs", dest = "procs", |
||
160 | metavar = "PROCS", |
||
161 | default = multiprocessing.cpu_count() * 4, |
||
162 | type=int, |
||
163 | help = "Maximum number of concurrent processes, default is 4 times cpu count." |
||
164 | ) |
||
165 | parser_ai_pool.add_argument("--galaxy", dest = "galaxies", |
||
166 | metavar = "NAME", |
||
167 | action = "append", |
||
168 | default = [], |
||
169 | help = "Name of galaxy to enable AI for, no argument means all galaxies" |
||
170 | ) |
||
171 | parser_ai_pool.add_argument("--game", dest = "game", |
||
172 | metavar = "NAME", |
||
173 | default = "Alpha", |
||
174 | help = "Name of the game for which the AIs should be run.", |
||
175 | ) |
||
176 | |||
177 | options = parser.parse_args() |
||
178 | options.configDir = os.path.expanduser(options.configDir) |
||
179 | if options.local: |
||
180 | # we will set localhost as a connections |
||
181 | options.server = 'localhost:9080' |
||
182 | |||
183 | if subcommand == 'server': |
||
184 | from main_server import runServer |
||
185 | task = runServer |
||
186 | |||
187 | elif subcommand == 'ai': |
||
188 | from main_ai import runAIClient |
||
189 | task = runAIClient |
||
190 | |||
191 | elif subcommand == 'ai-pool': |
||
192 | from main_ai_pool import runAIPool |
||
193 | task = runAIPool |
||
194 | |||
195 | # basically default (as we force it in case of nonexistent subcommand |
||
196 | elif subcommand == 'client': |
||
197 | from main_client import runClient |
||
198 | task = runClient |
||
199 | |||
200 | if __name__ == '__main__': |
||
201 | if not os.path.exists(options.configDir): |
||
202 | os.makedirs(options.configDir) |
||
203 | # first, we have to initialize Rules (to provide it with configDir) |
||
204 | import ige.ospace.Rules as Rules |
||
205 | Rules.init(options.configDir) |
||
206 | if options.profile: |
||
207 | import cProfile |
||
208 | profiling_output = '{0}.raw'.format(options.profile) |
||
209 | cProfile.run('task(options)', profiling_output ) |
||
210 | import pstats |
||
211 | with open(options.profile, 'w') as pro_file: |
||
212 | stats = pstats.Stats(profiling_output, stream=pro_file) |
||
213 | stats.strip_dirs() |
||
214 | stats.sort_stats('time') |
||
215 | stats.print_stats() |
||
216 | else: |
||
217 | task(options) |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
218 | |||
219 | exit() |
||
220 | |||
221 |