Skip to content

Commit efcd88e

Browse files
committed
Add stop battle functionality
- Add stop button that appears during battle - Add stopBattle() method to handle battle interruption - Show 'Battle stopped' status when user stops - Reset shouldStop flag and hide progress bar in finally block - Add i18n messages for stop/stopping/stopped (EN/CN)
1 parent 6f10ad3 commit efcd88e

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

examples/src/i18n/locales.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export const en = {
6565
custom: {
6666
title: "⚙️ Battle Configuration",
6767
start: "Start Battle",
68+
stop: "Stop Battle",
6869
running: "Fighting...",
6970
simCount: "Simulations:",
7071
hero: "🦸 Hero",
@@ -93,6 +94,8 @@ export const en = {
9394
status: {
9495
running: "🔄 Running {scenario} simulation...",
9596
simulating: "🔄 Simulating battles... {current}/{total} ({percent}%)",
97+
stopping: "⏹️ Stopping battle...",
98+
stopped: "⏹️ Battle stopped",
9699
success: "✅ Simulation complete! Click on any chart point to see detailed battle data.",
97100
error: "❌ Simulation failed: {message}",
98101
cannotModifyLevel: "⚠️ Cannot modify level during battle!",
@@ -173,6 +176,7 @@ export const zhCN = {
173176
custom: {
174177
title: "⚙️ 战斗配置",
175178
start: "开始战斗",
179+
stop: "停止战斗",
176180
running: "战斗中...",
177181
simCount: "模拟次数:",
178182
hero: "🦸 英雄",
@@ -201,6 +205,8 @@ export const zhCN = {
201205
status: {
202206
running: "🔄 正在运行 {scenario} 模拟...",
203207
simulating: "🔄 正在模拟战斗... {current}/{total} ({percent}%)",
208+
stopping: "⏹️ 正在停止战斗...",
209+
stopped: "⏹️ 战斗已停止",
204210
success: "✅ 模拟完成!点击图表上的任意点查看详细战斗数据。",
205211
error: "❌ 模拟失败: {message}",
206212
cannotModifyLevel: "⚠️ 战斗中不能修改等级!",

examples/src/ui/DemoApp.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,13 @@ export class DemoApp {
111111
startCustomBtn.addEventListener('click', (e) => {
112112
e.preventDefault();
113113
e.stopPropagation();
114-
console.log('Start Custom Battle clicked');
115-
if (!this.isRunning) {
114+
if (this.isRunning) {
115+
// Stop battle
116+
console.log('Stop Battle clicked');
117+
this.stopBattle();
118+
} else {
119+
// Start battle
120+
console.log('Start Custom Battle clicked');
116121
this.runCustomBattle();
117122
}
118123
});
@@ -435,11 +440,11 @@ export class DemoApp {
435440
this.setStatus('loading', i18n.t('status.running', { scenario: 'Custom' }));
436441
this.clearOutput();
437442

438-
// Update button state to show running
443+
// Update button state to show stop option
439444
const startBtn = document.getElementById('startCustomBattle');
440445
if (startBtn) {
441446
startBtn.classList.add('running');
442-
startBtn.querySelector('.label')!.textContent = '️ ' + i18n.t('custom.running');
447+
startBtn.querySelector('.label')!.textContent = '️ ' + i18n.t('custom.stop');
443448
}
444449

445450
// Show and update character view
@@ -486,6 +491,7 @@ export class DemoApp {
486491
// Check if should stop
487492
if (this.shouldStop) {
488493
console.log('Simulation stopped by user');
494+
this.setStatus('success', i18n.t('status.stopped'));
489495
return;
490496
}
491497

@@ -515,6 +521,7 @@ export class DemoApp {
515521
// Check if stopped during animation
516522
if (this.shouldStop) {
517523
console.log('Simulation stopped during battle animation');
524+
this.setStatus('success', i18n.t('status.stopped'));
518525
return;
519526
}
520527
} else {
@@ -619,12 +626,16 @@ ${i18n.t('status.battleDetails.hp', { hp: Math.round(lastResult.battleData[lastR
619626
if (progressContainer) progressContainer.style.display = 'none';
620627
} finally {
621628
this.isRunning = false;
629+
this.shouldStop = false;
622630
// Reset button state
623631
const startBtn = document.getElementById('startCustomBattle');
624632
if (startBtn) {
625633
startBtn.classList.remove('running');
626634
startBtn.querySelector('.label')!.textContent = '⚔️ ' + i18n.t('custom.start');
627635
}
636+
// Hide progress bar
637+
const progressContainer = document.getElementById('customProgress');
638+
if (progressContainer) progressContainer.style.display = 'none';
628639
}
629640
}
630641

@@ -903,6 +914,15 @@ ${i18n.t('status.battleDetails.footer')}
903914
}
904915
}
905916

917+
private stopBattle() {
918+
if (!this.isRunning) return;
919+
920+
this.shouldStop = true;
921+
this.setStatus('loading', i18n.t('status.stopping'));
922+
923+
// Button will be reset when the battle loop detects shouldStop
924+
}
925+
906926
private levelWarningToast: HTMLElement | null = null;
907927
private levelWarningTimer: number | null = null;
908928

0 commit comments

Comments
 (0)