-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocol.py
208 lines (187 loc) · 5.68 KB
/
protocol.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
class Protocol:
"""
规定:
数据包头部占4字节
整型占4字节
字符串长度位占2字节
字符串不定长
"""
def __init__(self, bs=None):
"""
如果bs为None则代表需要创建一个数据包
否则代表需要解析一个数据包
"""
if bs:
self.bs = bytearray(bs)
else:
self.bs = bytearray(0)
def get_int32(self):
try:
ret = self.bs[:4]
self.bs = self.bs[4:]
return int.from_bytes(ret, byteorder='little')
except:
raise Exception("数据异常!")
def get_str(self):
try:
# 拿到字符串字节长度(字符串长度位2字节)
length = int.from_bytes(self.bs[:2], byteorder='little')
# 再拿字符串
ret = self.bs[2:length + 2]
# 删掉取出来的部分
self.bs = self.bs[2 + length:]
return ret.decode(encoding='utf8')
except:
raise Exception("数据异常!")
def add_int32(self, val):
bytes_val = bytearray(val.to_bytes(4, byteorder='little'))
self.bs += bytes_val
def add_str(self, val):
bytes_val = bytearray(val.encode(encoding='utf8'))
bytes_length = bytearray(len(bytes_val).to_bytes(2, byteorder='little'))
self.bs += (bytes_length + bytes_val)
def get_pck_not_head(self):
return self.bs
def get_pck_has_head(self):
bytes_pck_length = bytearray(len(self.bs).to_bytes(4, byteorder='little'))
return bytes_pck_length + self.bs
def s_connection():
p = Protocol()
p.add_str("successful connection")
return p.get_pck_has_head()
def original_handbrick(brick_list):
p = Protocol()
p.add_str("original brick")
p.add_int32(len(brick_list))
for brick in brick_list:
p.add_int32(brick)
return p.get_pck_has_head()
def send_place_brick(ID,brick_idx): #place a new brick
p = Protocol()
p.add_str("place brick")
p.add_int32(ID)
p.add_int32(brick_idx)
return p.get_pck_has_head()
def new_brick(ID,brick_idx): #tell the client the new brick index
p = Protocol()
p.add_str("new brick")
p.add_int32(ID)
p.add_int32(brick_idx)
return p.get_pck_has_head()
def acquire_msg(brick_idx,company_id):
p = Protocol()
p.add_str("acquire msg")
p.add_int32(brick_idx)
p.add_int32(company_id)
return p.get_pck_has_head()
def buy_stock_msg(buy_stock_list): #which stock the player bought
p = Protocol()
p.add_str("buy stock")
for i in range(7):
p.add_int32(buy_stock_list[i])
return p.get_pck_has_head()
def deal_stock_msg(ID,sell,change,small,large):
p = Protocol()
p.add_str("deal stock choice")
p.add_int32(ID)
p.add_int32(sell)
p.add_int32(change)
p.add_int32(small)
p.add_int32(large)
return p.get_pck_has_head()
def send_exit():
p = Protocol()
p.add_str("exit")
return p.get_pck_has_head()
def get_id(id):
p = Protocol()
p.add_str("id")
p.add_int32(id)
return p.get_pck_has_head()
def send_name(ID,name):
p = Protocol()
p.add_str("name")
p.add_int32(ID)
p.add_str(name)
return p.get_pck_has_head()
def send_name_list(origin_list,sequence):
p = Protocol()
p.add_str("name list")
p.add_int32(len(origin_list))
for i in range(len(origin_list)):
p.add_str(origin_list[sequence[i]])
return p.get_pck_has_head()
def send_end_turn(ID):
p = Protocol()
p.add_str("end turn")
p.add_int32(ID)
return p.get_pck_has_head()
def send_establish(company_id):
p = Protocol()
p.add_str("establish")
p.add_int32(company_id)
return p.get_pck_has_head()
def pck_handler(pck):
p = Protocol(pck)
pck_type = p.get_str()
# if pck_type != "":
# print(pck_type)
print(pck_type)
if pck_type=="successful connection":
print("Successfully connect the server!")
return [-2,]
elif pck_type=="exit":
return [-1,]
elif pck_type=="place brick":
ID = p.get_int32()
brick_idx = p.get_int32()
# print("%s place a brick on %s" %(ID,brick_idx))
return [0,ID,brick_idx]
elif pck_type=="new brick":
ID = p.get_int32()
brick_idx = p.get_int32()
# print("%s get brick %s" %(ID,brick_idx))
return [1,ID,brick_idx]
elif pck_type=="id":
ID = p.get_int32()
return [2,ID]
elif pck_type=="acquire msg":
brick_idx = p.get_int32()
company_id = p.get_int32()
return [3,brick_idx,company_id]
elif pck_type=="name":
ID = p.get_int32()
name = p.get_str()
return [4,ID,name]
elif pck_type=="name list":
length = p.get_int32()
name_list = []
for i in range(length):
name_list.append(p.get_str())
return [5,length,name_list]
elif pck_type=="original brick":
length = p.get_int32()
brick_list = []
for i in range(length):
brick_list.append(p.get_int32())
return [6,brick_list]
elif pck_type=="buy stock":
stock_list = []
for i in range(7):
stock_list.append(p.get_int32())
return [7,stock_list]
elif pck_type=="end turn":
ID = p.get_int32()
return [8,ID]
elif pck_type=="establish":
company_id = p.get_int32()
return [9,company_id]
elif pck_type=="deal stock choice":
ID = p.get_int32()
sell = p.get_int32()
change = p.get_int32()
small = p.get_int32()
large = p.get_int32()
return [10,ID,sell,change,small,large]
else:
return [-3,]