Skip to content

Commit

Permalink
chore(fight): return false instead of exception on FightTurn#perform()
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent4vx committed Jul 13, 2024
1 parent a88cf9a commit 07c5cba
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import fr.quatrevieux.araknemu.game.fight.ai.memory.AiMemory;
import fr.quatrevieux.araknemu.game.fight.ai.memory.MemoryKey;
import fr.quatrevieux.araknemu.game.fight.ai.util.AIHelper;
import fr.quatrevieux.araknemu.game.fight.exception.FightException;
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter;
import fr.quatrevieux.araknemu.game.fight.fighter.FighterData;
import fr.quatrevieux.araknemu.game.fight.fighter.PlayableFighter;
Expand Down Expand Up @@ -109,7 +110,10 @@ public void run() {
);

if (action.isPresent()) {
currentTurn.perform(action.get());
if (!currentTurn.perform(action.get())) {
throw new FightException("The AI has generated an invalid action + " + action.get());
}

currentTurn.later(() -> fight.schedule(this, Duration.ofMillis(800)));
stop = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public boolean active() {
}

@Override
public void perform(Action action) throws FightException {
public boolean perform(Action action) throws FightException {
throw new UnsupportedOperationException("This is a proxy turn");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void stop() {
}

@Override
public void perform(FightAction action) throws FightException {
public boolean perform(FightAction action) throws FightException {
if (!active.get()) {
throw new FightException("Turn is not active");
}
Expand All @@ -151,9 +151,7 @@ public void perform(FightAction action) throws FightException {
throw new FightException("The fighter is dead");
}

if (!actionHandler.start(action)) {
throw new FightException("Cannot start the action");
}
return actionHandler.start(action);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ public interface Turn<A extends Action> {
* Perform a fight action
*
* @param action The action to perform
* @return true if the action is successfully performed, or false if the action cannot be performed
*/
public void perform(A action) throws FightException;
public boolean perform(A action) throws FightException;

/**
* Execute the action when the current is terminated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public void handle(GameSession session, Fight fight, PlayerFighter fighter, Game
try {
final FightTurn turn = fighter.turn();

turn.perform(fighter.fight().actions().create(fighter, ActionType.byId(packet.type()), packet.arguments()));
if (!turn.perform(fighter.fight().actions().create(fighter, ActionType.byId(packet.type()), packet.arguments()))) {
session.send(new NoneAction());
}
} catch (Exception e) {
throw new ErrorPacket(new NoneAction(), e);
}
Expand Down
10 changes: 9 additions & 1 deletion src/test/java/fr/quatrevieux/araknemu/game/GameBaseCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,15 @@ public GamePlayer makeOtherPlayer() throws Exception {
return makeOtherPlayer(1);
}

public GameSession makeOtherPlayerSession() throws Exception {
return makeOtherPlayerSession(1);
}

public GamePlayer makeOtherPlayer(int level) throws Exception {
return makeOtherPlayerSession(level).player();
}

public GameSession makeOtherPlayerSession(int level) throws Exception {
dataSet
.pushSpells()
.pushRaces()
Expand Down Expand Up @@ -472,7 +480,7 @@ public GamePlayer makeOtherPlayer(int level) throws Exception {

session.setPlayer(gp);

return gp;
return session;
}

public ExplorationPlayer makeOtherExplorationPlayer() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import fr.arakne.utils.maps.path.Path;
import fr.arakne.utils.maps.path.PathStep;
import fr.arakne.utils.value.Interval;
import fr.quatrevieux.araknemu.core.network.util.DummyChannel;
import fr.quatrevieux.araknemu.game.GameBaseCase;
import fr.quatrevieux.araknemu.game.exploration.map.ExplorationMap;
import fr.quatrevieux.araknemu.game.exploration.map.ExplorationMapService;
Expand Down Expand Up @@ -55,6 +56,7 @@
import fr.quatrevieux.araknemu.game.fight.turn.action.util.CriticalityStrategy;
import fr.quatrevieux.araknemu.game.item.ItemService;
import fr.quatrevieux.araknemu.game.player.GamePlayer;
import fr.quatrevieux.araknemu.network.game.GameSession;
import fr.quatrevieux.araknemu.network.game.out.account.Stats;
import fr.quatrevieux.araknemu.network.game.out.fight.BeginFight;
import fr.quatrevieux.araknemu.network.game.out.fight.FightEnd;
Expand Down Expand Up @@ -105,7 +107,9 @@ void challengeFight() throws Exception {
FightHandler<ChallengeBuilder> handler = service.handler(ChallengeBuilder.class);

GamePlayer player = gamePlayer(true);
GamePlayer other = makeOtherPlayer();
GameSession otherSession = makeOtherPlayerSession();
GamePlayer other = otherSession.player();
SendingRequestStack otherRequestStack = new SendingRequestStack((DummyChannel) otherSession.channel());

// Equip a weapon
other.inventory().add(container.get(ItemService.class).create(88), 1, 1);
Expand Down Expand Up @@ -372,14 +376,15 @@ public void apply(FightTurn turn) {
requestStack.assertOne(ActionEffect.alterLifePoints(other.fighter(), player.fighter(), -damage));
nextTurn();

assertThrows(FightException.class, () -> castNormal(3, fight.map().get(95)));
castNormal(3, fight.map().get(95));
requestStack.assertLast(Error.cantCastBadRange(new Interval(1, 6), 7));
nextTurn();

castNormal(6, other.fighter().cell());
other.fighter().turn().terminate();

assertThrows(FightException.class, () -> castNormal(6, other.fighter().cell())); // Cooldown
castNormal(6, other.fighter().cell()); // Cooldown
otherRequestStack.assertLast(Error.cantCast());

// Skip 5 turns
for (int i = 0; i < 5; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,31 @@ void startWithExceptionShouldStopTurn() throws InterruptedException {
assertFalse(turn.active());
}

@RepeatedIfExceptionsTest
void startWithInvalidActionShouldStopTurn() throws InterruptedException {
ActionGenerator generator1 = Mockito.mock(ActionGenerator.class);
Mockito.when(generator1.generate(Mockito.any(), Mockito.any())).thenReturn(
Optional.of(fight.actions().cast().create(
fighter,
fighter.spells().get(3),
fight.map().get(0)
))
);

fight.turnList().start();
FightTurn turn = fight.turnList().current().get();

FighterAI ai = new FighterAI(fighter, fight, new GeneratorAggregate(new ActionGenerator[] {generator1}));

ai.start(turn);

Mockito.verify(generator1).initialize(ai);

Mockito.verify(generator1).generate(Mockito.eq(ai), Mockito.any(AiActionFactory.class));

assertFalse(turn.active());
}

@RepeatedIfExceptionsTest
void startShouldCallMemoryRefresh() throws InterruptedException {
ActionGenerator generator = Mockito.mock(ActionGenerator.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ void invocationLimit() throws SQLException {

passTurns(3); // spell cooldown

assertThrows(FightException.class, () -> castNormal(35, fight.map().get(200)));
castNormal(35, fight.map().get(200));
requestStack.assertLast(Error.cantCastMaxSummonedCreaturesReached(1));
assertFalse(fight.map().get(200).hasFighter());
}
Expand Down Expand Up @@ -1381,7 +1381,7 @@ void trap() {
requestStack.assertOne(ActionEffect.packet(fighter1, new UpdateCells(UpdateCells.Data.fromProperties(126, true, UpdateCells.LAYER_2_OBJECT_NUMBER.set(25)))));
requestStack.clear();

assertThrows(FightException.class, () -> castNormal(65, fight.map().get(126))); // Already a trap
castNormal(65, fight.map().get(126)); // Already a trap
requestStack.assertLast(ActionEffect.spellBlockedByInvisibleObstacle(fighter1, service.get(65).level(5)));
assertEquals(1, fight.map().objects().stream().count());
requestStack.clear();
Expand Down Expand Up @@ -1565,7 +1565,7 @@ void doubleInvoc() {

passTurns(9); // spell cooldown

assertThrows(FightException.class, () -> castNormal(74, fight.map().get(200)));
castNormal(74, fight.map().get(200));
requestStack.assertLast(Error.cantCastMaxSummonedCreaturesReached(1));
}

Expand Down Expand Up @@ -1699,7 +1699,7 @@ void invocLastDeadFighterShouldIgnoreLeaveFighter() {
fight.fighters().leave(ally);
requestStack.clear();

assertThrows(FightException.class, () -> castNormal(420, fight.map().get(200))); // Laisse spirituelle
castNormal(420, fight.map().get(200)); // Laisse spirituelle

assertTrue(ally.dead());
assertFalse(fight.map().get(200).hasFighter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void performInvalidAction() {
FightAction action = Mockito.mock(FightAction.class);
Mockito.when(action.validate(turn)).thenReturn(false);

assertThrows(FightException.class, () -> turn.perform(action));
assertFalse(turn.perform(action));
}

@Test
Expand All @@ -165,7 +165,7 @@ void performSuccess() {
Mockito.when(action.start()).thenReturn(result);
Mockito.when(result.success()).thenReturn(false);

turn.perform(action);
assertTrue(turn.perform(action));

Mockito.verify(action).start();
}
Expand Down

0 comments on commit 07c5cba

Please sign in to comment.