use of net.kodehawa.mantarobot.core.listeners.operations.core.InteractiveOperation in project MantaroBot by Mantaro.
the class GuessTheNumber method call.
@Override
public void call(GameLobby lobby, List<String> players) {
// This class is not using Game<T>#callDefault due to it being custom/way too different from the default ones (aka give hints/etc)
InteractiveOperations.createOverriding(lobby.getChannel(), 30, new InteractiveOperation() {
@Override
public int run(GuildMessageReceivedEvent e) {
if (!e.getChannel().getId().equals(lobby.getChannel().getId())) {
return Operation.IGNORED;
}
for (String s : MantaroData.config().get().getPrefix()) {
if (e.getMessage().getContentRaw().startsWith(s)) {
return Operation.IGNORED;
}
}
if (MantaroData.db().getGuild(lobby.getChannel().getGuild()).getData().getGuildCustomPrefix() != null && e.getMessage().getContentRaw().startsWith(MantaroData.db().getGuild(lobby.getChannel().getGuild()).getData().getGuildCustomPrefix())) {
return Operation.IGNORED;
}
if (players.contains(e.getAuthor().getId())) {
if (e.getMessage().getContentRaw().equalsIgnoreCase("end")) {
lobby.getChannel().sendMessage(EmoteReference.CORRECT + "Ended game. The number was: " + number).queue();
lobby.startNextGame();
GameLobby.LOBBYS.remove(lobby.getChannel());
return Operation.COMPLETED;
}
if (e.getMessage().getContentRaw().equalsIgnoreCase("endlobby")) {
lobby.getChannel().sendMessage(EmoteReference.CORRECT + "Ended lobby correctly! Thanks for playing!").queue();
lobby.getGamesToPlay().clear();
lobby.startNextGame();
return Operation.COMPLETED;
}
int parsedAnswer;
try {
parsedAnswer = Integer.parseInt(e.getMessage().getContentRaw());
} catch (NumberFormatException ex) {
lobby.getChannel().sendMessage(EmoteReference.ERROR + "That's not even a number...").queue();
attempts = attempts + 1;
return Operation.IGNORED;
}
if (e.getMessage().getContentRaw().equals(String.valueOf(number))) {
Player player = MantaroData.db().getPlayer(e.getMember());
int gains = 95;
player.addMoney(gains);
player.getData().setGamesWon(player.getData().getGamesWon() + 1);
if (player.getData().getGamesWon() == 100)
player.getData().addBadgeIfAbsent(Badge.GAMER);
player.save();
TextChannelGround.of(e).dropItemWithChance(Items.FLOPPY_DISK, 3);
lobby.getChannel().sendMessage(EmoteReference.MEGA + "**" + e.getMember().getEffectiveName() + "**" + " Just won $" + gains + " credits by answering correctly!").queue();
lobby.startNextGame();
return Operation.COMPLETED;
}
if (attempts >= maxAttempts) {
lobby.getChannel().sendMessage(EmoteReference.ERROR + "Already used all attempts, ending game. The number was: " + number).queue();
// This should take care of removing the lobby, actually.
lobby.startNextGame();
return Operation.COMPLETED;
}
lobby.getChannel().sendMessage(EmoteReference.ERROR + "That's not it, you have " + (maxAttempts - attempts) + " attempts remaning.\n" + "Hint: The number is " + (parsedAnswer < number ? "higher" : "lower") + " than your input number.").queue();
attempts = attempts + 1;
return Operation.IGNORED;
}
return Operation.IGNORED;
}
@Override
public void onExpire() {
if (lobby.getChannel() == null)
return;
lobby.getChannel().sendMessage(EmoteReference.ERROR + "The time ran out! The number was: " + number).queue();
GameLobby.LOBBYS.remove(lobby.getChannel());
}
@Override
public void onCancel() {
GameLobby.LOBBYS.remove(lobby.getChannel());
}
});
}
use of net.kodehawa.mantarobot.core.listeners.operations.core.InteractiveOperation in project MantaroBot by Mantaro.
the class MoneyCmds method gamble.
@Subscribe
public void gamble(CommandRegistry cr) {
cr.register("gamble", new SimpleCommand(Category.CURRENCY) {
final RateLimiter rateLimiter = new RateLimiter(TimeUnit.SECONDS, 20, true);
SecureRandom r = new SecureRandom();
@Override
public void call(GuildMessageReceivedEvent event, String content, String[] args) {
Player player = MantaroData.db().getPlayer(event.getMember());
if (!handleDefaultRatelimit(rateLimiter, event.getAuthor(), event))
return;
if (player.getMoney() <= 0) {
event.getChannel().sendMessage(EmoteReference.ERROR2 + "You're broke. Search for some credits first!").queue();
return;
}
if (player.getMoney() > GAMBLE_ABSOLUTE_MAX_MONEY) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You have too much money! Maybe transfer or buy items? Now you can also use `~>slots`" + " for all your gambling needs! Thanks for not breaking the local bank. *(Gamble limit: " + GAMBLE_ABSOLUTE_MAX_MONEY + ")*").queue();
return;
}
double multiplier;
long i;
int luck;
try {
switch(content) {
case "all":
case "everything":
i = player.getMoney();
multiplier = 1.3d + (r.nextInt(1450) / 1000d);
luck = 21 + (int) (multiplier * 13) + r.nextInt(18);
break;
case "half":
i = player.getMoney() == 1 ? 1 : player.getMoney() / 2;
multiplier = 1.2d + (r.nextInt(1350) / 1000d);
luck = 19 + (int) (multiplier * 13) + r.nextInt(18);
break;
case "quarter":
i = player.getMoney() == 1 ? 1 : player.getMoney() / 4;
multiplier = 1.1d + (r.nextInt(1250) / 1000d);
luck = 18 + (int) (multiplier * 12) + r.nextInt(18);
break;
default:
i = content.endsWith("%") ? Math.round(PERCENT_FORMAT.get().parse(content).doubleValue() * player.getMoney()) : Long.parseLong(content);
if (i > player.getMoney() || i < 0)
throw new UnsupportedOperationException();
multiplier = 1.1d + (i / player.getMoney() * r.nextInt(1300) / 1000d);
luck = 17 + (int) (multiplier * 13) + r.nextInt(12);
break;
}
} catch (NumberFormatException e) {
event.getChannel().sendMessage(EmoteReference.ERROR2 + "Please type a valid number less than or equal to your current balance or" + " `all` to gamble all your credits.").queue();
return;
} catch (UnsupportedOperationException e) {
event.getChannel().sendMessage(EmoteReference.ERROR2 + "Please type a value within your balance.").queue();
return;
} catch (ParseException e) {
event.getChannel().sendMessage(EmoteReference.ERROR2 + "Please type a valid percentage value.").queue();
return;
}
User user = event.getAuthor();
long gains = (long) (i * multiplier);
gains = Math.round(gains * 0.45);
final int finalLuck = luck;
final long finalGains = gains;
if (i >= Integer.MAX_VALUE / 4) {
player.setLocked(true);
player.save();
event.getChannel().sendMessage(String.format("%sYou're about to bet **%d** credits (which seems to be a lot). " + "Are you sure? Type **yes** to continue and **no** otherwise.", EmoteReference.WARNING, i)).queue();
InteractiveOperations.create(event.getChannel(), 30, new InteractiveOperation() {
@Override
public int run(GuildMessageReceivedEvent e) {
if (e.getAuthor().getId().equals(user.getId())) {
if (e.getMessage().getContentRaw().equalsIgnoreCase("yes")) {
proceedGamble(event, player, finalLuck, random, i, finalGains);
return COMPLETED;
} else if (e.getMessage().getContentRaw().equalsIgnoreCase("no")) {
e.getChannel().sendMessage(EmoteReference.ZAP + "Cancelled bet.").queue();
player.setLocked(false);
player.saveAsync();
return COMPLETED;
}
}
return IGNORED;
}
@Override
public void onExpire() {
event.getChannel().sendMessage(EmoteReference.ERROR + "Time to complete the operation has ran out.").queue();
player.setLocked(false);
player.saveAsync();
}
});
return;
}
proceedGamble(event, player, luck, random, i, gains);
}
@Override
public MessageEmbed help(GuildMessageReceivedEvent event) {
return helpEmbed(event, "Gamble command").setDescription("Gambles your money").addField("Usage", "~>gamble <all/half/quarter> or ~>gamble <amount>\n" + "You can also use percentages now, for example `~>gamble 35%`", false).build();
}
});
}
Aggregations