Skip to content

Commit

Permalink
1,悬浮窗增加记录双方墓地的功能。
Browse files Browse the repository at this point in the history
  • Loading branch information
keluokeda committed Nov 13, 2022
1 parent f9f970d commit 05aaff1
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 40 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
applicationId "com.ke.hs_tracker.app"
minSdk libs.versions.minsdk.get().toInteger()
targetSdk libs.versions.targetsdk.get().toInteger()
versionCode 18
versionName "1.1.8"
versionCode 19
versionName "1.1.9"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.ke.hs_tracker.module.db.Game
*/
sealed interface GameEvent {

object None : GameEvent



/**
Expand Down Expand Up @@ -36,4 +36,9 @@ sealed interface GameEvent {
data class RemoveCardFromUserDeck(
val cardId: String
) : GameEvent

/**
* 插入一张卡牌到墓地
*/
data class InsertCardToGraveyard(val cardId: String, val isUser: Boolean) : GameEvent
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ interface DeckCardObserver {
*/
val deckCardList: StateFlow<List<CardBean>>

/**
* 自己的墓地
*/
val userGraveyardCardList: StateFlow<List<CardBean>>

/**
* 对手的墓地
*/
val opponentGraveyardCardList: StateFlow<List<CardBean>>

/**
* 初始化
*/
Expand Down Expand Up @@ -59,6 +69,15 @@ class DeckCardObserverImpl @Inject constructor(
override val deckCardList: StateFlow<List<CardBean>>
get() = _deckCardList

private val _userGraveyardCardList = MutableStateFlow<List<CardBean>>(emptyList())

override val userGraveyardCardList: StateFlow<List<CardBean>>
get() = _userGraveyardCardList

private val _opponentGraveyardCardList = MutableStateFlow<List<CardBean>>(emptyList())
override val opponentGraveyardCardList: StateFlow<List<CardBean>>
get() = _opponentGraveyardCardList

/**
* 所有卡牌
*/
Expand All @@ -73,21 +92,7 @@ class DeckCardObserverImpl @Inject constructor(
* 当前卡组剩余的卡牌
*/
private var deckLeftCardList: List<CardBean> = listOf()
set(value) {
// "设置了剩余卡牌 ${
// value.map {
// it.count
// }.reduce { acc, i ->
// acc + i
// }
// }".log()
var count = 0
value.forEach {
count += it.count
}
"设置了剩余卡牌 $count".log()
field = value
}


/**
* 获取炉石log文件夹
Expand Down Expand Up @@ -152,21 +157,21 @@ class DeckCardObserverImpl @Inject constructor(
scope.launch {
powerTagHandler.gameEventFlow.collect {
when (it) {
null -> {

}

is GameEvent.OnGameOver -> {

_userGraveyardCardList.value = emptyList()
_opponentGraveyardCardList.value = emptyList()

clearPowerLogFile()
"清空卡牌 OnGameOver ,deckLeftCardList ${deckLeftCardList.size} , currentDeckList ${currentDeckList.size} , _deckCardList = ${_deckCardList.value.size}".log()

// deckLeftCardList.clear()
// deckLeftCardList.addAll(currentDeckList)
deckLeftCardList = currentDeckList.toList()
_deckCardList.value = deckLeftCardList.toList()
// _deckCardList.send(deckLeftCardList)

"清空卡牌 OnGameOver ,deckLeftCardList ${deckLeftCardList.size} , currentDeckList ${currentDeckList.size} , _deckCardList = ${_deckCardList.value.size}".log()

it.game.apply {
userDeckCode = currentUserDeck?.code ?: ""
Expand All @@ -176,6 +181,9 @@ class DeckCardObserverImpl @Inject constructor(
powerFileObserver.reset()
}
GameEvent.OnGameStart -> {

_userGraveyardCardList.value = emptyList()
_opponentGraveyardCardList.value = emptyList()
// deckLeftCardList = currentDeckList
"清空卡牌 OnGameStart ,deckLeftCardList ${deckLeftCardList.size} , currentDeckList ${currentDeckList.size}".log()
// deckLeftCardList.clear()
Expand All @@ -192,8 +200,9 @@ class DeckCardObserverImpl @Inject constructor(
is GameEvent.InsertCardToUserDeck -> {
onUserDeckCardListChanged(it.cardId, false)
}
GameEvent.None -> {

is GameEvent.InsertCardToGraveyard -> {
onGraveyardCardsChanged(it.cardId, it.isUser)
}
}
}
Expand All @@ -218,6 +227,34 @@ class DeckCardObserverImpl @Inject constructor(

}

private fun onGraveyardCardsChanged(cardId: String, isUser: Boolean) {

//TAG_CHANGE Entity=[entityName=UNKNOWN ENTITY [cardType=INVALID] id=106 zone=PLAY zonePos=0 cardId= player=1] tag=ZONE value=GRAVEYARD
//TAG_CHANGE Entity=[entityName=UNKNOWN ENTITY [cardType=INVALID] id=5 zone=SECRET zonePos=0 cardId= player=1] tag=COST value=2
//如果对面打出一张奥秘拍 会直接进入墓地
// ?: throw RuntimeException("没有id $entity")

val card = allCards.find {
it.id == cardId
} ?: return

if (card.type == CardType.Enchantment) {
// "衍生牌 $card 不能放到墓地去".log()
return
}

// "插入一张牌到墓地 $card $entity".log()

//TAG_CHANGE Entity=[entityName=破霰元素 id=62 zone=PLAY zonePos=1 cardId=AV_260 player=2] tag=ZONE value=GRAVEYARD
if (isUser) {
_userGraveyardCardList.value += CardBean(card, 1)
} else {
_opponentGraveyardCardList.value += CardBean(card, 1)
}


}

/**
* 清空log文件
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface PowerTagHandler {
/**
* 游戏事件流
*/
val gameEventFlow: Flow<GameEvent>
val gameEventFlow: Flow<GameEvent?>
}

class PowerTagHandlerImpl @Inject constructor(
Expand All @@ -31,9 +31,9 @@ class PowerTagHandlerImpl @Inject constructor(

private val _gameEventFlow =
// Channel<GameEvent>(capacity = Channel.UNLIMITED)
MutableStateFlow<GameEvent>(GameEvent.None)
MutableStateFlow<GameEvent?>(null)

override val gameEventFlow: Flow<GameEvent>
override val gameEventFlow: Flow<GameEvent?>
get() = _gameEventFlow


Expand Down Expand Up @@ -227,7 +227,7 @@ class PowerTagHandlerImpl @Inject constructor(
insertCardToDeck(cardId)

} else if (it.currentZone == Zone.Play && it.newZone == Zone.Graveyard) {
// onGraveyardCardsChanged(cardId, it.isUser)
onGraveyardCardsChanged(cardId, it.isUser)
} else if (it.newZone == Zone.Hand || it.currentZone == Zone.Hand) {
//手牌
if (!it.isUser) {
Expand All @@ -241,6 +241,13 @@ class PowerTagHandlerImpl @Inject constructor(

}

private fun onGraveyardCardsChanged(cardId: String?, user: Boolean) {
if (cardId == null) {
return
}
_gameEventFlow.value = GameEvent.InsertCardToGraveyard(cardId, user)
}


// private fun findCardById(cardId: String) =
// allCard.find { it.id == cardId } ?: throw RuntimeException("id为 $cardId 没有这张牌")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import android.os.Build
import android.os.IBinder
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup.LayoutParams
import android.view.WindowManager
import androidx.core.view.isVisible
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope
import com.ke.hs_tracker.module.R
import com.ke.hs_tracker.module.databinding.ModuleFloatingWindowBinding
import com.ke.hs_tracker.module.log
import com.ke.hs_tracker.module.parser.DeckCardObserver
Expand All @@ -32,8 +36,11 @@ class WindowService : LifecycleService() {
ModuleFloatingWindowBinding.inflate(layoutInflater)
}

private val adapter = CardAdapter()
private val deckAdapter = CardAdapter()

private val graveyardAdapter = CardAdapter()

private val opponentGraveyardAdapter = CardAdapter()

@Inject
lateinit var deckCardObserver: DeckCardObserver
Expand All @@ -58,17 +65,50 @@ class WindowService : LifecycleService() {

windowManager.addView(binding.root, layoutParams)

binding.recyclerView.adapter = adapter
binding.recyclerView.adapter = deckAdapter

binding.zoom.setOnClickListener {
binding.recyclerView.isVisible = !binding.recyclerView.isVisible
}
// binding.zoom.setOnClickListener {
// binding.recyclerView.isVisible = !binding.recyclerView.isVisible
// }

binding.close.setOnClickListener {
windowManager.removeView(binding.root)
stopSelf()
}

binding.spinner.adapter = ArrayAdapter.createFromResource(
applicationContext,
R.array.module_spinner,
android.R.layout.simple_list_item_1
)
binding.spinner.onItemSelectedListener = object : OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>,
view: View,
position: Int,
id: Long
) {
when (position) {
0 -> {
binding.recyclerView.adapter = deckAdapter
}
1 -> {
binding.recyclerView.adapter = graveyardAdapter
}
2 -> {
binding.recyclerView.adapter = opponentGraveyardAdapter
}
}
}

override fun onNothingSelected(parent: AdapterView<*>?) {

}

}

binding.spinner.setSelection(0)

binding.root.setOnTouchListener(
ItemViewTouchListener(
layoutParams,
Expand All @@ -89,10 +129,21 @@ class WindowService : LifecycleService() {
lifecycleScope.launch {
deckCardObserver.deckCardList.collect {
// adapter.setList(it)
adapter.setDiffNewData(it.toMutableList())
deckAdapter.setDiffNewData(it.toMutableList())
}
}

lifecycleScope.launch {
deckCardObserver.userGraveyardCardList.collect {
graveyardAdapter.setDiffNewData(it.toMutableList())
}
}

lifecycleScope.launch {
deckCardObserver.opponentGraveyardCardList.collect {
opponentGraveyardAdapter.setDiffNewData(it.toMutableList())
}
}

}

Expand Down
22 changes: 16 additions & 6 deletions module/src/main/res/layout/module_floating_window.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#33000000"
android:orientation="vertical">


Expand All @@ -19,32 +20,41 @@
app:layout_constraintTop_toTopOf="parent" />

<ImageView
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/header"
android:id="@+id/zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@null"
android:padding="8dp"
android:src="@drawable/module_baseline_zoom_out_map_white_24dp" />
android:src="@drawable/module_baseline_zoom_out_map_white_24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/header" />

<ImageView
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@null"
android:padding="8dp"
android:src="@drawable/module_baseline_clear_red_500_24dp"
app:layout_constraintStart_toEndOf="@id/zoom"
app:layout_constraintTop_toTopOf="@id/zoom"
android:src="@drawable/module_baseline_clear_red_500_24dp" />
app:layout_constraintTop_toTopOf="@id/zoom" />

<Spinner
android:id="@+id/spinner"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
app:layout_constraintBottom_toBottomOf="@id/close"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/close" />


<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/zoom"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintTop_toBottomOf="@id/zoom"
tools:listitem="@layout/module_item_card" />

</androidx.constraintlayout.widget.ConstraintLayout>
7 changes: 7 additions & 0 deletions module/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@
<item>时间正序</item>
<item>时间倒序</item>
</string-array>

<string-array name="module_spinner">
<item>@string/module_deck</item>
<item>@string/module_graveyard</item>
<item>@string/module_opponent_graveyard</item>

</string-array>
</resources>

0 comments on commit 05aaff1

Please sign in to comment.