mirror of
https://github.com/jie65535/mirai-console-j24-plugin.git
synced 2025-06-01 17:29:15 +08:00
Update version to v0.1.2
修改为以群为单位答题,答对后出下一题,增加答题计时 支持更多中文符号
This commit is contained in:
parent
2bed56e1b5
commit
cf51506432
@ -7,7 +7,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = "top.jie65535"
|
||||
version = "0.1.1"
|
||||
version = "0.1.2"
|
||||
|
||||
repositories {
|
||||
maven("https://maven.aliyun.com/repository/public") // 阿里云国内代理仓库
|
||||
|
@ -4,14 +4,17 @@ import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
|
||||
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
|
||||
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
|
||||
import net.mamoe.mirai.event.GlobalEventChannel
|
||||
import net.mamoe.mirai.event.events.GroupMessageEvent
|
||||
import net.mamoe.mirai.event.subscribeGroupMessages
|
||||
import net.mamoe.mirai.utils.info
|
||||
import java.time.Duration
|
||||
import java.time.LocalDateTime
|
||||
|
||||
object PluginMain : KotlinPlugin(
|
||||
JvmPluginDescription(
|
||||
id = "top.jie65535.j24",
|
||||
name = "J 24点游戏",
|
||||
version = "0.1.1"
|
||||
version = "0.1.2"
|
||||
) {
|
||||
author("jie65535")
|
||||
info("24点游戏")
|
||||
@ -27,23 +30,26 @@ object PluginMain : KotlinPlugin(
|
||||
PluginConfig.reload()
|
||||
|
||||
val eventChannel = GlobalEventChannel.parentScope(this)
|
||||
eventChannel.subscribeGroupMessages {
|
||||
startsWith("24点") and content { PluginConfig.enabledGroups.contains(this.group.id) } quoteReply {
|
||||
val game = Point24()
|
||||
games[this.sender.id] = game
|
||||
"请用 [${game.points[0]}] [${game.points[1]}] [${game.points[2]}] [${game.points[3]}] 组成结果为24的算式,以'$prefix'开头验证"
|
||||
eventChannel.filter { it is GroupMessageEvent && PluginConfig.enabledGroups.contains(it.group.id) }.subscribeGroupMessages {
|
||||
startsWith("24点") reply {
|
||||
var game = games[group.id]
|
||||
if (game == null || game.time.plusMinutes(1) < LocalDateTime.now()) {
|
||||
game = Point24()
|
||||
games[group.id] = game
|
||||
"请用 $game 组成结果为24的算式,以'$prefix'开头验证"
|
||||
} else Unit
|
||||
}
|
||||
|
||||
startsWith(prefix) and content { PluginConfig.enabledGroups.contains(this.group.id) } quoteReply {
|
||||
val game = games[sender.id]
|
||||
if (game == null) {
|
||||
"你还没有抽数字哦,说“24点”来开始游戏吧"
|
||||
} else {
|
||||
startsWith(prefix) quoteReply {
|
||||
val game = games[group.id]
|
||||
if (game != null) {
|
||||
try {
|
||||
val result = game.evaluate(message.contentToString().removePrefix(prefix).trim())
|
||||
if (result == 24.0) {
|
||||
games.remove(sender.id)
|
||||
"厉害,答对了!"
|
||||
val newGame = Point24()
|
||||
games[group.id] = newGame
|
||||
val duration = Duration.between(game.time, LocalDateTime.now())
|
||||
"答对了! ${duration.toMinutes()}:${duration.toSecondsPart()}\n下一题:$newGame"
|
||||
} else {
|
||||
"答错了,计算结果为 $result"
|
||||
}
|
||||
@ -51,7 +57,7 @@ object PluginMain : KotlinPlugin(
|
||||
// logger.error(e)
|
||||
"错误:${e.message}"
|
||||
}
|
||||
}
|
||||
} else Unit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import net.objecthunter.exp4j.operator.Operator
|
||||
import net.objecthunter.exp4j.shuntingyard.ShuntingYard
|
||||
import net.objecthunter.exp4j.tokenizer.NumberToken
|
||||
import net.objecthunter.exp4j.tokenizer.Token
|
||||
import java.time.LocalDateTime
|
||||
import kotlin.random.Random
|
||||
|
||||
class Point24 {
|
||||
@ -35,6 +36,15 @@ class Point24 {
|
||||
return (args[0].toInt() or args[1].toInt()).toDouble()
|
||||
}
|
||||
},
|
||||
// 阶乘禁用,因为这会让游戏从24点变成4点
|
||||
// object : Operator("!", 1, true, Operator.PRECEDENCE_POWER) {
|
||||
// override fun apply(vararg args: Double): Double {
|
||||
// var sum = 1
|
||||
// for (i in 2..args[0].toInt())
|
||||
// sum *= i
|
||||
// return sum.toDouble()
|
||||
// }
|
||||
// },
|
||||
)
|
||||
private val myOperatorMap: Map<String, Operator>
|
||||
init {
|
||||
@ -46,10 +56,7 @@ class Point24 {
|
||||
}
|
||||
|
||||
var points = genPoints()
|
||||
|
||||
fun regenPoints() {
|
||||
points = genPoints()
|
||||
}
|
||||
var time: LocalDateTime = LocalDateTime.now()
|
||||
|
||||
private fun genPoints() = arrayOf(
|
||||
Random.nextInt(1, 14),
|
||||
@ -58,8 +65,21 @@ class Point24 {
|
||||
Random.nextInt(1, 14)
|
||||
)
|
||||
|
||||
override fun toString() = "[${points[0]}] [${points[1]}] [${points[2]}] [${points[3]}]"
|
||||
|
||||
fun evaluate(expression: String): Double {
|
||||
val expr = expression.replace('(', '(').replace(')', ')')
|
||||
val expr = expression
|
||||
.replace('(', '(')
|
||||
.replace(')', ')')
|
||||
.replace('x', '*')
|
||||
.replace('×', '*')
|
||||
.replace('÷', '/')
|
||||
.replace('-', '-')
|
||||
.replace('+', '+')
|
||||
.replace('!', '!')
|
||||
.replace('<', '<')
|
||||
.replace('>', '>')
|
||||
|
||||
|
||||
if (expr.contains('%'))
|
||||
throw IllegalArgumentException("禁止使用%运算符")
|
||||
@ -72,6 +92,7 @@ class Point24 {
|
||||
false
|
||||
)
|
||||
|
||||
var usedAll = true
|
||||
val nums = points.toMutableList()
|
||||
for (token in tokens) {
|
||||
if (token.type == Token.TOKEN_NUMBER.toInt()) {
|
||||
@ -84,18 +105,24 @@ class Point24 {
|
||||
if (i < nums.size)
|
||||
nums.removeAt(i)
|
||||
else
|
||||
throw IllegalArgumentException("不能使用未得到的数值")
|
||||
usedAll = false
|
||||
// throw IllegalArgumentException("不能使用未得到的数值")
|
||||
} else if (token.type == Token.TOKEN_FUNCTION.toInt()) {
|
||||
throw IllegalArgumentException("禁止使用函数哦")
|
||||
}
|
||||
}
|
||||
if (nums.isNotEmpty())
|
||||
throw IllegalArgumentException("必须使用所有数值")
|
||||
usedAll = false
|
||||
// throw IllegalArgumentException("必须使用所有数值")
|
||||
|
||||
return ExpressionBuilder(expr)
|
||||
val result = ExpressionBuilder(expr)
|
||||
.operator(myOperators)
|
||||
.implicitMultiplication(false)
|
||||
.build()
|
||||
.evaluate()
|
||||
if (usedAll)
|
||||
return result
|
||||
else
|
||||
throw IllegalArgumentException("结果为$result,请使用系统生成的数值!")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user