Made networking work hopefully
[progcomp2013.git] / qchess / src / main.py
1 #!/usr/bin/python -u
2
3 # Do you know what the -u does? It unbuffers stdin and stdout
4 # I can't remember why, but last year things broke without that
5
6 """
7         UCC::Progcomp 2013 Quantum Chess game
8         @author Sam Moore [SZM] "matches"
9         @copyright The University Computer Club, Incorporated
10                 (ie: You can copy it for not for profit purposes)
11 """
12
13 # system python modules or whatever they are called
14 import sys
15 import os
16 import time
17
18 turn_delay = 0.5
19 sleep_timeout = None
20 [game, graphics] = [None, None]
21
22 def make_player(name, colour):
23         if name[0] == '@':
24                 if name[1:] == "human":
25                         return HumanPlayer(name, colour)
26                 s = name[1:].split(":")
27                 if s[0] == "network":
28                         ip = None
29                         port = 4562
30                         if len(s) > 1:
31                                 ip = s[1]
32                                 
33                         if ip == None:
34                                 if colour == "black":
35                                         port = 4563
36                         elif colour == "white":
37                                 port = 4563
38                                                 
39                         return NetworkPlayer(colour, Network((ip, port)), None)
40                 if s[0] == "internal":
41
42                         import inspect
43                         internal_agents = inspect.getmembers(sys.modules[__name__], inspect.isclass)
44                         internal_agents = [x for x in internal_agents if issubclass(x[1], InternalAgent)]
45                         internal_agents.remove(('InternalAgent', InternalAgent)) 
46                         
47                         if len(s) != 2:
48                                 sys.stderr.write(sys.argv[0] + " : '@internal' should be followed by ':' and an agent name\n")
49                                 sys.stderr.write(sys.argv[0] + " : Choices are: " + str(map(lambda e : e[0], internal_agents)) + "\n")
50                                 return None
51
52                         for a in internal_agents:
53                                 if s[1] == a[0]:
54                                         return a[1](name, colour)
55                         
56                         sys.stderr.write(sys.argv[0] + " : Can't find an internal agent matching \"" + s[1] + "\"\n")
57                         sys.stderr.write(sys.argv[0] + " : Choices are: " + str(map(lambda e : e[0], internal_agents)) + "\n")
58                         return None
59                         
60
61         else:
62                 return ExternalAgent(name, colour)
63                         
64
65
66 # The main function! It does the main stuff!
67 def main(argv):
68
69         # Apparently python will silently treat things as local unless you do this
70         # Anyone who says "You should never use a global variable" can die in a fire
71         global game
72         global graphics
73         
74         global turn_delay
75         global agent_timeout
76         global log_files
77         global src_file
78         global graphics_enabled
79         global always_reveal_states
80         global sleep_timeout
81
82         max_moves = None
83         src_file = None
84         
85         style = "quantum"
86         colour = "white"
87
88         # Get the important warnings out of the way
89         if platform.system() == "Windows":
90                 sys.stderr.write(sys.argv[0] + " : Warning - You are using " + platform.system() + "\n")
91                 if platform.release() == "Vista":
92                         sys.stderr.write(sys.argv[0] + " : God help you.\n")
93         
94
95         players = []
96         i = 0
97         while i < len(argv)-1:
98                 i += 1
99                 arg = argv[i]
100                 if arg[0] != '-':
101                         p = make_player(arg, colour)
102                         if not isinstance(p, Player):
103                                 sys.stderr.write(sys.argv[0] + " : Fatal error creating " + colour + " player\n")
104                                 return 100
105                         players.append(p)
106                         if colour == "white":
107                                 colour = "black"
108                         elif colour == "black":
109                                 pass
110                         else:
111                                 sys.stderr.write(sys.argv[0] + " : Too many players (max 2)\n")
112                         continue
113
114                 # Option parsing goes here
115                 if arg[1] == '-' and arg[2:] == "classical":
116                         style = "classical"
117                 elif arg[1] == '-' and arg[2:] == "quantum":
118                         style = "quantum"
119                 elif arg[1] == '-' and arg[2:] == "reveal":
120                         always_reveal_states = True
121                 elif (arg[1] == '-' and arg[2:] == "graphics"):
122                         graphics_enabled = True
123                 elif (arg[1] == '-' and arg[2:] == "no-graphics"):
124                         graphics_enabled = False
125                 elif (arg[1] == '-' and arg[2:].split("=")[0] == "file"):
126                         # Load game from file
127                         if len(arg[2:].split("=")) == 1:
128                                 src_file = sys.stdin
129                         else:
130                                 f = arg[2:].split("=")[1]
131                                 if f[0:7] == "http://":
132                                         src_file = HttpReplay(f)
133                                 else:
134                                         src_file = FileReplay(f.split(":")[0])
135
136                                         if len(f.split(":")) == 2:
137                                                 max_moves = int(f.split(":")[1])
138
139                 elif (arg[1] == '-' and arg[2:].split("=")[0] == "log"):
140                         # Log file
141                         if len(arg[2:].split("=")) == 1:
142                                 log_files.append(LogFile(sys.stdout))
143                         else:
144                                 f = arg[2:].split("=")[1]
145                                 if f[0] == '@':
146                                         log_files.append(ShortLog(f[1:]))
147                                 else:
148                                         log_files.append(LogFile(open(f, "w", 0)))
149                 elif (arg[1] == '-' and arg[2:].split("=")[0] == "delay"):
150                         # Delay
151                         if len(arg[2:].split("=")) == 1:
152                                 turn_delay = 0
153                         else:
154                                 turn_delay = float(arg[2:].split("=")[1])
155
156                 elif (arg[1] == '-' and arg[2:].split("=")[0] == "timeout"):
157                         # Timeout
158                         if len(arg[2:].split("=")) == 1:
159                                 agent_timeout = -1
160                         else:
161                                 agent_timeout = float(arg[2:].split("=")[1])
162                 elif (arg[1] == '-' and arg[2:].split("=")[0] == "blackout"):
163                         # Screen saver delay
164                         if len(arg[2:].split("=")) == 1:
165                                 sleep_timeout = -1
166                         else:
167                                 sleep_timeout = float(arg[2:].split("=")[1])
168                                 
169                 elif (arg[1] == '-' and arg[2:] == "help"):
170                         # Help
171                         os.system("less data/help.txt") # The best help function
172                         return 0
173
174
175         # Create the board
176         
177         # Construct a GameThread! Make it global! Damn the consequences!
178                         
179         if src_file != None:
180                 # Hack to stop ReplayThread from exiting
181                 #if len(players) == 0:
182                 #       players = [HumanPlayer("dummy", "white"), HumanPlayer("dummy", "black")]
183
184                 # Normally the ReplayThread exits if there are no players
185                 # TODO: Decide which behaviour to use, and fix it
186                 end = (len(players) == 0)
187                 if end:
188                         players = [Player("dummy", "white"), Player("dummy", "black")]
189                 elif len(players) != 2:
190                         sys.stderr.write(sys.argv[0] + " : Usage " + sys.argv[0] + " white black\n")
191                         if graphics_enabled:
192                                 sys.stderr.write(sys.argv[0] + " : (You won't get a GUI, because --file was used, and the author is lazy)\n")
193                         return 44
194                 game = ReplayThread(players, src_file, end=end, max_moves=max_moves)
195         else:
196                 board = Board(style)
197                 board.max_moves = max_moves
198                 game = GameThread(board, players) 
199
200
201
202
203         # Initialise GUI
204         if graphics_enabled == True:
205                 try:
206                         graphics = GraphicsThread(game.board, grid_sz = [64,64]) # Construct a GraphicsThread!
207                         
208                         graphics.sleep_timeout = sleep_timeout
209
210                 except Exception,e:
211                         graphics = None
212                         sys.stderr.write(sys.argv[0] + " : Got exception trying to initialise graphics\n"+str(e.message)+"\nDisabled graphics\n")
213                         graphics_enabled = False
214
215         # If there are no players listed, display a nice pretty menu
216         if len(players) != 2:
217                 if graphics != None:
218                         players = graphics.SelectPlayers(players)
219                 else:
220                         sys.stderr.write(sys.argv[0] + " : Usage " + sys.argv[0] + " white black\n")
221                         return 44
222
223         # If there are still no players, quit
224         if players == None or len(players) != 2:
225                 sys.stderr.write(sys.argv[0] + " : Graphics window closed before players chosen\n")
226                 return 45
227
228         old = players[:]
229         for p in old:
230                 if isinstance(p, NetworkPlayer):
231                         for i in range(len(old)):
232                                 if old[i] == p or isinstance(old[i], NetworkPlayer):
233                                         continue
234                                 players[i] = NetworkPlayer(old[i].colour, p.network, old[i])
235                 
236         for p in players:
237                 debug(str(p))
238                 if isinstance(p, NetworkPlayer):
239                         p.board = game.board
240                         if not p.network.connected:
241                                 if not p.network.server:
242                                         time.sleep(0.2)
243                                 p.network.connect()
244                                 
245         
246         # If using windows, select won't work; use horrible TimeoutPlayer hack
247         if agent_timeout > 0:
248                 if platform.system() == "Windows":
249                         for i in range(len(players)):
250                                 if isinstance(players[i], ExternalAgent) or isinstance(players[i], InternalAgent):
251                                         players[i] = TimeoutPlayer(players[i], agent_timeout)
252
253                 else:
254                         warned = False
255                         # InternalAgents get wrapped to an ExternalAgent when there is a timeout
256                         # This is not confusing at all.
257                         for i in range(len(players)):
258                                 if isinstance(players[i], InternalAgent):
259                                                 players[i] = ExternalWrapper(players[i])
260
261
262                 
263
264
265
266
267         log_init(game.board, players)
268         
269         
270         if graphics != None:
271                 game.start() # This runs in a new thread
272                 graphics.run()
273                 if game.is_alive():
274                         game.join()
275         
276
277                 error = game.error + graphics.error
278         else:
279                 game.run()
280                 error = game.error
281         
282
283         for l in log_files:
284                 l.close()
285
286         if src_file != None and src_file != sys.stdin:
287                 src_file.close()
288
289         sys.stdout.write(game.final_result + "\n")
290
291         return error
292
293 # This is how python does a main() function...
294 if __name__ == "__main__":
295         try:
296                 sys.exit(main(sys.argv))
297         except KeyboardInterrupt:
298                 sys.stderr.write(sys.argv[0] + " : Got KeyboardInterrupt. Stopping everything\n")
299                 if isinstance(graphics, StoppableThread):
300                         graphics.stop()
301                         graphics.run() # Will clean up graphics because it is stopped, not run it (a bit dodgy)
302
303                 if isinstance(game, StoppableThread):
304                         game.stop()
305                         if game.is_alive():
306                                 game.join()
307
308                 sys.exit(102)
309

UCC git Repository :: git.ucc.asn.au