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

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