def new_animation(self, trigger, charId): ani = Protocol() ani.add_int32(4) ani.add_int32(trigger) ani.add_int32(charId) for r in g_conn_pool: if r != self.get_conn(): r.conn.sendall(ani.get_pck_has_head())
def new_mouse(self): p = Protocol() p.add_int32(3) p.add_int32(self.get_conn().mouseX) p.add_int32(self.get_conn().mouseY) p.add_int32(self.get_conn().charId) for r in g_conn_pool: if r != self.get_conn(): r.conn.sendall(p.get_pck_has_head())
def pck_handler(pck): p = Protocol(pck) try: pck_type = p.get_str() if pck_type == "games": try: return eval(p.get_str()) # print(time.time()) # print(messages[0]) except Exception as e: print(e) except Exception as e: print(e)
def other_role(self): # 告诉当前玩家,其他玩家的信息 for conn in g_conn_pool: if conn != self.get_conn(): ret = Protocol() ret.add_str("newplayer") ret.add_int32(conn.x) ret.add_int32(conn.y) ret.add_str(conn.name) self.request.sendall(ret.get_pck_has_head())
def move_role(self): # 告诉各个客户端有玩家移动了 ret = Protocol() ret.add_int32(0) ret.add_int32(self.get_conn().x) ret.add_int32(self.get_conn().y) ret.add_int32(self.get_conn().charId) for r in g_conn_pool: if r != self.get_conn(): r.conn.sendall(ret.get_pck_has_head())
def move_role(self): # 告诉各个客户端有玩家移动了 ret = Protocol() ret.add_str("playermove") ret.add_int32(self.get_conn().x) ret.add_int32(self.get_conn().y) ret.add_str(self.get_conn().name) for r in g_conn_pool: if r != self.get_conn(): r.conn.sendall(ret.get_pck_has_head())
def new_role(self): # 告诉各个客户端有新玩家加入 ret = Protocol() ret.add_str("newplayer") ret.add_int32(self.get_conn().x) ret.add_int32(self.get_conn().y) ret.add_str(self.get_conn().name) for r in g_conn_pool: if r != self.get_conn(): r.conn.sendall(ret.get_pck_has_head())
def pck_handler(pck): p = Protocol(pck) pck_type = p.get_str() if pck_type == 'playermove': # 玩家移动的数据包 x = p.get_int32() y = p.get_int32() name = p.get_str() for r in g_other_player: if r.name == name: r.x = x r.y = y break elif pck_type == 'newplayer': # 新玩家数据包 x = p.get_int32() y = p.get_int32() name = p.get_str() r = Role(x, y, name) g_other_player.append(r) elif pck_type == 'logout': # 玩家掉线 name = p.get_str() for r in g_other_player: if r.name == name: g_other_player.remove(r) break
def send_role_animation(self, trigger): #发送角色的动画状态给服务端 # 构建数据包 p = Protocol() p.add_int32(4) p.add_int32(trigger) p.add_int32(self.player.charId) data = p.get_pck_has_head() # 发送数据包 self.g_client.sendall(data)
def send_new_mouse(self): #发送角色的鼠标的位置 # 构建数据包 p = Protocol() p.add_int32(3) p.add_int32(self.player.mouseX) p.add_int32(self.player.mouseY) p.add_int32(self.player.charId) data = p.get_pck_has_head() # 发送数据包 self.g_client.sendall(data)
def send_new_role(): """ 告诉服务端有新玩家加入 """ # 构建数据包 p = Protocol() p.add_str("newrole") p.add_int32(g_player.x) p.add_int32(g_player.y) p.add_str(g_player.name) data = p.get_pck_has_head() # 发送数据包 g_client.sendall(data)
def readying(): """ 告诉服务端有新玩家加入 """ # 构建数据包 p = Protocol() # 请求名称,用来区分该请求执行的操作 p.add_str("ready") # 传入角色的名字 p.add_str(deploy.user_name) p.add_str(deploy.isready) data = p.get_pck_has_head() # 发送数据包 deploy.g_client.sendall(data)
def remove(self): # 告诉各个客户端有玩家离线 ret = Protocol() ret.add_str("logout") ret.add_str(self.get_conn().name) for r in g_conn_pool: if r != self.get_conn(): r.conn.sendall(ret.get_pck_has_head()) g_conn_pool.remove(self.get_conn())
def remove(self): # 告诉各个客户端有玩家离线 ret = Protocol() ret.add_int32(2) ret.add_int32(self.get_conn().charId) for r in g_conn_pool: if r != self.get_conn(): r.conn.sendall(ret.get_pck_has_head()) g_conn_pool.remove(self.get_conn())
def pck_handler(self, pck): """ 解析数据包 """ p = Protocol(pck) pck_type = p.get_str() if pck_type == 'newrole': self.get_conn().x = p.get_int32() self.get_conn().y = p.get_int32() self.get_conn().name = p.get_str() self.get_conn().number = len(g_conn_pool) self.get_conn().my = None print(len(g_conn_pool)) for i in g_conn_pool: print(i.__dir__()) print('x : %s ' % i.x) print('y : %s ' % i.y) print('name : %s ' % i.name) print('number : %s ' % i.number) self.new_role() # 告诉当前服务器的其他玩家,有新玩家加入 self.other_role() # 告诉新加入的玩家,当前服务器的其他玩家信息 elif pck_type == 'move': self.get_conn().x = p.get_int32() self.get_conn().y = p.get_int32() self.move_role()
def send_role_move(): """ 发送角色的坐标给服务端 """ # 构建数据包 p = Protocol() p.add_str("move") p.add_int32(g_player.x) p.add_int32(g_player.y) data = p.get_pck_has_head() # 发送数据包 g_client.sendall(data)
def send_new_role(name): """ 告诉服务端有新玩家加入 """ # 构建数据包 p = Protocol() # 请求名称,用来区分该请求执行的操作 p.add_str("newuser") # 传入角色的名字 p.add_str(name) data = p.get_pck_has_head() # 发送数据包 deploy.g_client.sendall(data)
def send_role_move(self): #发送角色的坐标给服务端 # 构建数据包 p = Protocol() p.add_int32(0) x = self.player.rect[0] y = self.player.rect[1] if x < 0: x = 0 if y <= 0: y = 0 p.add_int32(x) p.add_int32(y) data = p.get_pck_has_head() # 发送数据包 self.g_client.sendall(data)
def pck_handler(self, pck): """ 解析数据包 """ p = Protocol(pck) pck_type = p.get_str() if pck_type == 'newrole': self.get_conn().x = p.get_int32() self.get_conn().y = p.get_int32() self.get_conn().name = p.get_str() self.new_role() # 告诉当前服务器的其他玩家,有新玩家加入 self.other_role() # 告诉新加入的玩家,当前服务器的其他玩家信息 elif pck_type == 'move': self.get_conn().x = p.get_int32() self.get_conn().y = p.get_int32() self.move_role()
def handle(self): while True: try: # 读取数据包 bytes = self.request.recv(1024) # 切割数据包 while True: # 读取包长度 length_pck = int.from_bytes(bytes[:4], byteorder='little') # 截取封包 pck = bytes[4:4 + length_pck] # 删除已经读取的字节 bytes = bytes[4 + length_pck:] # 把封包交给处理函数 self.pck_handler(pck) # 如果bytes没数据了,就跳出循环 if len(bytes) == 0: break # print("客户端消息:", bytes.decode(encoding="utf8")) except Exception as e: # 意外掉线 err = e.args[0] if err == 'timed out': pamras = { 'users': eval(MR.get(room['users'])), 'talk': MR.get(room['talk']), 'monster': eval(MR.get(room['monster'])) } for u in pamras['users']: if u['name'] == self.get_conn().user['name']: u['my'] = 'Y' ret = Protocol() ret.add_str('games') ret.add_str(str(pamras)) self.request.sendall(ret.get_pck_has_head()) else: print('用户可能掉线了') break
def send_new_role(): """ 告诉服务端有新玩家加入 """ # 构建数据包 p = Protocol() # 请求名称,用来区分该请求执行的操作 p.add_str("newrole") # 传入角色的x坐标 p.add_int32(0) # 传入角色的y坐标 p.add_int32(1) # 传入角色的名字 p.add_str(deploy.g_player.name) data = p.get_pck_has_head() # 发送数据包 deploy.g_client.sendall(data)
def pck_handler(pck): p = Protocol(pck) pck_type = p.get_str() if pck_type == 'playermove': # 玩家移动的数据包 x = p.get_int32() y = p.get_int32() name = p.get_str() for r in deploy.g_other_player: if r.name == name: r.x = x r.y = y break elif pck_type == 'newplayer': # 新玩家数据包 print('-----------------') print(pck) print(pck.decode('UTF-8')) print('-----------------') x = p.get_int32() y = p.get_int32() name = p.get_str() number = p.get_str() my = p.get_str() race = p.get_str() r = Role(x, y, name, number, my, race) deploy.g_other_player.append(r) elif pck_type == 'logout': # 玩家掉线 name = p.get_str() for r in deploy.g_other_player: if r.name == name: deploy.g_other_player.remove(r) break
def pck_handler(self, pck): p = Protocol(pck) pck_type = p.get_int32() if pck_type == self.netPlayerMove: # 玩家移动的数据包 x = p.get_int32() y = p.get_int32() charId = p.get_int32() for r in self.charList: if r.charId == charId: r.rect[0] = x r.rect[1] = y break elif pck_type == self.netPlayerNew: # 新玩家数据包 playerName = p.get_str() x = p.get_int32() y = p.get_int32() charId = p.get_int32() r = Model(self, charId) r.playerName = playerName self.charList.append(r) elif pck_type == self.netPlayerOff: # 玩家掉线 charId = p.get_int32() for r in self.charList: if r.charId == charId: self.charList.remove(r) break elif pck_type == self.netArmSwordNew: # 手臂和剑状态更新 mouseX = p.get_int32() mouseY = p.get_int32() charId = p.get_int32() for r in self.charList: if r.charId == charId: r.oldMouseX = r.mouseX r.oldMouseY = r.mouseY r.mouseX = mouseX r.mouseY = mouseY #break elif pck_type == self.netAnimationNew: trigger = p.get_int32() charId = p.get_int32() self.charTrigger(trigger, charId) else: return
def send_new_role(self): #告诉服务端有新玩家加入 # 构建数据包 p = Protocol() p.add_int32(1) p.add_str(self.playerName) p.add_int32(self.player.rect[0]) p.add_int32(self.player.rect[1]) p.add_int32(self.player.charId) data = p.get_pck_has_head() # 发送数据包 self.g_client.sendall(data)
def pck_handler(self, pck): """ 解析数据包 """ p = Protocol(pck) pck_type = p.get_int32() if pck_type == 1: self.get_conn().playerName = p.get_str() self.get_conn().x = p.get_int32() self.get_conn().y = p.get_int32() self.get_conn().charId = p.get_int32() self.new_role() # 告诉当前服务器的其他玩家,有新玩家加入 self.other_role() # 告诉新加入的玩家,当前服务器的其他玩家信息 elif pck_type == 0: self.get_conn().x = p.get_int32() self.get_conn().y = p.get_int32() self.move_role() elif pck_type == 3: self.get_conn().mouseX = p.get_int32() self.get_conn().mouseY = p.get_int32() self.get_conn().charId = p.get_int32() self.new_mouse() elif pck_type == 4: trigger = p.get_int32() charId = p.get_int32() self.new_animation(trigger, charId)
def other_role(self): # 告诉当前玩家,其他玩家的信息 for conn in g_conn_pool: ret = Protocol() ret.add_str("newplayer") ret.add_int32(conn.x) ret.add_int32(conn.y) ret.add_str(conn.name) ret.add_str(conn.number) # 本人返回1,非本人返回0 if conn != self.get_conn(): ret.add_str('N') else: ret.add_str('Y') ret.add_str(conn.race) self.request.sendall(ret.get_pck_has_head())
def pck_handler(self, pck): """ 解析数据包 """ global usercount p = Protocol(pck) pck_type = p.get_str() users = eval(MR.get(room['users'])) talk = MR.get(room['talk']) print(users) if pck_type == "newuser": self.get_conn().user['name'] = '%s%s' % (p.get_str(), usercount) usercount += 1 self.get_conn().user['number'] = len(g_conn_pool) users.append(self.get_conn().user) talk += '欢迎%s加入游戏!\n' % self.get_conn().user['name'] MR.set(room['users'], str(users)) MR.set(room['talk'], str(talk)) elif pck_type == "change": pass elif pck_type == "ready": print('收到一个ready请求') user_name = p.get_str() user_number = int(user_name[-1]) user_isready = userreadydict[p.get_str()] # print(user_name) # print(user_isready) for r in g_conn_pool: if r == self.get_conn() and r.user['name'] == user_name: for u in users: if u['name'] == user_name: u['isready'] = user_isready MR.set(room['users'], str(users)) print('修改成功') break break elif pck_type == "talk": pass elif pck_type == "out": pass elif pck_type == "move": pass elif pck_type == "build": pass elif pck_type == "up": pass if pck_type == 'newrole': self.get_conn().x = p.get_int32() self.get_conn().y = p.get_int32() self.get_conn().name = p.get_str() self.get_conn().number = len(g_conn_pool) self.get_conn().my = None self.new_role() # 告诉当前服务器的其他玩家,有新玩家加入 self.other_role() # 告诉新加入的玩家,当前服务器的其他玩家信息 elif pck_type == 'move': self.get_conn().x = p.get_int32() self.get_conn().y = p.get_int32() self.move_role()