Skip to content

打乱更新 ScramblePlugin #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
44 changes: 37 additions & 7 deletions src/main/kotlin/net/lz1998/zbot/plugin/ScramblePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import java.net.URLEncoder
@SwitchFilter("打乱")
class ScramblePlugin : BotPlugin() {

var ins = ""
var rate = 0
Copy link
Member

Choose a reason for hiding this comment

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

共享变量需要考虑线程安全问题


@Autowired
lateinit var scrambleService: ScrambleService

Expand All @@ -42,15 +45,31 @@ class ScramblePlugin : BotPlugin() {
val groupId = event.groupId
val rawMsg = event.rawMessage

getInsAndRate(rawMsg)

for (puzzle in TNoodleEnum.values()) {
if (rawMsg == puzzle.instruction) {
if (ins == puzzle.instruction) {
return try {
val scramble = getScramble(puzzle)
val imgUrl = "http://${ServiceConfig.tnoodle}/view/${puzzle.shortName}.png?scramble=" + URLEncoder.encode(scramble, "utf-8")
Msg.builder()
if(rate == 1) {
val scramble = getScramble(puzzle)
val imgUrl =
"http://${ServiceConfig.tnoodle}/view/${puzzle.shortName}.png?scramble=" + URLEncoder.encode(
scramble,
"utf-8"
)
Msg.builder()
.text("${puzzle.showName}\n${scramble}\n")
.image(imgUrl)
.sendToGroup(bot, groupId)
}else if(ins == "2" || ins == "3" || ins == "py" || ins == "sk" || ins == "cl" || ins == "fm"){
var sendResult = StringBuilder()
for(i in 1..rate) {
sendResult = sendResult.append("\n").append(i).append(". ").append(getScramble(puzzle))
}
Msg.builder()
.text("${puzzle.showName}*$rate${sendResult}")
.sendToGroup(bot, groupId)
}
MESSAGE_BLOCK
} catch (e: IOException) {
e.printStackTrace()
Expand All @@ -61,7 +80,7 @@ class ScramblePlugin : BotPlugin() {
}

for (puzzle in SlidysimEnum.values()) {
if (rawMsg == puzzle.instruction) {
if (ins == puzzle.instruction) {
return try {
val scramble = scrambleService.getScrambleSlidysim(puzzle.n)
val retMsg = "${puzzle.showName}\n${scramble}"
Expand All @@ -75,7 +94,18 @@ class ScramblePlugin : BotPlugin() {
}
}
}

return MESSAGE_IGNORE
}
}
fun getInsAndRate(str: String) {
val matchIns = Regex("[A-Za-z0-9]+")
val matchRate = Regex("([*])([0-9]+)")
val resultIns = matchIns.find(str)?.value ?: str
val resultRate = matchRate.find(str)?.value
ins = resultIns
if (resultRate != null){
if(resultRate.substring(1).toInt() > 1 && resultRate.substring(1).toInt() < 6){
rate = resultRate.substring(1).toInt()
} else if (resultRate.substring(1).toInt() > 5 ) { rate = 5 }
}else rate = 1
}
}