Skip to content

Commit

Permalink
Track completions of The Pied Piper
Browse files Browse the repository at this point in the history
  • Loading branch information
AntumDeluge committed Jul 9, 2024
1 parent 12a4eec commit 015dc33
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/games/stendhal/server/maps/quests/ThePiedPiper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* $Id$ */
/***************************************************************************
* (C) Copyright 2003-2023 - Stendhal *
* (C) Copyright 2003-2024 - Stendhal *
***************************************************************************
***************************************************************************
* *
Expand Down Expand Up @@ -63,7 +63,7 @@
*
* REPETITIONS: <ul><li> once between a week and two weeks.</ul>
*/
public class ThePiedPiper extends AbstractQuest implements ITPPQuestConstants {
public class ThePiedPiper extends CompletionsTrackingQuest implements ITPPQuestConstants {

protected static final Logger logger = Logger.getLogger(ThePiedPiper.class);

Expand Down Expand Up @@ -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();
}
Expand Down
25 changes: 21 additions & 4 deletions src/games/stendhal/server/maps/quests/piedpiper/InvasionPhase.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* $Id$ */
/***************************************************************************
* Copyright (C) 2003-2023 - Arianne *
* Copyright (C) 2003-2024 - Arianne *
***************************************************************************
***************************************************************************
* *
Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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.piedpiper;


Expand Down Expand Up @@ -31,6 +42,11 @@ public static SpeakerNPC getMainNPC() {
* gold amount for hunting rats.
*/
public static int calculateReward(Player player) {
if (player.isQuestInState(QUEST_SLOT, 0, "done")) {
// information for previous raid is no longer available
return 0;
}

int moneys = 0;
int kills = 0;
for(int i=0; i<RAT_TYPES.size(); i++) {
Expand Down
33 changes: 32 additions & 1 deletion tests/games/stendhal/server/maps/quests/ThePiedPiperTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* $Id$ */
/***************************************************************************
* (C) Copyright 2003-2010 - Stendhal *
* (C) Copyright 2003-2024 - Stendhal *
***************************************************************************
***************************************************************************
* *
Expand All @@ -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 {
Expand Down Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<numb;i++) {
Expand Down Expand Up @@ -146,4 +157,11 @@ protected String details() {
return(sb.toString());
}

public static Player getPlayer() {
return player;
}

public static SpeakerNPC getNPC() {
return npc;
}
}

0 comments on commit 015dc33

Please sign in to comment.