Fix bugs with FifoPlayer and Networking
[progcomp2013.git] / qchess / src / server.py
1 def dedicated_server():
2         global log_files
3         
4         max_games = 5
5         games = []
6         gameID = 0
7         while True:
8                 # Get players
9                 gameID += 1
10                 log("Getting clients...")
11                 s = socket.socket()
12                 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
13                 s.bind(("0.0.0.0", 4562))
14                 s.listen(2)
15                 ss = s.accept()
16                 
17                 log("Got white player")
18                 
19                 args = ["python", "qchess.py", "--no-graphics", "@network::"+str(4600+2*len(games)), "@network::"+str(4600+2*len(games))]
20                 if len(log_files) != 0:
21                         for l in log_files:
22                                 if l.name == "":
23                                         args.append("--log")
24                                 else:
25                                         args.append("--log="+str(l.name)+"_"+str(gameID))
26                 
27                 g = subprocess.Popen(args, stdout=subprocess.PIPE)
28                 games.append(g)
29                 
30                 time.sleep(0.5)
31                 ss[0].send("white " + str(4600 + 2*(len(games)-1)))
32                 ss[0].shutdown(socket.SHUT_RD)
33                 ss[0].close()
34                 
35                 time.sleep(0.5)
36                 ss = s.accept()
37                 
38                 log("Got black player")
39                 
40                 time.sleep(0.5)
41                 ss[0].send("black " + str(4600 + 2*(len(games)-1)))
42                 ss[0].shutdown(socket.SHUT_RD)
43                 ss[0].close()
44                 
45                 s.shutdown(socket.SHUT_RDWR)
46                 s.close()
47                 
48                 
49                 while len(games) > max_games:
50                         #log("Too many games; waiting for game to finish...")
51                         ready = select.select(map(lambda e : e.stdout, games),[], [])
52                         for r in ready[0]:
53                                 s = r.readline().strip(" \r\n").split(" ")
54                                 if s[0] == "white" or s[0] == "black":
55                                         for g in games[:]:
56                                                 if g.stdout == r:
57                                                         log("Game " + str(g) + " has finished")
58                                                         games.remove(g)
59                                                         
60         return 0
61         
62 def client(addr, player="@human"):
63         
64         
65         debug("Client " + player + " starts")
66         s = socket.socket()
67         s.connect((addr, 4562))
68         
69         [colour,port] = s.recv(1024).strip(" \r\n").split(" ")
70         
71         debug("Colour: " + colour + ", port: " + port)
72         
73         s.shutdown(socket.SHUT_RDWR)
74         s.close()
75         
76         if colour == "white":
77                 p = subprocess.Popen(["python", "qchess.py", player, "@network:"+addr+":"+port])
78         else:
79                 p = subprocess.Popen(["python", "qchess.py", "@network:"+addr+":"+port, player])
80         p.wait()
81         return 0

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