From 13d87309e141f49f05c8c336301c09e750a6afea Mon Sep 17 00:00:00 2001 From: max Date: Wed, 5 Jun 2024 15:56:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E8=A1=A5=E5=85=851v1=E6=B8=B8=E6=88=8F?= =?UTF-8?q?=E6=88=BF=E4=BB=8E=E5=90=8E=E5=8F=B0=E6=81=A2=E5=A4=8D=E7=9A=84?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E5=90=8C=E6=AD=A5=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chwl/app/game/core/GameStateAbility.kt | 55 ++++++++++++++++--- .../app/game/core/engine/GameEngineAbility.kt | 25 ++++++++- .../java/com/chwl/app/game/data/GameModel2.kt | 3 - 3 files changed, 70 insertions(+), 13 deletions(-) diff --git a/app/src/module_game/java/com/chwl/app/game/core/GameStateAbility.kt b/app/src/module_game/java/com/chwl/app/game/core/GameStateAbility.kt index a6f5bcf40..10a695b93 100644 --- a/app/src/module_game/java/com/chwl/app/game/core/GameStateAbility.kt +++ b/app/src/module_game/java/com/chwl/app/game/core/GameStateAbility.kt @@ -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?>() + private var viewBackground = false + private val queueAbility get() = roomContext?.findAbility(GameQueueAbility::class.java.simpleName) override fun onStart(context: RoomContext) { super.onStart(context) - requestRoomInfo() + requestRoomInfo(true) val imEngineAbility = context.findAbility(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) } } diff --git a/app/src/module_game/java/com/chwl/app/game/core/engine/GameEngineAbility.kt b/app/src/module_game/java/com/chwl/app/game/core/engine/GameEngineAbility.kt index 36235109d..e942e296d 100644 --- a/app/src/module_game/java/com/chwl/app/game/core/engine/GameEngineAbility.kt +++ b/app/src/module_game/java/com/chwl/app/game/core/engine/GameEngineAbility.kt @@ -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) + } } \ No newline at end of file diff --git a/app/src/module_game/java/com/chwl/app/game/data/GameModel2.kt b/app/src/module_game/java/com/chwl/app/game/data/GameModel2.kt index 2f03f318f..1da2aee2e 100644 --- a/app/src/module_game/java/com/chwl/app/game/data/GameModel2.kt +++ b/app/src/module_game/java/com/chwl/app/game/data/GameModel2.kt @@ -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