Search in sources :

Example 1 with LottoPlayer

use of me.shadorc.shadbot.data.lotto.LottoPlayer in project Shadbot by Shadorc.

the class LottoCmd method show.

private void show(Context context) {
    List<LottoPlayer> players = LottoManager.getPlayers();
    EmbedBuilder embed = EmbedUtils.getDefaultEmbed().withAuthorName("Lotto").withThumbnail("https://cdn.onlineunitedstatescasinos.com/wp-content/uploads/2016/04/Lottery-icon.png").withDescription(String.format("The next draw will take place in **%s**%nTo participate, type: `%s%s %d-%d`", FormatUtils.formatCustomDate(LottoCmd.getDelay()), context.getPrefix(), this.getName(), MIN_NUM, MAX_NUM)).appendField("Number of participants", Integer.toString(players.size()), false).appendField("Prize pool", FormatUtils.formatCoins(LottoManager.getPool()), false);
    LottoPlayer player = players.stream().filter(lottoPlayer -> lottoPlayer.getUserID() == context.getAuthor().getLongID()).findAny().orElse(null);
    if (player != null) {
        embed.withFooterIcon("https://images.emojiterra.com/twitter/512px/1f39f.png");
        embed.withFooterText(String.format("%s, you bet on number %d.", context.getAuthorName(), player.getNum()));
    }
    LottoHistoric historic = LottoManager.getHistoric();
    if (historic != null) {
        String people;
        switch(historic.getWinnersCount()) {
            case 0:
                people = "nobody";
                break;
            case 1:
                people = "one person";
                break;
            default:
                people = historic.getWinnersCount() + " people";
                break;
        }
        embed.appendField("Historic", String.format("Last week, the prize pool contained **%s**, the winning number was **%d** and **%s won**.", FormatUtils.formatCoins(historic.getPool()), historic.getNum(), people), false);
    }
    BotUtils.sendMessage(embed.build(), context.getChannel());
}
Also used : EmbedBuilder(sx.blah.discord.util.EmbedBuilder) LottoPlayer(me.shadorc.shadbot.data.lotto.LottoPlayer) LottoHistoric(me.shadorc.shadbot.data.lotto.LottoHistoric)

Example 2 with LottoPlayer

use of me.shadorc.shadbot.data.lotto.LottoPlayer in project Shadbot by Shadorc.

the class LottoCmd method draw.

public static void draw() {
    LogUtils.infof("Lottery draw started...");
    int winningNum = ThreadLocalRandom.current().nextInt(MIN_NUM, MAX_NUM + 1);
    List<LottoPlayer> winners = LottoManager.getPlayers().stream().filter(player -> player.getNum() == winningNum && Shadbot.getClient().getGuildByID(player.getGuildID()) != null && Shadbot.getClient().getUserByID(player.getUserID()) != null).collect(Collectors.toList());
    for (LottoPlayer winner : winners) {
        IGuild guild = Shadbot.getClient().getGuildByID(winner.getGuildID());
        IUser user = Shadbot.getClient().getUserByID(winner.getUserID());
        int coins = (int) Math.ceil((double) LottoManager.getPool() / winners.size());
        Database.getDBUser(guild, user).addCoins(coins);
        BotUtils.sendMessage(String.format("Congratulations, you have the winning Lotto number! You earn %s.", FormatUtils.formatCoins(coins)), user.getOrCreatePMChannel());
    }
    LogUtils.infof("Lottery draw done (Winning number: %d | %d winner(s) | Prize pool: %d)", winningNum, winners.size(), LottoManager.getPool());
    LottoManager.setHistoric(winners.size(), LottoManager.getPool(), winningNum);
    LottoManager.resetUsers();
    if (!winners.isEmpty()) {
        LottoManager.resetPool();
    }
}
Also used : HelpBuilder(me.shadorc.shadbot.utils.embed.HelpBuilder) CommandCategory(me.shadorc.shadbot.core.command.CommandCategory) LottoManager(me.shadorc.shadbot.data.lotto.LottoManager) ZonedDateTime(java.time.ZonedDateTime) LogUtils(me.shadorc.shadbot.utils.LogUtils) DBUser(me.shadorc.shadbot.data.db.DBUser) LottoPlayer(me.shadorc.shadbot.data.lotto.LottoPlayer) BotUtils(me.shadorc.shadbot.utils.BotUtils) TimeUtils(me.shadorc.shadbot.utils.TimeUtils) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) Command(me.shadorc.shadbot.core.command.annotation.Command) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) IUser(sx.blah.discord.handle.obj.IUser) TextUtils(me.shadorc.shadbot.utils.TextUtils) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) RateLimited(me.shadorc.shadbot.core.command.annotation.RateLimited) LottoHistoric(me.shadorc.shadbot.data.lotto.LottoHistoric) EmbedObject(sx.blah.discord.api.internal.json.objects.EmbedObject) FormatUtils(me.shadorc.shadbot.utils.FormatUtils) Collectors(java.util.stream.Collectors) Shadbot(me.shadorc.shadbot.Shadbot) IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) CastUtils(me.shadorc.shadbot.utils.CastUtils) IGuild(sx.blah.discord.handle.obj.IGuild) List(java.util.List) Context(me.shadorc.shadbot.core.command.Context) DayOfWeek(java.time.DayOfWeek) AbstractCommand(me.shadorc.shadbot.core.command.AbstractCommand) Database(me.shadorc.shadbot.data.db.Database) Emoji(me.shadorc.shadbot.utils.object.Emoji) EmbedUtils(me.shadorc.shadbot.utils.embed.EmbedUtils) LottoPlayer(me.shadorc.shadbot.data.lotto.LottoPlayer) IUser(sx.blah.discord.handle.obj.IUser) IGuild(sx.blah.discord.handle.obj.IGuild)

Example 3 with LottoPlayer

use of me.shadorc.shadbot.data.lotto.LottoPlayer in project Shadbot by Shadorc.

the class LottoCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    if (!context.hasArg()) {
        this.show(context);
        return;
    }
    DBUser dbUser = Database.getDBUser(context.getGuild(), context.getAuthor());
    if (dbUser.getCoins() < PAID_COST) {
        BotUtils.sendMessage(TextUtils.notEnoughCoins(context.getAuthor()), context.getChannel());
        return;
    }
    LottoPlayer player = LottoManager.getPlayers().stream().filter(lottoPlayer -> lottoPlayer.getUserID() == context.getAuthor().getLongID()).findAny().orElse(null);
    if (player != null) {
        throw new IllegalCmdArgumentException("You're already participating.");
    }
    Integer num = CastUtils.asIntBetween(context.getArg(), MIN_NUM, MAX_NUM);
    if (num == null) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid number, it must be between %d and %d.", context.getArg(), MIN_NUM, MAX_NUM));
    }
    dbUser.addCoins(-PAID_COST);
    LottoManager.addPlayer(context.getGuild(), context.getAuthor(), num);
    BotUtils.sendMessage(String.format(Emoji.TICKET + " You bought a lottery ticket and bet on number **%d**. Good luck !", num), context.getChannel());
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) LottoPlayer(me.shadorc.shadbot.data.lotto.LottoPlayer) DBUser(me.shadorc.shadbot.data.db.DBUser)

Aggregations

LottoPlayer (me.shadorc.shadbot.data.lotto.LottoPlayer)3 DBUser (me.shadorc.shadbot.data.db.DBUser)2 LottoHistoric (me.shadorc.shadbot.data.lotto.LottoHistoric)2 IllegalCmdArgumentException (me.shadorc.shadbot.exception.IllegalCmdArgumentException)2 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)2 DayOfWeek (java.time.DayOfWeek)1 ZonedDateTime (java.time.ZonedDateTime)1 List (java.util.List)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 Collectors (java.util.stream.Collectors)1 Shadbot (me.shadorc.shadbot.Shadbot)1 AbstractCommand (me.shadorc.shadbot.core.command.AbstractCommand)1 CommandCategory (me.shadorc.shadbot.core.command.CommandCategory)1 Context (me.shadorc.shadbot.core.command.Context)1 Command (me.shadorc.shadbot.core.command.annotation.Command)1 RateLimited (me.shadorc.shadbot.core.command.annotation.RateLimited)1 Database (me.shadorc.shadbot.data.db.Database)1 LottoManager (me.shadorc.shadbot.data.lotto.LottoManager)1 MissingArgumentException (me.shadorc.shadbot.exception.MissingArgumentException)1 BotUtils (me.shadorc.shadbot.utils.BotUtils)1