Skip to content

Commit

Permalink
Replays update targetIndex & states after each action
Browse files Browse the repository at this point in the history
  • Loading branch information
SharpnelXu committed Oct 24, 2024
1 parent 16d8a2d commit d67c395
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 69 deletions.
74 changes: 6 additions & 68 deletions lib/app/battle/models/battle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ class BattleData {
}
}

_updateTargetedIndex();
updateTargetedIndex();

final List<BattleServantData?> allActors = [
...onFieldEnemies,
Expand Down Expand Up @@ -525,7 +525,7 @@ class BattleData {
}
}

_updateTargetedIndex();
updateTargetedIndex();

final List<BattleServantData?> newEnemies = [...onFieldEnemies, ...backupEnemies];
for (final actor in newEnemies) {
Expand Down Expand Up @@ -1035,7 +1035,7 @@ class BattleData {
await _nextTurn();
}

_updateTargetedIndex();
updateTargetedIndex();
},
);
}
Expand Down Expand Up @@ -1148,7 +1148,7 @@ class BattleData {
checkActorStatus();
});

_updateTargetedIndex();
updateTargetedIndex();
},
);
}
Expand Down Expand Up @@ -1413,7 +1413,7 @@ class BattleData {
Future<void> _removeDeadActors() async {
await _removeDeadActorsFromList(onFieldAllyServants);
await _removeDeadActorsFromList(onFieldEnemies);
_updateTargetedIndex();
updateTargetedIndex();

if (niceQuest != null && niceQuest!.flags.contains(QuestFlag.enemyImmediateAppear)) {
await _replenishActors(replenishAlly: false);
Expand Down Expand Up @@ -1450,7 +1450,7 @@ class BattleData {
}
}

void _updateTargetedIndex() {
void updateTargetedIndex() {
playerTargetIndex = getNonNullTargetIndex(onFieldAllyServants, playerTargetIndex, false);
enemyTargetIndex = getNonNullTargetIndex(onFieldEnemies, enemyTargetIndex, true);
}
Expand Down Expand Up @@ -1614,68 +1614,6 @@ class BattleData {
..replayDataRecord = copy.replayDataRecord
..deadAttackCommandDict = copy.deadAttackCommandDict.map((key, value) => MapEntry(key, value.copy()));
}

// replay
Future<void> 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<void> _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<void> _replayBattle(BattleRecordData action) async {
if (action.attacks == null) return;

final List<CombatAction> 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 {
Expand Down
69 changes: 68 additions & 1 deletion lib/app/modules/battle/battle_simulation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -84,7 +85,7 @@ class _BattleSimulationPageState extends State<BattleSimulationPage> {

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}',
Expand All @@ -99,6 +100,72 @@ class _BattleSimulationPageState extends State<BattleSimulationPage> {
if (mounted) setState(() {});
}

// replay
Future<void> 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<void> _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<void> _replayBattle(BattleRecordData action) async {
if (action.attacks == null) return;

final List<CombatAction> 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(
Expand Down

0 comments on commit d67c395

Please sign in to comment.