Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix exceptions from v0.11 logs #358

Merged
merged 8 commits into from
Jul 20, 2024
Prev Previous commit
Next Next commit
chore: Do not throw exception on game action end when id do not match
  • Loading branch information
vincent4vx committed Jul 19, 2024
commit 9cedb3265addec3656c7f406ab63999fe2707667
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ public void push(Action action) {
* End an action which is successfully terminated
*
* @param actionId The action to end
* @return true if the action was ended, false if there is no pending action with this id
*/
public void end(int actionId) {
gameActions.end(actionId);
public boolean end(int actionId) {
return gameActions.end(actionId);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,21 @@ public void push(Action action) {
* End an action which is successfully terminated
*
* @param actionId The action to end
*
* @return true if the action was ended, false if there is no pending action with this id
*/
public void end(int actionId) {
public boolean end(int actionId) {
final BlockingAction current = this.current;

if (current == null || current.id() != actionId) {
throw new NoSuchElementException("The action ID do not corresponds");
return false;
}

current.end();
this.current = null;

runNextAction();
return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,30 @@
import fr.quatrevieux.araknemu.game.handler.AbstractExploringPacketHandler;
import fr.quatrevieux.araknemu.network.game.GameSession;
import fr.quatrevieux.araknemu.network.game.in.game.action.GameActionAcknowledge;
import fr.quatrevieux.araknemu.network.game.out.basic.Noop;
import org.apache.logging.log4j.Logger;

/**
* End the current game action with success
*/
public final class EndGameAction extends AbstractExploringPacketHandler<GameActionAcknowledge> {
private final Logger logger;

public EndGameAction(Logger logger) {
this.logger = logger;
}

@Override
public void handle(GameSession session, ExplorationPlayer exploration, GameActionAcknowledge packet) throws Exception {
exploration
final boolean success = exploration
.interactions()
.end(packet.actionId())
;

if (!success) {
logger.warn("Failed to end game action {}", packet.actionId());
session.send(new Noop());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import fr.quatrevieux.araknemu.game.handler.game.ValidateGameAction;
import fr.quatrevieux.araknemu.game.handler.object.UseObject;
import fr.quatrevieux.araknemu.network.game.GameSession;
import org.apache.logging.log4j.Logger;

/**
* Loader for exploring or fighter switch packet handlers
Expand All @@ -49,7 +50,7 @@ public final class ExploringOrFightingLoader implements Loader {
new PerformTurnAction()
),
new ExploringOrFightingSwitcher<>(
new EndGameAction(),
new EndGameAction(container.get(Logger.class)),
new TerminateTurnAction()
),
new ExploringOrFightingSwitcher<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,33 @@ void stopWillStopGameActions() throws Exception {
Mockito.verify(current).cancel(null);
}

@Test
void endSuccess() throws Exception {
BlockingAction current = Mockito.spy(MyBlockingAction.class);

handler.push(current);
assertTrue(handler.end(current.id()));

assertFalse(handler.busy());
Mockito.verify(current).end();
}

@Test
void endIdDoesNotCorrespond() throws Exception {
BlockingAction current = Mockito.spy(MyBlockingAction.class);

handler.push(current);
assertFalse(handler.end(404));

assertTrue(handler.busy());
Mockito.verify(current, Mockito.never()).end();
}

@Test
void endWithoutPendingAction() throws Exception {
assertFalse(handler.end(404));
}

@Test
void getNoInteraction() {
assertThrows(IllegalArgumentException.class, () -> handler.get(Interaction.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void endWithOneBlockingActionWillTerminateTheCurrentActionAndClearTheQueue() {
Mockito.when(action.id()).thenReturn(1);

queue.push(action);
queue.end(1);
assertTrue(queue.end(1));

Mockito.verify(action).end();
assertFalse(queue.isBusy());
Expand All @@ -102,7 +102,7 @@ void endWithTwoActionWillStartTheNextAction() {
queue.push(a1);
queue.push(a2);

queue.end(1);
assertTrue(queue.end(1));

Mockito.verify(a1).end();
Mockito.verify(a2).start(queue);
Expand Down Expand Up @@ -138,7 +138,7 @@ void startWithExceptionWillExecuteTheNextAction() {

Mockito.verify(a3).start(queue);

queue.end(3);
assertTrue(queue.end(3));

Mockito.verify(a3).end();

Expand All @@ -147,7 +147,7 @@ void startWithExceptionWillExecuteTheNextAction() {

@Test
void endWithoutAction() {
assertThrows(NoSuchElementException.class, () -> queue.end(1));
assertFalse(queue.end(1));
}

@Test
Expand All @@ -157,7 +157,7 @@ void endWithBadId() {

queue.push(a1);

assertThrows(NoSuchElementException.class, () -> queue.end(15), "The action ID do not corresponds");
assertFalse(queue.end(15));
}

@Test
Expand Down Expand Up @@ -220,7 +220,7 @@ void nonBlockingActionAfterBlockingWillBeExecuteAtEnd() {
Mockito.verify(a1).start(queue);

queue.push(a2);
queue.end(a1.id());
assertTrue(queue.end(a1.id()));

Mockito.verify(a1).end();
Mockito.verify(a2).start(queue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,27 @@
import fr.quatrevieux.araknemu.game.exploration.interaction.action.move.validator.PathValidator;
import fr.quatrevieux.araknemu.game.exploration.interaction.action.move.validator.ValidateWalkable;
import fr.quatrevieux.araknemu.network.game.in.game.action.GameActionAcknowledge;
import fr.quatrevieux.araknemu.network.game.out.basic.Noop;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class EndGameActionTest extends GameBaseCase {
private EndGameAction handler;
private Logger logger;

@Override
@BeforeEach
public void setUp() throws Exception {
super.setUp();

handler = new EndGameAction();
logger = Mockito.mock(Logger.class);
handler = new EndGameAction(logger);
}

@Test
Expand All @@ -62,4 +68,36 @@ void handleSuccess() throws Exception {
assertFalse(player.interactions().busy());
assertEquals(395, player.position().cell());
}
}

@Test
void handleIdDoesNotCorresponds() throws Exception {
dataSet.pushMaps().pushSubAreas().pushAreas();

ExplorationPlayer player = explorationPlayer();

player.interactions().push(
new Move(
player,
new Decoder<>(player.map()).decode("bftdgl", player.map().get(279)),
new PathValidator[] {new ValidateWalkable()}
)
);

handler.handle(session, new GameActionAcknowledge(404));
requestStack.assertLast(new Noop());

assertTrue(player.interactions().busy());
Mockito.verify(logger).warn("Failed to end game action {}", 404);
}

@Test
void handleWithoutPendingAction() throws Exception {
dataSet.pushMaps().pushSubAreas().pushAreas();
explorationPlayer();

handler.handle(session, new GameActionAcknowledge(404));
requestStack.assertLast(new Noop());

Mockito.verify(logger).warn("Failed to end game action {}", 404);
}
}
Loading