diff --git a/src/main/kotlin/PluginMain.kt b/src/main/kotlin/PluginMain.kt index bbaaa32..49bc4e5 100644 --- a/src/main/kotlin/PluginMain.kt +++ b/src/main/kotlin/PluginMain.kt @@ -9,14 +9,14 @@ import net.mamoe.mirai.event.events.GroupMessageEvent import net.mamoe.mirai.event.subscribeGroupMessages import net.mamoe.mirai.utils.info import java.text.DecimalFormat -import java.time.Duration -import java.time.LocalDateTime +import java.time.* +import kotlin.math.max object PluginMain : KotlinPlugin( JvmPluginDescription( id = "top.jie65535.j24", name = "J 24点游戏", - version = "0.1.3" + version = "1.0.0" ) { author("jie65535") info("24点游戏") @@ -27,12 +27,26 @@ object PluginMain : KotlinPlugin( private val df = DecimalFormat("0.00") private const val prefix = "=" + private const val maxRound = 10 + + class GroupState( + var lastGameTimes: Int = 1, + + /** + * 游戏计时器 + * 为了防止频繁刷屏,记录开始时间,一定间隔后才可再次触发 + */ + var lastStartedAt: LocalDateTime = LocalDateTime.MIN, + + /** + * 排行榜计时器 + * 为了防止频繁刷屏,记录出榜时间,一定间隔后才可再次触发 + */ + var lastRankedAt: LocalDateTime = LocalDateTime.MIN + ) + + private val gameStats = mutableMapOf() - /** - * 排行榜计时器 - * 为了防止频繁刷屏,记录出榜时间,一定间隔后才可再次触发 - */ - private val rankTimes = mutableMapOf() override fun onEnable() { logger.info { "Plugin loaded" } @@ -45,19 +59,30 @@ object PluginMain : KotlinPlugin( .subscribeGroupMessages { "24点" reply { var game = games[group.id] - if (game == null || game.time.plusMinutes(1) < LocalDateTime.now()) { + val state = gameStats.getOrPut(group.id) { GroupState() } + + if (state.lastStartedAt.plusMinutes(10).isBefore(LocalDateTime.now())) { + state.lastStartedAt = LocalDateTime.now() + state.lastGameTimes = 0 + } + + if (state.lastGameTimes >= maxRound) { + "您参与的太过频繁了,为避免影响他人聊天体验,请稍后再试~" + } else if (game == null || game.time.plusMinutes(1) < LocalDateTime.now()) { game = Point24() games[group.id] = game - "请用 $game 组成结果为24的算式,以'$prefix'开头验证" + state.lastGameTimes++ + "{${state.lastGameTimes}/$maxRound} 请用 $game 组成结果为24的算式,以'$prefix'开头验证" } else Unit } - "24点榜" reply { - val g = PluginData.stats[group.id] + "24点榜" reply aaa@{ + val g = PluginData.stats[group.id] ?: return@aaa Unit + val state = gameStats.getOrPut(group.id) { GroupState() } // 一小时内仅可查询一次 - if (g != null && rankTimes[group.id]?.plusHours(1)?.isBefore(LocalDateTime.now()) != false) { + if (state.lastRankedAt.plusHours(1).isBefore(LocalDateTime.now())) { // 记录查询时间 - rankTimes[group.id] = LocalDateTime.now() + state.lastRankedAt = LocalDateTime.now() // 拼接排行榜 val sb = StringBuilder() sb.appendLine("[均时榜]") @@ -80,11 +105,14 @@ object PluginMain : KotlinPlugin( val game = games[group.id] if (game != null) { try { + val now = LocalDateTime.now() val result = game.evaluate(message.contentToString().removePrefix(prefix).trim()) if (result == 24.0) { - val newGame = Point24() - games[group.id] = newGame - val duration = Duration.between(game.time, LocalDateTime.now()) + val duration = Duration.between(game.time, now) +// Instant.ofEpochSecond( +// this.time.toLong()) +// .atZone(ZoneId.systemDefault()) +// .toLocalDateTime()) // 群 var g = PluginData.stats[group.id] @@ -105,18 +133,30 @@ object PluginMain : KotlinPlugin( if (stat.minTime > t) { stat.minTime = t } - // 仅统计百秒内的平均值 - if (t < 100) { + // 仅统计一定时间内的均值 + val resultText = if (t < 60) { // 计数增加 stat.count += 1 // 更新均值 - stat.avgTime += (t - stat.avgTime) / stat.count + stat.avgTime += (t - stat.avgTime) / max(20, stat.count) - "答对了!用时:${df.format(t)}s 平均:${df.format(stat.avgTime)}s 最快:${df.format(stat.minTime)}s\n" + - "下一题:$newGame" + "答对了!用时:${df.format(t)}s 平均:${df.format(stat.avgTime)}s 最快:${df.format(stat.minTime)}s\n" } else { - "答对了!下一题:$newGame" + "回答正确!" } + + val state = gameStats[group.id]!! + val nextTip = if (state.lastGameTimes >= maxRound) { + games.remove(group.id) + "本轮游戏已经结束,感谢参与,请稍作休息~" + } else { + state.lastGameTimes++ + val newGame = Point24() + games[group.id] = newGame + "{${state.lastGameTimes}/$maxRound}下一题:$newGame" + } + + resultText + nextTip } else { "答错了,计算结果为 $result" } diff --git a/src/main/kotlin/Point24.kt b/src/main/kotlin/Point24.kt index 729552b..5054f26 100644 --- a/src/main/kotlin/Point24.kt +++ b/src/main/kotlin/Point24.kt @@ -71,6 +71,7 @@ class Point24 { val expr = expression .replace('(', '(') .replace(')', ')') + .replace('X', '*') .replace('x', '*') .replace('×', '*') .replace('÷', '/')