44aa36075a8444ace5777991b5d1141923d020ac
[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                         #print str(s)
31                         if len(s) > 1:
32                                 if s[1] != "":
33                                         ip = s[1]
34                         if len(s) > 2:
35                                 port = int(s[2])
36                                 
37                         if ip == None:
38                                 if colour == "black":
39                                         port += 1
40                         elif colour == "white":
41                                 port += 1
42                                                 
43                         return NetworkPlayer(colour, Network((ip, port)), None)
44                 if s[0] == "internal":
45
46                         import inspect
47                         internal_agents = inspect.getmembers(sys.modules[__name__], inspect.isclass)
48                         internal_agents = [x for x in internal_agents if issubclass(x[1], InternalAgent)]
49                         internal_agents.remove(('InternalAgent', InternalAgent)) 
50                         
51                         if len(s) != 2:
52                                 sys.stderr.write(sys.argv[0] + " : '@internal' should be followed by ':' and an agent name\n")
53                                 sys.stderr.write(sys.argv[0] + " : Choices are: " + str(map(lambda e : e[0], internal_agents)) + "\n")
54                                 return None
55
56                         for a in internal_agents:
57                                 if s[1] == a[0]:
58                                         return a[1](name, colour)
59                         
60                         sys.stderr.write(sys.argv[0] + " : Can't find an internal agent matching \"" + s[1] + "\"\n")
61                         sys.stderr.write(sys.argv[0] + " : Choices are: " + str(map(lambda e : e[0], internal_agents)) + "\n")
62                         return None
63                 if s[0] == "fifo":
64                         if len(s) > 1:
65                                 return FifoPlayer(s[1], colour)
66                         else:
67                                 return FifoPlayer(str(os.getpid())+"."+colour, colour)
68
69         else:
70                 return ExternalAgent(name, colour)
71                         
72
73
74 # The main function! It does the main stuff!
75 def main(argv):
76
77         # Apparently python will silently treat things as local unless you do this
78         # Anyone who says "You should never use a global variable" can die in a fire
79         global game
80         global graphics
81         
82         global turn_delay
83         global agent_timeout
84         global log_files
85         global src_file
86         global graphics_enabled
87         global always_reveal_states
88         global sleep_timeout
89
90
91         retry_illegal = False
92         server_addr = None
93
94         max_moves = None
95         src_file = None
96         
97         style = "quantum"
98         colour = "white"
99
100         # Get the important warnings out of the way
101         if platform.system() == "Windows":
102                 sys.stderr.write(sys.argv[0] + " : Warning - You are using " + platform.system() + "\n")
103                 if platform.release() == "Vista":
104                         sys.stderr.write(sys.argv[0] + " : God help you.\n")
105         
106
107         players = []
108         i = 0
109         while i < len(argv)-1:
110                 i += 1
111                 arg = argv[i]
112                 if arg[0] != '-':
113                         p = make_player(arg, colour)
114                         if not isinstance(p, Player):
115                                 sys.stderr.write(sys.argv[0] + " : Fatal error creating " + colour + " player\n")
116                                 return 100
117                         players.append(p)
118                         if colour == "white":
119                                 colour = "black"
120                         elif colour == "black":
121                                 pass
122                         else:
123                                 sys.stderr.write(sys.argv[0] + " : Too many players (max 2)\n")
124                         continue
125
126                 # Option parsing goes here
127                 if arg[1] == '-' and arg[2:] == "classical":
128                         style = "classical"
129                 elif arg[1] == '-' and arg[2:] == "quantum":
130                         style = "quantum"
131                 elif arg[1] == '-' and arg[2:] == "reveal":
132                         always_reveal_states = True
133                 elif (arg[1] == '-' and arg[2:] == "graphics"):
134                         graphics_enabled = True
135                 elif (arg[1] == '-' and arg[2:] == "no-graphics"):
136                         graphics_enabled = False
137                 elif (arg[1] == '-' and arg[2:].split("=")[0] == "file"):
138                         # Load game from file
139                         if len(arg[2:].split("=")) == 1:
140                                 src_file = sys.stdin
141                         else:
142                                 f = arg[2:].split("=")[1]
143                                 if f[0:7] == "http://":
144                                         src_file = HttpReplay(f)
145                                 else:
146                                         src_file = FileReplay(f.split(":")[0])
147
148                                         if len(f.split(":")) == 2:
149                                                 max_moves = int(f.split(":")[1])
150                                                 
151                 elif (arg[1] == '-' and arg[2:].split("=")[0] == "server"):
152                         #debug("Server: " + str(arg[2:]))
153                         if len(arg[2:].split("=")) <= 1:
154                                 server_addr = True
155                         else:
156                                 server_addr = arg[2:].split("=")[1]
157                         
158                 elif (arg[1] == '-' and arg[2:].split("=")[0] == "log"):
159                         # Log file
160                         if len(arg[2:].split("=")) == 1:
161                                 log_files.append(LogFile(sys.stdout,""))
162                         else:
163                                 f = arg[2:].split("=")[1]
164                                 if f == "":
165                                         log_files.append(LogFile(sys.stdout, ""))
166                                 elif f[0] == '@':
167                                         log_files.append(ShortLog(f[1:]))
168                                 else:
169                                         log_files.append(LogFile(open(f, "w", 0), f))
170                 elif (arg[1] == '-' and arg[2:].split("=")[0] == "delay"):
171                         # Delay
172                         if len(arg[2:].split("=")) == 1:
173                                 turn_delay = 0
174                         else:
175                                 turn_delay = float(arg[2:].split("=")[1])
176
177                 elif (arg[1] == '-' and arg[2:].split("=")[0] == "timeout"):
178                         # Timeout
179                         if len(arg[2:].split("=")) == 1:
180                                 agent_timeout = -1
181                         else:
182                                 agent_timeout = float(arg[2:].split("=")[1])
183                 elif (arg[1] == '-' and arg[2:].split("=")[0] == "blackout"):
184                         # Screen saver delay
185                         if len(arg[2:].split("=")) == 1:
186                                 sleep_timeout = -1
187                         else:
188                                 sleep_timeout = float(arg[2:].split("=")[1])
189                 elif (arg[1] == '-' and arg[2:] == "retry-illegal"):
190                         retry_illegal = not retry_illegal
191                 elif (arg[1] == '-' and arg[2:] == "help"):
192                         # Help
193                         os.system("less data/help.txt") # The best help function
194                         return 0
195                 
196         # Dedicated server?
197         
198         #debug("server_addr = " + str(server_addr))
199         
200         if server_addr != None:
201                 if server_addr == True:
202                         return dedicated_server()
203                 else:
204                         if len(players) > 1:
205                                 sys.stderr.write("Only a single player may be provided when --server is used\n")
206                                 return 1
207                         if len(players) == 1:
208                                 return client(server_addr, players[0].name)
209                         else:
210                                 return client(server_addr)
211                 
212
213         # Create the board
214         
215         # Construct a GameThread! Make it global! Damn the consequences!
216                         
217         if src_file != None:
218                 # Hack to stop ReplayThread from exiting
219                 #if len(players) == 0:
220                 #       players = [HumanPlayer("dummy", "white"), HumanPlayer("dummy", "black")]
221
222                 # Normally the ReplayThread exits if there are no players
223                 # TODO: Decide which behaviour to use, and fix it
224                 end = (len(players) == 0)
225                 if end:
226                         players = [Player("dummy", "white"), Player("dummy", "black")]
227                 elif len(players) != 2:
228                         sys.stderr.write(sys.argv[0] + " : Usage " + sys.argv[0] + " white black\n")
229                         if graphics_enabled:
230                                 sys.stderr.write(sys.argv[0] + " : (You won't get a GUI, because --file was used, and the author is lazy)\n")
231                         return 44
232                 game = ReplayThread(players, src_file, end=end, max_moves=max_moves)
233         else:
234                 board = Board(style)
235                 board.max_moves = max_moves
236                 game = GameThread(board, players) 
237                 game.retry_illegal = retry_illegal
238
239
240
241
242         # Initialise GUI
243         if graphics_enabled == True:
244                 try:
245                         graphics = GraphicsThread(game.board, grid_sz = [64,64]) # Construct a GraphicsThread!
246                         
247                         graphics.sleep_timeout = sleep_timeout
248
249                 except Exception,e:
250                         graphics = None
251                         sys.stderr.write(sys.argv[0] + " : Got exception trying to initialise graphics\n"+str(e.message)+"\nDisabled graphics\n")
252                         graphics_enabled = False
253
254         # If there are no players listed, display a nice pretty menu
255         if len(players) != 2:
256                 if graphics != None:
257                         
258                         server_addr = graphics.SelectServer()
259                         if server_addr != None:
260                                 pygame.quit() # Time to say goodbye
261                                 if server_addr == True:
262                                         return dedicated_server()
263                                 else:
264                                         return client(server_addr)      
265                         
266                         players = graphics.SelectPlayers(players)
267                 else:
268                         sys.stderr.write(sys.argv[0] + " : Usage " + sys.argv[0] + " white black\n")
269                         return 44
270
271         # If there are still no players, quit
272         if players == None or len(players) != 2:
273                 sys.stderr.write(sys.argv[0] + " : Graphics window closed before players chosen\n")
274                 return 45
275
276         old = players[:]
277         for p in old:
278                 if isinstance(p, NetworkPlayer):
279                         for i in range(len(old)):
280                                 if old[i] == p or isinstance(old[i], NetworkPlayer):
281                                         continue
282                                 players[i] = NetworkPlayer(old[i].colour, p.network, old[i])
283                 
284         for p in players:
285                 #debug(str(p))
286                 if isinstance(p, NetworkPlayer):
287                         p.board = game.board
288                         if not p.network.connected:
289                                 if not p.network.server:
290                                         time.sleep(0.2)
291                                 p.network.connect()
292                                 
293         
294         # If using windows, select won't work; use horrible TimeoutPlayer hack
295         if agent_timeout > 0:
296                 if platform.system() == "Windows":
297                         for i in range(len(players)):
298                                 if isinstance(players[i], ExternalAgent) or isinstance(players[i], InternalAgent):
299                                         players[i] = TimeoutPlayer(players[i], agent_timeout)
300
301                 else:
302                         warned = False
303                         # InternalAgents get wrapped to an ExternalAgent when there is a timeout
304                         # This is not confusing at all.
305                         for i in range(len(players)):
306                                 if isinstance(players[i], InternalAgent):
307                                                 players[i] = ExternalWrapper(players[i])
308
309
310                 
311
312
313
314
315         log_init(game.board, players)
316         
317         
318         if graphics != None:
319                 game.start() # This runs in a new thread
320                 graphics.run()
321                 if game.is_alive():
322                         game.join()
323         
324
325                 error = game.error + graphics.error
326         else:
327                 game.run()
328                 error = game.error
329         
330
331         for l in log_files:
332                 l.close()
333
334         if src_file != None and src_file != sys.stdin:
335                 src_file.close()
336
337         sys.stdout.write(game.final_result + "\n")
338
339         return error
340                 
341                 
342         
343                 
344         
345                 
346                 
347
348 # This is how python does a main() function...
349 if __name__ == "__main__":
350         retcode = 0
351         try:
352                 retcode = main(sys.argv)
353         except KeyboardInterrupt:
354                 sys.stderr.write(sys.argv[0] + " : Got KeyboardInterrupt. Stopping everything\n")
355                 if isinstance(graphics, StoppableThread):
356                         graphics.stop()
357                         graphics.run() # Will clean up graphics because it is stopped, not run it (a bit dodgy)
358
359                 if isinstance(game, StoppableThread):
360                         game.stop()
361                         if game.is_alive():
362                                 game.join()
363                 retcode = 102
364         #except Exception, e:
365         #       sys.stderr.write(sys.argv[0] + " : " + e.message + "\n")
366         #       retcode = 103   
367                 
368         try:
369                 sys.stdout.close()
370         except:
371                 pass
372         try:
373                 sys.stderr.close()
374         except:
375                 pass
376         sys.exit(retcode)
377                 
378

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