From 1a8409994cc0927cd7d3af159bedc204ce2de4ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Mon, 19 Dec 2022 01:36:43 +0100 Subject: [PATCH] Implement basic functional diceroll command --- .../wtf/beatrice/hidekobot/HidekoBot.java | 1 + .../commands/message/DiceRollCommand.java | 128 ++++++++++++++++++ .../wtf/beatrice/hidekobot/objects/Dice.java | 45 ++++++ 3 files changed, 174 insertions(+) create mode 100644 src/main/java/wtf/beatrice/hidekobot/commands/message/DiceRollCommand.java create mode 100644 src/main/java/wtf/beatrice/hidekobot/objects/Dice.java diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index 97594f7..aa41431 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -133,6 +133,7 @@ public class HidekoBot messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.BotInfoCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.CoinFlipCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.ClearCommand()); + messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.DiceRollCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.InviteCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.SayCommand()); Cache.setMessageCommandListener(messageCommandListener); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/DiceRollCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/DiceRollCommand.java new file mode 100644 index 0000000..8bb86b0 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/DiceRollCommand.java @@ -0,0 +1,128 @@ +package wtf.beatrice.hidekobot.commands.message; + +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import org.jetbrains.annotations.Nullable; +import wtf.beatrice.hidekobot.objects.Dice; +import wtf.beatrice.hidekobot.objects.commands.MessageCommand; + +import java.util.*; + +public class DiceRollCommand implements MessageCommand +{ + @Override + public LinkedList getCommandLabels() { + return new LinkedList<>(Arrays.asList("diceroll", "droll", "roll")); + } + + @Nullable + @Override + public List getPermissions() { + return null; // anyone can use it + } + + @Override + public boolean passRawArgs() { + return false; + } + + @Override + public void runCommand(MessageReceivedEvent event, String label, String[] args) + { + LinkedHashMap dicesToRoll = new LinkedHashMap(); + String diceRegex = "d[0-9]+"; + String amountRegex = "[0-9]+"; + + Dice currentDice = null; + int currentAmount; + UUID lastPushedDice = null; + + for(String arg : args) + { + if(arg.matches(amountRegex)) + { + currentAmount = Integer.parseInt(arg); + + if(currentDice == null) + { + currentDice = new Dice(6); + } else { + currentDice = new Dice(currentDice); + } + + lastPushedDice = currentDice.getUUID(); + dicesToRoll.put(currentDice, currentAmount); + } + else if(arg.matches(diceRegex)) + { + int sides = Integer.parseInt(arg.substring(1)); + + if(args.length == 1) + { + dicesToRoll.put(new Dice(sides), 1); + } else + { + if(currentDice != null) + { + if(lastPushedDice == null || !lastPushedDice.equals(currentDice.getUUID())) + { + dicesToRoll.put(currentDice, 1); + lastPushedDice = currentDice.getUUID(); + } + } + + currentDice = new Dice(sides); + } + } + } + + if(lastPushedDice == null) + { + if(currentDice != null) + { + dicesToRoll.put(currentDice, 1); + } + } else + { + if(!lastPushedDice.equals(currentDice.getUUID())) + { + dicesToRoll.put(new Dice(currentDice), 1); + } + } + + LinkedList rolledDices = new LinkedList<>(); + + for(Dice dice : dicesToRoll.keySet()) + { + for(int roll = 0; roll < dicesToRoll.get(dice); roll++) + { + dice.roll(); + rolledDices.add(new Dice(dice)); + } + } + + StringBuilder message = new StringBuilder(); + int total = 0; + + int previousDiceSides = 0; + for (Dice dice : rolledDices) { + int diceSize = dice.getSides(); + + if (previousDiceSides != diceSize) { + message.append("\nd").append(diceSize).append(": "); + previousDiceSides = diceSize; + } else if (previousDiceSides != 0) { + message.append(", "); + } + + message.append(dice.getValue()); + + total += dice.getValue(); + } + + message.append("\n\n").append("**Total**: ").append(total); + + event.getMessage().reply(message.toString()).queue(); + } + +} diff --git a/src/main/java/wtf/beatrice/hidekobot/objects/Dice.java b/src/main/java/wtf/beatrice/hidekobot/objects/Dice.java new file mode 100644 index 0000000..d3012b6 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/objects/Dice.java @@ -0,0 +1,45 @@ +package wtf.beatrice.hidekobot.objects; + +import wtf.beatrice.hidekobot.util.RandomUtil; + +import java.util.UUID; + +public class Dice +{ + private final int sides; + private int value = 0; + private final UUID uuid; + + public Dice(int sides) + { + this.sides = sides; + this.uuid = UUID.randomUUID(); + } + + public Dice(Dice old) + { + this.sides = old.sides; + this.value = old.value; + this.uuid = UUID.randomUUID(); + } + + public int getValue() + { + return value; + } + + public int getSides() + { + return sides; + } + + public void roll() + { + value = RandomUtil.getRandomNumber(1, sides); + } + + public UUID getUUID() + { + return uuid; + } +}