def test_new(): pkt = Packet.new(65, 65) assert pkt.buffer == b'AA' pkt = Packet.new(0x4141) assert pkt.buffer == b'AA' pkt = Packet.new([65, 65]) assert pkt.buffer == b'AA'
async def on_player_won(self, player, order, player_time): if self.username == player.username: self.last_record_time = player_time if self.is_playing: return if player.username not in self.stalked_players: return if self._room.map.xml: hole_pos = self._room.map.hole_pos for key in hole_pos: hole_color = key["CT"] if "CT" in key else 0 packet = Packet.new(5, 18).write8(hole_color).write32( self._room.round_code).write32( self._room.map.code).write16(15).write16( key["X"]).write16(key["Y"]) if not self.is_recording: if self.first_stalked_player == player.username: await self.bulle.send(packet) return if self.is_bootcamp_room: if self._room.map.is_reversed: self.stalked_players[ player.username]["map_movements"] *= 0 return await self.sendRoomMessage( "!me Run ignored. Map is reversed") if self.last_record_time < player_time: self.stalked_players[ player.username]["map_movements"] *= 0 return self.last_record_time = player_time await self.save_movement(player.username, str(bytes(packet)), "hole") loop.create_task( update_maps_storage( "@" + str(self._room.map.code), (ujson.dumps(self.stalked_players[player.username] ["map_movements"], ensure_ascii=False), self._room.map.xml))) self.stalked_players[player.username]["map_movements"] *= 0 break else: if self.first_stalked_player == player.username: await self.bulle.send( Packet.new(5, 18).write8(0).write32( self._room.round_code).write32( self._room.map.code).write16(15).write16( fix_pos(player.x)).write16(fix_pos(player.y)))
async def on_player_cheese_state_change(self, player): if self.is_playing or player.username not in self.stalked_players: return if self._room.map.xml: cheese_pos = self._room.map.cheese_pos for key in cheese_pos: packet = Packet.new(5, 19).write32(self._room.round_code).write16(key["X"]).write16(key["Y"]).write24(15) if not self.is_recording: if self.first_stalked_player == player.username: await self.bulle.send(packet) return return await self.save_movement(player.username, str(bytes(packet)), "cheese") if self.first_stalked_player == player.username: await self.bulle.send(Packet.new(5, 19).write32(self._room.round_code).write16(fix_pos(player.x)).write16(fix_pos(player.y)).write24(15))
async def play_map(self, sequence=None): if not self.is_playing: return if not self.is_bootcamp_room: await asyncio.sleep(self.born_period) cheese_pos, hole_pos = self._room.map.cheese_pos, self._room.map.hole_pos round_code = self._room.round_code move = 0 _sequence = json.loads(sequence) for i in _sequence: time_sleep = i[2] if len(i) >= 3 else 1 if not isinstance(i[0], dict): _bytes = i[0][2:].encode().decode('unicode-escape').encode() step = i[1] if step == 'walk': movingRight, movingLeft = i[0]['movingRight'], i[0]['movingLeft'] x, vx = i[0]['x'], i[0]['vx'] if self._room.map.is_reversed: x = calc_pos(800 - fix_pos(unpack(">l", pack(">L", x))[0])) if movingRight or movingLeft: movingLeft, movingRight = not movingLeft, not movingRight vx = -unpack(">h", pack(">H", vx))[0] if movingRight else -vx packet = Packet.new(4, 4).write32(round_code).writeBool(movingRight).writeBool(movingLeft).write32(x).write32(i[0]['y']).write16(vx).write16(i[0]['vy']).writeBool(i[0]['jumping']).write8(i[0]['frame']).write8(i[0]['onPortal']) elif i[1] in ['crouch', 'duck']: packet = Packet.new(4, 9).write8(_bytes[2]) elif step == 'emote': packet = Packet.new(8, 1).write8(_bytes[2]).write32(0) if len(_bytes) > 8: _len = _bytes[8] flag = unpack(f'!{_len}s', _bytes[9:9+_len])[0] packet.writeString(flag) if move > 0: await asyncio.sleep(time_sleep) if step == "cheese": await self._cheese() elif step in ["hole", "win"]: await self._win() else: await self.bulle.send(packet) move += 1 self.is_playing = False
async def on_player_movement(self, player): if self.is_playing or player.username not in self.stalked_players: return if self.is_recording: #await self.bulle.send(Packet.new(8, 5).write8(5).write32(0)) prop = { 'movingLeft': player.moving_left, 'movingRight': player.moving_right, 'x': player.x, 'y': player.y, 'vx': player.vx, 'vy': player.vy, 'jumping': player.jumping, 'frame': player.frame, 'onPortal': player.on_portal } return await self.save_movement(player.username, prop, "walk") packet = Packet.new(4, 4).write32(self._room.round_code) packet.writeBool(player.moving_right).writeBool(player.moving_left) packet.write32(player.x).write32(player.y) packet.write16(player.vx).write16(player.vy) packet.writeBool(player.jumping) packet.write8(player.frame).write8(player.on_portal) if self.first_stalked_player == player.username: await self.bulle.send(packet) s_player = self._room.get_player(username=self.username) if s_player.hasCheese: return if player.hasCheese: await self._cheese()
async def on_player_send_emoticon(self, player, emoticon): if self.is_playing or self.is_recording or player.username not in self.stalked_players: return packet = Packet.new(8, 5).write8(int(emoticon)).write32(0) if self.first_stalked_player == player.username: await self.bulle.send(packet)
async def _cheese(self): if self._room.map.xml: cheese_pos = self._room.map.cheese_pos for key in cheese_pos: return await self.bulle.send( Packet.new(5, 19).write32(self._room.round_code).write16( key["X"]).write16(key["Y"]).write24(15))
async def move(self): packet = Packet.new(4, 4).write32(self._room.round_code) packet.writeBool(True).writeBool(False) packet.write32(400).write32(200) packet.write16(0).write16(0) packet.writeBool(True) packet.write8(0).write8(0) await self.bulle.send(packet)
async def on_player_duck(self, player): if self.is_playing or player.username not in self.stalked_players: return packet = Packet.new(4, 9).writeBool(player.ducking) if self.is_recording: return await self.save_movement(player.username, str(bytes(packet)), "crouch") if self.first_stalked_player == player.username: await self.bulle.send(packet)
async def _win(self): if self._room.map.xml: hole_pos = self._room.map.hole_pos for key in hole_pos: hole_color = key["CT"] if "CT" in key else 0 return await self.bulle.send( Packet.new(5, 18).write8(hole_color).write32( self._room.round_code).write32( self._room.map.code).write16(15).write16( key["X"]).write16(key["Y"]))
async def on_emote(self, player, emote, flag): if self.is_playing or player.username not in self.stalked_players: return packet = Packet.new(8, 1).write8(emote).write32(0) if flag: packet.writeString(flag) if self.is_recording: return await self.save_movement(player.username, str(bytes(packet)), "emote") if self.first_stalked_player == player.username: await self.bulle.send(packet)
async def on_text_area(self, _id, text, callback_list): if callback_list: for callback in callback_list: packet = Packet.new(29, 21).write32(_id) _callback = callback.lower() if 'particip' in _callback or 'ent' in _callback or 'join' in _callback: return await self.bulle.send(packet.writeString(callback)) if self.elim_target: if self.elim_target.lower() in _callback or self.elim_target.lower() in text.lower(): return await self.bulle.send(packet.writeString(callback)) if self.can_press_callback: await self.bulle.send(packet.writeString(callback))
async def _mort(self): await self.bulle.send(Packet.new(4, 5).write32(self._room.round_code).write8(0))
def test_bytes(): pkt = Packet(b'hi') assert bytes(pkt) == b'hi' pkt = Packet.new(0x4141) assert pkt.buffer == b'AA'