Compare commits

..

No commits in common. "ed5ee331262a2e17b059fe0cbb3b8462f7824f7d" and "05c115e3e856127e94bb0f60f2f7313e0ecb6403" have entirely different histories.

3 changed files with 26 additions and 67 deletions

View File

@ -1,13 +1,13 @@
plugins { plugins {
val kotlinVersion = "1.8.10" val kotlinVersion = "1.7.0"
kotlin("jvm") version kotlinVersion kotlin("jvm") version kotlinVersion
kotlin("plugin.serialization") version kotlinVersion kotlin("plugin.serialization") version kotlinVersion
id("net.mamoe.mirai-console") version "2.16.0" id("net.mamoe.mirai-console") version "2.12.0"
} }
group = "top.jie65535" group = "top.jie65535"
version = "1.0.0" version = "0.1.3"
repositories { repositories {
maven("https://maven.aliyun.com/repository/public") // 阿里云国内代理仓库 maven("https://maven.aliyun.com/repository/public") // 阿里云国内代理仓库

View File

@ -9,14 +9,14 @@ import net.mamoe.mirai.event.events.GroupMessageEvent
import net.mamoe.mirai.event.subscribeGroupMessages import net.mamoe.mirai.event.subscribeGroupMessages
import net.mamoe.mirai.utils.info import net.mamoe.mirai.utils.info
import java.text.DecimalFormat import java.text.DecimalFormat
import java.time.* import java.time.Duration
import kotlin.math.max import java.time.LocalDateTime
object PluginMain : KotlinPlugin( object PluginMain : KotlinPlugin(
JvmPluginDescription( JvmPluginDescription(
id = "top.jie65535.j24", id = "top.jie65535.j24",
name = "J 24点游戏", name = "J 24点游戏",
version = "1.0.0" version = "0.1.3"
) { ) {
author("jie65535") author("jie65535")
info("24点游戏") info("24点游戏")
@ -27,26 +27,12 @@ object PluginMain : KotlinPlugin(
private val df = DecimalFormat("0.00") private val df = DecimalFormat("0.00")
private const val prefix = "=" 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 rankTimes = mutableMapOf<Long, LocalDateTime>()
)
private val gameStats = mutableMapOf<Long, GroupState>()
override fun onEnable() { override fun onEnable() {
logger.info { "Plugin loaded" } logger.info { "Plugin loaded" }
@ -59,30 +45,19 @@ object PluginMain : KotlinPlugin(
.subscribeGroupMessages { .subscribeGroupMessages {
"24点" reply { "24点" reply {
var game = games[group.id] var game = games[group.id]
val state = gameStats.getOrPut(group.id) { GroupState() } if (game == null || game.time.plusMinutes(1) < LocalDateTime.now()) {
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() game = Point24()
games[group.id] = game games[group.id] = game
state.lastGameTimes++ "请用 $game 组成结果为24的算式以'$prefix'开头验证"
"{${state.lastGameTimes}/$maxRound} 请用 $game 组成结果为24的算式以'$prefix'开头验证"
} else Unit } else Unit
} }
"24点榜" reply aaa@{ "24点榜" reply {
val g = PluginData.stats[group.id] ?: return@aaa Unit val g = PluginData.stats[group.id]
val state = gameStats.getOrPut(group.id) { GroupState() }
// 一小时内仅可查询一次 // 一小时内仅可查询一次
if (state.lastRankedAt.plusHours(1).isBefore(LocalDateTime.now())) { if (g != null && rankTimes[group.id]?.plusHours(1)?.isBefore(LocalDateTime.now()) != false) {
// 记录查询时间 // 记录查询时间
state.lastRankedAt = LocalDateTime.now() rankTimes[group.id] = LocalDateTime.now()
// 拼接排行榜 // 拼接排行榜
val sb = StringBuilder() val sb = StringBuilder()
sb.appendLine("[均时榜]") sb.appendLine("[均时榜]")
@ -105,14 +80,11 @@ object PluginMain : KotlinPlugin(
val game = games[group.id] val game = games[group.id]
if (game != null) { if (game != null) {
try { try {
val now = LocalDateTime.now()
val result = game.evaluate(message.contentToString().removePrefix(prefix).trim()) val result = game.evaluate(message.contentToString().removePrefix(prefix).trim())
if (result == 24.0) { if (result == 24.0) {
val duration = Duration.between(game.time, now) val newGame = Point24()
// Instant.ofEpochSecond( games[group.id] = newGame
// this.time.toLong()) val duration = Duration.between(game.time, LocalDateTime.now())
// .atZone(ZoneId.systemDefault())
// .toLocalDateTime())
// 群 // 群
var g = PluginData.stats[group.id] var g = PluginData.stats[group.id]
@ -133,30 +105,18 @@ object PluginMain : KotlinPlugin(
if (stat.minTime > t) { if (stat.minTime > t) {
stat.minTime = t stat.minTime = t
} }
// 仅统计一定时间内的均值 // 仅统计百秒内的平均值
val resultText = if (t < 60) { if (t < 100) {
// 计数增加 // 计数增加
stat.count += 1 stat.count += 1
// 更新均值 // 更新均值
stat.avgTime += (t - stat.avgTime) / max(20, stat.count) stat.avgTime += (t - stat.avgTime) / stat.count
"答对了!用时:${df.format(t)}s 平均:${df.format(stat.avgTime)}s 最快:${df.format(stat.minTime)}s\n" "答对了!用时:${df.format(t)}s 平均:${df.format(stat.avgTime)}s 最快:${df.format(stat.minTime)}s\n" +
"下一题:$newGame"
} else { } 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 { } else {
"答错了,计算结果为 $result" "答错了,计算结果为 $result"
} }

View File

@ -71,7 +71,6 @@ class Point24 {
val expr = expression val expr = expression
.replace('', '(') .replace('', '(')
.replace('', ')') .replace('', ')')
.replace('X', '*')
.replace('x', '*') .replace('x', '*')
.replace('×', '*') .replace('×', '*')
.replace('÷', '/') .replace('÷', '/')