Add tests for player actions

This commit is contained in:
Major-
2018-08-20 22:01:09 +01:00
parent d4de32c82c
commit 2080e1e700
6 changed files with 76 additions and 23 deletions
@@ -1,7 +1,8 @@
import org.apollo.game.plugin.entity.player_action.PlayerActionEvent
import org.apollo.game.plugin.entity.player_action.PlayerActionType import org.apollo.game.plugin.entity.player_action.PlayerActionType
import org.apollo.plugin.entity.following.FollowAction import org.apollo.plugin.entity.following.FollowAction
on_player_event { org.apollo.game.plugin.entity.player_action.PlayerActionEvent::class } on_player_event { PlayerActionEvent::class }
.where { action == PlayerActionType.FOLLOW } .where { action == PlayerActionType.FOLLOW }
.then { .then {
FollowAction.start(it, target) FollowAction.start(it, target)
@@ -3,35 +3,29 @@ package org.apollo.game.plugin.entity.player_action
import org.apollo.game.message.impl.SetPlayerActionMessage import org.apollo.game.message.impl.SetPlayerActionMessage
import org.apollo.game.model.entity.Player import org.apollo.game.model.entity.Player
import org.apollo.game.model.event.PlayerEvent import org.apollo.game.model.event.PlayerEvent
import java.util.EnumSet import java.util.*
enum class PlayerActionType(val displayName: String, val slot: Int, val primary: Boolean = true) {
ATTACK("Attack", 2),
CHALLENGE("Challenge", 2),
FOLLOW("Follow", 4),
TRADE("Trade with", 5)
}
class PlayerActionEvent(player: Player, val target: Player, val action: PlayerActionType) : PlayerEvent(player) class PlayerActionEvent(player: Player, val target: Player, val action: PlayerActionType) : PlayerEvent(player)
private val playerActionsMap = mutableMapOf<Player, EnumSet<PlayerActionType>>()
private val Player.actions: EnumSet<PlayerActionType>
get() = playerActionsMap.computeIfAbsent(this, { EnumSet.noneOf(PlayerActionType::class.java) })
fun Player.enableAction(action: PlayerActionType) { fun Player.enableAction(action: PlayerActionType) {
send(SetPlayerActionMessage(action.displayName, action.slot, action.primary)) send(SetPlayerActionMessage(action.displayName, action.slot, action.primary))
actions.add(action) actions += action
} }
fun Player.disableAction(action: PlayerActionType) { fun Player.disableAction(action: PlayerActionType) {
send(SetPlayerActionMessage("null", action.slot, action.primary)) send(SetPlayerActionMessage("null", action.slot, action.primary))
actions.remove(action) actions -= action
} }
fun Player.actionEnabled(action: PlayerActionType): Boolean { fun Player.actionEnabled(action: PlayerActionType): Boolean {
return actions.contains(action) return action in actions
} }
fun Player.actionAt(slot: Int): PlayerActionType? { fun Player.actionAt(slot: Int): PlayerActionType? {
return actions.find { it.slot == slot } return actions.find { it.slot == slot }
} }
private val playerActionsMap = mutableMapOf<Player, EnumSet<PlayerActionType>>()
private val Player.actions: EnumSet<PlayerActionType>
get() = playerActionsMap.computeIfAbsent(this) { EnumSet.noneOf(PlayerActionType::class.java) }
@@ -0,0 +1,8 @@
package org.apollo.game.plugin.entity.player_action
enum class PlayerActionType(val displayName: String, val slot: Int, val primary: Boolean = true) {
ATTACK("Attack", 2),
CHALLENGE("Challenge", 2),
FOLLOW("Follow", 4),
TRADE("Trade with", 5)
}
@@ -1,9 +1,7 @@
package org.apollo.game.plugin.entity.player_action
import org.apollo.game.message.impl.PlayerActionMessage import org.apollo.game.message.impl.PlayerActionMessage
import org.apollo.game.model.event.impl.LoginEvent import org.apollo.game.model.event.impl.LoginEvent
import org.apollo.game.plugin.entity.player_action.PlayerActionEvent
import org.apollo.game.plugin.entity.player_action.PlayerActionType
import org.apollo.game.plugin.entity.player_action.actionAt
import org.apollo.game.plugin.entity.player_action.enableAction
on { PlayerActionMessage::class } on { PlayerActionMessage::class }
.then { .then {
@@ -0,0 +1,34 @@
package org.apollo.game.plugin.entity.player_action
import io.mockk.verify
import org.apollo.game.message.impl.SetPlayerActionMessage
import org.apollo.game.model.entity.Player
import org.apollo.game.plugin.testing.junit.ApolloTestingExtension
import org.apollo.game.plugin.testing.junit.api.annotations.TestMock
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.EnumSource
@ExtendWith(ApolloTestingExtension::class)
class PlayerActionTests {
@TestMock
lateinit var player: Player
@ParameterizedTest
@EnumSource(PlayerActionType::class)
fun `enabling and disabling PlayerActions sends SetPlayerActionMessages`(type: PlayerActionType) {
player.enableAction(type)
verify { player.send(eq(SetPlayerActionMessage(type.displayName, type.slot, type.primary))) }
assertTrue(player.actionEnabled(type)) { "Action $type should have been enabled, but was not." }
player.disableAction(type)
verify { player.send(eq(SetPlayerActionMessage("null", type.slot, type.primary))) }
assertFalse(player.actionEnabled(type)) { "Action $type should not have been enabled, but was." }
}
}
@@ -2,6 +2,8 @@ package org.apollo.game.message.impl;
import org.apollo.net.message.Message; import org.apollo.net.message.Message;
import java.util.Objects;
/** /**
* A {@link Message} sent by the client to add an action to the menu when a player right-clicks another. * A {@link Message} sent by the client to add an action to the menu when a player right-clicks another.
* *
@@ -37,8 +39,8 @@ public final class SetPlayerActionMessage extends Message {
/** /**
* Creates the set player action message. * Creates the set player action message.
* *
* @param text The action text. * @param text The action text.
* @param slot The menu slot. * @param slot The menu slot.
* @param primaryInteraction Whether or not the action is the primary action. * @param primaryInteraction Whether or not the action is the primary action.
*/ */
public SetPlayerActionMessage(String text, int slot, boolean primaryInteraction) { public SetPlayerActionMessage(String text, int slot, boolean primaryInteraction) {
@@ -75,4 +77,20 @@ public final class SetPlayerActionMessage extends Message {
return primaryAction; return primaryAction;
} }
@Override
public boolean equals(Object o) {
if (o instanceof SetPlayerActionMessage) {
SetPlayerActionMessage other = (SetPlayerActionMessage) o;
return slot == other.slot && primaryAction == other.primaryAction && Objects.equals(text, other.text);
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(text, slot, primaryAction);
}
} }