e0c1460d04423ec5e2aa5a5813f798f707f74508
[progcomp2013.git] / web / qchess.cgi
1 #!/usr/bin/python
2
3 # CGI wrapper to qchess
4
5 import sys
6 import os
7
8 import cgi
9 import subprocess
10
11 def main(argv):
12         form = cgi.FieldStorage()
13         client = cgi.escape(os.environ["REMOTE_ADDR"])
14         
15         print "Content-Type: text/plain\r\n\r\n"
16         
17         try:
18                 with open(client): pass
19         except IOError:
20                 args = ["python", "../qchess/qchess.py", "--no-graphics", "@fifo:"+client, "@internal:AgentBishop"]
21                 subprocess.Popen(args)
22                 form["mode"] = "query"
23         
24         if form["mode"] == "response":
25                 x = int(form["x"])
26                 y = int(form["y"])
27                 fifo_out = open(client+".in", "w")
28                 fifo_out.write("%d %d\n" % (x, y))
29                 fifo_out.close()
30                 form["mode"] = "query"
31         
32                 
33         if form["mode"] == "query":
34                 fifo_in = open(client+".out", "r")
35                 s = fifo_in.readline().strip(" \r\n")
36                 while s != "SELECT?" and s != "MOVE?" and s.split(" ")[0] != "white" and s.split(" ")[0] != "black":
37                         print s
38                         s = fifo_in.readline().strip(" \r\n")
39                 print s
40                 fifo_in.close()
41                 form["mode"] = "response"
42                 
43                 if s == "quit":
44                         os.remove(client)
45                         
46                 
47         return 0
48
49
50 if __name__ == "__main__":
51         try:
52                 sys.exit(main(sys.argv))
53         except, e:
54                 print "Exception: ", e
55                 sys.exit(1)

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