Skip to content

Commit

Permalink
调整人机限制和人机分数规则
Browse files Browse the repository at this point in the history
  • Loading branch information
CuteReimu committed Oct 31, 2023
1 parent ef8cb93 commit e14359e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 40 deletions.
47 changes: 29 additions & 18 deletions src/main/kotlin/Game.kt
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,30 @@ class Game private constructor(totalPlayerCount: Int) {
}))
}

val affectScore: Boolean
get() = players.filterNotNull().run {
size >= 5 && (all { it is HumanPlayer } || all {
it !is HumanPlayer || (Statistics.getScore(it.playerName) ?: 0) < 60
})
}

fun end(declaredWinners: List<Player>?, winners: List<Player>?, forceEnd: Boolean = false) {
isEnd = true
GameCache.remove(id)
val humanPlayers = players.filterIsInstance<HumanPlayer>()
val addScoreMap = HashMap<String, Int>()
val newScoreMap = HashMap<String, Int>()
if (declaredWinners != null && winners != null) {
if (players.size == humanPlayers.size && players.size >= 5) {
if (affectScore) {
if (winners.isNotEmpty() && winners.size < players.size) {
val totalWinners = winners.sumOf { (Statistics.getScore(it.playerName) ?: 0).coerceIn(180..1900) }
val totalPlayers = players.sumOf { (Statistics.getScore(it!!.playerName) ?: 0).coerceIn(180..1900) }
val totalLoser = totalPlayers - totalWinners
val delta = totalLoser / (players.size - winners.size) - totalWinners / winners.size
for ((i, p) in humanPlayers.withIndex()) {
val score = p.calScore(players.filterNotNull(), winners, delta / 10)
val score = p.calScore(players.filterNotNull(), winners, delta / 10).let {
if (players.size == humanPlayers.size) it else it.coerceAtMost(1)
}
val (newScore, deltaScore) = Statistics.updateScore(
p.playerName,
score,
Expand All @@ -214,25 +223,27 @@ class Game private constructor(totalPlayerCount: Int) {
newScoreMap[p.playerName] = newScore
}
}
val records = ArrayList<Statistics.Record>(players.size)
val playerGameResultList = ArrayList<PlayerGameResult>()
for (p in players) {
val win = p!! in winners
records.add(
Statistics.Record(
p.originRole,
win,
p.originIdentity,
p.originSecretTask,
players.size
if (players.size == humanPlayers.size) {
val records = ArrayList<Statistics.Record>(players.size)
val playerGameResultList = ArrayList<PlayerGameResult>()
for (p in players) {
val win = p!! in winners
records.add(
Statistics.Record(
p.originRole,
win,
p.originIdentity,
p.originSecretTask,
players.size
)
)
)
if (p is HumanPlayer) playerGameResultList.add(PlayerGameResult(p.playerName, win))
if (p is HumanPlayer) playerGameResultList.add(PlayerGameResult(p.playerName, win))
}
Statistics.add(records)
Statistics.addPlayerGameCount(playerGameResultList)
MiraiPusher.push(this, declaredWinners, winners, addScoreMap, newScoreMap)
}
Statistics.add(records)
Statistics.addPlayerGameCount(playerGameResultList)
Statistics.calculateRankList()
MiraiPusher.push(this, declaredWinners, winners, addScoreMap, newScoreMap)
}
this.players.forEach { it!!.notifyWin(declaredWinners, winners, addScoreMap, newScoreMap) }
}
Expand Down
25 changes: 12 additions & 13 deletions src/main/kotlin/handler/add_robot_tos.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ class add_robot_tos : AbstractProtoHandler<Fengsheng.add_robot_tos>() {
return
}
if (!Config.IsGmEnable) {
val score = Statistics.getScore(r.playerName) ?: 0
if (score <= 0) {
val now = System.currentTimeMillis()
val startTrialTime = Statistics.getTrialStartTime(r.playerName)
if (startTrialTime == 0L) {
Statistics.setTrialStartTime(r.playerName, now)
} else if (now - 3 * 24 * 3600 * 1000 >= startTrialTime) {
r.sendErrorMessage("您已被禁止添加机器人,多参与群内活动即可解锁")
return
}
}
val humanCount = r.game!!.players.count { it is HumanPlayer }
if (humanCount >= 2) {
// val score = Statistics.getScore(r.playerName) ?: 0
// if (score <= 0) {
// val now = System.currentTimeMillis()
// val startTrialTime = Statistics.getTrialStartTime(r.playerName)
// if (startTrialTime == 0L) {
// Statistics.setTrialStartTime(r.playerName, now)
// } else if (now - 3 * 24 * 3600 * 1000 >= startTrialTime) {
// r.sendErrorMessage("您已被禁止添加机器人,多参与群内活动即可解锁")
// return
// }
// }
if (r.game!!.affectScore) {
r.sendErrorMessage("房间内有其他玩家,禁止添加机器人")
return
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/handler/heart_tos.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package com.fengsheng.handler

import com.fengsheng.Game
import com.fengsheng.HumanPlayer
import com.fengsheng.protos.Fengsheng
import com.fengsheng.protos.Fengsheng.heart_toc
import com.google.protobuf.GeneratedMessageV3

class heart_tos : ProtoHandler {
override fun handle(player: HumanPlayer, message: GeneratedMessageV3) {
val buf = Fengsheng.heart_toc.newBuilder().setOnlineCount(Game.playerNameCache.size).build().toByteArray()
player.send("heart_toc", buf, true)
val c = Game.GameCache.values.sumOf { it.players.size } + (player.game?.players?.count { it != null } ?: 0)
val builder = heart_toc.newBuilder().setOnlineCount(c)
player.send("heart_toc", builder.build().toByteArray(), true)
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/handler/join_room_tos.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class join_room_tos : ProtoHandler {
player.game = newGame
val builder = Fengsheng.get_room_info_toc.newBuilder()
builder.myPosition = player.location
builder.onlineCount = Game.playerNameCache.size
builder.onlineCount = Game.GameCache.values.sumOf { it.players.size } + newGame.players.count { it != null }
for (p in player.game!!.players) {
if (p == null) {
builder.addNames("")
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/handler/remove_one_position_tos.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fengsheng.handler

import com.fengsheng.Config
import com.fengsheng.HumanPlayer
import com.fengsheng.protos.Fengsheng
import org.apache.log4j.Logger
Expand All @@ -21,6 +22,10 @@ class remove_one_position_tos : AbstractProtoHandler<Fengsheng.remove_one_positi
r.sendErrorMessage("至少2人局")
return
}
if (!Config.IsGmEnable && oldPlayers.size <= 5) {
r.sendErrorMessage("至少5人局")
return
}
val players = oldPlayers.filterIndexed { i, _ -> i != index }.toTypedArray()
r.game!!.players = players
players.forEachIndexed { i, p ->
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/network/ProtoServerChannelHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ProtoServerChannelHandler : SimpleChannelInboundHandler<ByteBuf>() {
GameExecutor.post(game) {
if (player.game !== game) return@post
if (game.isStarted) {
if (game.players.all { it !is HumanPlayer || !it.isActive })
if (game.players.all { it !is HumanPlayer || !it.isActive } && !game.affectScore)
game.end(null, null)
else
player.notifyPlayerUpdateStatus()
Expand Down
6 changes: 2 additions & 4 deletions src/main/kotlin/network/WebSocketServerChannelHandler.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.fengsheng.network

import com.fengsheng.Game
import com.fengsheng.GameExecutor
import com.fengsheng.HumanPlayer
import com.fengsheng.*
import com.fengsheng.handler.ProtoHandler
import com.fengsheng.protos.Fengsheng
import com.fengsheng.protos.Fengsheng.leave_room_toc
Expand Down Expand Up @@ -92,7 +90,7 @@ class WebSocketServerChannelHandler : SimpleChannelInboundHandler<WebSocketFrame
GameExecutor.post(game) {
if (player.game !== game) return@post
if (game.isStarted) {
if (game.players.all { it !is HumanPlayer || !it.isActive })
if (game.players.all { it !is HumanPlayer || !it.isActive } && !game.affectScore)
game.end(null, null)
else
player.notifyPlayerUpdateStatus()
Expand Down

0 comments on commit e14359e

Please sign in to comment.