136 lines
4.7 KiB
Dart
136 lines
4.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:sudoku_app/models/game_level.dart';
|
|
import 'package:sudoku_app/models/game_rank_dto.dart';
|
|
import 'package:sudoku_app/services/puzzle_service.dart';
|
|
|
|
class RankingScreen extends StatefulWidget {
|
|
// 🔽 [수정] 홈 화면에서 전달받을 초기 난이도 이름
|
|
final String? initialDifficultyName;
|
|
|
|
const RankingScreen({
|
|
super.key,
|
|
this.initialDifficultyName, // 👈 [수정] 생성자에 이 파라미터 추가
|
|
});
|
|
|
|
@override
|
|
State<RankingScreen> createState() => _RankingScreenState();
|
|
}
|
|
|
|
class _RankingScreenState extends State<RankingScreen> {
|
|
final PuzzleService _puzzleService = PuzzleService();
|
|
late Future<List<GameRankDto>> _rankingFuture;
|
|
|
|
// 9단계 난이도에 맞는 (이름 -> ContextId) 맵
|
|
final Map<String, String> difficultyContexts = {
|
|
for (var level in AppLevels.allLevels) level.name : level.contextId
|
|
};
|
|
late String _selectedDifficultyName;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
String defaultDifficulty = widget.initialDifficultyName ?? "중급 (9x9)";
|
|
|
|
if (!difficultyContexts.containsKey(defaultDifficulty)) {
|
|
defaultDifficulty = "중급 (9x9)";
|
|
}
|
|
|
|
_fetchRanksForDifficulty(defaultDifficulty);
|
|
}
|
|
|
|
void _fetchRanksForDifficulty(String difficultyName) {
|
|
setState(() {
|
|
_selectedDifficultyName = difficultyName;
|
|
_rankingFuture = _puzzleService.fetchRanks('SUDOKU', difficultyContexts[_selectedDifficultyName]);
|
|
});
|
|
}
|
|
|
|
// 시간(초)을 'mm:ss' 형식으로 변환
|
|
String _formatTime(int seconds) {
|
|
final min = (seconds ~/ 60).toString().padLeft(2, '0');
|
|
final sec = (seconds % 60).toString().padLeft(2, '0');
|
|
return '$min:$sec';
|
|
}
|
|
|
|
// (5 - score)로 저장된 값을 -> "SCORE: 5"로 변환
|
|
String _formatScore(int? storedScore) {
|
|
int score = 5 - (storedScore ?? 5);
|
|
return 'SCORE: $score';
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('스도쿠 랭킹')),
|
|
body: Column(
|
|
children: [
|
|
// 1. 난이도 선택 Dropdown
|
|
Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: DropdownButton<String>(
|
|
value: _selectedDifficultyName,
|
|
isExpanded: true,
|
|
items: AppLevels.allLevels.map((level) {
|
|
return DropdownMenuItem<String>(
|
|
value: level.name,
|
|
child: Text(level.name),
|
|
);
|
|
}).toList(),
|
|
onChanged: (String? newValue) {
|
|
if (newValue != null) {
|
|
_fetchRanksForDifficulty(newValue);
|
|
}
|
|
},
|
|
),
|
|
),
|
|
// 2. 랭킹 리스트
|
|
Expanded(
|
|
child: FutureBuilder<List<GameRankDto>>(
|
|
future: _rankingFuture,
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
if (snapshot.hasError) {
|
|
return Center(child: Text('랭킹 로딩 실패: ${snapshot.error}'));
|
|
}
|
|
if (!snapshot.hasData || snapshot.data!.isEmpty) {
|
|
return const Center(child: Text('등록된 랭킹이 없습니다.'));
|
|
}
|
|
|
|
final ranks = snapshot.data!;
|
|
return ListView.builder(
|
|
itemCount: ranks.length,
|
|
itemBuilder: (context, index) {
|
|
final rank = ranks[index];
|
|
return ListTile(
|
|
leading: Text(
|
|
'${index + 1}.',
|
|
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
|
),
|
|
title: Text(rank.playerName, style: const TextStyle(fontSize: 18)),
|
|
trailing: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
children: [
|
|
Text(
|
|
_formatTime(rank.primaryScore),
|
|
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Colors.blue),
|
|
),
|
|
Text(
|
|
_formatScore(rank.secondaryScore),
|
|
style: const TextStyle(fontSize: 12, color: Colors.grey),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
} |