<commit_before>package net.perfectdreams.loritta.plugin.helpinghands.commands

import net.perfectdreams.loritta.api.commands.ArgumentType
import net.perfectdreams.loritta.api.commands.CommandCategory
import net.perfectdreams.loritta.api.commands.arguments
import net.perfectdreams.loritta.api.messages.LorittaReply
import net.perfectdreams.loritta.platform.discord.commands.DiscordAbstractCommandBase
import net.perfectdreams.loritta.plugin.helpinghands.HelpingHandsPlugin
import net.perfectdreams.loritta.tables.SonhosTransaction
import net.perfectdreams.loritta.utils.Emotes
import net.perfectdreams.loritta.utils.SonhosPaymentReason
import net.perfectdreams.loritta.utils.extensions.toJDA
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.sum

class CoinFlipBetStatsCommand(val plugin: HelpingHandsPlugin) : DiscordAbstractCommandBase(
		plugin.loritta,
		listOf("coinflip", "flipcoin", "girarmoeda", "caracoroa")
				.flatMap { listOf("$it bet stats", "$it apostar stats") },
		CommandCategory.ECONOMY
) {
	override fun command() = create {
		localizedDescription("commands.economy.flipcoinbetstats.description")

		examples {
			+ "@MrPowerGamerBR"
		}

		usage {
			arguments {
				argument(ArgumentType.USER) {
					optional = true
				}
			}
		}

		executesDiscord {
			var checkStatsOfUser = user
			val checkStatsOfOtherUser = user(0)
			var checkingYourself = true

			if (checkStatsOfOtherUser != null) {
				checkStatsOfUser = validate(checkStatsOfOtherUser)
						.toJDA()
				checkingYourself = false
			}

			val winCount = loritta.newSuspendedTransaction {
				SonhosTransaction.select {
					(SonhosTransaction.receivedBy eq checkStatsOfUser.idLong) and
							(SonhosTransaction.reason eq SonhosPaymentReason.COIN_FLIP_BET)
				}.count()
			}

			val loseCount = loritta.newSuspendedTransaction {
				SonhosTransaction.select {
					(SonhosTransaction.givenBy eq checkStatsOfUser.idLong) and
							(SonhosTransaction.reason eq SonhosPaymentReason.COIN_FLIP_BET)
				}.count()
			}

			val playedCount = winCount + loseCount

			if (playedCount == 0L)
			// Never played!
				fail(locale["commands.economy.flipcoinbetstats.playerHasNeverPlayed"])

			val sumField = SonhosTransaction.quantity.sum()
			val winSum = loritta.newSuspendedTransaction {
				SonhosTransaction.slice(sumField).select {
					(SonhosTransaction.receivedBy eq checkStatsOfUser.idLong) and
							(SonhosTransaction.reason eq SonhosPaymentReason.COIN_FLIP_BET)
				}.first()[sumField]!!
			}

			val loseSum = loritta.newSuspendedTransaction {
				SonhosTransaction.slice(sumField).select {
					(SonhosTransaction.givenBy eq checkStatsOfUser.idLong) and
							(SonhosTransaction.reason eq SonhosPaymentReason.COIN_FLIP_BET)
				}.first()[sumField]!!
			}

			val winPercentage = (winCount) / (winCount.toDouble() + loseCount.toDouble())
			val losePercentage = (loseCount) / (winCount.toDouble() + loseCount.toDouble())

			reply(
					LorittaReply(
							if (checkingYourself)
								locale["commands.economy.flipcoinbetstats.yourStats"]
							else
								locale["commands.economy.flipcoinbetstats.statsOfUser", checkStatsOfUser.asMention]
							,
							Emotes.LORI_RICH
					),
					LorittaReply(
							locale["commands.economy.flipcoinbetstats.playedMatches", playedCount],
							mentionUser = false
					),
					LorittaReply(
							locale["commands.economy.flipcoinbetstats.wonMatches", winPercentage, winCount],
							mentionUser = false
					),
					LorittaReply(<commit_msg>Fix exception if the player played coin flip only once<commit_after>package net.perfectdreams.loritta.plugin.helpinghands.commands

import net.perfectdreams.loritta.api.commands.ArgumentType
import net.perfectdreams.loritta.api.commands.CommandCategory
import net.perfectdreams.loritta.api.commands.arguments
import net.perfectdreams.loritta.api.messages.LorittaReply
import net.perfectdreams.loritta.platform.discord.commands.DiscordAbstractCommandBase
import net.perfectdreams.loritta.plugin.helpinghands.HelpingHandsPlugin
import net.perfectdreams.loritta.tables.SonhosTransaction
import net.perfectdreams.loritta.utils.Emotes
import net.perfectdreams.loritta.utils.SonhosPaymentReason
import net.perfectdreams.loritta.utils.extensions.toJDA
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.sum
import java.math.BigDecimal

class CoinFlipBetStatsCommand(val plugin: HelpingHandsPlugin) : DiscordAbstractCommandBase(
		plugin.loritta,
		listOf("coinflip", "flipcoin", "girarmoeda", "caracoroa")
				.flatMap { listOf("$it bet stats", "$it apostar stats") },
		CommandCategory.ECONOMY
) {
	override fun command() = create {
		localizedDescription("commands.economy.flipcoinbetstats.description")

		examples {
			+ "@MrPowerGamerBR"
		}

		usage {
			arguments {
				argument(ArgumentType.USER) {
					optional = true
				}
			}
		}

		executesDiscord {
			var checkStatsOfUser = user
			val checkStatsOfOtherUser = user(0)
			var checkingYourself = true

			if (checkStatsOfOtherUser != null) {
				checkStatsOfUser = validate(checkStatsOfOtherUser)
						.toJDA()
				checkingYourself = false
			}

			val winCount = loritta.newSuspendedTransaction {
				SonhosTransaction.select {
					(SonhosTransaction.receivedBy eq checkStatsOfUser.idLong) and
							(SonhosTransaction.reason eq SonhosPaymentReason.COIN_FLIP_BET)
				}.count()
			}

			val loseCount = loritta.newSuspendedTransaction {
				SonhosTransaction.select {
					(SonhosTransaction.givenBy eq checkStatsOfUser.idLong) and
							(SonhosTransaction.reason eq SonhosPaymentReason.COIN_FLIP_BET)
				}.count()
			}

			val playedCount = winCount + loseCount

			if (playedCount == 0L)
			// Never played!
				fail(locale["commands.economy.flipcoinbetstats.playerHasNeverPlayed"])

			val sumField = SonhosTransaction.quantity.sum()
			val winSum = loritta.newSuspendedTransaction {
				SonhosTransaction.slice(sumField).select {
					(SonhosTransaction.receivedBy eq checkStatsOfUser.idLong) and
							(SonhosTransaction.reason eq SonhosPaymentReason.COIN_FLIP_BET)
				}.firstOrNull()?.get(sumField)
			} ?: BigDecimal.ZERO

			val loseSum = loritta.newSuspendedTransaction {
				SonhosTransaction.slice(sumField).select {
					(SonhosTransaction.givenBy eq checkStatsOfUser.idLong) and
							(SonhosTransaction.reason eq SonhosPaymentReason.COIN_FLIP_BET)
				}.firstOrNull()?.get(sumField)
			} ?: BigDecimal.ZERO

			val winPercentage = (winCount) / (winCount.toDouble() + loseCount.toDouble())
			val losePercentage = (loseCount) / (winCount.toDouble() + loseCount.toDouble())

			reply(
					LorittaReply(
							if (checkingYourself)
								locale["commands.economy.flipcoinbetstats.yourStats"]
							else
								locale["commands.economy.flipcoinbetstats.statsOfUser", checkStatsOfUser.asMention]
							,
							Emotes.LORI_RICH
					),
					LorittaReply(
							locale["commands.economy.flipcoinbetstats.playedMatches", playedCount],
							mentionUser = false
					),
					LorittaReply(
							locale["commands.economy.flipcoinbetstats.wonMatches", winPercentage, winCount],
							mentionUser = false
					),
					LorittaReply(
{'fraction_non_alphanumeric': 0.09452529452529453, 'fraction_numerical': 0.0005544005544005544, 'mean_word_length': 4.221418234442837, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4676035', 'n_tokens_mistral': 2846, 'n_tokens_neox': 2212, 'n_words': 348}