fix:补充1v1游戏房从后台恢复的状态同步机制
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package com.chwl.app.game.core
|
||||
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import com.chwl.app.game.data.GameModel2
|
||||
import com.chwl.app.game.data.bean.GameInfoUiState
|
||||
import com.chwl.app.game.ui.game.GameIntent
|
||||
@@ -10,14 +12,11 @@ import com.chwl.core.im.custom.bean.GameForcedEndAttachment
|
||||
import com.chwl.core.im.custom.bean.GameQueueChangedAttachment
|
||||
import com.chwl.core.support.room.RoomAbility
|
||||
import com.chwl.core.support.room.RoomContext
|
||||
import com.chwl.library.utils.SingleToastUtil
|
||||
import com.netease.nimlib.sdk.StatusCode
|
||||
import com.netease.nimlib.sdk.chatroom.model.ChatRoomMessage
|
||||
import com.netease.nimlib.sdk.msg.constant.MsgTypeEnum
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.flow.flatMapConcat
|
||||
|
||||
class GameStateAbility : RoomAbility(), GameIMEngineAbility.Listener {
|
||||
|
||||
@@ -33,6 +32,9 @@ class GameStateAbility : RoomAbility(), GameIMEngineAbility.Listener {
|
||||
|
||||
// 匹配失败
|
||||
const val STATE_MATCH_FAILED = 3
|
||||
|
||||
// 错误状态
|
||||
const val STATE_ERROR = -1
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,33 +66,68 @@ class GameStateAbility : RoomAbility(), GameIMEngineAbility.Listener {
|
||||
|
||||
val gameResultFlow = MutableSharedFlow<List<GameResultBean>?>()
|
||||
|
||||
private var viewBackground = false
|
||||
|
||||
private val queueAbility get() = roomContext?.findAbility<GameQueueAbility>(GameQueueAbility::class.java.simpleName)
|
||||
|
||||
override fun onStart(context: RoomContext) {
|
||||
super.onStart(context)
|
||||
requestRoomInfo()
|
||||
requestRoomInfo(true)
|
||||
val imEngineAbility =
|
||||
context.findAbility<GameIMEngineAbility>(GameIMEngineAbility::class.java.simpleName)
|
||||
imEngineAbility?.addListener(this)
|
||||
safeLaunch {
|
||||
imEngineAbility?.reconnectedFlow?.collectLatest {
|
||||
requestRoomInfo()
|
||||
requestSyncState(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun requestRoomInfo() {
|
||||
gameInfoUiStateFlow.value = GameInfoUiState.Loading
|
||||
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
|
||||
super.onStateChanged(source, event)
|
||||
when (event) {
|
||||
Lifecycle.Event.ON_RESUME -> {
|
||||
if (viewBackground) {
|
||||
if (gameStateFlow.value == STATE_MATCHING || gameStateFlow.value == STATE_MATCH_SUCCESS) {
|
||||
requestSyncState(false)
|
||||
}
|
||||
}
|
||||
viewBackground = false
|
||||
}
|
||||
|
||||
Lifecycle.Event.ON_STOP -> {
|
||||
viewBackground = true
|
||||
}
|
||||
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 请求同步服务端的最新状态
|
||||
private fun requestSyncState(needLoading: Boolean) {
|
||||
logD("requestSyncState")
|
||||
requestRoomInfo(needLoading)
|
||||
}
|
||||
|
||||
private fun requestRoomInfo(needLoading: Boolean) {
|
||||
logD("requestRoomInfo needLoading:$needLoading")
|
||||
if (needLoading) {
|
||||
gameInfoUiStateFlow.value = GameInfoUiState.Loading
|
||||
}
|
||||
safeLaunch(onError = {
|
||||
logD("requestRoomInfo Failed:$it")
|
||||
gameInfoUiStateFlow.value = GameInfoUiState.Failed(it, gameInfoFlow.value)
|
||||
}) {
|
||||
val info = GameModel2.getGameRoomInfo()
|
||||
if (info != null) {
|
||||
logD("requestRoomInfo Success")
|
||||
gameInfoUiStateFlow.value = GameInfoUiState.Success
|
||||
syncRoomInfo(info)
|
||||
} else {
|
||||
logD("requestRoomInfo Empty")
|
||||
gameInfoUiStateFlow.value = GameInfoUiState.Empty(gameInfoFlow.value)
|
||||
syncGameState(null)
|
||||
syncGameState(STATE_ERROR)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -101,7 +138,7 @@ class GameStateAbility : RoomAbility(), GameIMEngineAbility.Listener {
|
||||
gameInfoUiStateFlow.value = GameInfoUiState.Failed(it, gameInfoFlow.value)
|
||||
}) {
|
||||
GameModel2.startGame(intent.gameId.toString(), intent.gameMode)
|
||||
requestRoomInfo()
|
||||
requestRoomInfo(true)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -125,6 +125,7 @@ class GameEngineAbility : SudEngineAbility() {
|
||||
|
||||
override fun onGameStarted() {
|
||||
super.onGameStarted()
|
||||
logD("onGameStarted")
|
||||
autoJoinGame()
|
||||
autoPlayGame()
|
||||
updateGameSettingSelectConfig()
|
||||
@@ -144,7 +145,10 @@ class GameEngineAbility : SudEngineAbility() {
|
||||
model: SudMGPMGState.MGCommonPlayerReady?
|
||||
) {
|
||||
super.onPlayerMGCommonPlayerReady(handle, userId, model)
|
||||
autoPlayGame()
|
||||
logD("onPlayerMGCommonPlayerReady userId:$userId model:${model?.isReady}")
|
||||
if (model?.isReady != false) {
|
||||
autoPlayGame()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onGameMGCommonGameSettle(
|
||||
@@ -189,4 +193,23 @@ class GameEngineAbility : SudEngineAbility() {
|
||||
handle.success(configStr)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPlayerStateChange(
|
||||
handle: ISudFSMStateHandle?,
|
||||
userId: String?,
|
||||
state: String?,
|
||||
dataJson: String?
|
||||
): Boolean {
|
||||
logD("onPlayerStateChange userId:$userId state:$state dataJson:$dataJson")
|
||||
return super.onPlayerStateChange(handle, userId, state, dataJson)
|
||||
}
|
||||
|
||||
override fun onGameStateChange(
|
||||
handle: ISudFSMStateHandle?,
|
||||
state: String?,
|
||||
dataJson: String?
|
||||
): Boolean {
|
||||
logD("onGameStateChange state:$state dataJson:${dataJson}")
|
||||
return super.onGameStateChange(handle, state, dataJson)
|
||||
}
|
||||
}
|
@@ -1,6 +1,5 @@
|
||||
package com.chwl.app.game.data
|
||||
|
||||
import com.chwl.core.auth.AuthModel
|
||||
import com.chwl.core.bean.game.GameConfigBean
|
||||
import com.chwl.core.bean.game.GameRoomInfo
|
||||
import com.chwl.core.base.BaseModel
|
||||
@@ -8,8 +7,6 @@ import com.chwl.core.bean.response.ServiceResult
|
||||
import com.chwl.core.bean.room.BaseRoomInfo
|
||||
import com.chwl.core.home.bean.*
|
||||
import com.chwl.core.pay.PayModel
|
||||
import com.chwl.core.room.game.GameModel
|
||||
import com.chwl.core.room.game.bean.GameCodeInfo
|
||||
import com.chwl.core.utils.net.RxHelper
|
||||
import com.chwl.core.utils.net.launchRequest
|
||||
import com.chwl.library.net.rxnet.RxNet
|
||||
|
Reference in New Issue
Block a user