Skip to content

Commit

Permalink
Added the exit on off-script
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurKun21 committed Jun 25, 2024
1 parent 71736bb commit 55d2ac5
Show file tree
Hide file tree
Showing 13 changed files with 297 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,20 +217,33 @@ private fun BattleConfigContent(
.height(IntrinsicSize.Min)
) {
PartySelection(
config = config
config = config,
modifier = Modifier.weight(1f)
)

VerticalDivider()

ServerSelection(
config = config
config = config,
modifier = Modifier.weight(1f)
)
}
HorizontalDivider()

Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.height(IntrinsicSize.Min)
) {
ExitOffScriptScreen(
config = config,
modifier = Modifier.weight(1f)
)
VerticalDivider()

OutOfCommandsExitScreen(
config = config,
modifier = Modifier.weight(2f)
modifier = Modifier.weight(1f)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package io.github.fate_grand_automata.ui.battle_config_item

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import io.github.fate_grand_automata.R
import io.github.fate_grand_automata.prefs.core.BattleConfigCore
import io.github.fate_grand_automata.ui.dialog.FgaDialog
import io.github.fate_grand_automata.ui.prefs.remember

@Composable
fun ExitOffScriptScreen(
modifier: Modifier = Modifier,
config: BattleConfigCore
) {
var exitOnOffScript by config.exitOnOffScript.remember()

val dialog = FgaDialog()

dialog.build(
color = MaterialTheme.colorScheme.background
) {
title(stringResource(R.string.p_battle_config_exit_on_off_script))

message(text = stringResource(R.string.p_battle_config_exit_on_off_script_message))

Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.padding(
horizontal = 4.dp
)
) {
Card(
shape = RoundedCornerShape(25),
colors = CardDefaults.cardColors(
containerColor =
if (exitOnOffScript) MaterialTheme.colorScheme.primaryContainer
else MaterialTheme.colorScheme.surfaceVariant
),
onClick = {
exitOnOffScript = true
dialog.hide()
},
modifier = Modifier
.weight(1f)
.padding(horizontal = 2.dp)
) {
Text(
text = stringResource(R.string.state_on).uppercase(),
style = MaterialTheme.typography.bodySmall,
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 4.dp),
textAlign = TextAlign.Center,
fontWeight = if (exitOnOffScript) FontWeight.Bold else null
)
}
Card(
shape = RoundedCornerShape(25),
colors = CardDefaults.cardColors(
containerColor =
if (!exitOnOffScript) MaterialTheme.colorScheme.primaryContainer
else MaterialTheme.colorScheme.surfaceVariant
),
onClick = {
exitOnOffScript = false
dialog.hide()
},
modifier = Modifier
.weight(1f)
.padding(horizontal = 2.dp)
) {
Text(
text = stringResource(R.string.state_off).uppercase(),
style = MaterialTheme.typography.bodySmall,
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 4.dp),
textAlign = TextAlign.Center,
fontWeight = if (!exitOnOffScript) FontWeight.Bold else null
)
}
}
}

Column(
modifier = modifier
.fillMaxSize()
.clickable(
onClick = { dialog.show() }
)
.padding(
horizontal = 0.dp,
vertical = 4.dp
),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(
text = stringResource(R.string.p_battle_config_exit_on_off_script).uppercase(),
style = MaterialTheme.typography.bodySmall,
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth(),
maxLines = 1
)
Text(
text = when (exitOnOffScript) {
true -> stringResource(R.string.state_on).uppercase()
false -> stringResource(R.string.state_off).uppercase()
},
style = MaterialTheme.typography.bodySmall,
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import io.github.fate_grand_automata.ui.dialog.FgaDialog
import io.github.fate_grand_automata.ui.prefs.remember

@Composable
fun PartySelection(config: BattleConfigCore) {
fun PartySelection(
modifier: Modifier = Modifier,
config: BattleConfigCore,
) {
var party by config.party.remember()

val dialog = FgaDialog()
Expand All @@ -55,7 +58,7 @@ fun PartySelection(config: BattleConfigCore) {
}

Column(
modifier = Modifier
modifier = modifier
.fillMaxHeight()
.clickable(onClick = { dialog.show() })
.padding(16.dp, 5.dp),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import io.github.fate_grand_automata.ui.prefs.remember
import io.github.fate_grand_automata.util.stringRes

@Composable
fun ServerSelection(config: BattleConfigCore) {
fun ServerSelection(
modifier: Modifier = Modifier,
config: BattleConfigCore
) {
var server by config.server.remember()

val dialog = FgaDialog()
Expand Down Expand Up @@ -65,7 +68,7 @@ fun ServerSelection(config: BattleConfigCore) {
}

Column(
modifier = Modifier
modifier = modifier
.fillMaxHeight()
.clickable(onClick = { dialog.show() })
.padding(16.dp, 5.dp),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ private fun AutoBattle.ExitReason.text(): String = when (this) {
AutoBattle.ExitReason.Paused -> stringResource(R.string.script_paused)
AutoBattle.ExitReason.StopAfterThisRun -> stringResource(R.string.stop_after_this_run)
AutoBattle.ExitReason.ExitOnOutOfCommands -> stringResource(R.string.p_stop_on_out_of_commands)
AutoBattle.ExitReason.ExitOnOffScript -> stringResource(id = R.string.p_stop_on_off_script)
}

@Composable
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/localized.xml
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,9 @@ After pressing on the button, switch the app filter from \"Not optimized\" to \"
<string name="p_stop_on_out_of_commands">Out of Commands. Exiting Early for manual override.</string>
<string name="p_exit_on_out_of_commands">"Out of Commands Exit"</string>
<string name="p_exit_on_out_of_commands_summary">"Exit Battle when ran out of commands"</string>
<string name="p_battle_config_exit_on_off_script">"Off-script Exit"</string>
<string name="p_battle_config_exit_on_off_script_message">"Exit Battle when a turn/wave that is not part of the script have been reached."</string>
<string name="p_stop_on_off_script">Off-script. Exiting Early for manual override.</string>
<string name="state_on">On</string>
<string name="state_off">Off</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ internal class BattleConfig(

override var exitOnOutOfCommands by prefs.exitOnOutOfCommands

override var exitOnOffScript by prefs.exitOnOffScript

override val server by prefs.server.map(
defaultValue = null,
convert = { it.asGameServer() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,7 @@ class BattleConfigCore(
val autoChooseTarget = maker.bool("auto_choose_target")

val exitOnOutOfCommands = maker.bool("exit_on_out_of_commands")


val exitOnOffScript = maker.bool("exit_on_off_script")
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class AutoBattle @Inject constructor(
data object Paused : ExitReason()
data object StopAfterThisRun : ExitReason()
data object ExitOnOutOfCommands : ExitReason()
data object ExitOnOffScript : ExitReason()
}

internal class BattleExitException(val reason: ExitReason) : Exception(reason.cause)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ class AutoSkillCommand private constructor(

fun commandTurnsUntilStage(stage: Int): Int = when (stage) {
0 -> stages[stage].flatten().size
else -> stages.subList(0, stage).flatten().size
else -> try {
stages.subList(0, stage).flatten().size
} catch (e: IndexOutOfBoundsException) {
stages.flatten().size
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.github.fate_grand_automata.scripts.modules

import io.github.fate_grand_automata.scripts.IFgoAutomataApi
import io.github.fate_grand_automata.scripts.Images
import io.github.fate_grand_automata.scripts.ScriptLog
import io.github.fate_grand_automata.scripts.entrypoints.AutoBattle
import io.github.fate_grand_automata.scripts.models.NPUsage
import io.github.fate_grand_automata.scripts.models.ParsedCard
Expand All @@ -11,7 +10,6 @@ import io.github.fate_grand_automata.scripts.models.battle.BattleState
import io.github.fate_grand_automata.scripts.prefs.IBattleConfig
import io.github.lib_automata.dagger.ScriptScope
import javax.inject.Inject
import kotlin.math.max
import kotlin.time.Duration.Companion.seconds

@ScriptScope
Expand All @@ -26,7 +24,8 @@ class Battle @Inject constructor(
private val skillSpam: SkillSpam,
private val shuffleChecker: ShuffleChecker,
private val stageTracker: StageTracker,
private val autoChooseTarget: AutoChooseTarget
private val autoChooseTarget: AutoChooseTarget,
private val commandTurnsTracker: CommandTurnsTracker
) : IFgoAutomataApi by api {
init {
prefs.stopAfterThisRun = false
Expand All @@ -35,8 +34,6 @@ class Battle @Inject constructor(
resetState()
}

var outOfCommands = false

fun resetState() {
// Don't increment no. of runs if we're just clicking on quest again and again
// This can happen due to lags introduced during some events
Expand Down Expand Up @@ -123,42 +120,7 @@ class Battle @Inject constructor(
autoChooseTarget.choose()
}

// It is already verified out of commands, no need to check further
if (outOfCommands) return@useSameSnapIn

trackSkipTurns()

outOfCommands = isOutOfCommand()

if (outOfCommands && battleConfig.exitOnOutOfCommands) {
throw AutoBattle.BattleExitException(AutoBattle.ExitReason.ExitOnOutOfCommands)
}
commandTurnsTracker.trackTurns()
}

private fun trackSkipTurns() {
if (!(state.stage > 0 && state.turn < 1)) return

var commandTurnsUntilStage = autoSkill.commandTurnsUntilStage(state.stage)

// add additional turn since it is checking at next stage/wave
commandTurnsUntilStage += 1

val skipTurns = max(0, commandTurnsUntilStage - (state.currentTurn + state.skipCommandTurns))

messages.log(
ScriptLog.TurnTrackingAtNewStage(
wave = state.stage,
currentTurn = state.currentTurn,
skipTurn = state.skipCommandTurns
)
)

state.addSkipCommandTurns(skipTurns)
}

private fun isOutOfCommand(): Boolean {
val totalCommandTurns = autoSkill.getTotalCommandTurns

return (state.currentTurn + state.skipCommandTurns) > totalCommandTurns
}
}
Loading

0 comments on commit 55d2ac5

Please sign in to comment.