From d67c395e5704879d8ad51caa1d3bb049405ba0af Mon Sep 17 00:00:00 2001 From: Yome <695312637@qq.com> Date: Thu, 24 Oct 2024 08:02:27 -0700 Subject: [PATCH] Replays update targetIndex & states after each action --- lib/app/battle/models/battle.dart | 74 ++----------------- lib/app/modules/battle/battle_simulation.dart | 69 ++++++++++++++++- 2 files changed, 74 insertions(+), 69 deletions(-) diff --git a/lib/app/battle/models/battle.dart b/lib/app/battle/models/battle.dart index 6af107a35..46e2bc6b2 100644 --- a/lib/app/battle/models/battle.dart +++ b/lib/app/battle/models/battle.dart @@ -341,7 +341,7 @@ class BattleData { } } - _updateTargetedIndex(); + updateTargetedIndex(); final List allActors = [ ...onFieldEnemies, @@ -525,7 +525,7 @@ class BattleData { } } - _updateTargetedIndex(); + updateTargetedIndex(); final List newEnemies = [...onFieldEnemies, ...backupEnemies]; for (final actor in newEnemies) { @@ -1035,7 +1035,7 @@ class BattleData { await _nextTurn(); } - _updateTargetedIndex(); + updateTargetedIndex(); }, ); } @@ -1148,7 +1148,7 @@ class BattleData { checkActorStatus(); }); - _updateTargetedIndex(); + updateTargetedIndex(); }, ); } @@ -1413,7 +1413,7 @@ class BattleData { Future _removeDeadActors() async { await _removeDeadActorsFromList(onFieldAllyServants); await _removeDeadActorsFromList(onFieldEnemies); - _updateTargetedIndex(); + updateTargetedIndex(); if (niceQuest != null && niceQuest!.flags.contains(QuestFlag.enemyImmediateAppear)) { await _replenishActors(replenishAlly: false); @@ -1450,7 +1450,7 @@ class BattleData { } } - void _updateTargetedIndex() { + void updateTargetedIndex() { playerTargetIndex = getNonNullTargetIndex(onFieldAllyServants, playerTargetIndex, false); enemyTargetIndex = getNonNullTargetIndex(onFieldEnemies, enemyTargetIndex, true); } @@ -1614,68 +1614,6 @@ class BattleData { ..replayDataRecord = copy.replayDataRecord ..deadAttackCommandDict = copy.deadAttackCommandDict.map((key, value) => MapEntry(key, value.copy())); } - - // replay - Future replay(BattleShareData replayActions) async { - recorder.reasons.setReplay('Replaying team'); - options.manualAllySkillTarget = false; - delegate = BattleReplayDelegate(replayActions.delegate ?? BattleReplayDelegateData()); - for (final action in replayActions.actions) { - playerTargetIndex = action.options.playerTarget; - enemyTargetIndex = action.options.enemyTarget; - options.random = action.options.random; - options.threshold = action.options.threshold; - options.tailoredExecution = action.options.tailoredExecution; - if (action.type == BattleRecordDataType.skill) { - await _replaySkill(action); - } else if (action.type == BattleRecordDataType.attack) { - await _replayBattle(action); - } - } - delegate = null; - } - - Future _replaySkill(BattleRecordData action) async { - if (action.skill == null) return; - - if (action.svt == null) { - await activateMysticCodeSkill(action.skill!); - } else { - await activateSvtSkill(action.svt!, action.skill!); - } - } - - Future _replayBattle(BattleRecordData action) async { - if (action.attacks == null) return; - - final List actions = []; - for (final attackRecord in action.attacks!) { - final svt = onFieldAllyServants[attackRecord.svt]; - if (svt == null) continue; - - final cardIndex = attackRecord.card; - - CommandCardData? card; - if (attackRecord.isTD) { - card = svt.getNPCard(); - } else if (cardIndex != null) { - final cards = svt.getCards(); - if (cardIndex < 0 || cardIndex >= cards.length) { - continue; - } - card = cards[cardIndex]; - } - - if (card == null) { - continue; - } - card.critical = attackRecord.critical; - - actions.add(CombatAction(svt, card)); - } - - await playerTurn(actions); - } } class StackMismatchException implements Exception { diff --git a/lib/app/modules/battle/battle_simulation.dart b/lib/app/modules/battle/battle_simulation.dart index 37e2f0c41..c3f5e46aa 100644 --- a/lib/app/modules/battle/battle_simulation.dart +++ b/lib/app/modules/battle/battle_simulation.dart @@ -14,6 +14,7 @@ import 'package:chaldea/models/models.dart'; import 'package:chaldea/packages/app_info.dart'; import 'package:chaldea/utils/utils.dart'; import 'package:chaldea/widgets/widgets.dart'; +import '../../battle/interactions/_delegate.dart'; import '../../descriptors/skill_descriptor.dart'; import '../quest/quest.dart'; import 'formation/formation_storage.dart'; @@ -84,7 +85,7 @@ class _BattleSimulationPageState extends State { final replayActions = widget.replayActions; if (replayActions != null) { - await battleData.replay(replayActions); + await replay(replayActions); if (widget.replayTeamId != null && widget.replayTeamId != 0) { battleData.recorder.messageRich(BattleMessageRecord( 'Team ${widget.replayTeamId}', @@ -99,6 +100,72 @@ class _BattleSimulationPageState extends State { if (mounted) setState(() {}); } + // replay + Future replay(BattleShareData replayActions) async { + battleData.recorder.reasons.setReplay('Replaying team'); + options.manualAllySkillTarget = false; + battleData.delegate = BattleReplayDelegate(replayActions.delegate ?? BattleReplayDelegateData()); + for (final action in replayActions.actions) { + battleData.playerTargetIndex = action.options.playerTarget; + battleData.enemyTargetIndex = action.options.enemyTarget; + battleData.updateTargetedIndex(); + + options.random = action.options.random; + options.threshold = action.options.threshold; + options.tailoredExecution = action.options.tailoredExecution; + if (action.type == BattleRecordDataType.skill) { + await _replaySkill(action); + } else if (action.type == BattleRecordDataType.attack) { + await _replayBattle(action); + } + + if (mounted) setState(() {}); + } + battleData.delegate = null; + } + + Future _replaySkill(BattleRecordData action) async { + if (action.skill == null) return; + + if (action.svt == null) { + await battleData.activateMysticCodeSkill(action.skill!); + } else { + await battleData.activateSvtSkill(action.svt!, action.skill!); + } + } + + Future _replayBattle(BattleRecordData action) async { + if (action.attacks == null) return; + + final List actions = []; + for (final attackRecord in action.attacks!) { + final svt = battleData.onFieldAllyServants[attackRecord.svt]; + if (svt == null) continue; + + final cardIndex = attackRecord.card; + + CommandCardData? card; + if (attackRecord.isTD) { + card = svt.getNPCard(); + } else if (cardIndex != null) { + final cards = svt.getCards(); + if (cardIndex < 0 || cardIndex >= cards.length) { + continue; + } + card = cards[cardIndex]; + } + + if (card == null) { + continue; + } + card.critical = attackRecord.critical; + + actions.add(CombatAction(svt, card)); + } + + await battleData.playerTurn(actions); + } + @override Widget build(final BuildContext context) { return Scaffold(