fix:游戏结束后退出云信
This commit is contained in:
@@ -14,6 +14,7 @@ import io.reactivex.Single
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import java.lang.IllegalStateException
|
||||
|
||||
class GameIMEngineAbility(private val listenerOwner: ListenerOwner<GameIMEngineAbility.Listener> = SafeListenerOwner()) :
|
||||
@@ -36,23 +37,46 @@ class GameIMEngineAbility(private val listenerOwner: ListenerOwner<GameIMEngineA
|
||||
override fun onStart(context: RoomContext) {
|
||||
super.onStart(context)
|
||||
safeLaunch {
|
||||
stateAbility?.imIdFlow?.collectLatest {
|
||||
loadIM(it?.toString())
|
||||
stateAbility?.let {
|
||||
combine(
|
||||
*arrayOf(
|
||||
it.imIdFlow,
|
||||
it.gameStateFlow
|
||||
), transform = {
|
||||
true
|
||||
})
|
||||
.collectLatest {
|
||||
checkRunning()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadIM(sessionId: String?) {
|
||||
if (chatRoomClient != null && chatRoomClient?.sessionId != sessionId) {
|
||||
chatRoomClient?.onCleared()
|
||||
chatRoomClient = null
|
||||
}
|
||||
if (sessionId != null) {
|
||||
initClient(sessionId)
|
||||
private fun checkRunning() {
|
||||
val stateAbility = this.stateAbility
|
||||
val gameState = stateAbility?.gameStateFlow?.value
|
||||
val sessionId = stateAbility?.imIdFlow?.value?.toString()
|
||||
logD("checkRunning() gameState:$gameState sessionId:$sessionId")
|
||||
if (sessionId != null && (gameState == GameStateAbility.STATE_MATCHING || gameState == GameStateAbility.STATE_MATCH_SUCCESS)) {
|
||||
if (chatRoomClient?.sessionId != sessionId) {
|
||||
logD("checkRunning() 1")
|
||||
exitRoom()
|
||||
initClient(sessionId)
|
||||
}
|
||||
} else {
|
||||
logD("checkRunning() 2")
|
||||
exitRoom()
|
||||
}
|
||||
}
|
||||
|
||||
private fun exitRoom() {
|
||||
logD("exitRoom() id:${chatRoomClient?.sessionId}")
|
||||
chatRoomClient?.onCleared()
|
||||
chatRoomClient = null
|
||||
}
|
||||
|
||||
private fun initClient(sessionId: String) {
|
||||
logD("initClient() sessionId:$sessionId")
|
||||
netBrokenTag = false
|
||||
chatRoomClient = ChatRoomClientManager.getClient(sessionId)
|
||||
chatRoomClient?.let {
|
||||
@@ -102,8 +126,7 @@ class GameIMEngineAbility(private val listenerOwner: ListenerOwner<GameIMEngineA
|
||||
override fun onStop(context: RoomContext) {
|
||||
super.onStop(context)
|
||||
listenerOwner.removeAllListener()
|
||||
chatRoomClient?.onCleared()
|
||||
chatRoomClient = null
|
||||
exitRoom()
|
||||
}
|
||||
|
||||
interface Listener {
|
||||
|
@@ -53,12 +53,13 @@ class GameEngineAbility : SudEngineAbility() {
|
||||
logD("checkRunning() gameViewRect:null")
|
||||
return
|
||||
}
|
||||
val gameState = stateAbility?.gameStateFlow?.value ?: return
|
||||
val stateAbility = this.stateAbility
|
||||
val gameState = stateAbility?.gameStateFlow?.value
|
||||
logD("checkRunning() gameState:$gameState")
|
||||
if (gameState == GameStateAbility.STATE_MATCH_SUCCESS) {
|
||||
val activity = roomView?.getActivity() ?: return
|
||||
val roomId = stateAbility?.roomIdFlow?.value ?: return
|
||||
val gameId = stateAbility?.gameIdFlow?.value ?: return
|
||||
val roomId = stateAbility.roomIdFlow.value ?: return
|
||||
val gameId = stateAbility.gameIdFlow.value ?: return
|
||||
loadGame(activity, roomId.toString(), gameId)
|
||||
} else {
|
||||
logD("checkRunning() destroyGame")
|
||||
|
@@ -107,6 +107,7 @@ open class SudEngineAbility : RoomAbility(), SudFSMMGListener, ILog {
|
||||
logD("destroyGame()")
|
||||
isRunning = false
|
||||
sudFSTAPP.destroyMG()
|
||||
sudFSMMG.destroyMG()
|
||||
gameViewFlow.value = null
|
||||
roomId = ""
|
||||
gameId = 0
|
||||
@@ -139,6 +140,8 @@ open class SudEngineAbility : RoomAbility(), SudFSMMGListener, ILog {
|
||||
}
|
||||
logD("loadGameSDK")
|
||||
sudFSTAPP.destroyMG()
|
||||
sudFSMMG.destroyMG()
|
||||
sudFSMMG.setSudFSMMGListener(this)
|
||||
val iSudFSTAPP =
|
||||
SudMGP.loadMG(activity, getUserId().toString(), getRoomId(), code, gameId, getGameLanguage(), sudFSMMG)
|
||||
sudFSTAPP.setISudFSTAPP(iSudFSTAPP)
|
||||
|
Reference in New Issue
Block a user