From 1d3dc06d3ee20a6d3684b568db075fc54acff653 Mon Sep 17 00:00:00 2001 From: Jordan Irwin Date: Thu, 6 Jun 2024 22:04:31 -0700 Subject: [PATCH] Track completions of The Pied Piper --- .../server/maps/quests/ThePiedPiper.java | 6 +-- .../maps/quests/piedpiper/InvasionPhase.java | 25 ++++++++-- .../quests/piedpiper/RewardPlayerAction.java | 26 +++++++++- .../server/maps/quests/ThePiedPiperTest.java | 33 ++++++++++++- .../thepiedpiper/InvasionPhaseTest.java | 47 +++++++++++++++---- .../quests/thepiedpiper/TPPTestHelper.java | 20 +++++++- 6 files changed, 137 insertions(+), 20 deletions(-) diff --git a/src/games/stendhal/server/maps/quests/ThePiedPiper.java b/src/games/stendhal/server/maps/quests/ThePiedPiper.java index e5c92be27c8..93a7f72fb82 100644 --- a/src/games/stendhal/server/maps/quests/ThePiedPiper.java +++ b/src/games/stendhal/server/maps/quests/ThePiedPiper.java @@ -1,6 +1,6 @@ /* $Id$ */ /*************************************************************************** - * (C) Copyright 2003-2023 - Stendhal * + * (C) Copyright 2003-2024 - Stendhal * *************************************************************************** *************************************************************************** * * @@ -63,7 +63,7 @@ * * REPETITIONS: */ -public class ThePiedPiper extends AbstractQuest implements ITPPQuestConstants { +public class ThePiedPiper extends CompletionsTrackingQuest implements ITPPQuestConstants { protected static final Logger logger = Logger.getLogger(ThePiedPiper.class); @@ -256,7 +256,7 @@ public void addToWorld() { fillQuestInfo( "The Pied Piper", "Ados City has a rat problem from time to time.", - true); + true, 7, 1); startQuest(); } diff --git a/src/games/stendhal/server/maps/quests/piedpiper/InvasionPhase.java b/src/games/stendhal/server/maps/quests/piedpiper/InvasionPhase.java index 26c058f0795..28c998cc80f 100644 --- a/src/games/stendhal/server/maps/quests/piedpiper/InvasionPhase.java +++ b/src/games/stendhal/server/maps/quests/piedpiper/InvasionPhase.java @@ -1,6 +1,6 @@ /* $Id$ */ /*************************************************************************** - * Copyright (C) 2003-2023 - Arianne * + * Copyright (C) 2003-2024 - Arianne * *************************************************************************** *************************************************************************** * * @@ -18,6 +18,7 @@ import java.util.List; import java.util.Map; +import games.stendhal.common.MathHelper; import games.stendhal.common.Rand; import games.stendhal.common.grammar.Grammar; import games.stendhal.common.parser.Sentence; @@ -295,6 +296,22 @@ public void update (Observable obj, Object arg) { } } + /** + * Initializes quest state string after player kills first rat. + * + * @param player + * Player doing quest. + */ + private void initQuestState(final Player player) { + int index = 7; + if ("done".equals(player.getQuest(QUEST_SLOT, 0))) { + index = 1; + } + // preserve completions count + final int completions = MathHelper.parseIntDefault(player.getQuest(QUEST_SLOT, index), 0); + player.setQuest(QUEST_SLOT, "rats;0;0;0;0;0;0;" + completions); + } + /** * method for making records about killing rats * in player's quest slot. @@ -316,17 +333,17 @@ private void killsRecorder(Player player, final RPEntity victim) { } if((player.getQuest(QUEST_SLOT)==null)|| - (player.getQuest(QUEST_SLOT).equals("done")|| + (player.getQuest(QUEST_SLOT, 0).equals("done")|| (player.getQuest(QUEST_SLOT).equals("")))){ // player just killed his first creature. - player.setQuest(QUEST_SLOT, "rats;0;0;0;0;0;0"); + initQuestState(player); } // we using here and after "i+1" because player's quest index 0 // is occupied by quest stage description. if ("".equals(player.getQuest(QUEST_SLOT,i+1))){ // something really wrong, will correct this... - player.setQuest(QUEST_SLOT,"rats;0;0;0;0;0;0"); + initQuestState(player); } int kills; try { diff --git a/src/games/stendhal/server/maps/quests/piedpiper/RewardPlayerAction.java b/src/games/stendhal/server/maps/quests/piedpiper/RewardPlayerAction.java index 05420345673..b1794daf502 100644 --- a/src/games/stendhal/server/maps/quests/piedpiper/RewardPlayerAction.java +++ b/src/games/stendhal/server/maps/quests/piedpiper/RewardPlayerAction.java @@ -1,5 +1,17 @@ +/*************************************************************************** + * Copyright © 2003-2024 - Faiumoni e. V. * + *************************************************************************** + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ package games.stendhal.server.maps.quests.piedpiper; +import games.stendhal.common.MathHelper; import games.stendhal.common.parser.Sentence; import games.stendhal.server.core.engine.SingletonRepository; import games.stendhal.server.entity.item.StackableItem; @@ -27,6 +39,18 @@ public void fire(final Player player, final Sentence sentence, final EventRaiser moneys.setQuantity(quantity); player.equipOrPutOnGround(moneys); mayor.say("Please take "+quantity+" money, thank you very much for your help."); - player.setQuest(QUEST_SLOT, "done"); + setQuestDone(player); + } + + /** + * Sets quest state to done and increments number of completions. + * + * @param player + * Player completing quest. + */ + private void setQuestDone(final Player player) { + // preserve completions count + final int completions = MathHelper.parseIntDefault(player.getQuest(QUEST_SLOT, 7), 0); + player.setQuest(QUEST_SLOT, "done;" + (completions + 1)); } } diff --git a/tests/games/stendhal/server/maps/quests/ThePiedPiperTest.java b/tests/games/stendhal/server/maps/quests/ThePiedPiperTest.java index f5b955b18f6..e5edcb3fcb6 100644 --- a/tests/games/stendhal/server/maps/quests/ThePiedPiperTest.java +++ b/tests/games/stendhal/server/maps/quests/ThePiedPiperTest.java @@ -1,6 +1,6 @@ /* $Id$ */ /*************************************************************************** - * (C) Copyright 2003-2010 - Stendhal * + * (C) Copyright 2003-2024 - Stendhal * *************************************************************************** *************************************************************************** * * @@ -12,13 +12,20 @@ ***************************************************************************/ package games.stendhal.server.maps.quests; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; import org.apache.log4j.Logger; import org.junit.BeforeClass; import org.junit.Test; +import games.stendhal.common.MathHelper; +import games.stendhal.server.entity.npc.SpeakerNPC; +import games.stendhal.server.entity.player.Player; import games.stendhal.server.maps.quests.piedpiper.ITPPQuestConstants; +import games.stendhal.server.maps.quests.thepiedpiper.InvasionPhaseTest; import games.stendhal.server.maps.quests.thepiedpiper.TPPTestHelper; public class ThePiedPiperTest implements ITPPQuestConstants { @@ -48,4 +55,28 @@ public void testPhaseChanging() { assertEquals(TPP_Phase.TPP_INACTIVE, ThePiedPiper.getPhase()); } + private void doQuest(final InvasionPhaseTest phase) { + phase.startInvasion(); + phase.killRats(15); + phase.endInvasion(); + phase.collectReward(); + phase.resetReward(); + } + + @Test + public void testCompletions() { + final String questSlot = quest.getSlotName(); + final Player player = TPPTestHelper.getPlayer(); + assertThat(player, notNullValue()); + final SpeakerNPC npc = TPPTestHelper.getNPC(); + assertThat(npc, notNullValue()); + + final InvasionPhaseTest phaseTest = new InvasionPhaseTest(); + for (int count = 0; count < 5; count++) { + assertThat(MathHelper.parseIntDefault(player.getQuest(questSlot, 1), 0), is(count)); + // run quest + doQuest(phaseTest); + } + assertThat(player.getQuest(questSlot), is("done;5")); + } } diff --git a/tests/games/stendhal/server/maps/quests/thepiedpiper/InvasionPhaseTest.java b/tests/games/stendhal/server/maps/quests/thepiedpiper/InvasionPhaseTest.java index 9e27469f016..a4af4dfe5c3 100644 --- a/tests/games/stendhal/server/maps/quests/thepiedpiper/InvasionPhaseTest.java +++ b/tests/games/stendhal/server/maps/quests/thepiedpiper/InvasionPhaseTest.java @@ -1,6 +1,19 @@ +/*************************************************************************** + * Copyright © 2003-2024 - Faiumoni e. V. * + *************************************************************************** + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ package games.stendhal.server.maps.quests.thepiedpiper; +import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static utilities.SpeakerNPCTestHelper.getReply; @@ -20,11 +33,31 @@ public static void setUpBeforeClass() throws Exception { TPPTestHelper.setUpBeforeClass(); } - @Test - public void testInvasionPhase() { + public void startInvasion() { // [17:50] Mayor Chalmers shouts: Ados city is under rats invasion! Anyone who will help to clean up city, will be rewarded! ThePiedPiper.setPhase(TPP_Phase.TPP_INACTIVE); ThePiedPiper.switchToNextPhase(); + } + + public void endInvasion() { + ThePiedPiper.setPhase(TPP_Phase.TPP_INACTIVE); + } + + public void collectReward() { + en.step(player, "hi"); + en.step(player, "reward"); + assertThat(getReply(npc), is("Please take "+ rewardMoneys +" money, thank you very much for your help.")); + en.step(player, "bye"); + assertThat(getReply(npc), is("Good day to you.")); + } + + public void resetReward() { + rewardMoneys = 0; + } + + @Test + public void testInvasionPhase() { + startInvasion(); //quest.phaseInactiveToInvasion(); en.step(player, "bye"); // in case if previous test was failed en.step(player, "hi"); @@ -53,13 +86,10 @@ public void testInvasionPhase() { "so I will give you "+rewardMoneys+ " money as a #reward for that job.", getReply(npc)); assertEquals(questHistory, quest.getHistory(player)); - en.step(player, "reward"); - assertEquals("Please take "+ rewardMoneys +" money, thank you very much for your help.", getReply(npc)); + collectReward(); questHistory.clear(); questHistory.add("I have killed some rats in Ados city and got a reward from Mayor Chalmers!"); assertEquals(questHistory, quest.getHistory(player)); - en.step(player, "bye"); - assertEquals("Good day to you.", getReply(npc)); } @Test @@ -121,13 +151,10 @@ public void testAccumulatingRewards() { " money as a #reward for that job.", getReply(npc)); assertTrue("", (rewardMoneys > tempReward)); assertEquals(questHistory, quest.getHistory(player)); - en.step(player, "reward"); - assertEquals("Please take "+ rewardMoneys +" money, thank you very much for your help.", getReply(npc)); + collectReward(); questHistory.clear(); questHistory.add("I have killed some rats in Ados city and got a reward from Mayor Chalmers!"); assertEquals(questHistory, quest.getHistory(player)); - en.step(player, "bye"); - assertEquals("Good day to you.", getReply(npc)); } } diff --git a/tests/games/stendhal/server/maps/quests/thepiedpiper/TPPTestHelper.java b/tests/games/stendhal/server/maps/quests/thepiedpiper/TPPTestHelper.java index 0ad81210338..9e24ca38fee 100644 --- a/tests/games/stendhal/server/maps/quests/thepiedpiper/TPPTestHelper.java +++ b/tests/games/stendhal/server/maps/quests/thepiedpiper/TPPTestHelper.java @@ -1,3 +1,14 @@ +/*************************************************************************** + * Copyright © 2003-2024 - Faiumoni e. V. * + *************************************************************************** + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ package games.stendhal.server.maps.quests.thepiedpiper; import org.apache.log4j.Logger; @@ -112,7 +123,7 @@ private void killRat(Creature rat, int count) { * function for killing creatures. * @param numb - number of creatures to kill. */ - protected void killRats(int numb) { + public void killRats(int numb) { int count=0; logger.info("number of rats to kill: "+numb); for (int i=0; i