Search in sources :

Example 1 with Player

use of org.apollo.game.model.entity.Player in project apollo by apollo-rsps.

the class BinaryPlayerSerializer method loadPlayer.

@Override
public PlayerLoaderResponse loadPlayer(PlayerCredentials credentials) throws IOException {
    Path path = getFile(credentials.getUsername());
    if (!Files.exists(path)) {
        Player player = new Player(world, credentials, TUTORIAL_ISLAND_SPAWN);
        credentials.setPassword(SCryptUtil.scrypt(credentials.getPassword(), 16384, 8, 1));
        return new PlayerLoaderResponse(LoginConstants.STATUS_OK, player);
    }
    try (DataInputStream in = new DataInputStream(new BufferedInputStream(Files.newInputStream(path)))) {
        String name = StreamUtil.readString(in);
        String password = StreamUtil.readString(in);
        if (!name.equalsIgnoreCase(credentials.getUsername()) || !SCryptUtil.check(credentials.getPassword(), password)) {
            return new PlayerLoaderResponse(LoginConstants.STATUS_INVALID_CREDENTIALS);
        }
        // Update password to the hashed one.
        credentials.setPassword(password);
        PrivilegeLevel privilege = PrivilegeLevel.valueOf(in.readByte());
        MembershipStatus members = MembershipStatus.valueOf(in.readByte());
        PrivacyState chatPrivacy = PrivacyState.valueOf(in.readByte(), true);
        PrivacyState friendPrivacy = PrivacyState.valueOf(in.readByte(), false);
        PrivacyState tradePrivacy = PrivacyState.valueOf(in.readByte(), false);
        ScreenBrightness brightness = ScreenBrightness.valueOf(in.readByte());
        int x = in.readUnsignedShort();
        int y = in.readUnsignedShort();
        int height = in.readUnsignedByte();
        Gender gender = in.readUnsignedByte() == Gender.MALE.toInteger() ? Gender.MALE : Gender.FEMALE;
        int[] style = new int[7];
        for (int slot = 0; slot < style.length; slot++) {
            style[slot] = in.readUnsignedByte();
        }
        int[] colors = new int[5];
        for (int slot = 0; slot < colors.length; slot++) {
            colors[slot] = in.readUnsignedByte();
        }
        Player player = new Player(world, credentials, new Position(x, y, height));
        player.setPrivilegeLevel(privilege);
        player.setMembers(members);
        player.setChatPrivacy(chatPrivacy);
        player.setFriendPrivacy(friendPrivacy);
        player.setTradePrivacy(tradePrivacy);
        player.setScreenBrightness(brightness);
        player.setAppearance(new Appearance(gender, style, colors));
        readInventory(in, player.getInventory());
        readInventory(in, player.getEquipment());
        readInventory(in, player.getBank());
        int size = in.readUnsignedByte();
        SkillSet skills = player.getSkillSet();
        skills.stopFiringEvents();
        try {
            for (int i = 0; i < size; i++) {
                int level = in.readUnsignedByte();
                double experience = in.readDouble();
                skills.setSkill(i, new Skill(experience, level, SkillSet.getLevelForExperience(experience)));
            }
        } finally {
            skills.calculateCombatLevel();
            skills.startFiringEvents();
        }
        int friendCount = in.readByte();
        List<String> friends = new ArrayList<>(friendCount);
        for (int i = 0; i < friendCount; i++) {
            friends.add(NameUtil.decodeBase37(in.readLong()));
        }
        player.setFriendUsernames(friends);
        int ignoreCount = in.readByte();
        List<String> ignores = new ArrayList<>(ignoreCount);
        for (int times = 0; times < ignoreCount; times++) {
            ignores.add(NameUtil.decodeBase37(in.readLong()));
        }
        player.setIgnoredUsernames(ignores);
        Map<String, Attribute<?>> attributes = readAttributes(in);
        attributes.forEach(player::setAttribute);
        if (player.isBanned()) {
            return new PlayerLoaderResponse(LoginConstants.STATUS_ACCOUNT_DISABLED);
        }
        return new PlayerLoaderResponse(LoginConstants.STATUS_OK, player);
    }
}
Also used : Path(java.nio.file.Path) MembershipStatus(org.apollo.game.model.entity.setting.MembershipStatus) Player(org.apollo.game.model.entity.Player) Position(org.apollo.game.model.Position) NumericalAttribute(org.apollo.game.model.entity.attr.NumericalAttribute) BooleanAttribute(org.apollo.game.model.entity.attr.BooleanAttribute) Attribute(org.apollo.game.model.entity.attr.Attribute) StringAttribute(org.apollo.game.model.entity.attr.StringAttribute) ArrayList(java.util.ArrayList) PrivacyState(org.apollo.game.model.entity.setting.PrivacyState) Gender(org.apollo.game.model.entity.setting.Gender) DataInputStream(java.io.DataInputStream) PrivilegeLevel(org.apollo.game.model.entity.setting.PrivilegeLevel) Appearance(org.apollo.game.model.Appearance) Skill(org.apollo.game.model.entity.Skill) ScreenBrightness(org.apollo.game.model.entity.setting.ScreenBrightness) BufferedInputStream(java.io.BufferedInputStream) SkillSet(org.apollo.game.model.entity.SkillSet)

Example 2 with Player

use of org.apollo.game.model.entity.Player in project apollo by apollo-rsps.

the class GameService method pulse.

/**
 * Called every pulse.
 */
public synchronized void pulse() {
    finalizeRegistrations();
    finalizeUnregistrations();
    MobRepository<Player> players = world.getPlayerRepository();
    for (Player player : players) {
        GameSession session = player.getSession();
        if (session != null) {
            session.handlePendingMessages(handlers);
        }
    }
    world.pulse();
    synchronizer.synchronize(players, world.getNpcRepository());
}
Also used : GameSession(org.apollo.game.session.GameSession) Player(org.apollo.game.model.entity.Player)

Example 3 with Player

use of org.apollo.game.model.entity.Player in project apollo by apollo-rsps.

the class ItemOnItemVerificationHandlerTests method terminateWithNoTargetItem.

@Test
public void terminateWithNoTargetItem() throws Exception {
    Player player = mock(Player.class);
    Inventory inventory = new Inventory(28);
    inventory.set(1, new Item(4151, 1));
    when(player.getInventory()).thenReturn(inventory);
    ItemOnItemMessage itemOnItemMessage = new ItemOnItemMessage(BankConstants.SIDEBAR_INVENTORY_ID, 4151, 1, BankConstants.SIDEBAR_INVENTORY_ID, 4152, 2);
    itemOnItemVerificationHandler.handle(player, itemOnItemMessage);
    assertTrue("ItemOnItemVerificationHandler: failed terminating message with invalid target item", itemOnItemMessage.terminated());
}
Also used : Item(org.apollo.game.model.Item) Player(org.apollo.game.model.entity.Player) ItemOnItemMessage(org.apollo.game.message.impl.ItemOnItemMessage) Inventory(org.apollo.game.model.inv.Inventory) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with Player

use of org.apollo.game.model.entity.Player in project apollo by apollo-rsps.

the class ObjectActionVerificationHandlerTests method terminateIfNoObject.

@Test
public void terminateIfNoObject() throws Exception {
    Position playerPosition = new Position(3200, 3200);
    Position objectPosition = new Position(3200, 3201);
    World world = mock(World.class);
    Region region = mock(Region.class);
    RegionRepository regionRepository = mock(RegionRepository.class);
    Player player = mock(Player.class);
    Set<Entity> entitySet = new HashSet<>();
    when(world.getRegionRepository()).thenReturn(regionRepository);
    when(regionRepository.fromPosition(objectPosition)).thenReturn(region);
    when(player.getPosition()).thenReturn(playerPosition);
    when(region.getEntities(objectPosition, EntityType.STATIC_OBJECT, EntityType.DYNAMIC_OBJECT)).thenReturn(entitySet);
    ObjectActionMessage objectActionMessage = new ObjectActionMessage(1, 4151, objectPosition);
    ObjectActionVerificationHandler objectActionVerificationHandler = new ObjectActionVerificationHandler(world);
    objectActionVerificationHandler.handle(player, objectActionMessage);
    assertTrue("ObjectVerificationHandler: message not terminated when no object exists!", objectActionMessage.terminated());
}
Also used : Entity(org.apollo.game.model.entity.Entity) Player(org.apollo.game.model.entity.Player) ObjectActionMessage(org.apollo.game.message.impl.ObjectActionMessage) Position(org.apollo.game.model.Position) RegionRepository(org.apollo.game.model.area.RegionRepository) Region(org.apollo.game.model.area.Region) World(org.apollo.game.model.World) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with Player

use of org.apollo.game.model.entity.Player in project apollo by apollo-rsps.

the class SequentialClientSynchronizer method synchronize.

@Override
public void synchronize(MobRepository<Player> players, MobRepository<Npc> npcs) {
    Map<RegionCoordinates, Set<RegionUpdateMessage>> encodes = new HashMap<>(), updates = new HashMap<>();
    for (Player player : players) {
        SynchronizationTask task = new PrePlayerSynchronizationTask(player, encodes, updates);
        task.run();
    }
    for (Npc npc : npcs) {
        SynchronizationTask task = new PreNpcSynchronizationTask(npc);
        task.run();
    }
    for (Player player : players) {
        SynchronizationTask task = new PlayerSynchronizationTask(player);
        task.run();
        task = new NpcSynchronizationTask(player);
        task.run();
    }
    for (Player player : players) {
        SynchronizationTask task = new PostPlayerSynchronizationTask(player);
        task.run();
    }
    for (Npc npc : npcs) {
        SynchronizationTask task = new PostNpcSynchronizationTask(npc);
        task.run();
    }
}
Also used : Npc(org.apollo.game.model.entity.Npc) Player(org.apollo.game.model.entity.Player) Set(java.util.Set) PlayerSynchronizationTask(org.apollo.game.sync.task.PlayerSynchronizationTask) PrePlayerSynchronizationTask(org.apollo.game.sync.task.PrePlayerSynchronizationTask) PostPlayerSynchronizationTask(org.apollo.game.sync.task.PostPlayerSynchronizationTask) HashMap(java.util.HashMap) PrePlayerSynchronizationTask(org.apollo.game.sync.task.PrePlayerSynchronizationTask) NpcSynchronizationTask(org.apollo.game.sync.task.NpcSynchronizationTask) PlayerSynchronizationTask(org.apollo.game.sync.task.PlayerSynchronizationTask) PrePlayerSynchronizationTask(org.apollo.game.sync.task.PrePlayerSynchronizationTask) SynchronizationTask(org.apollo.game.sync.task.SynchronizationTask) PostNpcSynchronizationTask(org.apollo.game.sync.task.PostNpcSynchronizationTask) PostPlayerSynchronizationTask(org.apollo.game.sync.task.PostPlayerSynchronizationTask) PreNpcSynchronizationTask(org.apollo.game.sync.task.PreNpcSynchronizationTask) RegionCoordinates(org.apollo.game.model.area.RegionCoordinates) NpcSynchronizationTask(org.apollo.game.sync.task.NpcSynchronizationTask) PostNpcSynchronizationTask(org.apollo.game.sync.task.PostNpcSynchronizationTask) PreNpcSynchronizationTask(org.apollo.game.sync.task.PreNpcSynchronizationTask) PreNpcSynchronizationTask(org.apollo.game.sync.task.PreNpcSynchronizationTask) PostPlayerSynchronizationTask(org.apollo.game.sync.task.PostPlayerSynchronizationTask) PostNpcSynchronizationTask(org.apollo.game.sync.task.PostNpcSynchronizationTask)

Aggregations

Player (org.apollo.game.model.entity.Player)17 Position (org.apollo.game.model.Position)7 Test (org.junit.Test)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 Region (org.apollo.game.model.area.Region)6 RegionRepository (org.apollo.game.model.area.RegionRepository)6 HashSet (java.util.HashSet)5 World (org.apollo.game.model.World)5 Entity (org.apollo.game.model.entity.Entity)5 StaticGameObject (org.apollo.game.model.entity.obj.StaticGameObject)4 Inventory (org.apollo.game.model.inv.Inventory)4 ItemOnObjectMessage (org.apollo.game.message.impl.ItemOnObjectMessage)3 RegionCoordinates (org.apollo.game.model.area.RegionCoordinates)3 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 ObjectActionMessage (org.apollo.game.message.impl.ObjectActionMessage)2 Item (org.apollo.game.model.Item)2 Npc (org.apollo.game.model.entity.Npc)2 BufferedInputStream (java.io.BufferedInputStream)1 DataInputStream (java.io.DataInputStream)1