Dedicated server stuff should now work
[progcomp2013.git] / qchess / src / server.py
index 95af126..2bae000 100644 (file)
@@ -1,51 +1,74 @@
 def dedicated_server():
-       max_games = 4
+       global log_files
+       
+       max_games = 5
        games = []
+       gameID = 0
        while True:
                # Get players
+               gameID += 1
+               log("Getting clients...")
                s = socket.socket()
+               s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                s.bind(("0.0.0.0", 4562))
                s.listen(2)
                ss = s.accept()
                
-               debug("Got white player")
+               log("Got white player")
+               
+               args = ["python", "qchess.py", "--no-graphics", "@network::"+str(4600+2*len(games)), "@network::"+str(4600+2*len(games))]
+               if len(log_files) != 0:
+                       for l in log_files:
+                               if l.name == "":
+                                       args.append("--log")
+                               else:
+                                       args.append("--log="+str(l.name)+"_"+str(gameID))
                
-               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)
+               g = subprocess.Popen(args, stdout=subprocess.PIPE)
                games.append(g)
                
-               ss[0].send("white " + str(4700 + len(games)-1))
-               ss[0].shutdown(socket.SHUT_RDWR)
+               time.sleep(0.5)
+               ss[0].send("white " + str(4600 + 2*(len(games)-1)))
+               ss[0].shutdown(socket.SHUT_RD)
                ss[0].close()
                
                time.sleep(0.5)
                ss = s.accept()
                
-               debug("Got black player")
+               log("Got black player")
                
-               ss[0].send("black " + str(4700 + len(games)-1))
-               ss[0].shutdown(socket.SHUT_RDWR)
+               time.sleep(0.5)
+               ss[0].send("black " + str(4600 + 2*(len(games)-1)))
+               ss[0].shutdown(socket.SHUT_RD)
                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:
+                       #log("Too many games; waiting for game to finish...")
+                       ready = select.select(map(lambda e : e.stdout, games),[], [])
+                       for r in ready[0]:
                                s = r.readline().strip(" \r\n").split(" ")
                                if s[0] == "white" or s[0] == "black":
                                        for g in games[:]:
                                                if g.stdout == r:
+                                                       log("Game " + str(g) + " has finished")
                                                        games.remove(g)
+                                                       
+       return 0
        
 def client(addr):
        
+       
+       
        s = socket.socket()
        s.connect((addr, 4562))
        
        [colour,port] = s.recv(1024).strip(" \r\n").split(" ")
        
-       debug("Colour: " + colour + ", port: " + port)
+       #debug("Colour: " + colour + ", port: " + port)
        
        s.shutdown(socket.SHUT_RDWR)
        s.close()
@@ -55,4 +78,4 @@ def client(addr):
        else:
                p = subprocess.Popen(["python", "qchess.py", "@network:"+addr+":"+port, "@human"])
        p.wait()
-       sys.exit(0)
\ No newline at end of file
+       return 0
\ No newline at end of file

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