diff --git a/src/main/java/electrosphere/entity/state/attack/ClientAttackTree.java b/src/main/java/electrosphere/entity/state/attack/ClientAttackTree.java index 0b7f780c..3df50aac 100644 --- a/src/main/java/electrosphere/entity/state/attack/ClientAttackTree.java +++ b/src/main/java/electrosphere/entity/state/attack/ClientAttackTree.java @@ -350,7 +350,7 @@ public class ClientAttackTree implements BehaviorTree { * Gets the current attack type * @return The current attack type */ - String getAttackType(){ + protected String getAttackType(){ String rVal = null; if(ClientEquipState.hasEquipState(parent)){ ClientEquipState equipState = ClientEquipState.getEquipState(parent); @@ -381,7 +381,7 @@ public class ClientAttackTree implements BehaviorTree { * @param attackType The attack type * @return true if can attack, false otherwise */ - private boolean canAttack(String attackType){ + protected boolean canAttack(String attackType){ if(attackType == null){ return false; } diff --git a/src/main/java/electrosphere/entity/state/attack/ServerAttackTree.java b/src/main/java/electrosphere/entity/state/attack/ServerAttackTree.java index d2613638..d3d66df6 100644 --- a/src/main/java/electrosphere/entity/state/attack/ServerAttackTree.java +++ b/src/main/java/electrosphere/entity/state/attack/ServerAttackTree.java @@ -403,7 +403,7 @@ public class ServerAttackTree implements BehaviorTree { * Gets the current attack type * @return The current attack type */ - String getAttackType(){ + protected String getAttackType(){ String rVal = null; if(ServerEquipState.hasEquipState(parent)){ ServerEquipState equipState = ServerEquipState.getEquipState(parent); @@ -434,7 +434,7 @@ public class ServerAttackTree implements BehaviorTree { * @param attackType The attack type * @return true if can attack, false otherwise */ - private boolean canAttack(String attackType){ + protected boolean canAttack(String attackType){ if(attackType == null){ return false; } diff --git a/src/main/java/electrosphere/net/server/Server.java b/src/main/java/electrosphere/net/server/Server.java index fb44d7c3..5ea0d7b5 100644 --- a/src/main/java/electrosphere/net/server/Server.java +++ b/src/main/java/electrosphere/net/server/Server.java @@ -165,6 +165,17 @@ public class Server implements Runnable { this.connectListLock.release(); } + /** + * Gets the first connection + * @return The first connection + */ + public ServerConnectionHandler getFirstConnection(){ + connectListLock.acquireUninterruptibly(); + ServerConnectionHandler firstCon = this.activeConnections.get(0); + connectListLock.release(); + return firstCon; + } + /** * Cleans up dead connections on the server */ diff --git a/src/test/java/electrosphere/entity/state/attack/ClientAttackTreeTests.java b/src/test/java/electrosphere/entity/state/attack/ClientAttackTreeTests.java new file mode 100644 index 00000000..dfee945e --- /dev/null +++ b/src/test/java/electrosphere/entity/state/attack/ClientAttackTreeTests.java @@ -0,0 +1,56 @@ +package electrosphere.entity.state.attack; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import annotations.IntegrationTest; +import electrosphere.controls.ControlHandler; +import electrosphere.engine.Globals; +import electrosphere.entity.Entity; +import electrosphere.entity.state.attack.ClientAttackTree.AttackTreeState; +import electrosphere.server.datacell.utils.EntityLookupUtils; +import template.EntityTestTemplate; +import testutils.InputAPI; +import testutils.TestEngineUtils; + +/** + * Testing the client's attacking trees + */ +public class ClientAttackTreeTests extends EntityTestTemplate { + + + /** + * Make sure can attack in default scene + */ + @IntegrationTest + public void testClientAttack(){ + //warm up engine + TestEngineUtils.simulateFrames(1); + + //spy on the client-side player's attack state + ClientAttackTree clientAttackTree = ClientAttackTree.getClientAttackTree(Globals.playerEntity); + + //spy on the server-side player's attack tree + int serverIdForClientEntity = Globals.clientSceneWrapper.mapClientToServerId(Globals.playerEntity.getId()); + Entity serverPlayerEntity = EntityLookupUtils.getEntityById(serverIdForClientEntity); + ServerAttackTree serverAttackTree = ServerAttackTree.getServerAttackTree(serverPlayerEntity); + + //verify can attack + assertEquals(true, clientAttackTree.canAttack(clientAttackTree.getAttackType())); + assertEquals(true, serverAttackTree.canAttack(serverAttackTree.getAttackType())); + + //try attacking + InputAPI.simulatePress(ControlHandler.DATA_STRING_INPUT_CODE_ATTACK_PRIMARY); + InputAPI.simulateRelease(ControlHandler.DATA_STRING_INPUT_CODE_ATTACK_PRIMARY); + + //wait for the attack to propagate back to the client + TestEngineUtils.simulateFrames(10); + + //verify it was started on server + assertNotEquals(serverAttackTree.getState(), AttackTreeState.IDLE); + + //verify state transition was triggered on client + assertNotEquals(clientAttackTree.getState(), AttackTreeState.IDLE); + } + +} diff --git a/src/test/java/testutils/InputAPI.java b/src/test/java/testutils/InputAPI.java index dd06fd7c..ae475161 100644 --- a/src/test/java/testutils/InputAPI.java +++ b/src/test/java/testutils/InputAPI.java @@ -1,10 +1,34 @@ package testutils; +import electrosphere.engine.Globals; + /** * Used to programmatically send input signals as if a user had */ public class InputAPI { - - + + /** + * Simulates clicking a control + * @param controlName The name of the control + */ + public static void simulateClick(String controlName){ + Globals.controlHandler.getControl(controlName).onClick(); + } + + /** + * Simulates pressing a control + * @param controlName The name of the control + */ + public static void simulatePress(String controlName){ + Globals.controlHandler.getControl(controlName).onPress(); + } + + /** + * Simulates releasing a control + * @param controlName The name of the control + */ + public static void simulateRelease(String controlName){ + Globals.controlHandler.getControl(controlName).onRelease(); + } }