Skip to content

Commit

Permalink
三人以上进行游戏时在群内广播,四人以上局分数翻倍,取消周末分数翻倍
Browse files Browse the repository at this point in the history
  • Loading branch information
CuteReimu committed Jun 12, 2024
1 parent f5819ca commit 3adcd17
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
17 changes: 13 additions & 4 deletions src/main/kotlin/Game.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import com.google.protobuf.util.JsonFormat
import io.netty.util.Timeout
import org.apache.logging.log4j.kotlin.logger
import java.io.IOException
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
Expand Down Expand Up @@ -172,15 +171,13 @@ class Game(val id: Int, totalPlayerCount: Int, val actorRef: ActorRef) {

fun end(declaredWinners: List<Player>?, winners: List<Player>?, forceEnd: Boolean = false) {
isEnd = true
val c = Calendar.getInstance(TimeZone.getTimeZone("GMT+8:00"))
val isDouble = c.get(Calendar.DAY_OF_WEEK) in listOf(Calendar.FRIDAY, Calendar.SATURDAY, Calendar.SUNDAY) &&
c.get(Calendar.HOUR_OF_DAY) in 18 until 24
val humanPlayers = players.filterIsInstance<HumanPlayer>()
val addScoreMap = HashMap<String, Int>()
val newScoreMap = HashMap<String, Int>()
if (declaredWinners != null && winners != null) {
if (players.size >= 5) {
if (winners.isNotEmpty() && winners.size < players.size) {
val isDouble = humanPlayers.size >= 4
val totalWinners = winners.sumOf { (Statistics.getScore(it) ?: 0).coerceIn(180..1900) }
val totalPlayers = players.sumOf { (Statistics.getScore(it!!) ?: 0).coerceIn(180..1900) }
val totalLoser = totalPlayers - totalWinners
Expand Down Expand Up @@ -425,6 +422,18 @@ class Game(val id: Int, totalPlayerCount: Int, val actorRef: ActorRef) {
val onlineCount: Int
get() = gameCache.values.sumOf { it.players.count { p -> p != null } }

val humanPlayerCount: Pair<Int, Int>
get() {
var roomCount = 0
var humanCount = 0
gameCache.values.forEach {
val c = it.players.count { p -> p is HumanPlayer }
humanCount += c
if (c > 0) roomCount++
}
return roomCount to humanCount
}

val inGameCount: Int
get() = gameCache.values.count { it.isStarted }

Expand Down
28 changes: 23 additions & 5 deletions src/main/kotlin/QQPusher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import org.apache.logging.log4j.kotlin.logger
import java.time.Duration
import java.util.concurrent.atomic.AtomicLong

object QQPusher {
private val mu = Mutex()
private val notifyQueueOnStart = HashSet<Long>()
private val notifyQueueOnEnd = HashSet<Long>()
private val lastPushTime = AtomicLong()
private val lastAtAllTime = AtomicLong()

fun addIntoNotifyQueue(qq: Long, onStart: Boolean) = runBlocking {
mu.withLock {
Expand All @@ -31,16 +34,29 @@ object QQPusher {
}

fun notifyStart() {
var atAll = false
var s: String? = null
val (r, h) = Game.humanPlayerCount
if (h >= 3) {
val now = System.currentTimeMillis()
val last = lastPushTime.get()
if (now - last >= 3600000 && lastPushTime.compareAndSet(last, now))
s = "当前有${h}位群友在${r}桌房间进行游戏"
val last2 = lastAtAllTime.get()
if (now - last2 >= 12 * 3600000 && lastAtAllTime.compareAndSet(last2, now))
atAll = true
}
val at = runBlocking {
mu.withLock {
notifyQueueOnStart.toLongArray().apply { notifyQueueOnStart.clear() }
}
}
if (at.isNotEmpty()) {
if (at.isNotEmpty() || s != null) {
s = s ?: "开了"
@OptIn(DelicateCoroutinesApi::class)
GlobalScope.launch {
try {
Config.PushQQGroups.forEach { sendGroupMessage(it, "开了", *at) }
Config.PushQQGroups.forEach { sendGroupMessage(it, s, atAll, *at) }
} catch (e: Throwable) {
logger.error("catch throwable", e)
}
Expand Down Expand Up @@ -91,15 +107,17 @@ object QQPusher {
@OptIn(DelicateCoroutinesApi::class)
GlobalScope.launch {
try {
Config.PushQQGroups.forEach { sendGroupMessage(it, text, *at) }
Config.PushQQGroups.forEach { sendGroupMessage(it, text, false, *at) }
} catch (e: Throwable) {
logger.error("catch throwable", e)
}
}
}

private fun sendGroupMessage(groupId: Long, message: String, vararg at: Long) {
val atStr = at.joinToString(separator = "") { "{\"type\":\"at\",\"data\":{\"qq\":$it}}," }
private fun sendGroupMessage(groupId: Long, message: String, atAll: Boolean, vararg at: Long) {
val atStr =
if (atAll) "{\"type\":\"at\",\"data\":{\"qq\":\"all\"}},"
else at.joinToString(separator = "") { "{\"type\":\"at\",\"data\":{\"qq\":\"$it\"}}," }
val postData = """{
"group_id":$groupId,
"message":[$atStr{"type":"text","data":{"text":"$message"}}]
Expand Down
12 changes: 0 additions & 12 deletions src/main/kotlin/ScoreFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,6 @@ object ScoreFactory : Logging {
}
}
}

val c = Calendar.getInstance(TimeZone.getTimeZone("GMT+8:00"))
val dayOfWeekName = when (c.get(Calendar.DAY_OF_WEEK)) {
Calendar.SUNDAY -> "星期日"
Calendar.MONDAY -> "星期一"
Calendar.TUESDAY -> "星期二"
Calendar.WEDNESDAY -> "星期三"
Calendar.THURSDAY -> "星期四"
Calendar.FRIDAY -> "星期五"
else -> "星期六"
}
logger.info("现在是$dayOfWeekName${c.get(Calendar.HOUR_OF_DAY)}")
}

private val playerCountCount = ConcurrentHashMap<Int, MutableList<PlayerGameCount>>()
Expand Down

0 comments on commit 3adcd17

Please sign in to comment.