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

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