轻松聘
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

301 lines
7.7 KiB

import { Ionicons } from '@expo/vector-icons';
import { useRouter } from 'expo-router';
import {
ActivityIndicator,
Pressable,
ScrollView,
StyleSheet,
Text,
View,
} from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { useGameData } from '../src/hooks';
import { colors } from '../src/theme';
import type { GameLevel, GameLevelStatus, GameProgress } from '../src/types';
/** 关卡状态 → 样式配置 */
const LEVEL_STATUS_STYLE: Record<
GameLevelStatus,
{ bg: string; border: string; icon: string; text: string }
> = {
locked: {
bg: colors.borderLight,
border: colors.border,
icon: colors.textHint,
text: colors.textHint,
},
available: {
bg: colors.primaryLight,
border: colors.primaryBorder,
icon: colors.primary,
text: colors.primaryText,
},
completed: {
bg: colors.successLight,
border: colors.successBorder,
icon: colors.success,
text: colors.successText,
},
};
/** 根据进度计算关卡状态 */
function getLevelStatus(level: GameLevel, progress: GameProgress | null): GameLevelStatus {
if (!progress) return 'locked';
if (progress.results.some((r) => r.levelId === level.id && r.completed)) return 'completed';
if (progress.unlockedLevels.includes(level.id)) return 'available';
return 'locked';
}
/* ===================== 子组件:顶部导航 ===================== */
function Header({ onBack }: { onBack: () => void }) {
return (
<View style={styles.header}>
<Pressable style={styles.headerBtn} onPress={onBack} hitSlop={8}>
<Ionicons name="chevron-back" size={24} color={colors.textPrimary} />
</Pressable>
<Text style={styles.headerTitle}>求职通关</Text>
<View style={styles.headerBtn} />
</View>
);
}
/* ===================== 子组件:进度条 ===================== */
function ProgressBar({ percent }: { percent: number }) {
return (
<View style={styles.progressCard}>
<View style={styles.progressHead}>
<Text style={styles.progressLabel}>通关进度</Text>
<Text style={styles.progressValue}>{percent}%</Text>
</View>
<View style={styles.progressTrack}>
<View style={[styles.progressFill, { width: `${percent}%` }]} />
</View>
</View>
);
}
/* ===================== 子组件:关卡卡片 ===================== */
function LevelCard({
level,
status,
onPress,
}: {
level: GameLevel;
status: GameLevelStatus;
onPress: () => void;
}) {
const theme = LEVEL_STATUS_STYLE[status];
const disabled = status === 'locked';
return (
<Pressable
style={[styles.levelCard, { backgroundColor: theme.bg, borderColor: theme.border }]}
onPress={onPress}
disabled={disabled}
>
<View style={[styles.levelIconWrap, { backgroundColor: colors.white }]}>
<Ionicons name={level.icon as keyof typeof Ionicons.glyphMap} size={22} color={theme.icon} />
</View>
<View style={styles.levelBody}>
<Text style={[styles.levelOrder, { color: theme.text }]}>第 {level.order} 关</Text>
<Text style={[styles.levelTitle, { color: colors.textPrimary }]}>{level.title}</Text>
<Text style={[styles.levelSubtitle, { color: colors.textSecondary }]}>
{level.subtitle}
</Text>
</View>
<View style={styles.levelRight}>
{status === 'completed' ? (
<Ionicons name="checkmark-circle" size={26} color={colors.success} />
) : status === 'available' ? (
<Ionicons name="chevron-forward" size={20} color={colors.primary} />
) : (
<Ionicons name="lock-closed" size={20} color={colors.textHint} />
)}
</View>
</Pressable>
);
}
/* ===================== 页面 ===================== */
export default function GameScreen() {
const router = useRouter();
const { levels, progress, loading, error, refresh } = useGameData();
return (
<SafeAreaView style={styles.safeArea} edges={['top']}>
<Header onBack={() => router.back()} />
{loading ? (
<View style={styles.stateBox}>
<ActivityIndicator size="large" color={colors.primary} />
</View>
) : error ? (
<View style={styles.stateBox}>
<Text style={styles.stateText}>加载失败,请重试</Text>
<Pressable style={styles.retryBtn} onPress={refresh}>
<Text style={styles.retryText}>重试</Text>
</Pressable>
</View>
) : levels.length === 0 ? (
<View style={styles.stateBox}>
<Ionicons name="extension-puzzle-outline" size={48} color={colors.textHint} />
<Text style={styles.stateText}>暂无关卡数据</Text>
</View>
) : (
<ScrollView
style={styles.scroll}
contentContainerStyle={styles.content}
showsVerticalScrollIndicator={false}
>
<ProgressBar percent={progress?.totalPercent ?? 0} />
<View style={styles.levelsSection}>
{levels.map((level) => {
const status = getLevelStatus(level, progress);
return (
<LevelCard
key={level.id}
level={level}
status={status}
onPress={() => router.push(`/game/level?id=${level.id}`)}
/>
);
})}
</View>
</ScrollView>
)}
</SafeAreaView>
);
}
const styles = StyleSheet.create({
safeArea: {
flex: 1,
backgroundColor: colors.pageBg,
},
header: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingHorizontal: 8,
height: 48,
backgroundColor: colors.white,
borderBottomWidth: 1,
borderBottomColor: colors.borderLight,
},
headerBtn: {
width: 40,
height: 40,
alignItems: 'center',
justifyContent: 'center',
},
headerTitle: {
fontSize: 17,
fontWeight: '700',
color: colors.textPrimary,
},
scroll: {
flex: 1,
},
content: {
padding: 8,
paddingBottom: 24,
},
progressCard: {
backgroundColor: colors.white,
borderRadius: 8,
padding: 8,
marginBottom: 8,
},
progressHead: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
marginBottom: 10,
},
progressLabel: {
fontSize: 15,
fontWeight: '700',
color: colors.textPrimary,
},
progressValue: {
fontSize: 15,
fontWeight: '700',
color: colors.primary,
},
progressTrack: {
height: 8,
borderRadius: 4,
backgroundColor: colors.border,
},
progressFill: {
height: 8,
borderRadius: 4,
backgroundColor: colors.primary,
},
levelsSection: {
gap: 8,
},
levelCard: {
flexDirection: 'row',
alignItems: 'center',
padding: 8,
borderRadius: 8,
borderWidth: 1,
backgroundColor: colors.white,
},
levelIconWrap: {
width: 44,
height: 44,
borderRadius: 22,
alignItems: 'center',
justifyContent: 'center',
},
levelBody: {
flex: 1,
marginLeft: 12,
},
levelOrder: {
fontSize: 12,
fontWeight: '600',
marginBottom: 2,
},
levelTitle: {
fontSize: 16,
fontWeight: '700',
},
levelSubtitle: {
marginTop: 2,
fontSize: 13,
},
levelRight: {
width: 36,
alignItems: 'center',
justifyContent: 'center',
},
stateBox: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
stateText: {
marginTop: 12,
fontSize: 14,
color: colors.textSecondary,
},
retryBtn: {
marginTop: 16,
paddingHorizontal: 24,
paddingVertical: 8,
borderRadius: 18,
backgroundColor: colors.primaryBtn,
},
retryText: {
fontSize: 13,
fontWeight: '600',
color: colors.white,
},
});