Mostly networking
[progcomp2013.git] / qchess / network.py
1 import socket
2
3 class Network():
4         def __init__(self, colour, address = None):
5                 self.socket = socket.socket()
6
7                 if colour == "white":
8                         self.port = 4563
9                 else:
10                         self.port = 4564
11
12                 self.src = None
13
14                 if address == None:
15                         self.host = 'localhost' #socket.gethostname()
16                         self.socket.bind((self.host, self.port))
17                         self.socket.listen(5)   
18
19                         self.src, self.address = self.socket.accept()
20                 else:
21                         self.host = address
22                         self.socket.connect(('localhost', self.port))
23                         self.src = self.socket
24
25         def getline(self):
26                 s = self.src.recv(1)
27                 while s[len(s)-1] != '\n':
28                         s += self.src.recv(1)
29                 return s
30                 
31
32                 
33
34 class NetworkSender(Player,Network):
35         def __init__(self, base_player, board, address = None):
36                 self.base_player = base_player
37                 Player.__init__(self, base_player.name, base_player.colour)
38                 Network.__init__(self, base_player.colour, address)
39
40                 self.board = board
41
42         def select(self):
43                 [x,y] = self.base_player.select()
44                 choice = self.board.grid[x][y]
45                 s = str(x) + " " + str(y)
46                 print str(self) + ".select sends " + s
47                 self.src.send(s + "\n")
48                 return [x,y]
49
50         def get_move(self):
51                 [x,y] = self.base_player.get_move()
52                 s = str(x) + " " + str(y)
53                 print str(self) + ".get_move sends " + s
54                 self.src.send(s + "\n")
55                 return [x,y]
56
57         def update(self, s):
58                 self.base_player.update(s)
59                 s = s.split(" ")
60                 [x,y] = map(int, s[0:2])
61                 selected = self.board.grid[x][y]
62                 if selected != None and selected.colour == self.colour and len(s) > 2 and not "->" in s:
63                         s = " ".join(s[0:3])
64                         for i in range(2):
65                                 if selected.types_revealed[i] == True:
66                                         s += " " + str(selected.types[i])
67                                 else:
68                                         s += " unknown"
69                         print str(self) + ".update sends " + s
70                         self.src.send(s + "\n")
71                                 
72
73         def quit(self, final_result):
74                 self.base_player.quit(final_result)
75                 self.src.close()
76
77 class NetworkReceiver(Player,Network):
78         def __init__(self, colour, board, address=None):
79                 
80                 Player.__init__(self, address, colour)
81
82                 Network.__init__(self, colour, address)
83
84                 self.board = board
85                         
86
87         def select(self):
88                 s = self.getline().strip(" \r\n")
89                 return map(int,s.split(" "))
90         def get_move(self):
91                 s = self.getline().strip(" \r\n")
92                 print str(self) + ".get_move gets " + s
93                 return map(int, s.split(" "))
94
95         def update(self, result):
96                 
97                 result = result.split(" ")
98                 [x,y] = map(int, result[0:2])
99                 selected = self.board.grid[x][y]
100                 if selected != None and selected.colour == self.colour and len(result) > 2 and not "->" in result:
101                         s = self.getline().strip(" \r\n")
102                         print str(self) + ".update - receives " + str(s)
103                         s = s.split(" ")
104                         selected.choice = int(s[2])
105                         for i in range(2):
106                                 selected.types[i] = str(s[3+i])
107                                 if s[3+i] == "unknown":
108                                         selected.types_revealed[i] = False
109                                 else:
110                                         selected.types_revealed[i] = True
111                         selected.current_type = selected.types[selected.choice] 
112                 else:
113                         print str(self) + ".update - ignore result " + str(result)                      
114                 
115
116         def quit(self, final_result):
117                 self.src.close()
118         

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