Commit 64978647 authored by Sam Moore's avatar Sam Moore

Making dedicated match making server

Start server with `./qchess.py --server`
Connect from clients with `./qchess --server=address`
parent df2c20df
...@@ -1299,7 +1299,9 @@ class Network(): ...@@ -1299,7 +1299,9 @@ class Network():
def close(self):
self.src.shutdown()
self.src.close()
# --- network.py --- # # --- network.py --- #
import threading import threading
...@@ -2251,6 +2253,7 @@ class GraphicsThread(StoppableThread): ...@@ -2251,6 +2253,7 @@ class GraphicsThread(StoppableThread):
#print "Reject " + str(i) + str(event.pos) + " vs " + str(t) #print "Reject " + str(i) + str(event.pos) + " vs " + str(t)
# Function to pick players in a nice GUI way # Function to pick players in a nice GUI way
def SelectPlayers(self, players = []): def SelectPlayers(self, players = []):
...@@ -2313,7 +2316,7 @@ class GraphicsThread(StoppableThread): ...@@ -2313,7 +2316,7 @@ class GraphicsThread(StoppableThread):
time.sleep(1) time.sleep(1)
p = None p = None
players.append(p) players.append(p)
elif choice == 2: elif choice == 1:
address = "" address = ""
while address == "": while address == "":
self.board.display_grid(self.window, self.grid_sz) self.board.display_grid(self.window, self.grid_sz)
...@@ -2340,6 +2343,64 @@ class GraphicsThread(StoppableThread): ...@@ -2340,6 +2343,64 @@ class GraphicsThread(StoppableThread):
# --- graphics.py --- # # --- graphics.py --- #
def dedicated_server():
max_games = 4
games = []
while True:
# Get players
s = socket.socket()
s.bind(("0.0.0.0", 4562))
s.listen(2)
ss = s.accept()
debug("Got white player")
g = subprocess.Popen(["python", "qchess.py", "@network::"+str(4700+len(games)), "@network::"+str(4700+len(games)), "--log="+"_".join(str(datetime.datetime.now()).split(" ")) + ".log"], stdout=subprocess.PIPE)
games.append(g)
ss[0].send("white " + str(4700 + len(games)-1))
ss[0].shutdown(socket.SHUT_RDWR)
ss[0].close()
time.sleep(0.5)
ss = s.accept()
debug("Got black player")
ss[0].send("black " + str(4700 + len(games)-1))
ss[0].shutdown(socket.SHUT_RDWR)
ss[0].close()
s.shutdown(socket.SHUT_RDWR)
s.close()
while len(games) > max_games:
ready = select.select(map(lambda e : e.stdout, games),[], [], None)
for r in ready:
s = r.readline().strip(" \r\n").split(" ")
if s[0] == "white" or s[0] == "black":
for g in games[:]:
if g.stdout == r:
games.remove(g)
def client(addr):
s = socket.socket()
s.connect((addr, 4562))
[colour,port] = s.recv(1024).strip(" \r\n").split(" ")
debug("Colour: " + colour + ", port: " + port)
s.shutdown(socket.SHUT_RDWR)
s.close()
if colour == "white":
p = subprocess.Popen(["python", "qchess.py", "@human", "@network:"+addr+":"+port])
else:
p = subprocess.Popen(["python", "qchess.py", "@network:"+addr+":"+port, "@human"])
p.wait()
sys.exit(0)# --- server.py --- #
#!/usr/bin/python -u #!/usr/bin/python -u
# Do you know what the -u does? It unbuffers stdin and stdout # Do you know what the -u does? It unbuffers stdin and stdout
...@@ -2369,14 +2430,18 @@ def make_player(name, colour): ...@@ -2369,14 +2430,18 @@ def make_player(name, colour):
if s[0] == "network": if s[0] == "network":
ip = None ip = None
port = 4562 port = 4562
#print str(s)
if len(s) > 1: if len(s) > 1:
ip = s[1] if s[1] != "":
ip = s[1]
if len(s) > 2:
port = int(s[2])
if ip == None: if ip == None:
if colour == "black": if colour == "black":
port = 4563 port += 1
elif colour == "white": elif colour == "white":
port = 4563 port += 1
return NetworkPlayer(colour, Network((ip, port)), None) return NetworkPlayer(colour, Network((ip, port)), None)
if s[0] == "internal": if s[0] == "internal":
...@@ -2477,7 +2542,13 @@ def main(argv): ...@@ -2477,7 +2542,13 @@ def main(argv):
if len(f.split(":")) == 2: if len(f.split(":")) == 2:
max_moves = int(f.split(":")[1]) max_moves = int(f.split(":")[1])
elif (arg[1] == '-' and arg[2:] == "server"):
if len(arg[2:].split("=") <= 1):
dedicated_server()
else:
client(arg[2:].split("=")[1])
sys.exit(0)
elif (arg[1] == '-' and arg[2:].split("=")[0] == "log"): elif (arg[1] == '-' and arg[2:].split("=")[0] == "log"):
# Log file # Log file
if len(arg[2:].split("=")) == 1: if len(arg[2:].split("=")) == 1:
...@@ -2631,6 +2702,13 @@ def main(argv): ...@@ -2631,6 +2702,13 @@ def main(argv):
sys.stdout.write(game.final_result + "\n") sys.stdout.write(game.final_result + "\n")
return error return error
# This is how python does a main() function... # This is how python does a main() function...
if __name__ == "__main__": if __name__ == "__main__":
...@@ -2650,4 +2728,4 @@ if __name__ == "__main__": ...@@ -2650,4 +2728,4 @@ if __name__ == "__main__":
sys.exit(102) sys.exit(102)
# --- main.py --- # # --- main.py --- #
# EOF - created from make on Fri Apr 12 17:07:44 WST 2013 # EOF - created from make on Sat Apr 20 10:20:13 WST 2013
# Makefile that builds qchess.py from the component files # Makefile that builds qchess.py from the component files
TARGET = qchess.py TARGET = qchess.py
COMPONENTS = piece.py board.py player.py agent_bishop.py timeout_player.py network.py thread_util.py log.py game.py images.py graphics.py main.py COMPONENTS = piece.py board.py player.py agent_bishop.py timeout_player.py network.py thread_util.py log.py game.py images.py graphics.py server.py main.py
#COMPONENTS=$(shell ls *.py | tr '\t' '\n' | grep -v $(TARGET)) #COMPONENTS=$(shell ls *.py | tr '\t' '\n' | grep -v $(TARGET))
$(TARGET) : $(COMPONENTS) $(TARGET) : $(COMPONENTS)
......
...@@ -367,6 +367,7 @@ class GraphicsThread(StoppableThread): ...@@ -367,6 +367,7 @@ class GraphicsThread(StoppableThread):
#print "Reject " + str(i) + str(event.pos) + " vs " + str(t) #print "Reject " + str(i) + str(event.pos) + " vs " + str(t)
# Function to pick players in a nice GUI way # Function to pick players in a nice GUI way
def SelectPlayers(self, players = []): def SelectPlayers(self, players = []):
...@@ -429,7 +430,7 @@ class GraphicsThread(StoppableThread): ...@@ -429,7 +430,7 @@ class GraphicsThread(StoppableThread):
time.sleep(1) time.sleep(1)
p = None p = None
players.append(p) players.append(p)
elif choice == 2: elif choice == 1:
address = "" address = ""
while address == "": while address == "":
self.board.display_grid(self.window, self.grid_sz) self.board.display_grid(self.window, self.grid_sz)
......
...@@ -27,14 +27,18 @@ def make_player(name, colour): ...@@ -27,14 +27,18 @@ def make_player(name, colour):
if s[0] == "network": if s[0] == "network":
ip = None ip = None
port = 4562 port = 4562
#print str(s)
if len(s) > 1: if len(s) > 1:
ip = s[1] if s[1] != "":
ip = s[1]
if len(s) > 2:
port = int(s[2])
if ip == None: if ip == None:
if colour == "black": if colour == "black":
port = 4563 port += 1
elif colour == "white": elif colour == "white":
port = 4563 port += 1
return NetworkPlayer(colour, Network((ip, port)), None) return NetworkPlayer(colour, Network((ip, port)), None)
if s[0] == "internal": if s[0] == "internal":
...@@ -135,7 +139,13 @@ def main(argv): ...@@ -135,7 +139,13 @@ def main(argv):
if len(f.split(":")) == 2: if len(f.split(":")) == 2:
max_moves = int(f.split(":")[1]) max_moves = int(f.split(":")[1])
elif (arg[1] == '-' and arg[2:] == "server"):
if len(arg[2:].split("=") <= 1):
dedicated_server()
else:
client(arg[2:].split("=")[1])
sys.exit(0)
elif (arg[1] == '-' and arg[2:].split("=")[0] == "log"): elif (arg[1] == '-' and arg[2:].split("=")[0] == "log"):
# Log file # Log file
if len(arg[2:].split("=")) == 1: if len(arg[2:].split("=")) == 1:
...@@ -289,6 +299,13 @@ def main(argv): ...@@ -289,6 +299,13 @@ def main(argv):
sys.stdout.write(game.final_result + "\n") sys.stdout.write(game.final_result + "\n")
return error return error
# This is how python does a main() function... # This is how python does a main() function...
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -165,4 +165,6 @@ class Network(): ...@@ -165,4 +165,6 @@ class Network():
def close(self):
self.src.shutdown()
self.src.close()
def dedicated_server():
max_games = 4
games = []
while True:
# Get players
s = socket.socket()
s.bind(("0.0.0.0", 4562))
s.listen(2)
ss = s.accept()
debug("Got white player")
g = subprocess.Popen(["python", "qchess.py", "@network::"+str(4700+len(games)), "@network::"+str(4700+len(games)), "--log="+"_".join(str(datetime.datetime.now()).split(" ")) + ".log"], stdout=subprocess.PIPE)
games.append(g)
ss[0].send("white " + str(4700 + len(games)-1))
ss[0].shutdown(socket.SHUT_RDWR)
ss[0].close()
time.sleep(0.5)
ss = s.accept()
debug("Got black player")
ss[0].send("black " + str(4700 + len(games)-1))
ss[0].shutdown(socket.SHUT_RDWR)
ss[0].close()
s.shutdown(socket.SHUT_RDWR)
s.close()
while len(games) > max_games:
ready = select.select(map(lambda e : e.stdout, games),[], [], None)
for r in ready:
s = r.readline().strip(" \r\n").split(" ")
if s[0] == "white" or s[0] == "black":
for g in games[:]:
if g.stdout == r:
games.remove(g)
def client(addr):
s = socket.socket()
s.connect((addr, 4562))
[colour,port] = s.recv(1024).strip(" \r\n").split(" ")
debug("Colour: " + colour + ", port: " + port)
s.shutdown(socket.SHUT_RDWR)
s.close()
if colour == "white":
p = subprocess.Popen(["python", "qchess.py", "@human", "@network:"+addr+":"+port])
else:
p = subprocess.Popen(["python", "qchess.py", "@network:"+addr+":"+port, "@human"])
p.wait()
sys.exit(0)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment