mirror of
https://github.com/jie65535/mirai-console-jhr-plugin.git
synced 2025-06-02 17:39:16 +08:00
增加 比赛定时自动开始
调整 现在当多匹马同时获胜,将同时得到奖励
This commit is contained in:
parent
9236f3bd21
commit
1f834cd36a
@ -62,6 +62,12 @@ object JHRPluginConfig : AutoSavePluginConfig("HorseRacingPluginConfig") {
|
|||||||
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自动开始时间(s)
|
||||||
|
*/
|
||||||
|
@ValueDescription("自动开始时间(s)")
|
||||||
|
var autoStartTime by value(180)
|
||||||
}
|
}
|
||||||
// @ValueDescription("赛马数量")
|
// @ValueDescription("赛马数量")
|
||||||
// val horseCount by value(5)
|
// val horseCount by value(5)
|
@ -7,11 +7,15 @@ import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
|
|||||||
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregister
|
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregister
|
||||||
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
|
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
|
||||||
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
|
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
|
||||||
|
import net.mamoe.mirai.contact.Group
|
||||||
import net.mamoe.mirai.contact.isOperator
|
import net.mamoe.mirai.contact.isOperator
|
||||||
import net.mamoe.mirai.event.GlobalEventChannel
|
import net.mamoe.mirai.event.GlobalEventChannel
|
||||||
import net.mamoe.mirai.event.events.*
|
import net.mamoe.mirai.event.events.GroupMessageEvent
|
||||||
import net.mamoe.mirai.message.data.*
|
import net.mamoe.mirai.event.events.MessageEvent
|
||||||
|
import net.mamoe.mirai.message.data.At
|
||||||
|
import net.mamoe.mirai.message.data.MessageChainBuilder
|
||||||
import net.mamoe.mirai.message.data.MessageSource.Key.quote
|
import net.mamoe.mirai.message.data.MessageSource.Key.quote
|
||||||
|
import net.mamoe.mirai.message.data.PlainText
|
||||||
import net.mamoe.mirai.utils.info
|
import net.mamoe.mirai.utils.info
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.random.Random
|
import kotlin.random.Random
|
||||||
@ -66,7 +70,6 @@ object JHorseRacing : KotlinPlugin(
|
|||||||
"\uD83D\uDC1C"
|
"\uD83D\uDC1C"
|
||||||
)
|
)
|
||||||
private val ranks = mutableMapOf<Long, Rank>()
|
private val ranks = mutableMapOf<Long, Rank>()
|
||||||
private fun newRank(job: Job) = Rank(List(horseCount) { Horse(Random.nextInt(horseTypes.size)) }, job)
|
|
||||||
private fun drawHorse(horses: List<Horse>): String {
|
private fun drawHorse(horses: List<Horse>): String {
|
||||||
val sb = StringBuilder()
|
val sb = StringBuilder()
|
||||||
for ((i, horse) in horses.withIndex()) {
|
for ((i, horse) in horses.withIndex()) {
|
||||||
@ -80,6 +83,69 @@ object JHorseRacing : KotlinPlugin(
|
|||||||
}
|
}
|
||||||
return sb.toString()
|
return sb.toString()
|
||||||
}
|
}
|
||||||
|
private suspend fun startRank(subject: Group) {
|
||||||
|
if (ranks[subject.id] != null) return
|
||||||
|
subject.sendMessage("赛马开始辣,走过路过不要错过")
|
||||||
|
val rank = Rank(List(horseCount) { Horse(Random.nextInt(horseTypes.size)) }, Job())
|
||||||
|
ranks[subject.id] = rank
|
||||||
|
subject.sendMessage(drawHorse(rank.horses))
|
||||||
|
launch(rank.job) {
|
||||||
|
val winners = mutableListOf<Int>()
|
||||||
|
while (winners.size == 0) {
|
||||||
|
delay(Random.nextLong(1000) + 2000)
|
||||||
|
// 比赛事件触发
|
||||||
|
val steps = (1..3).random() //事件触发前进或后退随机大小
|
||||||
|
val eventHorseIndex = Random.nextInt(rank.horses.size)
|
||||||
|
val eventHorse = rank.horses[eventHorseIndex]
|
||||||
|
val eventMsg = if (Random.nextInt(77) > 32) {
|
||||||
|
eventHorse.position += steps
|
||||||
|
JHRPluginConfig.goodEvents[Random.nextInt(JHRPluginConfig.goodEvents.size)]
|
||||||
|
} else {
|
||||||
|
eventHorse.position -= steps
|
||||||
|
JHRPluginConfig.badEvents[Random.nextInt(JHRPluginConfig.badEvents.size)]
|
||||||
|
}
|
||||||
|
val number = (eventHorseIndex + 1).toString()
|
||||||
|
subject.sendMessage(
|
||||||
|
eventMsg
|
||||||
|
.replace("?", number)
|
||||||
|
.replace("?", number)
|
||||||
|
)
|
||||||
|
|
||||||
|
// 所有马前进
|
||||||
|
for ((i, horse) in rank.horses.withIndex()) {
|
||||||
|
if (++horse.position >= lapLength) {
|
||||||
|
winners.add(i + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
subject.sendMessage(drawHorse(rank.horses))
|
||||||
|
|
||||||
|
delay(Random.nextLong(1000) + 3000)
|
||||||
|
}
|
||||||
|
val mb = MessageChainBuilder()
|
||||||
|
if (winners.size == 1) {
|
||||||
|
mb.add("${winners[0]}最终赢得了胜利,让我们为它鼓掌")
|
||||||
|
} else {
|
||||||
|
mb.add("${winners.joinToString()}一起赢得了胜利,让我们为它们鼓掌")
|
||||||
|
}
|
||||||
|
ranks.remove(subject.id)
|
||||||
|
val pool = pools.remove(subject.id)
|
||||||
|
if (pool != null && pool.size > 0) {
|
||||||
|
for (bet in pool) {
|
||||||
|
val score = JHRPluginData.Scores[bet.id]!!
|
||||||
|
val income = if (winners.indexOf(bet.number) != -1) {
|
||||||
|
(bet.score * 1.5).toInt()
|
||||||
|
} else {
|
||||||
|
-bet.score
|
||||||
|
}
|
||||||
|
JHRPluginData.Scores[bet.id] = score + income
|
||||||
|
mb.add("\n")
|
||||||
|
mb.add(At(bet.id))
|
||||||
|
mb.add(PlainText("收益${income}"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
subject.sendMessage(mb.asMessageChain())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
@ -107,69 +173,17 @@ object JHorseRacing : KotlinPlugin(
|
|||||||
if (pools[subject.id] != null) {
|
if (pools[subject.id] != null) {
|
||||||
subject.sendMessage("已经有比赛在进行了")
|
subject.sendMessage("已经有比赛在进行了")
|
||||||
} else {
|
} else {
|
||||||
pools[subject.id] = mutableListOf()
|
val pool = mutableListOf<Bet>()
|
||||||
subject.sendMessage("赛马比赛开盘,有钱交钱妹钱交人")
|
pools[subject.id] = pool
|
||||||
}
|
subject.sendMessage("赛马比赛开盘,有钱交钱妹钱交人。\n${JHRPluginConfig.autoStartTime}秒后将自动开始")
|
||||||
}
|
launch {
|
||||||
msg.startsWith("开始赛马") -> {
|
delay(JHRPluginConfig.autoStartTime * 1000L)
|
||||||
if (ranks[subject.id] != null) return@subscribeAlways
|
if (pools[subject.id] == pool)
|
||||||
subject.sendMessage("赛马开始辣,走过路过不要错过")
|
startRank(subject)
|
||||||
val rank = newRank(Job())
|
}
|
||||||
ranks[subject.id] = rank
|
|
||||||
subject.sendMessage(drawHorse(rank.horses))
|
|
||||||
launch(rank.job) {
|
|
||||||
var winner = -1
|
|
||||||
while (winner == -1) {
|
|
||||||
delay(Random.nextLong(1000) + 2000)
|
|
||||||
// 比赛事件触发
|
|
||||||
val steps = (1..3).random() //事件触发前进或后退随机大小
|
|
||||||
val eventHorseIndex = Random.nextInt(rank.horses.size)
|
|
||||||
val eventHorse = rank.horses[eventHorseIndex]
|
|
||||||
val eventMsg = if (Random.nextInt(77) > 32) {
|
|
||||||
eventHorse.position += steps
|
|
||||||
JHRPluginConfig.goodEvents[Random.nextInt(JHRPluginConfig.goodEvents.size)]
|
|
||||||
} else {
|
|
||||||
eventHorse.position -= steps
|
|
||||||
JHRPluginConfig.badEvents[Random.nextInt(JHRPluginConfig.badEvents.size)]
|
|
||||||
}
|
|
||||||
val number = (eventHorseIndex + 1).toString()
|
|
||||||
subject.sendMessage(
|
|
||||||
eventMsg
|
|
||||||
.replace("?", number)
|
|
||||||
.replace("?", number)
|
|
||||||
)
|
|
||||||
|
|
||||||
// 所有马前进
|
|
||||||
for ((i, horse) in rank.horses.withIndex()) {
|
|
||||||
if (++horse.position >= lapLength) {
|
|
||||||
winner = i + 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
subject.sendMessage(drawHorse(rank.horses))
|
|
||||||
|
|
||||||
delay(Random.nextLong(1000) + 3000)
|
|
||||||
}
|
|
||||||
val mb = MessageChainBuilder()
|
|
||||||
mb.add("${winner}最终赢得了胜利,让我们为它鼓掌")
|
|
||||||
ranks.remove(subject.id)
|
|
||||||
val pool = pools.remove(subject.id)
|
|
||||||
if (pool != null && pool.size > 0) {
|
|
||||||
for (bet in pool) {
|
|
||||||
val score = JHRPluginData.Scores[bet.id]!!
|
|
||||||
val income = if (bet.number == winner) {
|
|
||||||
(bet.score * 1.5).toInt()
|
|
||||||
} else {
|
|
||||||
-bet.score
|
|
||||||
}
|
|
||||||
JHRPluginData.Scores[bet.id] = score + income
|
|
||||||
mb.add("\n")
|
|
||||||
mb.add(At(bet.id))
|
|
||||||
mb.add(PlainText("收益${income}"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
subject.sendMessage(mb.asMessageChain())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
msg.startsWith("开始赛马") -> launch { startRank(subject) }
|
||||||
msg == "关闭赛马" -> {
|
msg == "关闭赛马" -> {
|
||||||
if (sender.permission.isOperator()) {
|
if (sender.permission.isOperator()) {
|
||||||
JHRPluginConfig.enabledGroups.remove(subject.id)
|
JHRPluginConfig.enabledGroups.remove(subject.id)
|
||||||
|
Loading…
Reference in New Issue
Block a user