Skip to content

Commit

Permalink
Fix advantage/disadvantage
Browse files Browse the repository at this point in the history
  • Loading branch information
North101 committed Apr 23, 2018
1 parent c8e5498 commit 4732a2a
Show file tree
Hide file tree
Showing 9 changed files with 313 additions and 202 deletions.
1 change: 1 addition & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.*
import net.north101.android.ghplayertracker.data.Character
import net.north101.android.ghplayertracker.data.CharacterData
import net.north101.android.ghplayertracker.data.CharacterPerk
import net.north101.android.ghplayertracker.data.Level
import net.yslibrary.android.keyboardvisibilityevent.KeyboardVisibilityEvent
Expand Down Expand Up @@ -339,7 +340,7 @@ open class CharacterFragment : Fragment(), OnBackPressedListener {

open fun saveCharacterTask(callback: Runnable?) {
val data = try {
Character.CharacterData.load(context!!)
CharacterData.load(context!!)
} catch (e: IOException) {
e.printStackTrace()
null
Expand All @@ -349,7 +350,7 @@ open class CharacterFragment : Fragment(), OnBackPressedListener {
} catch (e: ParseException) {
e.printStackTrace()
null
} ?: Character.CharacterData(context!!, JSONObject())
} ?: CharacterData(context!!, JSONObject())

data.update(character)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import android.view.MenuItem
import android.view.View
import com.mikepenz.itemanimators.SlideDownAlphaAnimator
import net.north101.android.ghplayertracker.data.Character
import net.north101.android.ghplayertracker.data.CharacterData
import net.north101.android.ghplayertracker.data.SelectableCharacter
import org.androidannotations.annotations.*
import org.json.JSONException
Expand Down Expand Up @@ -161,7 +162,7 @@ open class CharacterListFragment : Fragment(), ActionMode.Callback {
@Background
open fun deleteCharacters(characterList: List<Character>) {
val data = try {
Character.CharacterData.load(context!!)
CharacterData.load(context!!)
} catch (e: IOException) {
e.printStackTrace()
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.Toolbar
import android.util.Log
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
Expand All @@ -31,6 +32,7 @@ import org.androidannotations.annotations.sharedpreferences.Pref
import org.json.JSONException
import java.io.IOException
import java.util.*
import kotlin.collections.ArrayList

@OptionsMenu(R.menu.character_tracker)
@EFragment(R.layout.character_tracker_layout)
Expand Down Expand Up @@ -88,8 +90,10 @@ open class CharacterTrackerFragment : Fragment() {
protected lateinit var cursePlusView: ImageView
@ViewById(R.id.curse_minus)
protected lateinit var curseMinusView: ImageView
@ViewById(R.id.split)
protected lateinit var splitIconView: ImageView
@ViewById(R.id.advantage)
protected lateinit var advantageIconView: ImageView
@ViewById(R.id.disadvantage)
protected lateinit var disadvantageIconView: ImageView
@ViewById(R.id.shuffle)
protected lateinit var shuffleIconView: ImageView
@ViewById(R.id.active_deck_list)
Expand All @@ -110,7 +114,7 @@ open class CharacterTrackerFragment : Fragment() {
protected var basicCards: BasicCards? = null
@JvmField
@InstanceState
protected var split = false
protected var attackStatus = AttackStatus.None
@JvmField
@InstanceState
protected var shuffle = false
Expand All @@ -122,6 +126,12 @@ open class CharacterTrackerFragment : Fragment() {
@InstanceState
protected var shuffleCount = 0

enum class AttackStatus {
None,
Advantage,
Disadvantage,
}

@Pref
protected lateinit var sharedPrefs: SharedPrefs_

Expand Down Expand Up @@ -192,7 +202,7 @@ open class CharacterTrackerFragment : Fragment() {
playedCardsListView.adapter = playedCardsAdapter
playedCardsAdapter.updateItems(characterTracker.playedCardsHistory)

setSplit(split)
setAttackStatus(attackStatus)
setShuffleEnabled(shuffle)
updateActiveDecks()
ViewCompat.setNestedScrollingEnabled(playedCardsListView, false)
Expand Down Expand Up @@ -358,38 +368,92 @@ open class CharacterTrackerFragment : Fragment() {

@Click(R.id.draw_deck)
fun onDrawDeckClicked() {
val item1 = drawCards()
val item2: ArrayList<Card>?
if (split) {
val playedCards = if (attackStatus == AttackStatus.Advantage) {
if (sharedPrefs.houseRuleVantage().get()) {
item2 = drawCards()
drawHouse()
} else {
item2 = ArrayList()
if (!hasSpecial(item1, CardSpecial.Rolling)) {
val splitCard = drawCard()
if (splitCard != null) {
if (splitCard.special == CardSpecial.Rolling) {
item1.add(splitCard)
} else {
item2.add(splitCard)
}
}
}
drawAdvantage()
}
} else if (attackStatus == AttackStatus.Disadvantage) {
if (sharedPrefs.houseRuleVantage().get()) {
drawHouse()
} else {
drawDisadvantage()
}
} else {
item2 = null
drawNormal()
}
if (hasSpecial(item1, CardSpecial.Shuffle) || item2 != null && hasSpecial(item2, CardSpecial.Shuffle)) {
if (playedCards.hasShuffle()) {
setShuffleEnabled(true)
}
characterTracker.playedCardsHistory.add(0, PlayedCards(item1, item2, null))
characterTracker.playedCardsHistory.add(0, playedCards)
playedCardsAdapter.updateItems(characterTracker.playedCardsHistory)
updateActiveDecks()
}

@Click(R.id.split)
fun onSplitIconClick() {
toggleSplit()
fun drawNormal(): PlayedCards {
return PlayedCards(drawCards(), null, null)
}

fun drawAdvantage(): PlayedCards {
val item1 = drawCards()
val item2 = ArrayList<Card>()
if (item1.count() < 2) {
val card = drawCard()
if (card.special == CardSpecial.Rolling) {
item1.add(card)
} else {
item2.add(card)
}
}
return PlayedCards(item1, item2, null)
}

fun drawDisadvantage(): PlayedCards {
val item1 = ArrayList<Card>()
val item2 = ArrayList<Card>()
val card1 = drawCard()
val card2 = drawCard()

item1.add(card1)
if (card1.special == CardSpecial.Rolling && card2.special == CardSpecial.Rolling) {
item1.add(card2)
while (true) {
val card = drawCard()
item1.add(card)
if (card.special != CardSpecial.Rolling)
break
}
} else {
item2.add(card2)
}
return PlayedCards(item1, item2, null)
}

fun drawHouse(): PlayedCards {
return PlayedCards(drawCards(), drawCards(), null)
}

@Click(R.id.advantage)
fun onAdvantageIconClick() {
val attackStatus = if (attackStatus == AttackStatus.Advantage) {
AttackStatus.None
} else {
AttackStatus.Advantage
}
setAttackStatus(attackStatus)
Log.d("advantage", attackStatus.toString())
}

@Click(R.id.disadvantage)
fun onDisadvantageIconClick() {
val attackStatus = if (attackStatus == AttackStatus.Disadvantage) {
AttackStatus.None
} else {
AttackStatus.Disadvantage
}
setAttackStatus(attackStatus)
Log.d("diadvantage", attackStatus.toString())
}

@Click(R.id.shuffle)
Expand Down Expand Up @@ -666,16 +730,23 @@ open class CharacterTrackerFragment : Fragment() {
}
}

fun toggleSplit() {
setSplit(!this.split)
}
fun setAttackStatus(attackStatus: AttackStatus) {
this.attackStatus = attackStatus

fun setSplit(split: Boolean) {
this.split = split
if (split) {
splitIconView.setImageResource(R.drawable.ic_call_split_black_24dp)
advantageIconView.colorFilter = if (attackStatus == AttackStatus.Advantage) {
null
} else {
splitIconView.setImageResource(R.drawable.ic_arrow_upward_black_24dp)
val matrix = ColorMatrix()
matrix.setSaturation(0f) //0 means grayscale
ColorMatrixColorFilter(matrix)
}

disadvantageIconView.colorFilter = if (attackStatus == AttackStatus.Disadvantage) {
null
} else {
val matrix = ColorMatrix()
matrix.setSaturation(0f) //0 means grayscale
ColorMatrixColorFilter(matrix)
}
}

Expand Down Expand Up @@ -726,7 +797,7 @@ open class CharacterTrackerFragment : Fragment() {
val cards = ArrayList<Card>()

while (true) {
val card = drawCard() ?: break
val card = drawCard()

cards.add(card)
if (card.special != CardSpecial.Rolling)
Expand All @@ -736,12 +807,9 @@ open class CharacterTrackerFragment : Fragment() {
return cards
}

fun drawCard(): Card? {
fun drawCard(): Card {
if (characterTracker.deck.size == 0) {
shuffle()
if (characterTracker.deck.size == 0) {
return null
}
}

val index = randomGenerator.nextInt(characterTracker.deck.size)
Expand Down
Loading

0 comments on commit 4732a2a

Please sign in to comment.