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

feat(fight): Handle boost effects without duration #27 #298

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void decrementRemainingTurns() {
* Increment remaining turns
* Use this method when a self-buff is added
*/
void incrementRemainingTurns() {
public void incrementRemainingTurns() {
if (remainingTurns != -1) {
++remainingTurns;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,19 @@ public Stream<Buff> stream() {

@Override
public void add(Buff buff) {
final boolean isPlayingFighter = fighter.isPlaying();

buffs.add(buff);
buff.hook().onBuffStarted(buff);

if (buff.remainingTurns() == 0) {
if (buff.remainingTurns() == 0 && !isPlayingFighter) {
buff.incrementRemainingTurns();
}

fighter.fight().send(new AddBuff(buff));

// Add one turn when it's the turn of the current fighter
if (fighter.isPlaying()) {
if (isPlayingFighter) {
buff.incrementRemainingTurns();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public AbstractAlterCharacteristicHandler(AlterCharacteristicHook hook, boolean

@Override
public void handle(FightCastScope cast, FightCastScope.EffectScope effect) {
throw new UnsupportedOperationException("Alter characteristic effect must be used as a buff");
buff(cast, effect);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ public ApplySpellOnStartTurnHandler(Fight fight, SpellService spellService) {

@Override
public void handle(FightCastScope cast, FightCastScope.EffectScope effect) {
buff(cast, effect);
for (Fighter target : effect.targets()) {
final Buff buff = new Buff(effect.effect(), cast.action(), cast.caster(), target, this);

// The duration must be at least 1 to ensure that the effect will be applied at the next start turn
buff.incrementRemainingTurns();
target.buffs().add(buff);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ public SkipTurnHandler(Fight fight) {

@Override
public void handle(FightCastScope cast, FightCastScope.EffectScope effect) {
buff(cast, effect);
for (Fighter target : effect.targets()) {
final Buff buff = new Buff(effect.effect(), cast.action(), cast.caster(), target, this);

// The duration must be at least 1 to ensure that the next turn will be skipped
buff.incrementRemainingTurns();
target.buffs().add(buff);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,23 @@ void addWhenTurnIsActiveShouldIncrementRemainingTurns() {
assertEquals(2, buff.remainingTurns());
}

@Test
void addWhenTurnIsActiveShouldIncrementRemainingTurnsWithADurationOfZero() {
fight.nextState();
fight.turnList().start();

SpellEffect effect = Mockito.mock(SpellEffect.class);
BuffHook hook = Mockito.mock(BuffHook.class);

Mockito.when(effect.duration()).thenReturn(0);

Buff buff = new Buff(effect, Mockito.mock(Spell.class), player.fighter(), player.fighter(), hook);

list.add(buff);

assertEquals(1, buff.remainingTurns());
}

@Test
void onStartTurnWithoutBuff() {
assertTrue(list.onStartTurn());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.Optional;

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

Expand Down Expand Up @@ -77,12 +78,22 @@ void handle() {
SpellConstraints constraints = Mockito.mock(SpellConstraints.class);

Mockito.when(effect.area()).thenReturn(new CellArea());
Mockito.when(effect.effect()).thenReturn(123);
Mockito.when(effect.min()).thenReturn(50);
Mockito.when(effect.max()).thenReturn(60);
Mockito.when(effect.target()).thenReturn(SpellEffectTarget.DEFAULT);
Mockito.when(spell.constraints()).thenReturn(constraints);
Mockito.when(constraints.freeCell()).thenReturn(false);

FightCastScope scope = makeCastScope(caster, spell, effect, caster.cell());
assertThrows(UnsupportedOperationException.class, () -> handler.handle(scope, scope.effects().get(0)));
handler.handle(scope, scope.effects().get(0));

Optional<Buff> buff1 = caster.buffs().stream().filter(buff -> buff.effect().effect() == 123).findFirst();

assertTrue(buff1.isPresent());
assertBetween(50, 60, buff1.get().effect().min());
assertEquals(1, buff1.get().remainingTurns());
assertTrue(buff1.get().canBeDispelled());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,22 @@ void handle() {
SpellConstraints constraints = Mockito.mock(SpellConstraints.class);

Mockito.when(effect.area()).thenReturn(new CellArea());
Mockito.when(effect.effect()).thenReturn(123);
Mockito.when(effect.min()).thenReturn(50);
Mockito.when(effect.max()).thenReturn(60);
Mockito.when(effect.target()).thenReturn(SpellEffectTarget.DEFAULT);
Mockito.when(spell.constraints()).thenReturn(constraints);
Mockito.when(constraints.freeCell()).thenReturn(false);

FightCastScope scope = makeCastScope(caster, spell, effect, caster.cell());
assertThrows(UnsupportedOperationException.class, () -> handler.handle(scope, scope.effects().get(0)));
handler.handle(scope, scope.effects().get(0));

Optional<Buff> buff1 = caster.buffs().stream().filter(buff -> buff.effect().effect() == 123).findFirst();

assertTrue(buff1.isPresent());
assertBetween(50, 60, buff1.get().effect().min());
assertEquals(1, buff1.get().remainingTurns());
assertFalse(buff1.get().canBeDispelled());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,22 @@ void handle() {
SpellConstraints constraints = Mockito.mock(SpellConstraints.class);

Mockito.when(effect.area()).thenReturn(new CellArea());
Mockito.when(effect.effect()).thenReturn(123);
Mockito.when(effect.min()).thenReturn(50);
Mockito.when(effect.max()).thenReturn(60);
Mockito.when(effect.target()).thenReturn(SpellEffectTarget.DEFAULT);
Mockito.when(spell.constraints()).thenReturn(constraints);
Mockito.when(constraints.freeCell()).thenReturn(false);

FightCastScope scope = makeCastScope(caster, spell, effect, caster.cell());
assertThrows(UnsupportedOperationException.class, () -> handler.handle(scope, scope.effects().get(0)));
handler.handle(scope, scope.effects().get(0));

Optional<Buff> buff1 = caster.buffs().stream().filter(buff -> buff.effect().effect() == 123).findFirst();

assertTrue(buff1.isPresent());
assertBetween(50, 60, buff1.get().effect().min());
assertEquals(1, buff1.get().remainingTurns());
assertTrue(buff1.get().canBeDispelled());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,22 @@ void handle() {
SpellConstraints constraints = Mockito.mock(SpellConstraints.class);

Mockito.when(effect.area()).thenReturn(new CellArea());
Mockito.when(effect.effect()).thenReturn(123);
Mockito.when(effect.min()).thenReturn(50);
Mockito.when(effect.max()).thenReturn(60);
Mockito.when(effect.target()).thenReturn(SpellEffectTarget.DEFAULT);
Mockito.when(spell.constraints()).thenReturn(constraints);
Mockito.when(constraints.freeCell()).thenReturn(false);

FightCastScope scope = makeCastScope(caster, spell, effect, caster.cell());
assertThrows(UnsupportedOperationException.class, () -> handler.handle(scope, scope.effects().get(0)));
handler.handle(scope, scope.effects().get(0));

Optional<Buff> buff1 = caster.buffs().stream().filter(buff -> buff.effect().effect() == 123).findFirst();

assertTrue(buff1.isPresent());
assertBetween(50, 60, buff1.get().effect().min());
assertEquals(1, buff1.get().remainingTurns());
assertFalse(buff1.get().canBeDispelled());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.Optional;

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

Expand Down Expand Up @@ -75,12 +76,22 @@ void handle() {
SpellConstraints constraints = Mockito.mock(SpellConstraints.class);

Mockito.when(effect.area()).thenReturn(new CellArea());
Mockito.when(effect.effect()).thenReturn(123);
Mockito.when(effect.min()).thenReturn(50);
Mockito.when(effect.max()).thenReturn(60);
Mockito.when(effect.target()).thenReturn(SpellEffectTarget.DEFAULT);
Mockito.when(spell.constraints()).thenReturn(constraints);
Mockito.when(constraints.freeCell()).thenReturn(false);

FightCastScope scope = makeCastScope(caster, spell, effect, caster.cell());
assertThrows(UnsupportedOperationException.class, () -> handler.handle(scope, scope.effects().get(0)));
handler.handle(scope, scope.effects().get(0));

Optional<Buff> buff1 = caster.buffs().stream().filter(buff -> buff.effect().effect() == 123).findFirst();

assertTrue(buff1.isPresent());
assertBetween(50, 60, buff1.get().effect().min());
assertEquals(1, buff1.get().remainingTurns());
assertTrue(buff1.get().canBeDispelled());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,22 @@ void handle() {
SpellConstraints constraints = Mockito.mock(SpellConstraints.class);

Mockito.when(effect.area()).thenReturn(new CellArea());
Mockito.when(effect.effect()).thenReturn(123);
Mockito.when(effect.min()).thenReturn(50);
Mockito.when(effect.max()).thenReturn(60);
Mockito.when(effect.target()).thenReturn(SpellEffectTarget.DEFAULT);
Mockito.when(spell.constraints()).thenReturn(constraints);
Mockito.when(constraints.freeCell()).thenReturn(false);

FightCastScope scope = makeCastScope(caster, spell, effect, caster.cell());
assertThrows(UnsupportedOperationException.class, () -> handler.handle(scope, scope.effects().get(0)));
handler.handle(scope, scope.effects().get(0));

Optional<Buff> buff1 = caster.buffs().stream().filter(buff -> buff.effect().effect() == 123).findFirst();

assertTrue(buff1.isPresent());
assertBetween(50, 60, buff1.get().effect().min());
assertEquals(1, buff1.get().remainingTurns());
assertTrue(buff1.get().canBeDispelled());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,35 @@ void handleShouldBeConsideredAsBuff() {
assertEquals(1, found.get().remainingTurns());
}

@Test
void handleOnSelfShouldBeConsideredAsBuff() {
SpellEffect effect = Mockito.mock(SpellEffect.class);
Spell spell = Mockito.mock(Spell.class);
SpellConstraints constraints = Mockito.mock(SpellConstraints.class);

Mockito.when(effect.effect()).thenReturn(787);
Mockito.when(effect.min()).thenReturn(183);
Mockito.when(effect.max()).thenReturn(1);
Mockito.when(effect.area()).thenReturn(new CellArea());
Mockito.when(effect.target()).thenReturn(SpellEffectTarget.DEFAULT);
Mockito.when(effect.duration()).thenReturn(0);
Mockito.when(spell.constraints()).thenReturn(constraints);
Mockito.when(constraints.freeCell()).thenReturn(false);

FightCastScope scope = makeCastScope(caster, spell, effect, caster.cell());
handler.handle(scope, scope.effects().get(0));

Optional<Buff> found = caster.buffs().stream().filter(buff -> buff.effect().equals(effect)).findFirst();

assertTrue(found.isPresent());
assertEquals(caster, found.get().caster());
assertEquals(caster, found.get().target());
assertEquals(effect, found.get().effect());
assertEquals(spell, found.get().action());
assertEquals(handler, found.get().hook());
assertEquals(2, found.get().remainingTurns());
}

@Test
void startTurnShouldApplySpell() {
SpellEffect effect = Mockito.mock(SpellEffect.class);
Expand Down