Skip to content

Commit 63ed21b

Browse files
committed
新增扩展包选择功能
1 parent dea2671 commit 63ed21b

10 files changed

Lines changed: 82 additions & 13 deletions

File tree

src/main/kotlin/Game.kt

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class Game(val id: Int, totalPlayerCount: Int, val actorRef: ActorRef) {
3939
@Volatile
4040
var isStarted = false
4141

42+
var extension = 3
43+
4244
@Volatile
4345
var isEnd = false
4446
private set
@@ -136,6 +138,14 @@ class Game(val id: Int, totalPlayerCount: Int, val actorRef: ActorRef) {
136138
isStarted = true
137139
players = players.shuffled()
138140
players.forEachIndexed { i, p -> p!!.location = i }
141+
val humanCount = players.count { it is HumanPlayer }
142+
extension = run {
143+
for (i in 3 downTo 1) {
144+
if (players.count { it is HumanPlayer && it.roomExtension >= i } * 2 >= humanCount) return@run i
145+
}
146+
3
147+
}
148+
139149
QQPusher.notifyStart()
140150
val identities = ArrayList<color>()
141151
when (players.size) {
@@ -217,12 +227,18 @@ class Game(val id: Int, totalPlayerCount: Int, val actorRef: ActorRef) {
217227
else -> players.size - 4
218228
}
219229
possibleSecretTasks = tasks.take(possibleSecretTaskCount.coerceAtMost(tasks.size)).shuffled()
230+
val n = if (extension == 1) players.size * 2 else players.size * 3
220231
val roleSkillsDataList = if (Config.IsGmEnable) RoleCache.getRandomRolesWithSpecific(
221-
players.size * 3,
222-
Config.DebugRoles
223-
) else RoleCache.getRandomRoles(players.size * 3)
232+
n,
233+
Config.DebugRoles,
234+
extension
235+
) else RoleCache.getRandomRoles(n, extension)
224236
resolve(WaitForSelectRole(this, players.indices.map {
225-
listOf(
237+
if (extension == 1) listOf(
238+
roleSkillsDataList[it],
239+
roleSkillsDataList[it + players.size],
240+
).filter { r -> r.role != unknown }.toMutableList()
241+
else listOf(
226242
roleSkillsDataList[it],
227243
roleSkillsDataList[it + players.size],
228244
roleSkillsDataList[it + players.size * 2]

src/main/kotlin/HumanPlayer.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ class HumanPlayer(var channel: Channel, var needWaitLoad: Boolean = false, val n
4747
@Volatile
4848
var isReconnecting = false
4949

50+
/** 扩展包,1-只有基础,2-基础一扩,3-全部 */
51+
@Volatile
52+
var roomExtension = 3
53+
set(value) {
54+
field = if (value in 1..3) value else 3
55+
}
56+
5057
/**
5158
* 游戏结束重置
5259
*/

src/main/kotlin/card/Deck.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class Deck(private val game: Game) {
125125
})
126126
}
127127

128-
fun init(totalPlayerCount: Int) {
128+
fun init(totalPlayerCount: Int, extension: Int) {
129129
cards.clear()
130130
if (totalPlayerCount <= 4) {
131131
cards.addAll(DefaultDeck.subList(0, 108))
@@ -140,6 +140,8 @@ class Deck(private val game: Game) {
140140
shiTanIndex.forEach { cards.removeAt(it) }
141141
}
142142
}
143+
if (extension < 3) cards = ArrayList(cards.filterNot { it is DiaoHuLiShan || it is YuQinGuZong })
144+
if (extension < 2) cards = ArrayList(cards.filterNot { it is FengYunBianHuan || it is MiLing })
143145
for (card in cards) {
144146
val index =
145147
if (card.colors.size == 1) card.colors.first().number * 4

src/main/kotlin/handler/JoinRoomTos.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ class JoinRoomTos : ProtoHandler {
113113
}
114114
}
115115
player.playerName = playerName
116+
player.roomExtension = pb.extension
116117
val count = PlayerGameCount(playerInfo.winCount, playerInfo.gameCount)
117118
if (!newGame.onPlayerJoinRoom(player, count)) {
118119
Game.playerNameCache.remove(playerName) // 登录失败的话,要把注册清掉
@@ -121,6 +122,7 @@ class JoinRoomTos : ProtoHandler {
121122
}
122123
player.game = newGame
123124
player.send(getRoomInfoToc {
125+
extension = player.roomExtension
124126
myPosition = player.location
125127
onlineCount = Game.onlineCount
126128
inGameCount = Game.inGameCount
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.fengsheng.handler
2+
3+
import com.fengsheng.HumanPlayer
4+
import com.fengsheng.protos.Fengsheng
5+
import com.fengsheng.protos.setRoomExtensionToc
6+
7+
class SetRoomExtensionTos : AbstractProtoHandler<Fengsheng.set_room_extension_tos>() {
8+
override fun handle0(r: HumanPlayer, pb: Fengsheng.set_room_extension_tos) {
9+
r.roomExtension = pb.extension
10+
r.send(setRoomExtensionToc {
11+
extension = r.roomExtension
12+
})
13+
}
14+
}

src/main/kotlin/phase/StartGame.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class StartGame(val game: Game, override val whoseTurn: Player) : Fsm {
1414
logger.info("游戏开始了,场上的角色依次是:${players.joinToString()}")
1515
game.turn = 0
1616
game.realTurn = 0
17-
game.deck.init(players.size)
17+
game.deck.init(players.size, game.extension)
1818
for (i in players.indices) players[(whoseTurn + i) % players.size]!!.init()
1919
for (i in players.indices) players[(whoseTurn + i) % players.size]!!.draw(Config.HandCardCountBegin)
2020
GameExecutor.post(game, { game.resolve(DrawPhase(players[whoseTurn]!!)) }, 1, TimeUnit.SECONDS)

src/main/kotlin/phase/WaitForSelectRole.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ class WaitForSelectRole(val game: Game, val options: List<MutableList<RoleSkills
112112
waitingSecond = ((endTime - System.currentTimeMillis()) / 1000.0).roundToInt().coerceAtLeast(1)
113113
possibleSecretTask.addAll(game.possibleSecretTasks)
114114
})
115+
if (player.roomExtension != game.extension) {
116+
val extensionName = when (game.extension) {
117+
1 -> "标准包"
118+
2 -> "标准包+一扩"
119+
else -> "全部扩展包"
120+
}
121+
player.sendErrorMessage("根据投票结果,本次游戏使用$extensionName")
122+
}
115123
if (game.players.size < 5)
116124
player.notifyIdentity()
117125
}

src/main/kotlin/skill/HouLaiRen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class HouLaiRen : ActiveSkill {
5757
r.incrSeq()
5858
r.addSkillUseCount(skillId)
5959
g.playerSetRoleFaceUp(r, true)
60-
val roles = RoleCache.getRandomRoles(3, g.players.map { it!!.role }.toSet())
60+
val roles = RoleCache.getRandomRoles(3, g.players.map { it!!.role }.toSet(), g.extension)
6161
val discardCards = r.messageCards.filter { it.id != message.remainCardId }
6262
logger.info("${r}发动了[后来人],弃掉了${discardCards.joinToString()}")
6363
r.messageCards.clear()

src/main/kotlin/skill/RoleCache.kt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,14 @@ object RoleCache {
141141
/**
142142
* @return 长度为 `n` 的列表
143143
*/
144-
fun getRandomRoles(n: Int): List<RoleSkillsData> = runBlocking {
144+
fun getRandomRoles(n: Int, extension: Int): List<RoleSkillsData> = runBlocking {
145145
mu.withLock {
146-
val indexList = cache.indices.shuffled()
146+
// TODO: 禁用角色再解禁的情况下会导致限制扩展包排序不对
147+
val indexList = when (extension) {
148+
2 -> 1..cache.indexOfFirst { it.role == sp_duan_mu_jing }
149+
1 -> 1..cache.indexOfFirst { it.role == sp_li_ning_yu }
150+
else -> cache.indices
151+
}.shuffled()
147152
List(n) { i -> if (i < indexList.size) cache[indexList[i]] else RoleSkillsData() }
148153
}
149154
}
@@ -152,10 +157,15 @@ object RoleCache {
152157
* @param except 排除的角色
153158
* @return 长度为 `n` 的列表
154159
*/
155-
fun getRandomRoles(n: Int, except: Set<role>): List<RoleSkillsData> = runBlocking {
160+
fun getRandomRoles(n: Int, except: Set<role>, extension: Int): List<RoleSkillsData> = runBlocking {
156161
mu.withLock {
162+
// TODO: 禁用角色再解禁的情况下会导致限制扩展包排序不对
157163
val cache = this@RoleCache.cache.filterNot { it.role in except }
158-
val indexList = cache.indices.shuffled()
164+
val indexList = when (extension) {
165+
2 -> 1..cache.indexOfFirst { it.role == sp_duan_mu_jing }
166+
1 -> 1..cache.indexOfFirst { it.role == sp_li_ning_yu }
167+
else -> cache.indices
168+
}.shuffled()
159169
List(n) { i -> if (i < indexList.size) cache[indexList[i]] else RoleSkillsData() }
160170
}
161171
}
@@ -164,8 +174,8 @@ object RoleCache {
164174
* @param roles 返回数组的前几个角色强行指定
165175
* @return 长度为 `n` 的列表
166176
*/
167-
fun getRandomRolesWithSpecific(n: Int, roles: List<role>): List<RoleSkillsData> {
168-
val roleSkillsDataList = getRandomRoles(n).toMutableList()
177+
fun getRandomRolesWithSpecific(n: Int, roles: List<role>, extension: Int): List<RoleSkillsData> {
178+
val roleSkillsDataList = getRandomRoles(n, extension).toMutableList()
169179
var roleIndex = 0
170180
while (roleIndex < roles.size && roleIndex < n) {
171181
val index = roleSkillsDataList.indexOfFirst { it.role == roles[roleIndex] }

src/main/proto/fengsheng.proto

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ message join_room_tos {
7373
string name = 2; // 玩家的名字
7474
string password = 4; // 密码,建议客户端先加密一下再发过来
7575
uint32 player_count = 6; // (创房)房间内的总人数
76+
uint32 extension = 7; // 扩展包,1-只有基础,2-基础一扩,3-全部,0-默认全部
7677
}
7778

7879
// 返回房间所有人的信息
@@ -87,6 +88,15 @@ message get_room_info_toc {
8788
string notice = 8; // 房间公告
8889
uint32 room_id = 9; // 房间号
8990
uint32 in_game_count = 10; // 正在进行游戏的桌数
91+
uint32 extension = 11; // 扩展包,1-只有基础,2-基础一扩,3-全部
92+
}
93+
94+
message set_room_extension_tos {
95+
uint32 extension = 1; // 扩展包,1-只有基础,2-基础一扩,3-全部
96+
}
97+
98+
message set_room_extension_toc {
99+
uint32 extension = 1; // 扩展包,1-只有基础,2-基础一扩,3-全部
90100
}
91101

92102
// 通知房间倒计时

0 commit comments

Comments
 (0)