def make_add_player_packet(self, context, display_name=True): packet_buffer = PacketBuffer() packet = PlayerListItemPacket( context=context, action_type=PlayerListItemPacket.AddPlayerAction, actions=[ PlayerListItemPacket.AddPlayerAction( uuid=fake_uuid, name='goodmonson', properties=[ PlayerListItemPacket.PlayerProperty( name='property1', value='value1', signature=None), PlayerListItemPacket.PlayerProperty( name='property2', value='value2', signature='gm') ], gamemode=42, ping=69, display_name='Goodmonson' if display_name else None ), ], ) if display_name: self.assertEqual( str(packet), "0x%02X PlayerListItemPacket(" "action_type=AddPlayerAction, actions=[AddPlayerAction(" "uuid=%r, name='goodmonson', properties=[PlayerProperty(" "name='property1', value='value1', signature=None), " "PlayerProperty(name='property2', value='value2', " "signature='gm')], gamemode=42, ping=69, " "display_name='Goodmonson')])" % (packet.id, fake_uuid)) packet.write_fields(packet_buffer) packet_buffer.reset_cursor() return packet_buffer
def test_add_and_others(self): for protocol_version in TEST_VERSIONS: context = ConnectionContext(protocol_version=protocol_version) player_list = PlayerListItemPacket.PlayerList() by_uuid = player_list.players_by_uuid packet_buffer = self.make_add_player_packet( context, display_name=False) self.read_and_apply(context, packet_buffer, player_list) self.assertEqual(by_uuid[fake_uuid].gamemode, 42) self.assertEqual(by_uuid[fake_uuid].ping, 69) self.assertIsNone(by_uuid[fake_uuid].display_name) # Change the game mode packet_buffer = PacketBuffer() packet = PlayerListItemPacket( context=context, action_type=PlayerListItemPacket.UpdateGameModeAction, actions=[ PlayerListItemPacket.UpdateGameModeAction( uuid=fake_uuid, gamemode=43), ], ) self.assertEqual( str(packet), "0x%02X PlayerListItemPacket(" "action_type=UpdateGameModeAction, actions=[" "UpdateGameModeAction(uuid=%r, gamemode=43)])" % (packet.id, fake_uuid)) packet.write_fields(packet_buffer) self.read_and_apply(context, packet_buffer, player_list) self.assertEqual(by_uuid[fake_uuid].gamemode, 43) # Change the ping packet_buffer = PacketBuffer() packet = PlayerListItemPacket( context=context, action_type=PlayerListItemPacket.UpdateLatencyAction, actions=[ PlayerListItemPacket.UpdateLatencyAction( uuid=fake_uuid, ping=70), ], ) self.assertEqual( str(packet), "0x%02X PlayerListItemPacket(" "action_type=UpdateLatencyAction, actions=[" "UpdateLatencyAction(uuid=%r, ping=70)])" % (packet.id, fake_uuid)) packet.write_fields(packet_buffer) self.read_and_apply(context, packet_buffer, player_list) self.assertEqual(by_uuid[fake_uuid].ping, 70) # Change the display name packet_buffer = PacketBuffer() packet = PlayerListItemPacket( context=context, action_type=PlayerListItemPacket.UpdateDisplayNameAction, actions=[ PlayerListItemPacket.UpdateDisplayNameAction( uuid=fake_uuid, display_name='Badmonson'), ], ) self.assertEqual( str(packet), "0x%02X PlayerListItemPacket(" "action_type=UpdateDisplayNameAction, actions=[" "UpdateDisplayNameAction(uuid=%r, display_name='Badmonson')])" % (packet.id, fake_uuid)) packet.write_fields(packet_buffer) self.read_and_apply(context, packet_buffer, player_list) self.assertEqual(by_uuid[fake_uuid].display_name, 'Badmonson') # Remove the display name packet_buffer = PacketBuffer() PlayerListItemPacket( context=context, action_type=PlayerListItemPacket.UpdateDisplayNameAction, actions=[ PlayerListItemPacket.UpdateDisplayNameAction( uuid=fake_uuid, display_name=None), ], ).write_fields(packet_buffer) self.read_and_apply(context, packet_buffer, player_list) self.assertIsNone(by_uuid[fake_uuid].display_name) # Remove the player packet_buffer = PacketBuffer() packet = PlayerListItemPacket( context=context, action_type=PlayerListItemPacket.RemovePlayerAction, actions=[ PlayerListItemPacket.RemovePlayerAction(uuid=fake_uuid), ], ) self.assertEqual( str(packet), "0x%02X PlayerListItemPacket(" "action_type=RemovePlayerAction, actions=[RemovePlayerAction(" "uuid=%r)])" % (packet.id, fake_uuid)) packet.write_fields(packet_buffer) self.read_and_apply(context, packet_buffer, player_list) self.assertNotIn(fake_uuid, player_list.players_by_uuid)