337 lines
14 KiB
Dart
337 lines
14 KiB
Dart
import 'dart:developer';
|
|
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/models/sudoku_game_dto.dart';
|
|
import 'package:sudoku_app/models/sudoku_theme.dart';
|
|
import 'package:sudoku_app/screens/game_screen.dart';
|
|
import 'package:sudoku_app/screens/ranking_screen.dart';
|
|
import 'package:sudoku_app/services/puzzle_service.dart';
|
|
import 'package:sudoku_app/services/identity_service.dart';
|
|
import 'package:sudoku_app/widgets/ad_banner_widget.dart';
|
|
|
|
// 🔽 [삭제] RankChangeStatus enum은 더 이상 필요하지 않습니다.
|
|
|
|
class HomeScreen extends StatefulWidget {
|
|
const HomeScreen({super.key});
|
|
|
|
@override
|
|
State<HomeScreen> createState() => _HomeScreenState();
|
|
}
|
|
|
|
class _HomeScreenState extends State<HomeScreen> {
|
|
int _maxUnlockedLevel = 1;
|
|
// 🔽 [수정] 랭킹 변동 상태 대신 (이전 랭킹, 현재 랭킹) 튜플을 저장합니다.
|
|
// (Key: levelIndex, Value: (oldRank, currentRank))
|
|
// (0은 랭킹에 없음을 의미)
|
|
Map<int, (int, int)> _rankHistory = {};
|
|
String? _userName;
|
|
|
|
late String _selectedThemeName;
|
|
bool _isLoading = false;
|
|
|
|
final PuzzleService _puzzleService = PuzzleService();
|
|
final IdentityService _identityService = IdentityService();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_selectedThemeName = AppThemes.random;
|
|
_loadProgress();
|
|
}
|
|
|
|
// 🔽 [삭제] _calculateRankStatus 헬퍼 함수는 더 이상 필요하지 않습니다.
|
|
|
|
// 🔽 [수정] 랭킹 변동 맵을 (int, int) 튜플로 저장하도록 수정
|
|
Future<void> _loadProgress() async {
|
|
// 1. 기본 정보 로드 (레벨, 유저 이름)
|
|
final maxLevel = await _identityService.getMaxUnlockedLevel();
|
|
final String? myName = await _identityService.getSavedUserName();
|
|
|
|
if (mounted) {
|
|
setState(() {
|
|
_maxUnlockedLevel = maxLevel;
|
|
_userName = myName;
|
|
});
|
|
}
|
|
|
|
if (myName == null) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
// 3-1. 이전에 저장된 랭킹 맵 로드
|
|
final Map<int, int> oldRankMap = await _identityService.getLastSavedRankMap();
|
|
|
|
// 3-2. 모든 레벨의 랭킹을 병렬로 조회
|
|
List<Future<List<GameRankDto>>> rankFutures = [];
|
|
for (final level in AppLevels.allLevels) {
|
|
rankFutures.add(_puzzleService.fetchRanks('SUDOKU', level.contextId));
|
|
}
|
|
|
|
final List<List<GameRankDto>> allRankResults = await Future.wait(rankFutures);
|
|
|
|
// 3-4. 결과 분석
|
|
Map<int, int> newRankMapForStorage = {}; // 👈 다음에 저장할 새로운 랭킹 맵
|
|
Map<int, (int, int)> newRankHistoryForState = {}; // 👈 UI에 반영할 변동 맵
|
|
|
|
for (int i = 0; i < AppLevels.allLevels.length; i++) {
|
|
final level = AppLevels.allLevels[i];
|
|
final currentRanks = allRankResults[i];
|
|
final int levelIndex = level.levelIndex;
|
|
|
|
// 3-5. 이 레벨의 이전 랭킹 (없으면 0)
|
|
final int oldRank = oldRankMap[levelIndex] ?? 0;
|
|
|
|
// 3-6. 이 레벨의 현재 랭킹 (없으면 0)
|
|
int currentRank = 0;
|
|
int myRankIndex = currentRanks.indexWhere((r) => r.playerName == myName);
|
|
if (myRankIndex != -1) {
|
|
currentRank = myRankIndex + 1; // 1-based 순위
|
|
}
|
|
|
|
// 3-7. 상태 저장 (숫자 쌍 자체를 저장)
|
|
newRankMapForStorage[levelIndex] = currentRank;
|
|
newRankHistoryForState[levelIndex] = (oldRank, currentRank);
|
|
}
|
|
|
|
// 4. 새로운 랭킹 맵을 로컬에 저장 (다음 실행 시 비교용)
|
|
await _identityService.saveLastRankMap(newRankMapForStorage);
|
|
|
|
// 5. UI 상태 업데이트
|
|
if (mounted) {
|
|
setState(() {
|
|
_rankHistory = newRankHistoryForState; // 👈 튜플 맵으로 UI 상태 업데이트
|
|
});
|
|
}
|
|
log("모든 레벨 랭킹 변동 확인 완료. (유저: $myName)");
|
|
|
|
} catch (e) {
|
|
log("HomeScreen: 랭킹 확인 실패: $e");
|
|
}
|
|
}
|
|
|
|
|
|
// (startGame 함수는 변경 없음)
|
|
Future<void> _startGame(GameLevel level) async {
|
|
setState(() { _isLoading = true; });
|
|
|
|
try {
|
|
final String difficulty = level.levelIndex.toString();
|
|
final SudokuGameDto gameData = await _puzzleService.startGame(difficulty);
|
|
|
|
final String userId = await _identityService.getOrCreateUserId();
|
|
final String? userName = _userName;
|
|
|
|
if (mounted) {
|
|
await Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => GameScreen(
|
|
gameData: gameData,
|
|
themeName: _selectedThemeName,
|
|
userId: userId,
|
|
userName: userName,
|
|
levelIndex: level.levelIndex,
|
|
),
|
|
),
|
|
);
|
|
|
|
_loadProgress();
|
|
}
|
|
} catch (e) {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('게임 로딩 실패: $e')),
|
|
);
|
|
}
|
|
} finally {
|
|
if (mounted) {
|
|
setState(() { _isLoading = false; });
|
|
}
|
|
}
|
|
}
|
|
|
|
// 🔽 [수정] build 메소드에서 랭킹 숫자(튜플)를 직접 비교하여 UI 생성
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final bool allLevelsUnlocked = _maxUnlockedLevel >= 99;
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('스도쿠 게임')),
|
|
body: LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
const double maxContentRatio = 0.6;
|
|
final double constrainedWidth = (constraints.maxHeight * maxContentRatio) > 500
|
|
? 500 : (constraints.maxHeight * maxContentRatio);
|
|
|
|
return Center(
|
|
child: ConstrainedBox(
|
|
constraints: BoxConstraints(maxWidth: constrainedWidth),
|
|
child: Column(
|
|
children: [
|
|
// 1. 테마 선택 (변경 없음)
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const Text("테마: ", style: TextStyle(fontSize: 18)),
|
|
DropdownButton<String>(
|
|
value: _selectedThemeName,
|
|
items: AppThemes.selectableThemeNames.map((themeName) {
|
|
return DropdownMenuItem<String>(
|
|
value: themeName,
|
|
child: Text(themeName, style: const TextStyle(fontSize: 20)),
|
|
);
|
|
}).toList(),
|
|
onChanged: (themeName) {
|
|
if (themeName != null) {
|
|
setState(() { _selectedThemeName = themeName; });
|
|
}
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
|
|
// 2. 레벨 선택 리스트
|
|
Expanded(
|
|
child: ListView.builder(
|
|
itemCount: AppLevels.allLevels.length,
|
|
itemBuilder: (context, index) {
|
|
final GameLevel level = AppLevels.allLevels[index];
|
|
final bool isUnlocked = allLevelsUnlocked || level.levelIndex <= _maxUnlockedLevel;
|
|
|
|
// 🔽 [수정] 랭킹 튜플(숫자 쌍)을 가져옵니다.
|
|
final (int oldRank, int currentRank) = _rankHistory[level.levelIndex] ?? (0, 0);
|
|
|
|
// 1. 기본값 설정 (잠금 해제 시 플레이 아이콘, 잠겼으면 null)
|
|
Widget? trailingWidget = isUnlocked ? const Icon(Icons.play_arrow_rounded) : null;
|
|
String? subtitleText;
|
|
Color? subtitleColor;
|
|
|
|
// 🔽 [수정] 랭킹 숫자를 직접 비교하여 UI 텍스트와 아이콘을 결정합니다.
|
|
if (currentRank > 0) {
|
|
// 1. 현재 랭킹이 있는 경우 (1위, 5위 등)
|
|
String rankStr = "${currentRank}위"; // 예: "5위"
|
|
|
|
if (oldRank > 0) {
|
|
// 1a. 이전 랭킹도 있는 경우 (변동 비교)
|
|
int change = oldRank - currentRank; // (7 -> 5) = +2 (상승) | (5 -> 7) = -2 (하락)
|
|
|
|
if (change > 0) { // 순위 상승
|
|
subtitleText = "$rankStr (▲ $change)";
|
|
subtitleColor = Colors.green;
|
|
trailingWidget = const Icon(Icons.arrow_circle_up_rounded, color: Colors.green, size: 28);
|
|
} else if (change < 0) { // 순위 하락
|
|
subtitleText = "$rankStr (▼ ${change.abs()})";
|
|
subtitleColor = Colors.red;
|
|
trailingWidget = const Icon(Icons.arrow_circle_down_rounded, color: Colors.red, size: 28);
|
|
} else { // 순위 유지
|
|
subtitleText = "$rankStr (유지)";
|
|
subtitleColor = Colors.grey;
|
|
trailingWidget = const Icon(Icons.check_circle_outline_rounded, color: Colors.grey, size: 28);
|
|
}
|
|
} else {
|
|
// 1b. 신규 랭킹 진입 (currentRank > 0, oldRank == 0)
|
|
subtitleText = "$rankStr (신규 진입)";
|
|
subtitleColor = Colors.blue;
|
|
trailingWidget = const Icon(Icons.new_releases_rounded, color: Colors.blue, size: 28);
|
|
}
|
|
|
|
} else {
|
|
// 2. 현재 랭킹이 없는 경우 (currentRank == 0)
|
|
if (oldRank > 0) {
|
|
// 2a. 랭킹에서 이탈 (currentRank == 0, oldRank > 0)
|
|
subtitleText = "랭킹 이탈 (이전 ${oldRank}위)";
|
|
subtitleColor = Colors.orange;
|
|
trailingWidget = const Icon(Icons.warning_amber_rounded, color: Colors.orange, size: 28);
|
|
} else {
|
|
// 2b. 랭킹 기록 없음 (currentRank == 0, oldRank == 0)
|
|
// subtitleText는 null, trailingWidget은 기본값(플레이 아이콘) 유지
|
|
}
|
|
}
|
|
// 🔼 [수정] 완료
|
|
|
|
return Card(
|
|
margin: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 4.0),
|
|
child: ListTile(
|
|
leading: Icon(
|
|
isUnlocked ? Icons.lock_open_rounded : Icons.lock_rounded,
|
|
color: isUnlocked ? Colors.blue : Colors.grey,
|
|
),
|
|
title: Text(level.name, style: TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: isUnlocked ? FontWeight.bold : FontWeight.normal,
|
|
color: isUnlocked ? Colors.black : Colors.grey,
|
|
)),
|
|
// 🔽 서브타이틀 표시 (null이 아닐 때만)
|
|
subtitle: subtitleText != null
|
|
? Text(subtitleText, style: TextStyle(color: subtitleColor, fontWeight: FontWeight.bold))
|
|
: null,
|
|
// 🔽 최종 결정된 trailingWidget 표시
|
|
trailing: trailingWidget,
|
|
onTap: isUnlocked && !_isLoading
|
|
? () => _startGame(level)
|
|
: null,
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
|
|
// 3. 랭킹 보기 버튼 (변경 없음 - 이전 스타일 유지)
|
|
Container(
|
|
margin: const EdgeInsets.fromLTRB(16.0, 0, 16.0, 8.0),
|
|
decoration: BoxDecoration(
|
|
color: Colors.grey.shade100,
|
|
borderRadius: const BorderRadius.only(
|
|
topLeft: Radius.circular(12.0),
|
|
topRight: Radius.circular(12.0),
|
|
),
|
|
border: Border.all(color: Colors.grey.shade300),
|
|
),
|
|
child: ClipRRect(
|
|
borderRadius: const BorderRadius.only(
|
|
topLeft: Radius.circular(12.0),
|
|
topRight: Radius.circular(12.0),
|
|
),
|
|
child: InkWell(
|
|
onTap: () {
|
|
final String currentDifficultyName = AppLevels.getLevel(_maxUnlockedLevel).name;
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => RankingScreen(
|
|
initialDifficultyName: currentDifficultyName,
|
|
),
|
|
),
|
|
);
|
|
},
|
|
child: Container(
|
|
width: double.infinity,
|
|
padding: const EdgeInsets.symmetric(vertical: 14.0),
|
|
child: const Text(
|
|
'🏆 전체 랭킹 보기',
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.black87,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
bottomNavigationBar: const AdBannerWidget(),
|
|
);
|
|
}
|
|
} |