use of net.kodehawa.mantarobot.data.entities.Player in project MantaroBot by Mantaro.
the class CurrencyCmds method market.
@Command
public static void market(CommandRegistry cr) {
cr.register("market", new SimpleCommand(Category.CURRENCY) {
RateLimiter rateLimiter = new RateLimiter(TimeUnit.SECONDS, 5);
@Override
public void call(GuildMessageReceivedEvent event, String content, String[] args) {
if (!rateLimiter.process(event.getAuthor().getId())) {
event.getChannel().sendMessage(EmoteReference.STOPWATCH + "Wait! You're calling me so fast that I can't get enough items!").queue();
return;
}
Player player = MantaroData.db().getPlayer(event.getMember());
if (args.length > 0) {
int itemNumber = 1;
String itemName = content.replace(args[0] + " ", "");
boolean isMassive = !itemName.isEmpty() && itemName.split(" ")[0].matches("^[0-9]*$");
if (isMassive) {
try {
itemNumber = Math.abs(Integer.valueOf(itemName.split(" ")[0]));
itemName = itemName.replace(args[1] + " ", "");
} catch (Exception e) {
if (e instanceof NumberFormatException) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Not a valid number of items to buy.").queue();
} else {
onHelp(event);
return;
}
}
}
if (itemNumber > 5000) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You can't buy more than 5000 items").queue();
return;
}
if (args[0].equals("sell")) {
try {
if (args[1].equals("all")) {
long all = player.getInventory().asList().stream().filter(item -> item.getItem().isSellable()).mapToLong(value -> (long) (value.getItem().getValue() * value.getAmount() * 0.9d)).sum();
if (args.length > 2 && args[2].equals("calculate")) {
event.getChannel().sendMessage(EmoteReference.THINKING + "You'll get **" + all + "** credits if you " + "sell all of your items").queue();
return;
}
player.getInventory().clearOnlySellables();
if (player.addMoney(all)) {
event.getChannel().sendMessage(EmoteReference.MONEY + "You sold all your inventory items and gained " + all + " credits!").queue();
} else {
event.getChannel().sendMessage(EmoteReference.MONEY + "You sold all your inventory items and gained " + all + " credits. But you already had too many credits. Your bag overflowed" + ".\nCongratulations, you exploded a Java long (how??). Here's a buggy money bag for you.").queue();
}
player.saveAsync();
return;
}
Item toSell = Items.fromAny(itemName).orElse(null);
if (!toSell.isSellable()) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot sell an item that cannot be sold.").queue();
return;
}
if (player.getInventory().asMap().getOrDefault(toSell, null) == null) {
event.getChannel().sendMessage(EmoteReference.STOP + "You cannot sell an item you don't have.").queue();
return;
}
if (player.getInventory().getAmount(toSell) < itemNumber) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot sell more items than what you have.").queue();
return;
}
int many = itemNumber * -1;
long amount = Math.round((toSell.getValue() * 0.9)) * Math.abs(many);
player.getInventory().process(new ItemStack(toSell, many));
if (player.addMoney(amount)) {
event.getChannel().sendMessage(EmoteReference.CORRECT + "You sold " + Math.abs(many) + " **" + toSell.getName() + "** and gained " + amount + " credits!").queue();
} else {
event.getChannel().sendMessage(EmoteReference.CORRECT + "You sold **" + toSell.getName() + "** and gained" + amount + " credits. But you already had too many credits. Your bag overflowed" + ".\nCongratulations, you exploded a Java long (how??). Here's a buggy money bag for you.").queue();
}
player.save();
return;
} catch (Exception e) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Item doesn't exist or invalid syntax").queue();
e.printStackTrace();
}
return;
}
if (args[0].equals("buy")) {
Item itemToBuy = Items.fromAny(itemName).orElse(null);
if (itemToBuy == null) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot buy an unexistant item.").queue();
return;
}
try {
if (!itemToBuy.isBuyable()) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot buy an item that cannot be bought.").queue();
return;
}
ItemStack stack = player.getInventory().getStackOf(itemToBuy);
if (stack != null && !stack.canJoin(new ItemStack(itemToBuy, itemNumber))) {
//assume overflow
event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot buy more of that object!").queue();
return;
}
if (player.removeMoney(itemToBuy.getValue() * itemNumber)) {
player.getInventory().process(new ItemStack(itemToBuy, itemNumber));
player.save();
event.getChannel().sendMessage(EmoteReference.OK + "Bought " + itemNumber + " " + itemToBuy.getEmoji() + " successfully. You now have " + player.getMoney() + " credits.").queue();
} else {
event.getChannel().sendMessage(EmoteReference.STOP + "You don't have enough money to buy this item.").queue();
}
return;
} catch (Exception e) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Item doesn't exist or invalid syntax.").queue();
}
return;
}
}
EmbedBuilder embed = baseEmbed(event, EmoteReference.MARKET + "Mantaro Market");
Stream.of(Items.ALL).forEach(item -> {
if (!item.isHidden()) {
String buyValue = item.isBuyable() ? EmoteReference.BUY + "$" + String.valueOf((int) Math.floor(item.getValue() * 1.1)) + " " : "";
String sellValue = item.isSellable() ? EmoteReference.SELL + "$" + String.valueOf((int) Math.floor(item.getValue() * 0.9)) : "";
embed.addField(item.getEmoji() + " " + item.getName(), buyValue + sellValue, true);
}
});
event.getChannel().sendMessage(embed.setThumbnail("https://i.imgur.com/OA7QCaM.png").build()).queue();
}
@Override
public MessageEmbed help(GuildMessageReceivedEvent event) {
return helpEmbed(event, "Mantaro's market").setDescription("**List current items for buying and selling.**").addField("Buying and selling", "To buy do ~>market buy <item emoji>. It will substract the value from your money" + " and give you the item.\n" + "To sell do `~>market sell all` to sell all your items or `~>market sell <item emoji>` to sell the " + "specified item. " + "**You'll get the sell value of the item on coins to spend.**", false).addField("To know", "If you don't have enough money you cannot buy the items.", false).addField("Information", "To buy and sell multiple items you need to do `~>market <buy/sell> <amount> <item>`", false).build();
}
});
}
use of net.kodehawa.mantarobot.data.entities.Player in project MantaroBot by Mantaro.
the class CurrencyCmds method rep.
@Command
public static void rep(CommandRegistry cr) {
cr.register("rep", new SimpleCommand(Category.CURRENCY) {
RateLimiter rateLimiter = new RateLimiter(TimeUnit.HOURS, 12);
@Override
public void call(GuildMessageReceivedEvent event, String content, String[] args) {
if (event.getMessage().getMentionedUsers().isEmpty()) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You need to mention at least one user.").queue();
return;
}
if (event.getMessage().getMentionedUsers().get(0).isBot()) {
event.getChannel().sendMessage(EmoteReference.THINKING + "You cannot rep a bot.").queue();
return;
}
if (event.getMessage().getMentionedUsers().get(0).equals(event.getAuthor())) {
event.getChannel().sendMessage(EmoteReference.THINKING + "You cannot rep yourself.").queue();
return;
}
if (event.getMessage().getMentionedUsers().isEmpty()) {
event.getChannel().sendMessage(EmoteReference.THINKING + "You need to mention one user.").queue();
return;
}
if (!rateLimiter.process(event.getMember())) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You can only rep once every 12 hours.\n**You'll be able to use this command again in " + Utils.getVerboseTime(rateLimiter.tryAgainIn(event.getMember())) + ".**").queue();
return;
}
User mentioned = event.getMessage().getMentionedUsers().get(0);
Player player = MantaroData.db().getPlayer(event.getGuild().getMember(mentioned));
player.addReputation(1L);
player.saveAsync();
event.getChannel().sendMessage(EmoteReference.CORRECT + "Added reputation to **" + mentioned.getName() + "**").queue();
}
@Override
public MessageEmbed help(GuildMessageReceivedEvent event) {
return helpEmbed(event, "Reputation command").setDescription("**Reps an user**").addField("Usage", "`~>rep <@user>` - **Gives reputation to x user**", false).addField("Parameters", "`@user` - user to mention", false).addField("Important", "Only usable every 24 hours.", false).build();
}
});
cr.registerAlias("rep", "reputation");
}
use of net.kodehawa.mantarobot.data.entities.Player in project MantaroBot by Mantaro.
the class CurrencyCmds method openLootBox.
public static void openLootBox(GuildMessageReceivedEvent event, boolean special) {
List<Item> toAdd = new ArrayList<>();
int amtItems = random.nextInt(3) + 3;
List<Item> items = new ArrayList<>();
items.addAll(Arrays.asList(Items.ALL));
items.removeIf(item -> item.isHidden() || !item.isBuyable() || !item.isSellable());
items.sort((o1, o2) -> {
if (o1.getValue() > o2.getValue())
return 1;
if (o1.getValue() == o2.getValue())
return 0;
return -1;
});
if (!special) {
for (Item i : Items.ALL) if (i.isHidden() || !i.isBuyable() || i.isSellable())
items.add(i);
}
for (int i = 0; i < amtItems; i++) toAdd.add(selectReverseWeighted(items));
Player player = MantaroData.db().getPlayer(event.getMember());
ArrayList<ItemStack> ita = new ArrayList<>();
toAdd.forEach(item -> ita.add(new ItemStack(item, 1)));
boolean overflow = player.getInventory().merge(ita);
player.save();
event.getChannel().sendMessage(EmoteReference.LOOT_CRATE.getDiscordNotation() + "**You won:** " + toAdd.stream().map(Item::toString).collect(Collectors.joining(", ")) + (overflow ? ". But you already had too much, so you decided to throw away the excess" : "")).queue();
}
use of net.kodehawa.mantarobot.data.entities.Player in project MantaroBot by Mantaro.
the class GameCmds method startGame.
private static void startGame(Game game, GuildMessageReceivedEvent event) {
if (GameLobby.LOBBYS.containsKey(event.getChannel())) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Cannot start a new game when there is a game currently running.").queue();
return;
}
LinkedList<Game> list = new LinkedList<>();
list.add(game);
HashMap<Member, Player> map = new HashMap<>();
map.put(event.getMember(), MantaroData.db().getPlayer(event.getMember()));
if (!event.getMessage().getMentionedRoles().isEmpty()) {
StringBuilder b = new StringBuilder();
event.getMessage().getMentionedRoles().forEach(role -> event.getGuild().getMembersWithRoles(role).forEach(user -> {
if (!user.getUser().getId().equals(event.getJDA().getSelfUser().getId()))
map.put(user, MantaroData.db().getPlayer(user));
b.append(user.getEffectiveName()).append(" ");
}));
event.getChannel().sendMessage(EmoteReference.MEGA + "Started a MP game with all users with the specfied role: " + b.toString()).queue();
}
if (!event.getMessage().getMentionedUsers().isEmpty()) {
StringBuilder builder = new StringBuilder();
event.getMessage().getMentionedUsers().forEach(user -> {
if (!user.getId().equals(event.getJDA().getSelfUser().getId()))
map.put(event.getGuild().getMember(user), MantaroData.db().getPlayer(event.getGuild().getMember(user)));
builder.append(user.getName()).append(" ");
});
event.getChannel().sendMessage(EmoteReference.MEGA + "Started a MP game with users: " + builder.toString()).queue();
}
GameLobby lobby = new GameLobby(event, map, list);
lobby.startFirstGame();
}
use of net.kodehawa.mantarobot.data.entities.Player in project MantaroBot by Mantaro.
the class MoneyCmds method gamble.
@Command
public static void gamble(CommandRegistry cr) {
RateLimiter rateLimiter = new RateLimiter(TimeUnit.SECONDS, 15);
Random r = new Random();
cr.register("gamble", new SimpleCommand(Category.CURRENCY) {
@Override
public void call(GuildMessageReceivedEvent event, String content, String[] args) {
String id = event.getAuthor().getId();
Player player = MantaroData.db().getPlayer(event.getMember());
if (!rateLimiter.process(id)) {
event.getChannel().sendMessage(EmoteReference.STOPWATCH + "Halt! You're gambling so fast that I can't print enough money!").queue();
return;
}
if (player.getMoney() <= 0) {
event.getChannel().sendMessage(EmoteReference.ERROR2 + "You're broke. Search for some credits first!").queue();
return;
}
if (player.getMoney() > (long) (Integer.MAX_VALUE) * 3) {
event.getChannel().sendMessage(EmoteReference.ERROR2 + "You have too much money! Maybe transfer or buy items?").queue();
return;
}
double multiplier;
long i;
int luck;
try {
switch(content) {
case "all":
case "everything":
i = player.getMoney();
multiplier = 1.4d + (r.nextInt(1500) / 1000d);
luck = 30 + (int) (multiplier * 10) + r.nextInt(20);
break;
case "half":
i = player.getMoney() == 1 ? 1 : player.getMoney() / 2;
multiplier = 1.2d + (r.nextInt(1500) / 1000d);
luck = 20 + (int) (multiplier * 15) + r.nextInt(20);
break;
case "quarter":
i = player.getMoney() == 1 ? 1 : player.getMoney() / 4;
multiplier = 1.1d + (r.nextInt(1100) / 1000d);
luck = 25 + (int) (multiplier * 10) + r.nextInt(18);
break;
default:
i = Long.parseLong(content);
if (i > player.getMoney() || i < 0)
throw new UnsupportedOperationException();
multiplier = 1.1d + (i / player.getMoney() * r.nextInt(1300) / 1000d);
luck = 15 + (int) (multiplier * 15) + r.nextInt(10);
break;
}
} catch (NumberFormatException e) {
event.getChannel().sendMessage(EmoteReference.ERROR2 + "Please type a valid number equal or less than your credits or" + " `all` to gamble all your credits.").queue();
return;
} catch (UnsupportedOperationException e) {
event.getChannel().sendMessage(EmoteReference.ERROR2 + "Please type a value within your credits amount.").queue();
return;
}
User user = event.getAuthor();
long gains = (long) (i * multiplier);
gains = Math.round(gains * 0.55);
final int finalLuck = luck;
final long finalGains = gains;
if (i >= Integer.MAX_VALUE / 4) {
event.getChannel().sendMessage(EmoteReference.WARNING + "You're about to bet **" + i + "** " + "credits (which seems to be a lot). Are you sure? Type **yes** to continue and **no** otherwise.").queue();
InteractiveOperations.create(event.getChannel(), "Gambling", (int) TimeUnit.SECONDS.toMillis(30), OptionalInt.empty(), new InteractiveOperation() {
@Override
public boolean run(GuildMessageReceivedEvent e) {
if (e.getAuthor().getId().equals(user.getId())) {
if (e.getMessage().getContent().equalsIgnoreCase("yes")) {
proceedGamble(event, player, finalLuck, random, i, finalGains);
return true;
} else if (e.getMessage().getContent().equalsIgnoreCase("no")) {
e.getChannel().sendMessage(EmoteReference.ZAP + "Cancelled bet.").queue();
return true;
}
}
return false;
}
@Override
public void onExpire() {
event.getChannel().sendMessage(EmoteReference.ERROR + "Time to complete the operation has ran out.").queue();
}
});
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>", false).build();
}
});
}
Aggregations