-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #220 from MLGPenguin/counting
Counting Improvements & Leaderboard
- Loading branch information
Showing
7 changed files
with
137 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/kotlin/com/learnspigot/bot/counting/CountingCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.learnspigot.bot.counting | ||
|
||
import com.learnspigot.bot.profile.ProfileRegistry | ||
import com.learnspigot.bot.util.embed | ||
import gg.flyte.neptune.annotation.Command | ||
import gg.flyte.neptune.annotation.Description | ||
import gg.flyte.neptune.annotation.Inject | ||
import gg.flyte.neptune.annotation.Optional | ||
import net.dv8tion.jda.api.entities.User | ||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent | ||
|
||
class CountingCommand { | ||
|
||
@Inject private lateinit var profileRegistry: ProfileRegistry | ||
@Inject private lateinit var countingRegistry: CountingRegistry | ||
|
||
@Command(name = "countingstats", description = "View counting statistics") | ||
fun onCountingCommand( | ||
event: SlashCommandInteractionEvent, | ||
@Description("User's stats to view") @Optional user: User? | ||
) { | ||
if (user == null) { // Server stats | ||
event.replyEmbeds( | ||
embed() | ||
.setTitle("Server counting statistics") | ||
.setDescription(""" | ||
- Current Count: ${countingRegistry.currentCount} | ||
- Total Counts: ${countingRegistry.serverTotalCounts} | ||
- Highest Count: ${countingRegistry.topServerCount} | ||
""".trimIndent()) | ||
.addField( | ||
"Top 5 counters", | ||
countingRegistry.leaderboard.take(5).joinToString("") { | ||
val profile = profileRegistry.findById(it)!! | ||
if (profile.tag == null || profile.totalCounts == 0) return@joinToString "" | ||
"\n- ${profile.tag}: ${profile.totalCounts}" | ||
}, | ||
false | ||
) | ||
.build() | ||
).setEphemeral(true).queue() | ||
} else { // Individual Stats | ||
val profile = profileRegistry.findByUser(user) | ||
event.replyEmbeds( | ||
embed() | ||
.setTitle(user.name + "'s counting statistics") | ||
.setDescription(""" | ||
- Total Counts: ${profile.totalCounts} | ||
- Highest Count: ${profile.highestCount} | ||
- Mistakes: ${profile.countingFuckUps} | ||
""".trimIndent()) | ||
.build() | ||
).setEphemeral(true).queue() | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/kotlin/com/learnspigot/bot/counting/CountingRegistry.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.learnspigot.bot.counting | ||
|
||
import com.learnspigot.bot.profile.ProfileRegistry | ||
import com.learnspigot.bot.util.Mongo | ||
import com.mongodb.client.model.Filters | ||
import net.dv8tion.jda.api.entities.User | ||
import org.bson.Document | ||
import java.util.* | ||
|
||
class CountingRegistry(private val profileRegistry: ProfileRegistry) { | ||
private val mongoCollection = Mongo.countingCollection | ||
|
||
var topServerCount: Int = 0 | ||
var serverTotalCounts: Int = 0 | ||
var currentCount = 0 | ||
|
||
// Queue of User Ids | ||
val leaderboard: PriorityQueue<String> = PriorityQueue { a, b -> | ||
profileRegistry.findById(b)!!.totalCounts.compareTo(profileRegistry.findById(a)!!.totalCounts) | ||
} | ||
|
||
init { | ||
val document = mongoCollection.find().first() | ||
if (document == null) { | ||
val newDoc = Document() | ||
newDoc["highestCount"] = 0 | ||
newDoc["currentCount"] = 0 | ||
newDoc["serverTotalCounts"] = 0 | ||
mongoCollection.insertOne(newDoc) | ||
} else { | ||
topServerCount = document.getInteger("highestCount", 0) | ||
currentCount = document.getInteger("currentCount", 0) | ||
serverTotalCounts = document.getInteger("serverTotalCounts", 0) | ||
} | ||
|
||
profileRegistry.profileCache.values.forEach { leaderboard.add(it.id) } | ||
} | ||
|
||
private fun <T> PriorityQueue<T>.recalculate(ele: T) = remove(ele).also { add(ele) } | ||
|
||
fun incrementCount(user: User) { | ||
currentCount++ | ||
serverTotalCounts++ | ||
if (currentCount > topServerCount) topServerCount = currentCount | ||
leaderboard.recalculate(user.id) | ||
profileRegistry.findByUser(user).incrementCount(currentCount) | ||
val newDoc = mongoCollection.find().first()!! | ||
newDoc["highestCount"] = topServerCount | ||
newDoc["currentCount"] = currentCount | ||
newDoc["serverTotalCounts"] = serverTotalCounts | ||
mongoCollection.replaceOne(Filters.eq("_id", newDoc.getObjectId("_id")), newDoc) | ||
} | ||
|
||
fun fuckedUp(user: User) { | ||
currentCount = 0 | ||
profileRegistry.findByUser(user).fuckedUpCounting() | ||
} | ||
|
||
} |