Skip to content

Instantly share code, notes, and snippets.

@FarisR99
Last active August 29, 2015 14:04
Show Gist options
  • Save FarisR99/727739d5c9e522f2423a to your computer and use it in GitHub Desktop.
Save FarisR99/727739d5c9e522f2423a to your computer and use it in GitHub Desktop.
MCSG Bot Extended Commands
import java.util.*;
public class Main {
public static void main(String[] args) {
System.out.println("Starting MCSG Bot Extended Commands...");
final Main main = new Main();
System.out.println("Started MCSG Bot Extended Commands by KingFaris10");
main.readCommand();
}
private Random random = null;
private Scanner scanner = null;
private MathGame mathGame = null;
private List<SkypeUser> skypeUsers = null;
private List<String> foodNames = null;
public Main() {
this.random = new Random();
this.scanner = new Scanner(System.in);
this.mathGame = new MathGame();
this.skypeUsers = new ArrayList<SkypeUser>();
this.skypeUsers.add(new SkypeUser("givemeapples", "Drew's Girl"));
this.skypeUsers.add(new SkypeUser("augusttrollback", "August T."));
this.skypeUsers.add(new SkypeUser("dread9nawt", "blagh"));
this.skypeUsers.add(new SkypeUser("denialmc", "Daniel"));
this.skypeUsers.add(new SkypeUser("drew.foland", "Drew"));
this.skypeUsers.add(new SkypeUser("parapenguin,.", "Harry / ParaPenguin"));
this.skypeUsers.add(new SkypeUser("kristianzadlo", "Kristian number1Master"));
this.skypeUsers.add(new SkypeUser("tempestcraft", "Tempest // Yive"));
this.skypeUsers.add(new SkypeUser("opafication", "Julian"));
this.skypeUsers.add(new SkypeUser("live:Business_Faris", "Business_Faris"));
this.foodNames = Arrays.asList("Fries", "Pasta", "Pizza", "Spaghetti", "Burger", "Chicken", "Beef", "Lamb", "Curry", "Rice", "Popcorn", "Noodles");
}
public void readCommand() {
String rawCommand = this.scanner.nextLine();
if (rawCommand != null) {
if (rawCommand.startsWith("/") || this.mathGame.hasStarted()) {
if ((this.mathGame.hasStarted() && (rawCommand.toLowerCase().contains("game") || rawCommand.toLowerCase().contains("exit"))) || !this.mathGame.hasStarted())
rawCommand = rawCommand.replaceFirst("/", "");
String[] commandSplit = rawCommand.contains(" ") ? rawCommand.split(" ") : new String[]{rawCommand};
String command = commandSplit[0];
List<String> args = new ArrayList<String>();
for (int i = 1; i < commandSplit.length; i++) args.add(commandSplit[i]);
if ((this.mathGame.hasStarted() && (command.toLowerCase().contains("game") || command.toLowerCase().contains("exit"))) || !this.mathGame.hasStarted()) {
this.onCommand(command, args);
} else {
if (!rawCommand.trim().isEmpty()) {
this.onMathCommand(rawCommand);
} else {
System.out.println("Please enter a valid number.");
this.readCommand();
}
}
} else {
System.out.println("The command must start with a forward slash!");
this.readCommand();
}
}
}
private void onCommand(String command, List<String> args) {
if (command.equalsIgnoreCase("help")) {
if (args.isEmpty()) {
System.out.println("== MCSG Bot Extended Help ==");
System.out.println("Type .out <scannerID> /<command> [<args>]");
System.out.println("MCSG Bot Extended Commands:");
System.out.println("feed [<user>]");
System.out.println("getnoobs");
System.out.println("memory");
System.out.println("game <math> [<start [<maxnumber>]>|<end>|<skip>|<maxnumber>]");
} else {
System.out.println("Usage: /help");
}
} else if (command.equalsIgnoreCase("exit")) {
System.out.println("Shutting down MCSGBot Extended Commands by KingFaris10");
System.exit(0);
} else if (command.equalsIgnoreCase("feed")) {
if (args.isEmpty()) {
List<SkypeUser> clonedUsers = new ArrayList<SkypeUser>();
clonedUsers.addAll(this.skypeUsers);
Collections.shuffle(clonedUsers);
List<SkypeUser> chosenUsers = new ArrayList<SkypeUser>();
int amountOfTimes = this.random.nextInt(clonedUsers.size() - 1);
while (amountOfTimes <= 0 && !clonedUsers.isEmpty())
amountOfTimes = this.random.nextInt(clonedUsers.size() - 1);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < amountOfTimes; i++) {
SkypeUser skypeUser = clonedUsers.get(this.random.nextInt(clonedUsers.size()));
while (chosenUsers.contains(skypeUser))
skypeUser = clonedUsers.get(this.random.nextInt(clonedUsers.size()));
chosenUsers.add(skypeUser);
stringBuilder.append(", ").append(skypeUser.getDisplayName()).append(" (").append(skypeUser.getUsername()).append(")");
}
System.out.println("You fed the following people " + this.foodNames.get(this.random.nextInt(this.foodNames.size())) + ": " + stringBuilder.substring(1));
} else if (args.size() == 1) {
System.out.println("You fed " + args.get(0) + " " + this.foodNames.get(this.random.nextInt(this.foodNames.size())) + "!");
} else {
System.out.println("Usage: /feed [<user>]");
}
} else if (command.equalsIgnoreCase("getnoobs")) {
if (args.isEmpty()) {
List<SkypeUser> clonedUsers = new ArrayList<SkypeUser>();
clonedUsers.addAll(this.skypeUsers);
SkypeUser faris = this.getUser("live:business_faris");
if (faris != null) clonedUsers.remove(faris);
Collections.shuffle(clonedUsers);
List<SkypeUser> chosenUsers = new ArrayList<SkypeUser>();
int amountOfTimes = this.random.nextInt(clonedUsers.size() - 1);
while (amountOfTimes <= 0 && !clonedUsers.isEmpty())
amountOfTimes = this.random.nextInt(clonedUsers.size() - 1);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < amountOfTimes; i++) {
SkypeUser skypeUser = clonedUsers.get(this.random.nextInt(clonedUsers.size()));
while (chosenUsers.contains(skypeUser))
skypeUser = clonedUsers.get(this.random.nextInt(clonedUsers.size()));
chosenUsers.add(skypeUser);
stringBuilder.append(", ").append(skypeUser.getDisplayName()).append(" (").append(skypeUser.getUsername()).append(")");
}
System.out.println("Noobs: " + stringBuilder.substring(1));
} else {
System.out.println("Usage: /getnoobs");
}
} else if (command.equalsIgnoreCase("memory")) {
if (args.isEmpty()) {
System.out.println("Maximum memory: " + String.format("%.2f", (((float) Runtime.getRuntime().maxMemory()) / 1000000)) + "MB");
System.out.println("Total memory: " + String.format("%.2f", (((float) Runtime.getRuntime().totalMemory()) / 1000000)) + "MB");
System.out.println("Free memory: " + String.format("%.2f", (((float) Runtime.getRuntime().freeMemory()) / 1000000)) + "MB");
} else {
System.out.println("Usage: /memory");
}
} else if (command.equalsIgnoreCase("game")) {
if (args.size() > 0) {
String strGame = args.get(0);
if (strGame.equalsIgnoreCase("math")) {
if (args.size() > 1) {
String strMathCommand = args.get(1);
if (strMathCommand.equalsIgnoreCase("start")) {
if (args.size() == 2) {
if (this.mathGame.startGame()) {
System.out.println("Let the Maths game begin!");
this.mathGame.print();
}
} else {
if (args.size() == 3) {
if (isInteger(args.get(2))) {
if (this.mathGame.startGame(Integer.parseInt(args.get(2)))) {
System.out.println("Let the Maths game begin!");
this.mathGame.print();
}
} else {
System.out.println("Usage: /game math start <maxnumber>");
}
} else {
System.out.println("Usage: /game math start <maxnumber>");
}
}
} else if (strMathCommand.equalsIgnoreCase("end")) {
if (args.size() == 2) {
if (this.mathGame.hasStarted()) {
this.mathGame.resetGame();
System.out.println("You've failed the Maths game!");
} else {
System.out.println("The Math game has not even started!");
}
} else {
System.out.println("Usage: /game math end");
}
} else if (strMathCommand.equalsIgnoreCase("skip")) {
if (args.size() == 2) {
if (this.mathGame.hasStarted()) {
int realAnswer = 0;
if (this.mathGame.getOperation() == 0)
realAnswer = this.mathGame.getNumbers()[0] + this.mathGame.getNumbers()[1];
else if (this.mathGame.getOperation() == 1)
realAnswer = this.mathGame.getNumbers()[0] - this.mathGame.getNumbers()[1];
else if (this.mathGame.getOperation() == 2)
realAnswer = this.mathGame.getNumbers()[0] * this.mathGame.getNumbers()[1];
else if (this.mathGame.getOperation() == 3)
realAnswer = (int) (this.mathGame.getNumbers()[0] / this.mathGame.getNumbers()[1]);
boolean lostPoint = this.mathGame.userScore > 0;
System.out.println(" ");
if (lostPoint) this.mathGame.decrementScore();
System.out.println("The correct answer was " + (this.mathGame.getOperation() == 3 ? "~" + realAnswer : realAnswer) + "!" + (lostPoint ? " You lost 1 point for skipping." : ""));
if (lostPoint) System.out.println("Your score: " + this.mathGame.getScore());
try {
this.mathGame.generateNext();
System.out.println(" ");
this.mathGame.print();
} catch (Exception ex) {
ex.printStackTrace();
this.mathGame.resetGame();
}
} else {
System.out.println("The Math game has not even started!");
}
} else {
System.out.println("Usage: /game math skip");
}
} else if (isInteger(strMathCommand)) {
if (!this.mathGame.hasStarted()) {
if (this.mathGame.startGame(Integer.parseInt(strMathCommand))) {
System.out.println("Let the Maths game begin!");
this.mathGame.print();
}
} else {
System.out.println("The Math game has already started!");
}
} else {
System.out.println("Unknown Math command: " + strMathCommand.toLowerCase());
}
} else {
if (!this.mathGame.hasStarted()) {
if (this.mathGame.startGame()) {
System.out.println("Let the Maths game begin!");
this.mathGame.print();
}
} else {
this.mathGame.resetGame();
System.out.println("You've failed the Maths game!");
}
}
} else {
System.out.println("Unknown game: " + strGame.toLowerCase());
}
} else {
System.out.println("Usage: /game <math> [<start [<maxnumber>]>|<end>|<skip>|<maxnumber>]");
}
} else {
System.out.println("Unknown command: /" + command.toLowerCase());
}
this.readCommand();
}
private void onMathCommand(String text) {
text = text.trim();
if (text.startsWith("/")) {
System.out.println("You cannot type a non-Math command whilst the Math game is running!");
} else {
int answer = 0;
if (isInteger(text)) answer = Integer.parseInt(text);
else if (isDouble(text)) answer = (int) Double.parseDouble(text);
if (isInteger(text) || isDouble(text)) {
int realAnswer = 0;
if (this.mathGame.getOperation() == 0)
realAnswer = this.mathGame.getNumbers()[0] + this.mathGame.getNumbers()[1];
else if (this.mathGame.getOperation() == 1)
realAnswer = this.mathGame.getNumbers()[0] - this.mathGame.getNumbers()[1];
else if (this.mathGame.getOperation() == 2)
realAnswer = this.mathGame.getNumbers()[0] * this.mathGame.getNumbers()[1];
else if (this.mathGame.getOperation() == 3)
realAnswer = (int) (this.mathGame.getNumbers()[0] / this.mathGame.getNumbers()[1]);
else {
System.out.println("Error! The Math game doesn't have an answer to that!");
try {
this.mathGame.generateNext();
this.mathGame.print();
} catch (Exception ex) {
System.out.println("An error occurred!");
ex.printStackTrace();
this.mathGame.resetGame();
}
this.readCommand();
return;
}
if (answer == realAnswer) {
this.mathGame.incrementScore();
if (this.mathGame.getScore() >= this.mathGame.getRequiredScore()) {
System.out.println("Congratulations! You won the Maths game!");
this.mathGame.resetGame();
} else {
try {
System.out.println("Your score: " + this.mathGame.getScore());
System.out.println("Correct! " + this.mathGame.getNumbers()[0] + " " + this.mathGame.getOperationAsString() + " " + this.mathGame.getNumbers()[1] + " is " + (this.mathGame.getOperation() == 3 ? "~" + answer : answer) + "!");
System.out.println("");
this.mathGame.generateNext();
this.mathGame.print();
} catch (Exception ex) {
System.out.println("An error occurred!");
ex.printStackTrace();
this.mathGame.resetGame();
this.readCommand();
return;
}
}
} else {
System.out.println("Incorrect! " + this.mathGame.getNumbers()[0] + " " + this.mathGame.getOperationAsString() + " " + this.mathGame.getNumbers()[1] + " is not " + (this.mathGame.getOperation() == 3 ? "~" + answer : answer) + "!");
}
} else {
System.out.println("Incorrect! " + this.mathGame.getNumbers()[0] + " " + this.mathGame.getOperationAsString() + " " + this.mathGame.getNumbers()[1] + " is not " + text + "!");
}
}
this.readCommand();
}
public SkypeUser getUser(String username) {
for (SkypeUser skypeUser : this.skypeUsers) {
if (skypeUser.getUsername().equalsIgnoreCase(username)) return skypeUser;
}
return null;
}
public class SkypeUser {
private String username = "", displayName = "";
public SkypeUser(String username, String displayName) {
this.username = username;
this.displayName = displayName;
}
public String getDisplayName() {
return this.displayName;
}
public String getUsername() {
return this.username;
}
}
public class MathGame {
private boolean hasStarted = false;
private int firstNumber = 0, secondNumber = 0;
private int operationType = 0;
private int maxNumber = 0;
private int currentQuestion = 0;
private int incrementScore = 1;
private int userScore = 0;
private int requiredScore = 0;
public MathGame() {
this.hasStarted = false;
this.firstNumber = this.secondNumber = 0;
this.operationType = 0;
this.maxNumber = Integer.MAX_VALUE;
this.userScore = this.requiredScore = 0;
this.currentQuestion = 0;
}
public void decrementScore() {
if (this.userScore - this.incrementScore < 0) this.userScore = 0;
else this.userScore -= this.incrementScore;
}
public void generateNext() throws Exception {
try {
this.firstNumber = random.nextInt(this.maxNumber);
this.secondNumber = random.nextInt(this.maxNumber);
while (this.firstNumber < -this.maxNumber) this.firstNumber = random.nextInt(this.maxNumber);
while (this.secondNumber < -this.maxNumber) this.secondNumber = random.nextInt(this.maxNumber);
if (Math.random() > 0.95 && this.maxNumber < 1000000) this.firstNumber *= -1;
if (Math.random() > 0.95 && this.maxNumber < 1000000) this.secondNumber *= -1;
this.operationType = random.nextInt(8);
if (this.operationType > 3) this.operationType /= 2;
while (this.operationType == 3 && this.secondNumber == 0)
this.operationType = random.nextInt(4);
this.currentQuestion++;
} catch (Exception ex) {
throw ex;
}
}
public int getIncrementer() {
return this.incrementScore;
}
public int[] getNumbers() {
return new int[]{this.firstNumber, this.secondNumber};
}
public int getOperation() {
return this.operationType;
}
public String getOperationAsString() {
return this.operationType == 0 ? "+" : (this.operationType == 1 ? "-" : (this.operationType == 2 ? "*" : "/"));
}
public int getRequiredScore() {
return this.requiredScore;
}
public int getScore() {
return this.userScore;
}
public boolean hasStarted() {
return this.hasStarted;
}
public void incrementScore() {
this.userScore += this.incrementScore;
}
public void print() {
System.out.println("Question " + (this.currentQuestion) + ": What is " + this.firstNumber + " " + this.getOperationAsString() + " " + this.secondNumber + "?");
}
public void resetGame() {
this.hasStarted = false;
this.firstNumber = this.secondNumber = 0;
this.maxNumber = 0;
this.operationType = 0;
this.userScore = 0;
this.requiredScore = 0;
this.currentQuestion = 0;
}
public MathGame setNumber(int index, int value) {
if (index == 1) this.firstNumber = value;
else if (index == 2) this.secondNumber = value;
return this;
}
public boolean startGame() {
return this.startGame(Integer.MAX_VALUE);
}
public boolean startGame(int maxNumber) {
if (!this.hasStarted) {
try {
this.hasStarted = true;
this.maxNumber = maxNumber;
if (this.maxNumber < Integer.MAX_VALUE) this.maxNumber++;
this.requiredScore = random.nextInt(16);
while (this.requiredScore < 8) this.requiredScore = random.nextInt(16);
this.generateNext();
System.out.println("Required score: " + this.requiredScore);
return true;
} catch (Exception ex) {
this.resetGame();
System.out.println("Couldn't start the Maths game!");
ex.printStackTrace();
return false;
}
} else {
return false;
}
}
}
private static boolean isDouble(String aString) {
try {
Double.parseDouble(aString);
return true;
} catch (Exception ex) {
return false;
}
}
private static boolean isInteger(String aString) {
try {
Integer.parseInt(aString);
return true;
} catch (Exception ex) {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment