Skip to content

Commit

Permalink
implemented DeliverItemQuestHistoryBuilder an infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
nhnb committed Sep 13, 2023
1 parent 67a9edc commit 7112080
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public LuaQuest create(final String slotName, final String name, final String de
* @return
* New quest.
*/
public IQuest createManuscript(final QuestBuilder<?, ?> builder) {
public IQuest createManuscript(final QuestBuilder<?, ?, ?> builder) {
return new BuiltQuest(builder);
}

Expand All @@ -136,7 +136,7 @@ public IQuest createManuscript(final QuestBuilder<?, ?> builder) {
* @return
* New QuestBuilder.
*/
public QuestBuilder<?, ?> createBuilder(final String task) {
public QuestBuilder<?, ?, ?> createBuilder(final String task) {
if ("BringItemTask".equals(task)) {
return new BringItemQuestBuilder();
} else if ("KillCreaturesTask".equals(task)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
***************************************************************************/
package games.stendhal.server.entity.npc.quest;

public class BringItemQuestBuilder extends QuestBuilder<BringItemTask, SimpleQuestCompleteBuilder> {
public class BringItemQuestBuilder extends QuestBuilder<BringItemTask, SimpleQuestCompleteBuilder, QuestHistoryBuilder> {

public BringItemQuestBuilder() {
super(new BringItemTask());
complete = new SimpleQuestCompleteBuilder();
history = new QuestHistoryBuilder();
}

}
12 changes: 7 additions & 5 deletions src/games/stendhal/server/entity/npc/quest/BuiltQuest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
* @author hendrik
*/
public class BuiltQuest extends AbstractQuest {
private QuestBuilder<?, ?> questBuilder;
private QuestBuilder<?, ?, ?> questBuilder;
private String questSlot = null;

/**
* creates a Quest based on a builder
*
* @param questBuilder quest builder
*/
public BuiltQuest(QuestBuilder<?, ?> questBuilder) {
public BuiltQuest(QuestBuilder<?, ?, ?> questBuilder) {
this.questBuilder = questBuilder;
this.questSlot = questBuilder.info().getInternalName().toLowerCase();
}
Expand All @@ -57,12 +57,14 @@ public List<String> getHistory(Player player) {
return res;
}
res.add(history.getWhenQuestWasAccepted());
List<String> progress = questBuilder.task().calculateHistoryProgress(player);
List<String> progress = questBuilder.task().calculateHistoryProgress(history, player);
if (progress != null) {
res.addAll(progress);
}
if ("done".equals(questState) || ("start".equals(questState) && questBuilder.task().isCompleted(player, questSlot))) {
res.add(history.getWhenTaskWasCompleted());
if (history.getWhenTaskWasCompleted() != null) {
res.add(history.getWhenTaskWasCompleted());
}
}
if ("done".equals(questState)) {
res.add(history.getWhenQuestWasCompleted());
Expand Down Expand Up @@ -139,7 +141,7 @@ public String getSlotName() {
@Override
public boolean isRepeatable(final Player player) {
return isCompleted(player)
&& questBuilder.info().getRepeatableAfterMinutes() > 0
&& questBuilder.info().getRepeatableAfterMinutes() > -1
&& new TimePassedCondition(questSlot, 1, questBuilder.info().getRepeatableAfterMinutes()).fire(player,null, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
***************************************************************************/
package games.stendhal.server.entity.npc.quest;

public class DeliverItemQuestBuilder extends QuestBuilder<DeliverItemTask, DeliverItemQuestCompleteBuilder> {
public class DeliverItemQuestBuilder extends QuestBuilder<DeliverItemTask, DeliverItemQuestCompleteBuilder, DeliverItemQuestHistoryBuilder> {

public DeliverItemQuestBuilder() {
super(new DeliverItemTask());
complete = new DeliverItemQuestCompleteBuilder(task());
history = new DeliverItemQuestHistoryBuilder();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/***************************************************************************
* (C) Copyright 2022-2023 - 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.entity.npc.quest;

/**
* defines the "history" of player progress as shown in the travel log
*
* @author hendrik
*/
public class DeliverItemQuestHistoryBuilder extends QuestHistoryBuilder {

private String whenItemWasGiven;
private String whenToldAboutCustomer;
private String whenInTime;
private String whenOutOfTime;


@Override
public DeliverItemQuestHistoryBuilder whenNpcWasMet(String whenNpcWasMet) {
super.whenNpcWasMet(whenNpcWasMet);
return this;
}

@Override
public DeliverItemQuestHistoryBuilder whenQuestWasRejected(String whenQuestWasRejected) {
super.whenQuestWasRejected(whenQuestWasRejected);
return this;
}

@Override
public DeliverItemQuestHistoryBuilder whenQuestWasAccepted(String whenQuestWasAccepted) {
super.whenQuestWasAccepted(whenQuestWasAccepted);
return this;
}

@Override
public DeliverItemQuestHistoryBuilder whenTaskWasCompleted(String whenTaskWasCompleted) {
super.whenTaskWasCompleted(whenTaskWasCompleted);
return this;
}

@Override
public DeliverItemQuestHistoryBuilder whenQuestWasCompleted(String whenQuestWasCompleted) {
super.whenQuestWasCompleted(whenQuestWasCompleted);
return this;
}

@Override
public DeliverItemQuestHistoryBuilder whenQuestCanBeRepeated(String whenQuestCanBeRepeated) {
super.whenQuestCanBeRepeated(whenQuestCanBeRepeated);
return this;
}

@Override
public DeliverItemQuestHistoryBuilder whenCompletionsShown(String whenCompletionsShown) {
super.whenCompletionsShown(whenCompletionsShown);
return this;
}


public DeliverItemQuestHistoryBuilder whenItemWasGiven(String whenItemWasGiven) {
this.whenItemWasGiven = whenItemWasGiven;
return this;
}

public DeliverItemQuestHistoryBuilder whenToldAboutCustomer(String whenToldAboutCustomer) {
this.whenToldAboutCustomer = whenToldAboutCustomer;
return this;
}

public DeliverItemQuestHistoryBuilder whenInTime(String whenInTime) {
this.whenInTime = whenInTime;
return this;
}

public DeliverItemQuestHistoryBuilder whenOutOfTime(String whenOutOfTime) {
this.whenOutOfTime = whenOutOfTime;
return this;
}


public String getWhenItemWasGiven() {
return whenItemWasGiven;
}

public String getWhenToldAboutCustomer() {
return whenToldAboutCustomer;
}

public String getWhenInTime() {
return whenInTime;
}

public String getWhenOutOfTime() {
return whenOutOfTime;
}

}
52 changes: 28 additions & 24 deletions src/games/stendhal/server/entity/npc/quest/DeliverItemTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
import games.stendhal.server.entity.npc.NPCList;
import games.stendhal.server.entity.npc.SpeakerNPC;
import games.stendhal.server.entity.npc.action.SetQuestToTimeStampAction;
import games.stendhal.server.entity.npc.condition.AlwaysFalseCondition;
import games.stendhal.server.entity.npc.condition.OutfitCompatibleWithClothesCondition;
import games.stendhal.server.entity.npc.condition.QuestCompletedCondition;
import games.stendhal.server.entity.player.Player;
import games.stendhal.server.util.StringUtils;


public class DeliverItemTask extends QuestTaskBuilder {
Expand All @@ -41,27 +42,6 @@ public class DeliverItemTask extends QuestTaskBuilder {

private Map<String, DeliverItemOrder> orders = new HashMap<>();

/*
@Override
public List<String> getHistory(final Player player) {
final String questState = player.getQuest(QUEST_SLOT);
res.add("I met Leander and agreed to help with pizza delivery.");
if (!"done".equals(questState)) {
final String[] questData = questState.split(";");
final String customerName = questData[0];
final CustomerData customerData = customerDB.get(customerName);
res.add("Leander gave me a " + customerData.flavor + " for " + customerName + ".");
res.add("Leander told me: \"" + customerData.npcDescription + "\"");
if (!isDeliveryTooLate(player)) {
res.add("If I hurry I might still get there with the pizza hot.");
} else {
res.add("The pizza has already gone cold.");
}
}
return res;
}
*/


/**
* Get a list of customers appropriate for a player
Expand Down Expand Up @@ -214,12 +194,11 @@ public void fire(final Player player, final Sentence sentence, final EventRaiser

@Override
ChatCondition buildQuestCompletedCondition(String questSlot) {
return new QuestCompletedCondition(questSlot);
return new AlwaysFalseCondition();
}

@Override
ChatAction buildQuestCompleteAction(String questSlot) {
// TODO Auto-generated method stub
return null;
}

Expand All @@ -234,4 +213,29 @@ Map<String, DeliverItemOrder> getOrders() {
String getItemName() {
return "pizza";
}

@Override
public List<String> calculateHistoryProgress(QuestHistoryBuilder historyBuilder, Player player) {
DeliverItemQuestHistoryBuilder history = (DeliverItemQuestHistoryBuilder) historyBuilder;
List<String> res = new LinkedList<>();
final String questState = player.getQuest(QUEST_SLOT, 0);
if (!"done".equals(questState)) {
final String[] questData = questState.split(";");
final String customerName = questData[0];
final DeliverItemOrder customerData = orders.get(customerName);
Map<String, String> params = new HashMap<>();
params.put("flavor", customerData.getFlavor());
params.put("customerName", customerName);
params.put("customerDescription", customerData.getNpcDescription());
res.add(StringUtils.substitute(history.getWhenItemWasGiven(), params));
res.add(StringUtils.substitute(history.getWhenToldAboutCustomer(), params));
if (!isDeliveryTooLate(player)) {
res.add(StringUtils.substitute(history.getWhenInTime(), params));
} else {
res.add(StringUtils.substitute(history.getWhenOutOfTime(), params));
}
}
return res;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
***************************************************************************/
package games.stendhal.server.entity.npc.quest;

public class KillCreaturesQuestBuilder extends QuestBuilder<KillCreaturesTask, SimpleQuestCompleteBuilder> {
public class KillCreaturesQuestBuilder extends QuestBuilder<KillCreaturesTask, SimpleQuestCompleteBuilder, QuestHistoryBuilder> {

public KillCreaturesQuestBuilder() {
super(new KillCreaturesTask());
complete = new SimpleQuestCompleteBuilder();
history = new QuestHistoryBuilder();
}

}
7 changes: 4 additions & 3 deletions src/games/stendhal/server/entity/npc/quest/QuestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
* @author hendrik
* @param <T> QuestTaskBuilder
*/
public class QuestBuilder<T extends QuestTaskBuilder, C extends QuestCompleteBuilder> {
public class QuestBuilder<T extends QuestTaskBuilder, C extends QuestCompleteBuilder, H extends QuestHistoryBuilder> {

private QuestInfoBuilder info = new QuestInfoBuilder();
private QuestHistoryBuilder history = new QuestHistoryBuilder();
@SuppressWarnings("unchecked")
private QuestOfferBuilder offer = new QuestOfferBuilder();
private T task = null;
protected C complete;
protected H history = null;

/**
* creates a QuestBuilder
Expand All @@ -48,7 +49,7 @@ public QuestInfoBuilder info() {
*
* @return QuestHistoryBuilder
*/
public QuestHistoryBuilder history() {
public H history() {
return history;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ public interface QuestManuscript {
*
* @return QuestBuilder
*/
public QuestBuilder<?, ?> story();
public QuestBuilder<?, ?, ?> story();
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ boolean isCompleted(Player player, String questSlot) {
return buildQuestCompletedCondition(questSlot).fire(player, null, null);
}

public List<String> calculateHistoryProgress(@SuppressWarnings("unused") Player player) {
public List<String> calculateHistoryProgress(
@SuppressWarnings("unused") QuestHistoryBuilder history,
@SuppressWarnings("unused") Player player) {
return null;
}

Expand Down
9 changes: 7 additions & 2 deletions src/games/stendhal/server/maps/quests/PizzaDelivery.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ public DeliverItemQuestBuilder story() {
.whenNpcWasMet("I met Leander, the baker of Semos.")
.whenQuestWasRejected("He asked me to deliver pizza but I rejected his request.")
.whenQuestWasAccepted("I agreed to help with pizza delivery")
.whenQuestWasCompleted("I delivered the last pizza Leander gave to me.");
.whenItemWasGiven("Leander gave me a [flavor] for [customerName].")
.whenToldAboutCustomer("Leander told me: \"[customerDescription]\"")
.whenInTime("If I hurry, I might still get there, with the pizza hot.")
.whenOutOfTime("The pizza has already gone cold.")
.whenQuestWasCompleted("I delivered the last pizza Leander gave to me.")
.whenQuestCanBeRepeated("But I'd bet, Leander has more orders.");
;

quest.offer()
.respondToRequest("I need you to quickly deliver a hot pizza. If you're fast enough, you might get quite a nice tip. So, will you do it?")
Expand All @@ -79,7 +85,6 @@ public DeliverItemQuestBuilder story() {
.remind(null);



// Don't add Sally here, as it would conflict with Leander telling
// about his daughter.
quest.task().order()
Expand Down

0 comments on commit 7112080

Please sign in to comment.