feat: added MatchManager

Signed-off-by: limited_dev <loginakkisativ@gmail.com>
This commit is contained in:
limited_dev 2023-07-12 13:20:01 +02:00
parent 62e230777b
commit 3f3fb87ebd

View file

@ -0,0 +1,88 @@
/*
* lilJudd
* Copyright (C) 2023 moonleay
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.moonleay.lilJudd.features
import dev.kord.common.entity.Snowflake
import net.moonleay.lilJudd.Bot
import net.moonleay.lilJudd.data.entry.MatchPlanningDataData
import net.moonleay.lilJudd.data.tables.MatchPlanningData
import net.moonleay.lilJudd.jobs.MatchJob
import net.moonleay.lilJudd.jobs.component.JobManager
import net.moonleay.lilJudd.util.Logger
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction
object MatchManager {
suspend fun update() {
Logger.out("Updating match roles...")
val dataList = mutableListOf<MatchPlanningDataData>()
transaction {
MatchPlanningData.selectAll().forEach {
dataList.add(
MatchPlanningDataData(
it[MatchPlanningData.id].value,
it[MatchPlanningData.serverid],
it[MatchPlanningData.channelid],
it[MatchPlanningData.matchtype],
it[MatchPlanningData.registererid],
it[MatchPlanningData.roleid],
it[MatchPlanningData.opponentName],
it[MatchPlanningData.messageid],
it[MatchPlanningData.timestamp],
it[MatchPlanningData.jobstr]
)
)
}
}
for (data in dataList) {
Logger.out("Checking match role ${data.id}...")
if (data.timestamp.toLong() < System.currentTimeMillis()) {
Logger.out("Match role ${data.id} is expired, removing...")
this.removeRoleFromGuild(data.serverid, data.roleid)
transaction {
MatchPlanningData.deleteWhere { MatchPlanningData.messageid eq data.messageid }
}
continue
}
this.registerJob(data)
}
Logger.out("Done. Until next time! <3 ")
}
private fun registerJob(data: MatchPlanningDataData) {
JobManager.addJob(
MatchJob(
data.jobstr,
data.id,
"fromdb-${data.matchtype}_Vs_${data.opponentname}_[${data.id}]-${data.serverid}_${data.channelid}"
)
)
Logger.out("Registered job for match ${data.id}...")
}
private suspend fun removeRoleFromGuild(gid: String, rid: String): Boolean {
val guild = Bot.bot.kordRef.getGuildOrNull(Snowflake(gid.toLong())) ?: return false
val role = guild.getRoleOrNull(Snowflake(rid.toLong())) ?: return false
role.delete()
return true
}
}