qchess.cgi 1.05 KB
Newer Older
Sam Moore's avatar
Sam Moore committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#!/usr/bin/python

# CGI wrapper to qchess

import sys
import os

import cgi
import subprocess

def main(argv):
	form = cgi.FieldStorage()
	client = cgi.escape(os.environ["REMOTE_ADDR"])
	
	try:
		with open(client): pass
	except IOError:
		args = ["python", "../qchess/qchess.py", "--no-graphics", "@fifo:"+client, "@internal:AgentBishop"]
		subprocess.Popen(args)
		form["mode"] = "query"
	
	if form["mode"] == "response":
		x = int(form["x"])
		y = int(form["y"])
		fifo_out = open(client+".in", "w")
		fifo_out.write("%d %d\n" % (x, y))
		fifo_out.close()
		form["mode"] = "query"
	
		
	if form["mode"] == "query":
		fifo_in = open(client+".out", "r")
		s = fifo_in.readline().strip(" \r\n")
		while s != "SELECT?" and s != "MOVE?" and s.split(" ")[0] != "white" and s.split(" ")[0] != "black":
			print s
			s = fifo_in.readline().strip(" \r\n")
		print s
		fifo_in.close()
		form["mode"] = "response"
		
		if s == "quit":
			os.remove(client)
			
		
	return 0


if __name__ == "__main__":
	try:
		sys.exit(main(sys.argv))
	except, e:
		print "Exception: ", e
		sys.exit(1)