diff --git a/src/main/java/org/jointheleague/discord_bot_example/Bot.java b/src/main/java/org/jointheleague/discord_bot_example/Bot.java index 0dac01fb..5ee3a29e 100644 --- a/src/main/java/org/jointheleague/discord_bot_example/Bot.java +++ b/src/main/java/org/jointheleague/discord_bot_example/Bot.java @@ -135,6 +135,10 @@ public void connect(boolean printInvite) { Poker poker = new Poker(channelName); api.addMessageCreateListener(poker); helpListener.addHelpEmbed(poker.getHelpEmbed()); + + EquationSolver eq = new EquationSolver(channelName); + api.addMessageCreateListener(eq); + helpListener.addHelpEmbed(eq.getHelpEmbed()); CovidCaseGetter covid = new CovidCaseGetter(channelName); api.addMessageCreateListener(covid); @@ -160,10 +164,7 @@ public void connect(boolean printInvite) { api.addMessageCreateListener(depress); helpListener.addHelpEmbed(depress.getHelpEmbed()); - Depression depress = new Depression(channelName); - api.addMessageCreateListener(depress); - helpListener.addHelpEmbed(depress.getHelpEmbed()); - + // old way to add listeners api.addMessageCreateListener(helpListener); api.addMessageCreateListener(new MomBot(channelName)); diff --git a/src/main/java/org/jointheleague/modules/EquationSolver.java b/src/main/java/org/jointheleague/modules/EquationSolver.java new file mode 100644 index 00000000..d7f7d42b --- /dev/null +++ b/src/main/java/org/jointheleague/modules/EquationSolver.java @@ -0,0 +1,206 @@ +package org.jointheleague.modules; + +import java.util.ArrayList; +import java.util.Random; + +import org.javacord.api.event.message.MessageCreateEvent; + +import net.aksingh.owmjapis.api.APIException; + +public class EquationSolver extends CustomMessageCreateListener { + private static final String command = "!equation"; + + public EquationSolver(String channelName) { + super(channelName); + + } + + @Override + public void handle(MessageCreateEvent event) throws APIException { + // TODO Auto-generated method stub + + if (event.getMessageContent().contains("!equation")) { + String eqws = event.getMessageContent().substring(9); + String eq = ""; + for (int i = 0; i < eqws.length(); i++) { + if (eqws.charAt(i) == ' ') { + continue; + } else { + eq += eqws.charAt(i); + + } + } + //eq is good here + String num = ""; + ArrayList hold; + + hold = new ArrayList(); + for (int i = 0; i < eq.length(); i++) { + + hold.add(""+ eq.charAt(i)); +// if (Character.isDigit(eq.charAt(i))) { +// num=""; +// num += "" + eq.charAt(i); +// //System.out.println(i); +// +// //System.out.println(num); +// +// } else { +// if(num.equals("")) { +// +// } +// // int add = Integer.parseInt(num); +// else { +// System.out.println("eq.charat1: " + eq.charAt(i)); +// hold.add(num); +// System.out.println(num); +// } +// +// +// } +// +// if (eq.charAt(i) == '+') { +// hold.add("+"); +// } +// if (eq.charAt(i) == '-') { +// hold.add("-"); +// } +// if (eq.charAt(i) == '*') { +// hold.add("*"); +// } +// if (eq.charAt(i) == '/') { +// hold.add("/"); +// } +// if (eq.charAt(i) == '%') { +// hold.add("%"); +// +// } +// if (eq.charAt(i) == '(') { +// hold.add("("); +// } +// if (eq.charAt(i) == ')') { +// hold.add(")"); +// } + + } + +// ArrayList input = new ArrayList(); +// +// for (int i = start; i < end; i++) { +// +// input.add("" + eq.charAt(i)); +// +// +// } +// +// +// + +// solveEquation(input); +// System.out.println("input " +solveEquation(input)); +// hold.add(0, solveEquation(input)); + + event.getChannel().sendMessage(solveEquation(hold)); + } + + } + + public String solveEquation(ArrayList hold) { + + boolean done = false; + String[] symbols = {"*", "/", "%", "-", "+" }; + int currentIndex = 0; + String currentSym = symbols[currentIndex]; + while (!done) { + boolean found = false; + + for (int i = 0; i < hold.size(); i++) { +//System.out.println("hold.geti is " + hold.get(i)); + if (hold.get(i).equals(currentSym)) { + double fVal = Double.parseDouble(hold.get(i - 1)); + double sVal = Double.parseDouble(hold.get(i + 1)); + double solution = 0; + + if (hold.get(i).equals("*")) { + solution = fVal * sVal; + + } + if (hold.get(i).equals("/")) { + solution = fVal / sVal; + + } + if (hold.get(i).equals("+")) { + solution = fVal + sVal; + + } + if (hold.get(i).equals("-")) { + solution = fVal - sVal; + + } + if (hold.get(i).equals("%")) { + solution = fVal % sVal; + + } + + + hold.remove(i - 1); + hold.remove(i - 1); + String ans = "" + solution; + hold.set(i - 1, ans); + found = true; + break; + } + if(hold.get(i).equals("(")) { + System.out.println("this is what is being put in for place " + i); + solveParenth(hold, i); + } + + } + if (!found) { + currentIndex++; + + if (currentIndex >= symbols.length) { + done = true; + } else { + + currentSym = symbols[currentIndex]; + } + } + } + String ans = hold.get(0); + return (ans); + + } + public void solveParenth(ArrayList hold, int place) { + ArrayList in = new ArrayList(); + int end = -1; + for(int i = place+1;i