Skip to content

Commit

Permalink
Merge pull request #54 from Mazawrath/bean_trivia_patch
Browse files Browse the repository at this point in the history
Bean trivia patch
  • Loading branch information
Mazawrath authored Jan 14, 2020
2 parents f6087f1 + bd55e36 commit 88fad0d
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class BeanTriviaCommand implements CommandExecutor {
Expand Down Expand Up @@ -85,7 +86,7 @@ public void onCommand(ServerTextChannel serverTextChannel, DiscordApi api, User
}
EmbedBuilder embed = new EmbedBuilder()
.setTitle(question)
.setDescription("Category: " + category)
.setDescription(category)
.setFooter("Difficulty: " + difficulty);
for (int i = 0; i < answers.size(); i++)
embed.addField("Answer " + (i + 1), answers.get(i));
Expand All @@ -96,86 +97,87 @@ public void onCommand(ServerTextChannel serverTextChannel, DiscordApi api, User
triviaMessage.addReaction(EmojiParser.parseToUnicode(":three:"));
triviaMessage.addReaction(EmojiParser.parseToUnicode(":four:"));

Thread.sleep(8000);
List<Reaction> reactions = triviaMessage.getReactions();

ArrayList<User> contestants = new ArrayList<>();
ArrayList<User> winners = new ArrayList<>();
ArrayList<User> cheaters = new ArrayList<>();

// Get the winners
for (int i = 0; i < reactions.size(); i++) {
if (reactions.get(i).getEmoji().equalsEmoji(EmojiParser.parseToUnicode(emojiCorrectAnswer))) {
Reaction correctEmoji = reactions.get(i);
for (int j = 0; j < correctEmoji.getUsers().get().size(); j++) {
if (correctEmoji.getUsers().get().get(j) != api.getYourself())
winners.add(correctEmoji.getUsers().get().get(j));
}
} else if (reactions.get(i).getUsers().get().contains(api.getYourself())) {
Reaction incorrectEmoji = reactions.get(i);
for (int j = 0; j < incorrectEmoji.getUsers().get().size(); j++) {
if (incorrectEmoji.getUsers().get().get(j) != api.getYourself())
contestants.add(incorrectEmoji.getUsers().get().get(j));
new Thread(() -> {
try {
Thread.sleep(10500);
List<Reaction> reactions = triviaMessage.getReactions();

ArrayList<User> contestants = new ArrayList<>();
ArrayList<User> winners = new ArrayList<>();
ArrayList<User> cheaters = new ArrayList<>();

// Get the winners
for (int i = 0; i < reactions.size(); i++) {
if (reactions.get(i).getEmoji().equalsEmoji(EmojiParser.parseToUnicode(emojiCorrectAnswer))) {
getUsersReacted(api, reactions, winners, i);
} else if (reactions.get(i).getUsers().get().contains(api.getYourself())) {
getUsersReacted(api, reactions, contestants, i);
}
}
}
}

// Check for cheaters
for (int i = 0; i < reactions.size(); i++) {
if (!reactions.get(i).getEmoji().equalsEmoji(EmojiParser.parseToUnicode(emojiCorrectAnswer)) && reactions.get(i).getUsers().get().contains(api.getYourself())) {
Reaction correctEmoji = reactions.get(i);
for (int j = 0; j < correctEmoji.getUsers().get().size(); j++) {
for (int k = 0; k < winners.size(); k++) {
// Found a cheater!
if (correctEmoji.getUsers().get().get(j) == winners.get(k)) {
winners.remove(winners.get(k));
cheaters.add(correctEmoji.getUsers().get().get(j));
// Check for cheaters
for (int i = 0; i < reactions.size(); i++) {
if (!reactions.get(i).getEmoji().equalsEmoji(EmojiParser.parseToUnicode(emojiCorrectAnswer)) && reactions.get(i).getUsers().get().contains(api.getYourself())) {
Reaction correctEmoji = reactions.get(i);
for (int j = 0; j < correctEmoji.getUsers().get().size(); j++) {
for (int k = 0; k < winners.size(); k++) {
// Found a cheater!
if (correctEmoji.getUsers().get().get(j) == winners.get(k)) {
winners.remove(winners.get(k));
cheaters.add(correctEmoji.getUsers().get().get(j));
}
}
}
}
}
}
}
// Anyone who reacted more then once but got it wrong also cheated
ArrayList<User> duplicates = (ArrayList<User>) getDuplicate(contestants);
for (User duplicate : duplicates) {
if (!cheaters.contains(duplicate))
cheaters.add(duplicate);
}
// Anyone who reacted more then once but got it wrong also cheated
ArrayList<User> duplicates = (ArrayList<User>) getDuplicate(contestants);
for (User duplicate : duplicates) {
if (!cheaters.contains(duplicate))
cheaters.add(duplicate);
}

StringBuilder winnersMessage = new StringBuilder();
StringBuilder winnersMessage = new StringBuilder();

if (winners.size() == 0) {
winnersMessage.append("No one got the answer correct!\n");
} else {
winnersMessage.append("The following users have won:\n");
for (int i = 0; i < winners.size(); i++) {
winnersMessage.append(winners.get(i).getDisplayName(server)).append(" got the correct answer!\n");
points.depositCoins(new PointsUser(author, server), Points.TRIVIA_CORRECT_ANSWER);
if (winners.size() == 0) {
winnersMessage.append("No one got the answer correct!\n");
} else {
winnersMessage.append("The following users have won:\n");
for (int i = 0; i < winners.size(); i++) {
winnersMessage.append(winners.get(i).getDisplayName(server)).append(" got the correct answer!\n");
points.depositCoins(new PointsUser(author, server), Points.TRIVIA_CORRECT_ANSWER);
}
}
winnersMessage.append("\n");
for (int i = 0; i < cheaters.size(); i++) {
winnersMessage.append(cheaters.get(i).getDisplayName(server)).append(" has cheated and has been fined ").append(Points.pointsToString(Points.TRIVIA_CHEAT_FINE)).append("!\n");
if (points.checkBalance(new PointsUser(cheaters.get(i), server)).compareTo(Points.TRIVIA_CHEAT_FINE) <= 0)
points.makePurchase(new PointsUser(cheaters.get(i), server), new PointsUser(api.getYourself(), server), points.checkBalance(new PointsUser(cheaters.get(i), server)));
else
points.makePurchase(new PointsUser(cheaters.get(i), server), new PointsUser(api.getYourself(), server), Points.TRIVIA_CHEAT_FINE);
// Reset their trivia too.
points.useTriviaQuestion(new PointsUser(author, server), true);
}
if (cheaters.size() != 0)
winnersMessage.append("\n");
winnersMessage.append("The correct answer was: ").append(correctAnswer).append(".\nAnyone who answered correctly received ").append(Points.pointsToString(Points.TRIVIA_CORRECT_ANSWER)).append(".");

serverTextChannel.sendMessage(winnersMessage.toString());
} catch (Exception e) {
e.printStackTrace();
Sentry.capture(e);
serverTextChannel.sendMessage("Attempted to send trivia message but failed.");
}
}
winnersMessage.append("\n");
for (int i = 0; i < cheaters.size(); i++) {
winnersMessage.append(cheaters.get(i).getDisplayName(server)).append(" has cheated and has been fined ").append(Points.pointsToString(Points.TRIVIA_CHEAT_FINE)).append("!\n");
if (points.checkBalance(new PointsUser(cheaters.get(i), server)).compareTo(Points.TRIVIA_CHEAT_FINE) <= 0)
points.makePurchase(new PointsUser(cheaters.get(i), server), new PointsUser(api.getYourself(), server), points.checkBalance(new PointsUser(cheaters.get(i), server)));
else
points.makePurchase(new PointsUser(cheaters.get(i), server), new PointsUser(api.getYourself(), server), Points.TRIVIA_CHEAT_FINE);
// Reset their trivia too.
points.useTriviaQuestion(new PointsUser(author, server), true);
}
if (cheaters.size() != 0)
winnersMessage.append("\n");
winnersMessage.append("The correct answer was: ").append(correctAnswer).append(".\nAnyone who answered correctly received ").append(Points.pointsToString(Points.TRIVIA_CORRECT_ANSWER)).append(".");

serverTextChannel.sendMessage(winnersMessage.toString());
}).start();
} catch (Exception e) {
serverTextChannel.sendMessage("Attempted to send trivia message but failed.");
Sentry.capture(e);
return;
}
} else {
StringBuilder message = new StringBuilder();

message.append("You have already did trivia today. You can use your trivia again in ");
message.append("You have already done your " + Points.MAX_TRIVIA_QUESTIONS_PER_DAY + " trivia questions today. You can use your trivia again in ");

String dateStart = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss")
.format(new java.util.Date(System.currentTimeMillis()));
Expand Down Expand Up @@ -216,6 +218,14 @@ public void onCommand(ServerTextChannel serverTextChannel, DiscordApi api, User
Sentry.clearContext();
}

private void getUsersReacted(DiscordApi api, List<Reaction> reactions, ArrayList<User> winners, int i) throws InterruptedException, java.util.concurrent.ExecutionException {
Reaction emote = reactions.get(i);
for (int j = 0; j < emote.getUsers().get().size(); j++) {
if (emote.getUsers().get().get(j) != api.getYourself())
winners.add(emote.getUsers().get().get(j));
}
}

public static <T> List getDuplicate(Collection<T> list) {

final List<T> duplicatedObjects = new ArrayList<T>();
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/com/mazawrath/beanbot/utilities/Points.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ public class Points {
public static final int ROUNDING_MODE = BigDecimal.ROUND_HALF_UP;
public static final int FREE_COIN_TIME_LIMIT = 168 * 60 * 60 * 1000;
public static final int TRIVIA_QUESTION_TIME_LIMIT = 24 * 60 * 60 * 1000;
public static final int MAX_TRIVIA_QUESTIONS_PER_DAY = 5;
public static final BigDecimal ZERO_POINTS = (BigDecimal.ZERO).setScale(SCALE, ROUNDING_MODE);
public static final BigDecimal STARTING_POINTS = (new BigDecimal("1000")).setScale(SCALE, ROUNDING_MODE);
public static final BigDecimal TRIVIA_CORRECT_ANSWER = (new BigDecimal("10")).setScale(SCALE, ROUNDING_MODE);
public static final BigDecimal TRIVIA_CHEAT_FINE = (new BigDecimal("200")).setScale(SCALE, ROUNDING_MODE);
public static final BigDecimal FREE_POINTS = new BigDecimal("50").setScale(SCALE, ROUNDING_MODE);
public static final BigDecimal FREE_POINTS = new BigDecimal("100.00").setScale(SCALE, ROUNDING_MODE);
public static final BigDecimal COMMAND_COST = new BigDecimal("10.00").setScale(SCALE, ROUNDING_MODE);
public static final BigDecimal COMMAND_COST_SPECIAL = new BigDecimal("15.00").setScale(SCALE, ROUNDING_MODE);
public static final BigDecimal LOTTERY_TICKET_COST = new BigDecimal("45.00").setScale(SCALE, ROUNDING_MODE);
Expand Down Expand Up @@ -62,6 +63,7 @@ private void checkUser(String userID, String serverID) {
.with("Points", buildValueForDB(Points.STARTING_POINTS))
.with("Last Received Free Points", 0)
.with("Last Used Trivia Question", 0)
.with("Trivia questions answered", 0)
)).run(conn);
}

Expand Down Expand Up @@ -168,9 +170,20 @@ public long giveFreePoints(String userID, String serverID) {
public long useTriviaQuestion(PointsUser user, boolean cheaterPunishment) {
checkUser(user.getUserId(), user.getServerId());
long timeLeft = r.db(DB_NAME).table(user.getServerId()).get(user.getUserId()).getField("Last Used Trivia Question").run(conn);
long triviaAnswered = r.db(DB_NAME).table(user.getServerId()).get(user.getUserId()).getField("Trivia questions answered").run(conn);

if (System.currentTimeMillis() - timeLeft > TRIVIA_QUESTION_TIME_LIMIT || cheaterPunishment) {
if (cheaterPunishment) {
r.db(DB_NAME).table(user.getServerId()).filter(r.hashMap("id", user.getUserId())).update(r.hashMap("Last Used Trivia Question", System.currentTimeMillis())).run(conn);
r.db(DB_NAME).table(user.getServerId()).filter(r.hashMap("id", user.getUserId())).update(r.hashMap("Trivia questions answered", Points.MAX_TRIVIA_QUESTIONS_PER_DAY)).run(conn);
return System.currentTimeMillis();
}

if (System.currentTimeMillis() - timeLeft > TRIVIA_QUESTION_TIME_LIMIT) {
r.db(DB_NAME).table(user.getServerId()).filter(r.hashMap("id", user.getUserId())).update(r.hashMap("Last Used Trivia Question", System.currentTimeMillis())).run(conn);
r.db(DB_NAME).table(user.getServerId()).filter(r.hashMap("id", user.getUserId())).update(r.hashMap("Trivia questions answered", 1)).run(conn);
return 0;
} else if (triviaAnswered < MAX_TRIVIA_QUESTIONS_PER_DAY) {
r.db(DB_NAME).table(user.getServerId()).filter(r.hashMap("id", user.getUserId())).update(r.hashMap("Trivia questions answered", triviaAnswered + 1)).run(conn);
return 0;
}
return timeLeft;
Expand Down

0 comments on commit 88fad0d

Please sign in to comment.