Skip to content

Commit

Permalink
Merge pull request #104 from Alan19/feature/kept_dice_count_in_messag…
Browse files Browse the repository at this point in the history
…e_command

Dice kept in message commands + prevent other people from enhancing your own rolls
  • Loading branch information
Alan19 authored Jul 9, 2022
2 parents d6a26da + a07ec5e commit bad5114
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 30 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ apply plugin: 'java'
apply plugin: 'maven-publish'

group = 'main'
version = '8.0.3'
version = '8.0.6'

description = "A bot for the Facets tabletop RPG"

Expand Down
42 changes: 23 additions & 19 deletions src/main/java/logic/RollLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

import javax.annotation.Nullable;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
Expand All @@ -41,24 +40,29 @@
*/
public class RollLogic implements VelenHybridHandler {

public static final SlashCommandOption DICE_POOL = SlashCommandOption.create(SlashCommandOptionType.STRING, "dicepool", "The dice pool to roll with.", true);
public static final SlashCommandOption DISCOUNT = SlashCommandOption.create(SlashCommandOptionType.LONG, "discount", "The number of plot points to discount (negative results in a plot point cost increase).", false);
public static final SlashCommandOption DICE_KEPT = SlashCommandOption.create(SlashCommandOptionType.LONG, "dicekept", "The number of dice kept. Keeps two dice by default.", false);
public static final SlashCommandOption ENHANCEABLE = SlashCommandOption.create(SlashCommandOptionType.BOOLEAN, "enhanceable", "Allows the roll to be enhanced after the roll.", false);
public static final SlashCommandOption OPPORTUNITY = SlashCommandOption.create(SlashCommandOptionType.BOOLEAN, "opportunity", "Allows for opportunities on the roll. Defaults to true.", false);
private final int diceKept;

public RollLogic(int diceKept) {
this.diceKept = diceKept;
}

public static void setupRollCommand(Velen velen) {
RollLogic rollLogic = new RollLogic();
final List<SlashCommandOption> rollCommandOptions = getRollCommandOptions();
rollCommandOptions.add(SlashCommandOption.create(SlashCommandOptionType.BOOLEAN, "opportunity", "Allows for opportunities on the roll. Defaults to true.", false));
VelenCommand.ofHybrid("roll", "Rolls some dice!", velen, rollLogic)
.addOptions(rollCommandOptions.toArray(new SlashCommandOption[0]))
RollLogic rollLogic = new RollLogic(2);
VelenCommand.ofHybrid("roll", "Roll some dice!", velen, rollLogic)
.addOptions(DICE_POOL, DISCOUNT, ENHANCEABLE, DICE_KEPT, OPPORTUNITY)
.addFormats("roll :[dicepool:of(string):hasMany()]")
.addShortcuts("r")
.addShortcuts("r", "r2")
.attach();
}

static List<SlashCommandOption> getRollCommandOptions() {
List<SlashCommandOption> options = new ArrayList<>();
options.add(SlashCommandOption.create(SlashCommandOptionType.STRING, "dicepool", "The dice pool to roll with.", true));
options.add(SlashCommandOption.create(SlashCommandOptionType.LONG, "discount", "The number of plot points to discount (negative results in a plot point cost increase).", false));
options.add(SlashCommandOption.create(SlashCommandOptionType.LONG, "dicekept", "The number of dice kept. Keeps two dice by default.", false));
options.add(SlashCommandOption.create(SlashCommandOptionType.BOOLEAN, "enhanceable", "Allows the roll to be enhanced after the roll.", false));
return options;
List.of(1, 3, 4, 5).forEach(integer -> VelenCommand.ofHybrid("roll%d".formatted(integer), "Roll some dice and keeps %s dice!".formatted(integer), velen, new RollLogic(integer))
.addOptions(DICE_POOL, DISCOUNT, ENHANCEABLE, OPPORTUNITY)
.addFormats("roll%d :[dicepool:of(string):hasMany()]".formatted(integer))
.addShortcuts("r%d".formatted(integer))
.attach());
}

/**
Expand All @@ -71,7 +75,7 @@ static List<SlashCommandOption> getRollCommandOptions() {
* @param enhanceable If there is an enhancement override on the roll
* @param opportunity If opportunities are enabled
*/
public static void handleSlashCommandRoll(VelenGeneralEvent event, String dicePool, Integer discount, Integer diceKept, @Nullable Boolean enhanceable, Boolean opportunity) {
public static void handleRoll(VelenGeneralEvent event, String dicePool, Integer discount, Integer diceKept, @Nullable Boolean enhanceable, Boolean opportunity) {
final User user = event.getUser();
final RollParameters rollParameters = new RollParameters(dicePool, discount, enhanceable, opportunity, diceKept);
final VelenGeneralResponder updaterFuture = event.createResponder();
Expand Down Expand Up @@ -156,12 +160,12 @@ public static EmbedBuilder[] removeFirstEmbedFooter(Message interactionMessage)
@Override
public void onEvent(VelenGeneralEvent event, VelenGeneralResponder responder, User user, VelenHybridArguments args) {
final String dicePool = args.getManyWithName("dicepool").orElse("");
final Long diceKept = args.withName("dicekept").flatMap(VelenOption::asLong).orElse(2L);
final Integer kept = args.withName("dicekept").flatMap(VelenOption::asInteger).orElse(this.diceKept);
final Boolean opportunity = args.withName("opportunity").flatMap(VelenOption::asBoolean).orElse(true);
final Boolean enhanceable = args.withName("enhanceable").flatMap(VelenOption::asBoolean).orElse(null);
final Long discount = args.withName("discount").flatMap(VelenOption::asLong).orElse(0L);

handleSlashCommandRoll(event, dicePool, Math.toIntExact(discount), Math.toIntExact(diceKept), enhanceable, opportunity);
handleRoll(event, dicePool, Math.toIntExact(discount), kept, enhanceable, opportunity);

}
}
4 changes: 2 additions & 2 deletions src/main/java/logic/RollPoolLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ else if (Stream.of("vitality", "initiative", "willpower").anyMatch(pool::equalsI
SheetsHandler.getSavePool(StringUtils.capitalize(pool.toLowerCase()), user)
.map(defaultPool -> MessageFormat.format("{0} {1}", defaultPool, bonuses))
.onFailure(throwable -> event.getChannel().sendMessage(throwable.getMessage()))
.onSuccess(dicePool -> RollLogic.handleSlashCommandRoll(event, dicePool, discount, diceKept, enhanceable, false));
.onSuccess(dicePool -> RollLogic.handleRoll(event, dicePool, discount, diceKept, enhanceable, false));
}
else {
Boolean opportunity = arguments.withName("opportunity").flatMap(VelenOption::asBoolean).orElse(true);
SheetsHandler.getSavedPool(pool.toLowerCase(), user)
.map(defaultPool -> MessageFormat.format("{0} {1}", defaultPool, bonuses))
.onFailure(throwable -> event.getChannel().sendMessage(throwable.getMessage()))
.onSuccess(dicePool -> RollLogic.handleSlashCommandRoll(event, dicePool, discount, diceKept, enhanceable, opportunity));
.onSuccess(dicePool -> RollLogic.handleRoll(event, dicePool, discount, diceKept, enhanceable, opportunity));
}
}
}
27 changes: 19 additions & 8 deletions src/main/java/logic/TestLogic.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package logic;

import org.javacord.api.entity.user.User;
import org.javacord.api.interaction.SlashCommandOption;
import pw.mihou.velen.interfaces.Velen;
import pw.mihou.velen.interfaces.VelenCommand;
import pw.mihou.velen.interfaces.VelenHybridHandler;
Expand All @@ -12,25 +11,37 @@

import java.util.List;

import static logic.RollLogic.*;

public class TestLogic implements VelenHybridHandler {
private final int diceKept;

public TestLogic(int diceKept) {
this.diceKept = diceKept;
}

public static void setTestCommand(Velen velen) {
TestLogic testLogic = new TestLogic();
List<SlashCommandOption> options = RollLogic.getRollCommandOptions();
TestLogic testLogic = new TestLogic(2);
VelenCommand.ofHybrid("test", "Rolls some dice with opportunities disabled!", velen, testLogic)
.addOptions(options.toArray(new SlashCommandOption[0]))
.addOptions(DICE_POOL, DISCOUNT, ENHANCEABLE, DICE_KEPT)
.addFormats("test :[dicepool:of(string):hasMany()]")
.addShortcuts("t")
.addShortcuts("t", "t2")
.attach();
List.of(1, 3, 4, 5).forEach(integer -> VelenCommand.ofHybrid("test%d".formatted(integer), "Roll some dice with opportuities disabled and keeps %s dice!".formatted(integer), velen, new TestLogic(integer))
.addOptions(DICE_POOL, DISCOUNT, ENHANCEABLE)
.addFormats("test%d :[dicepool:of(string):hasMany()]".formatted(integer))
.addShortcuts("t%d".formatted(integer))
.attach());

}

@Override
public void onEvent(VelenGeneralEvent event, VelenGeneralResponder responder, User user, VelenHybridArguments args) {
final String dicePool = args.getManyWithName("dicepool").orElse("");
final Integer kept = args.withName("dicekept").flatMap(VelenOption::asInteger).orElse(this.diceKept);
final Integer discount = args.withName("discount").flatMap(VelenOption::asInteger).orElse(0);
final Integer diceKept = args.withName("dicekept").flatMap(VelenOption::asInteger).orElse(2);
final Boolean enhanceable = args.withName("enhanceable").flatMap(VelenOption::asBoolean).orElse(null);

RollLogic.handleSlashCommandRoll(event, dicePool, discount, diceKept, enhanceable, false);

RollLogic.handleRoll(event, dicePool, discount, kept, enhanceable, false);
}
}

0 comments on commit bad5114

Please sign in to comment.