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 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 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 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 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 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 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 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_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(): """ 告诉服务端有新玩家加入 """ # 构建数据包 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 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 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 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 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 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 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 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