Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/main/kotlin/org/openredstone/trialore/TrialCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.temporal.ChronoUnit
import java.util.UUID

fun getDate(timestamp: Long) = LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneOffset.UTC)

Expand All @@ -32,9 +33,35 @@ class TrialCommand(
private val version: String,
) : BaseCommand() {

private fun isTrialOkay(testificate: UUID): Boolean {
val trials = trialORE.database.getTrials(testificate)
val now = System.currentTimeMillis()

val recentTrials = trials.map { trialORE.database.getTrialInfo(it) }
.filter {
val trialTime = it.start.toLong()
trialTime >= now - trialORE.config.periodDays * 24 * 60 * 60L
}
.sortedByDescending { it.start }

if (recentTrials.size >= trialORE.config.countPerPeriod) {
return false
}

val mostRecent = recentTrials.firstOrNull()
if (mostRecent != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use something like .firstOrNull()?.let { it -> etcetcetc } to "kotlinify" this. https://kotlinlang.org/docs/scope-functions.html#let

val hoursSinceLast = (now - mostRecent.start.toLong()) / (1000 * 60 * 60)
if (hoursSinceLast < trialORE.config.minSpacingHours) {
return false
}
}

return true
}

@Default()
@Subcommand("info")
@Description("Information about a TrialORE")
@Description("Information about TrialORE")
fun onInfo(player: Player) {
player.renderMiniMessage("Current TrialORE version: <gray>$version")
player.renderMiniMessage("For more details on commands, " +
Expand Down Expand Up @@ -72,6 +99,11 @@ class TrialCommand(
player.renderMessage(note)
}
}

// Warn of recent trials
if (!this.isTrialOkay(testificate)) {
player.renderMiniMessage("<yellow>Warning: According to TrialORE settings, this user is ineligible for trial. Check past trials")
}
}

@CommandAlias("trialstart")
Expand All @@ -96,6 +128,12 @@ class TrialCommand(
if (!app.startsWith("https://discourse.openredstone.org/")) {
throw TrialOreException("Invalid app: $app")
}

// Warn of recent trials
if (!this.isTrialOkay(testificate.uniqueId)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice if this gets stated and halts the start of the trial. Could be ignored with an extra argument, like /trialstart Nickster258 https://app.link confirm or something.

player.renderMiniMessage("<yellow>Warning: According to TrialORE settings, this user is ineligible for trial. Check past trials")
}

player.renderMessage("Starting trial of ${testificate.name}")
testificate.renderMessage("Starting trial with ${player.name}")
trialORE.startTrial(player.uniqueId, testificate.uniqueId, app)
Expand Down
5 changes: 4 additions & 1 deletion src/main/kotlin/org/openredstone/trialore/TrialOre.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ data class TrialOreConfig(
val testificateGroup: String = "testificate",
val builderGroup: String = "builder",
val webhook: String = "webhook",
val abandonForgiveness: Long = 6000
val abandonForgiveness: Long = 6000,
val countPerPeriod: Int = 2,
val periodDays: Int = 7,
val minSpacingHours: Int = 24
)

data class TrialMeta(
Expand Down