Skip to content

Commit f955976

Browse files
committed
Fixed bug when accepting a new serial
1 parent 69a2a56 commit f955976

File tree

5 files changed

+82
-9
lines changed

5 files changed

+82
-9
lines changed

src/main/kotlin/org/vzbot/discord/commands/SerialCommand.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import org.simpleyaml.configuration.file.YamlConfiguration
1919
import org.vzbot.discord.components.PrinterSelection
2020
import org.vzbot.discord.restrictions.AdminRestriction
2121
import org.vzbot.discord.restrictions.TeamMemberRestriction
22+
import org.vzbot.discord.util.fetchFilesForSerial
2223
import org.vzbot.io.buildPrettyEmbed
2324
import org.vzbot.io.respondSuccess
2425
import org.vzbot.models.*
@@ -49,6 +50,21 @@ class SerialCommand: DiscordCommand() {
4950
}
5051
}
5152

53+
@DSubCommand("returns the files for the given serial number")
54+
class SerialFiles: DiscordSubCommand() {
55+
56+
@DCommandOption("serial id")
57+
var serialID: Int = -1
58+
59+
@Restricted(TeamMemberRestriction::class, "mustBeInTeam")
60+
override fun execute(actionSender: ActionSender) {
61+
val serialFiles = fetchFilesForSerial(serialID.toLong())
62+
actionSender.textChannel.channel.sendFiles(serialFiles.map { FileUpload.fromData(it) }).queue()
63+
64+
actionSender.respondSuccess("The files have been sent!")
65+
}
66+
}
67+
5268
@DSubCommand("Imports serial data from a provided .yml")
5369
class Import(): DiscordSubCommand() {
5470

src/main/kotlin/org/vzbot/discord/components/AcceptSerialRequestButton.kt

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import com.zellerfeld.zellerbotapi.discord.components.DiscordButton
77
import com.zellerfeld.zellerbotapi.discord.components.PermanentDiscordButton
88
import com.zellerfeld.zellerbotapi.discord.components.commands.actionsenders.ActionSender
99
import com.zellerfeld.zellerbotapi.discord.components.custom.ConfirmModal
10+
import kotlinx.coroutines.DelicateCoroutinesApi
11+
import kotlinx.coroutines.GlobalScope
12+
import kotlinx.coroutines.async
1013
import net.dv8tion.jda.api.entities.Message
1114
import net.dv8tion.jda.api.entities.emoji.Emoji
1215
import net.dv8tion.jda.api.interactions.components.ActionRow
@@ -16,13 +19,15 @@ import org.jetbrains.exposed.sql.transactions.transaction
1619
import org.vzbot.discord.commands.fetchSerialTicket
1720
import org.vzbot.discord.restrictions.TeamMemberRestriction
1821
import org.vzbot.discord.restrictions.TicketRestrictions
22+
import org.vzbot.discord.util.fetchFilesForSerial
1923
import org.vzbot.io.*
2024
import org.vzbot.models.SerialNumber
2125
import java.awt.Color
2226
import java.io.File
2327

2428
@DCButton
2529
class AcceptSerialRequestButton: PermanentDiscordButton("vz_accept_serial", DiscordButton(label = "Accept", buttonStyle = ButtonStyle.SUCCESS, emoji = Emoji.fromUnicode("U+1F44D"))) {
30+
@OptIn(DelicateCoroutinesApi::class)
2631
@Restricted(TeamMemberRestriction::class, "mustBeInTeam")
2732
@Restricted(TicketRestrictions::class, "validTicket")
2833
override fun execute(actionSender: ActionSender, hook: Message) {
@@ -58,6 +63,15 @@ class AcceptSerialRequestButton: PermanentDiscordButton("vz_accept_serial", Disc
5863
}
5964
}
6065

66+
GlobalScope.async {
67+
val coordinates = ticket.country?.randomCoordinates() ?: return@async
68+
69+
transaction {
70+
serialNumber.latitude = coordinates.first
71+
serialNumber.longitude = coordinates.second
72+
}
73+
}
74+
6175
transaction {
6276
ticket.open = false
6377
ticket.accepted = true
@@ -68,27 +82,23 @@ class AcceptSerialRequestButton: PermanentDiscordButton("vz_accept_serial", Disc
6882

6983
val channel = actionSender.textChannel
7084

71-
val serialID = serialNumber.id.value
85+
val serialID = transaction { serialNumber.id.value }
7286

7387
val embed = prettyEmbed("Application Accepted", "**Congratulations**. Your application has been accepted and you have been granted your new serial id. In the following messages, we will send you the files to print your serial badge. Welcome to the **VZParty!** Feel free to delete your ticket, when you have grabbed your filed.", Color.GREEN)
7488
embed.addField("Serial ID", serialID.toString(), false)
7589

7690
channel.sendEmbedWithText(ticketOwner.asMention, embed.build(), ActionRow.of(DeleteTicketButton()))
7791
channel.channel.manager.setName("closed-serial-${ticketOwner.effectiveName}").queue()
7892

93+
ZellerBot.mainGuild!!.modifyNickname(ticketOwner, "${ticketOwner.effectiveName} VZ.${serialID}").queue()
94+
7995
val announcementEmbed = prettyEmbed("New Serial! #$serialID", "The user ${ticketOwner.effectiveName} has just finished their $printer. Spread some VZLove!", Color.GREEN)
8096
announcementChannel.sendMessageEmbeds(announcementEmbed.build()).queue {
8197
announcementChannel.sendMessage(transaction { ticket.mediaURL }).queue()
8298
}
8399

84-
val serialBaseSTL = File(env[EnvVariables.VZ_SERIAL_BASE_PLATE_LOCATION], "plate.stl")
85-
val serialNumberSTL = File(env[EnvVariables.VZ_SERIAL_NUMBER_PLATES_LOCATION], "${serialID}.stl")
86-
87-
if (serialBaseSTL.exists() && serialNumberSTL.exists()) {
88-
channel.channel.sendFiles(FileUpload.fromData(serialNumberSTL), FileUpload.fromData(serialBaseSTL)).queue()
89-
} else {
90-
BotLogger.logError( "The serial application for ID: $serialID was just finished, but one of the files was not found! Please check this.")
91-
}
100+
val serialFiles = fetchFilesForSerial(serialNumber.serialID)
101+
channel.channel.sendFiles(serialFiles.map { FileUpload.fromData(it) }).queue()
92102

93103
sender.respondText("You have accepted this application. You have been rewarded +1 VZSocialCredit", true)
94104
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.vzbot.discord.util
2+
3+
import net.dv8tion.jda.api.utils.FileUpload
4+
import org.vzbot.io.BotLogger
5+
import org.vzbot.io.EnvVariables
6+
import org.vzbot.io.env
7+
import java.io.File
8+
9+
fun fetchFilesForSerial(serial: Long): List<File> {
10+
val serialBaseSTL = File(env[EnvVariables.VZ_SERIAL_BASE_PLATE_LOCATION], "plate.stl")
11+
val serialNumberSTL = File(env[EnvVariables.VZ_SERIAL_NUMBER_PLATES_LOCATION], "${serial}.stl")
12+
13+
println(serialBaseSTL.absolutePath)
14+
println(serialNumberSTL.absoluteFile)
15+
16+
if (serialBaseSTL.exists() && serialNumberSTL.exists()) {
17+
return listOf(serialBaseSTL, serialNumberSTL)
18+
} else {
19+
BotLogger.logError( "The serial application for ID: $serial was just finished, but one of the files was not found! Please check this.")
20+
}
21+
return listOf()
22+
}

src/main/kotlin/org/vzbot/models/Countries.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package org.vzbot.models
33
import io.ktor.client.*
44
import io.ktor.client.call.*
55
import io.ktor.client.request.*
6+
import kotlinx.coroutines.runBlocking
67
import kotlinx.serialization.Serializable
8+
import org.vzbot.plugins.geoClient
79
import kotlin.random.Random
810

911
/**
@@ -267,6 +269,24 @@ enum class Country(val code: String, val countryName: String) {
267269
return null
268270
}
269271

272+
fun randomCoordinates(): Pair<Double, Double>? {
273+
return runBlocking {
274+
val geometry = getLocation(geoClient) ?: run {
275+
return@runBlocking null
276+
}
277+
278+
var coordinates: Pair<Double, Double>? = null
279+
280+
for (geometries in geometry) {
281+
coordinates = randomPointInPolygon(geometry.random())
282+
283+
if (coordinates != null) break
284+
}
285+
286+
return@runBlocking coordinates
287+
}
288+
}
289+
270290
private fun pointInPolygon(lat: Double, lon: Double, polygon: List<Geometry>): Boolean {
271291
var inside = false
272292
val n = polygon.size

taskfile.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ tasks:
1616
cmds:
1717
- docker compose -f docker-compose.dev.yml up -d
1818

19+
stop-dev:
20+
desc: "Start the Docker Compose development environment"
21+
cmds:
22+
- docker compose -f docker-compose.dev.yml down
23+
1924
dev:
2025
desc: "Build the project with Gradle and boot the Docker Compose dev environment"
2126
cmds:

0 commit comments

Comments
 (0)