-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
328 lines (285 loc) · 12 KB
/
App.tsx
File metadata and controls
328 lines (285 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import React, { useState, useEffect } from 'react';
import { GameState, GamePhase, TurnData, AIConfig, Language } from './types';
import { INITIAL_STATS } from './constants';
import { initializeAI, generateNextTurn } from './services/geminiService';
import { StartScreen } from './components/StartScreen';
import { GameUI } from './components/GameUI';
import { EndingScreen } from './components/EndingScreen';
import { AlertTriangle, RefreshCw, Power } from 'lucide-react';
const LOADING_TEXTS_ZH = [
"正在建立加密卫星连接...",
"同步加拉加斯战术地图...",
"计算地缘政治蝴蝶效应...",
"解密敌方通讯频道...",
"加载历史决策树...",
"分析公众情绪数据...",
"渲染战术模拟层..."
];
const LOADING_TEXTS_EN = [
"ESTABLISHING ENCRYPTED SAT-LINK...",
"SYNCING TACTICAL MAPS...",
"CALCULATING GEOPOLITICAL BUTTERFLY EFFECT...",
"DECRYPTING ENEMY CHANNELS...",
"LOADING DECISION TREES...",
"ANALYZING PUBLIC SENTIMENT...",
"RENDERING TACTICAL LAYER..."
];
const App: React.FC = () => {
const [gameState, setGameState] = useState<GameState>({
turnNumber: 0,
phase: GamePhase.POLITICAL,
stats: INITIAL_STATS,
history: [],
currentTurnData: null,
isLoading: false,
error: null,
language: 'zh'
});
const [previousStats, setPreviousStats] = useState(INITIAL_STATS);
const [hasStarted, setHasStarted] = useState(false);
const [currentConfig, setCurrentConfig] = useState<AIConfig | null>(null);
const [loadingProgress, setLoadingProgress] = useState(0);
const [loadingText, setLoadingText] = useState("");
const texts = gameState.language === 'zh' ? LOADING_TEXTS_ZH : LOADING_TEXTS_EN;
useEffect(() => {
let interval: ReturnType<typeof setInterval>;
if (gameState.isLoading) {
setLoadingProgress(0);
setLoadingText(texts[0]);
interval = setInterval(() => {
setLoadingProgress(prev => {
const remaining = 100 - prev;
const increment = Math.max(0.1, Math.random() * (remaining / 10));
const next = prev + increment;
if (next > 20 && next < 25) setLoadingText(texts[1]);
if (next > 40 && next < 45) setLoadingText(texts[2]);
if (next > 60 && next < 65) setLoadingText(texts[3]);
if (next > 80 && next < 85) setLoadingText(texts[4]);
return next >= 95 ? 95 : next;
});
}, 200);
} else {
setLoadingProgress(100);
}
return () => clearInterval(interval);
}, [gameState.isLoading, gameState.language]);
const handleStart = async (config: AIConfig) => {
try {
setCurrentConfig(config);
initializeAI(config);
setHasStarted(true);
// Ensure fresh state on start
const cleanState = {
turnNumber: 0,
phase: GamePhase.POLITICAL,
stats: INITIAL_STATS,
history: [],
currentTurnData: null,
isLoading: true,
error: null,
language: config.language
};
setGameState(cleanState);
const firstTurn = await generateNextTurn({ ...cleanState, turnNumber: 1 }, null);
setGameState(prev => ({
...prev,
turnNumber: 1,
currentTurnData: firstTurn,
isLoading: false
}));
} catch (e: any) {
console.error("Initialization Error:", e);
let errorMessage = config.language === 'zh' ? "连接建立失败" : "Connection Failed";
if (e.message && e.message.includes("fetch")) {
errorMessage = config.language === 'zh' ? "网络连接错误 (CORS/Network)" : "Network Error (CORS)";
} else if (e.message) {
errorMessage = e.message;
}
setGameState(prev => ({ ...prev, isLoading: false, error: errorMessage }));
}
};
const handleOptionSelect = async (optionId: string, text: string) => {
if (gameState.isLoading) return;
const newHistory = [
...gameState.history,
{
turn: gameState.turnNumber,
narrativeSummary: gameState.currentTurnData?.narrative.slice(0, 100) || "",
choiceMade: text
}
];
let nextPhase = gameState.phase;
if (gameState.turnNumber > 10) nextPhase = GamePhase.SIEGE;
if (gameState.turnNumber > 25) nextPhase = GamePhase.ESCAPE;
const currentStatsDelta = gameState.currentTurnData?.statsDelta || {};
const nextStats = {
publicSupport: Math.max(0, Math.min(100, gameState.stats.publicSupport + (currentStatsDelta.publicSupport || 0))),
militaryLoyalty: Math.max(0, Math.min(100, gameState.stats.militaryLoyalty + (currentStatsDelta.militaryLoyalty || 0))),
securityLevel: Math.max(0, Math.min(100, gameState.stats.securityLevel + (currentStatsDelta.securityLevel || 0))),
panic: Math.max(0, Math.min(100, gameState.stats.panic + (currentStatsDelta.panic || 0))),
};
// 保存当前状态为 previousStats
setPreviousStats(gameState.stats);
setGameState(prev => ({
...prev,
isLoading: true,
history: newHistory,
phase: nextPhase,
stats: nextStats,
error: null
}));
try {
const nextTurnData = await generateNextTurn({
...gameState,
turnNumber: gameState.turnNumber + 1,
history: newHistory,
stats: nextStats,
phase: nextPhase
}, text);
setGameState(prev => ({
...prev,
turnNumber: prev.turnNumber + 1,
currentTurnData: nextTurnData,
isLoading: false
}));
} catch (e: any) {
console.error("Turn Generation Error:", e);
let errorMessage = gameState.language === 'zh' ? "通讯中断" : "Signal Lost";
if (e.message) errorMessage = e.message;
setGameState(prev => ({ ...prev, isLoading: false, error: errorMessage }));
}
};
const handleReboot = () => {
setGameState(prev => ({
...prev,
error: null,
isLoading: false,
turnNumber: 0,
history: [],
currentTurnData: null,
stats: INITIAL_STATS,
phase: GamePhase.POLITICAL
}));
setHasStarted(false);
};
const handleRetryAction = async () => {
if (!currentConfig) return;
setGameState(prev => ({ ...prev, isLoading: true, error: null }));
try {
// Scenario A: Initialization failed (Turn 0/1 transition)
if (gameState.turnNumber === 0) {
const firstTurn = await generateNextTurn({ ...gameState, language: currentConfig.language, turnNumber: 1 }, null);
setGameState(prev => ({
...prev,
turnNumber: 1,
currentTurnData: firstTurn,
isLoading: false
}));
}
// Scenario B: Mid-game turn failed
else {
const lastHistoryItem = gameState.history[gameState.history.length - 1];
const lastChoiceText = lastHistoryItem ? lastHistoryItem.choiceMade : null;
// Note: The stats and phase were already updated optimistically before the error occurred.
// We calculate target turn number based on current state logic.
// If error happened during turn 2->3 generation, turnNumber is 2, history has 2 items. Target is 3.
const nextTurnData = await generateNextTurn({
...gameState,
turnNumber: gameState.turnNumber + 1
}, lastChoiceText);
setGameState(prev => ({
...prev,
turnNumber: prev.turnNumber + 1,
currentTurnData: nextTurnData,
isLoading: false
}));
}
} catch (e: any) {
console.error("Retry Error:", e);
let errorMessage = gameState.language === 'zh' ? "通讯中断" : "Signal Lost";
if (e.message) errorMessage = e.message;
setGameState(prev => ({ ...prev, isLoading: false, error: errorMessage }));
}
};
if (gameState.error) {
const isEn = gameState.language === 'en';
return (
<div className="h-screen w-full bg-stone-950 flex flex-col items-center justify-center text-red-500 font-tech p-8 relative overflow-hidden">
<div className="absolute inset-0 bg-[repeating-linear-gradient(0deg,transparent,transparent_2px,#ff000010_3px)] pointer-events-none"></div>
<AlertTriangle size={64} className="mb-6 animate-pulse" />
<h2 className="text-3xl uppercase tracking-widest mb-4 border-b-2 border-red-800 pb-2">CRITICAL ERROR</h2>
<p className="text-xl text-red-400 mb-8 text-center max-w-2xl bg-black/50 p-4 border border-red-900/50 font-mono">
{gameState.error}
</p>
<div className="flex gap-4">
<button
onClick={handleRetryAction}
className="flex items-center gap-2 px-8 py-3 bg-amber-900/20 border border-amber-600 text-amber-500 hover:bg-amber-800 hover:text-white transition-all uppercase tracking-widest text-sm"
>
<RefreshCw size={16} /> {isEn ? "RETRY CONNECTION" : "重试连接"}
</button>
<button
onClick={handleReboot}
className="flex items-center gap-2 px-8 py-3 bg-red-900/20 border border-red-600 hover:bg-red-800 hover:text-white transition-all uppercase tracking-widest text-sm"
>
<Power size={16} /> {isEn ? "REBOOT SYSTEM" : "重启系统"}
</button>
</div>
</div>
);
}
if (!hasStarted) {
return <StartScreen onStart={handleStart} />;
}
if (gameState.isLoading) {
return (
<div className="h-screen w-full bg-stone-950 flex flex-col items-center justify-center text-amber-600 font-tech relative overflow-hidden z-50">
<div className="absolute inset-0 bg-[repeating-linear-gradient(45deg,transparent,transparent_2px,rgba(245,158,11,0.03)_2px,rgba(245,158,11,0.03)_4px)] opacity-30"></div>
<div className="absolute top-0 w-full h-1 bg-amber-900/50"></div>
<div className="absolute bottom-0 w-full h-1 bg-amber-900/50"></div>
<div className="relative z-10 flex flex-col items-center w-full max-w-md px-4">
<div className="w-16 h-16 border-4 border-amber-900/30 border-t-amber-500 rounded-full animate-spin mb-8 shadow-[0_0_30px_rgba(245,158,11,0.2)]"></div>
<h2 className="text-2xl tracking-[0.2em] mb-2 animate-pulse">{gameState.language === 'zh' ? "正在演算战术推演..." : "RUNNING TACTICAL SIM"}</h2>
<p className="text-xs text-amber-800 uppercase tracking-widest mb-6">PROCESSING TENSORS</p>
<div className="w-full h-4 bg-stone-900 border border-amber-900/50 relative overflow-hidden mb-2">
<div
className="h-full bg-amber-600 shadow-[0_0_10px_rgba(217,119,6,0.5)] transition-all duration-200 ease-out"
style={{ width: `${loadingProgress}%` }}
></div>
<div className="absolute inset-0 bg-[repeating-linear-gradient(90deg,transparent,transparent_2px,rgba(0,0,0,0.5)_3px)] opacity-30"></div>
</div>
<div className="flex justify-between w-full font-mono text-xs text-amber-500/80">
<span>{loadingText}</span>
<span>{Math.floor(loadingProgress)}%</span>
</div>
</div>
<div className="absolute top-0 left-0 w-full h-full bg-gradient-to-b from-transparent via-amber-900/5 to-transparent animate-[scan_3s_linear_infinite] pointer-events-none"></div>
</div>
);
}
if (gameState.currentTurnData?.isGameOver) {
return <EndingScreen data={gameState.currentTurnData} turnCount={gameState.turnNumber} provider={currentConfig?.provider} />;
}
return (
<>
{gameState.currentTurnData && (
<GameUI
turnData={gameState.currentTurnData}
stats={gameState.stats}
previousStats={previousStats}
turnNumber={gameState.turnNumber}
onOptionSelect={handleOptionSelect}
language={gameState.language}
allowCustomInput={currentConfig?.provider !== 'local'}
/>
)}
<style>{`
@keyframes scan {
0% { transform: translateY(-100%); }
100% { transform: translateY(100%); }
}
`}</style>
</>
);
};
export default App;