games/packages/feature_common/lib/screens/game_completion_screen.dart
2025-11-14 18:03:50 +09:00

292 lines
9.5 KiB
Dart

import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; // 👈 [추가]
import 'package:service_api/service_api.dart';
import '../models/game_result_args.dart';
// 스도쿠/스파이더와 동일한 enum
enum _RankSubmissionStep { enterName, submitting, showList }
class GameCompletionScreen extends StatefulWidget {
final GameResultArgs args;
const GameCompletionScreen({super.key, required this.args});
@override
State<GameCompletionScreen> createState() => _GameCompletionScreenState();
}
class _GameCompletionScreenState extends State<GameCompletionScreen> {
// 서비스 초기화
final PuzzleService _puzzleService = PuzzleService();
final IdentityService _identityService = IdentityService();
// 상태 변수
late final TextEditingController _nameController;
_RankSubmissionStep _rankStep = _RankSubmissionStep.enterName;
List<GameRankDto> _rankingList = [];
GameRankWithRankNumber? _myRankResult;
String? _dialogErrorMessage;
String _submittedPlayerName = "";
@override
void initState() {
super.initState();
// 🔽 [수정] 세션에서 userName을 가져옴
// (이 화면은 build 이전에 호출되므로 'read' 사용)
final session = context.read<SessionNotifier>().session;
_nameController = TextEditingController(text: session?.userName ?? widget.args.userName);
// 🔽 [수정] 게스트가 아니면, 이름 입력 단계를 건너뛰고 즉시 등록
if (session != null && !session.isGuest) {
_rankStep = _RankSubmissionStep.submitting;
// build가 완료된 후 등록 시작
WidgetsBinding.instance.addPostFrameCallback((_) {
// [중요] 세션의 userName으로 자동 제출
_submitRank(autoSubmitName: session.userName);
});
}
}
@override
void dispose() {
_nameController.dispose();
super.dispose();
}
/// 랭킹 등록 로직 (공통화)
// 🔽 [수정] _submitRank가 자동 제출용 이름을 받도록
Future<void> _submitRank({String? autoSubmitName}) async {
String playerName;
// 자동 제출(로그인 상태)이 아니면(게스트면), 컨트롤러에서 이름을 가져옴
if (autoSubmitName == null) {
playerName = _nameController.text.trim();
if (playerName.isEmpty) {
setState(() { _dialogErrorMessage = "이름을 입력해주세요."; });
return;
}
} else {
playerName = autoSubmitName;
}
setState(() {
_rankStep = _RankSubmissionStep.submitting;
_submittedPlayerName = playerName;
_dialogErrorMessage = null;
});
final rankDto = UnifiedRankDto(
userId: widget.args.userId,
gameType: widget.args.gameType,
contextId: widget.args.contextId,
playerName: playerName, // 👈 [수정]
primaryScore: widget.args.primaryScore,
secondaryScore: widget.args.secondaryScore,
);
try {
// 1. 랭킹 등록
final RankSubmissionResult result = await _puzzleService.submitRank(rankDto);
// 2. 이름 저장 (공통)
// [수정] 게스트일 때만 이름을 저장 (소셜 로그인은 이미 이름이 있음)
if (autoSubmitName == null) {
await _identityService.saveUserName(playerName);
}
// 3. 게임별 후속 처리 (레벨 잠금 해제 등)
await widget.args.onProgressSave(playerName);
setState(() {
_rankingList = result.topRanks;
_myRankResult = result.myRank;
_rankStep = _RankSubmissionStep.showList;
});
} catch (e) {
log("!!! 랭킹 등록 실패 !!!", error: e);
setState(() {
_rankStep = _RankSubmissionStep.enterName;
// 🔽 [수정] 게스트가 아닐 때 실패하면, 이름 입력창 대신 리스트로 보냄
if (autoSubmitName != null) {
_rankStep = _RankSubmissionStep.showList;
}
_dialogErrorMessage = e.toString().replaceFirst("Exception: ", "");
});
}
}
/// 닫기 버튼 로직
void _closeScreen() {
// 1. 이 팝업 화면을 닫고
Navigator.of(context).pop();
// 2. 이전 화면(게임 화면)을 닫도록 콜백 호출
widget.args.onScreenClose();
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
// --- UI 섹션 정의 (스도쿠/스파이더와 동일) ---
Widget topRankListWidget = _rankingList.isEmpty
? const Center(child: Text("현재 랭킹이 없습니다."))
: ListView.builder(
itemCount: _rankingList.length,
shrinkWrap: true,
itemBuilder: (context, index) {
final rank = _rankingList[index];
final bool isMe = rank.playerName == _submittedPlayerName;
// [수정] 점수 포맷터를 주입받은 함수로 대체
final String scoreText = widget.args.scoreFormatter(
rank.primaryScore,
rank.secondaryScore
);
return ListTile(
selected: isMe,
selectedTileColor: theme.primaryColor.withOpacity(0.1),
leading: Text('${index + 1}.', style: const TextStyle(fontWeight: FontWeight.bold)),
title: Text(rank.playerName, style: TextStyle(fontWeight: isMe ? FontWeight.bold : FontWeight.normal)),
trailing: Text(
scoreText,
style: TextStyle(
fontWeight: FontWeight.bold,
color: theme.textTheme.bodyMedium?.color?.withOpacity(0.9)
)
),
);
},
);
Widget? myRankWidget;
if (_myRankResult != null) {
final myRank = _myRankResult!.rankData;
final myRankNum = _myRankResult!.rankNumber;
bool isMeInTop10 = _rankingList.any(
(topRank) => topRank.playerName == myRank.playerName
);
if (!isMeInTop10) {
// [수정] 점수 포맷터를 주입받은 함수로 대체
final String scoreText = widget.args.scoreFormatter(
myRank.primaryScore,
myRank.secondaryScore
);
myRankWidget = Padding(
padding: const EdgeInsets.only(top: 8.0),
child: ListTile(
selected: true,
selectedTileColor: theme.primaryColor.withOpacity(0.1),
leading: Text('$myRankNum.', style: const TextStyle(fontWeight: FontWeight.bold)),
title: Text(myRank.playerName, style: const TextStyle(fontWeight: FontWeight.bold)),
trailing: Text(
scoreText,
style: TextStyle(
fontWeight: FontWeight.bold,
color: theme.textTheme.bodyMedium?.color?.withOpacity(0.9)
)
),
),
);
}
}
Widget rankDisplaySection = Column(
children: [
if (_dialogErrorMessage != null)
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Text(_dialogErrorMessage!, style: TextStyle(color: theme.colorScheme.error)),
),
Expanded(child: topRankListWidget),
if (myRankWidget != null) ...[
const Divider(height: 16, thickness: 1),
myRankWidget,
],
],
);
Widget nameEntryWidget = Column(
mainAxisSize: MainAxisSize.min,
children: [
// [수정] 게임별 점수 표시 대신 범용 텍스트
Text(
'축하합니다! 랭킹에 등록할 이름을 입력하세요.',
style: theme.textTheme.titleMedium,
),
const SizedBox(height: 20),
TextField(
controller: _nameController,
autofocus: true,
maxLength: 20, // [수정] 10 -> 20
decoration: InputDecoration(
labelText: '이름 (20자 이내)',
border: const OutlineInputBorder(),
errorText: _dialogErrorMessage,
),
),
],
);
// --- 상태에 따라 UI와 버튼 결정 ---
Widget content;
List<Widget> actions = [];
String titleText;
if (_rankStep == _RankSubmissionStep.enterName) {
titleText = '🎉 게임 완료!';
content = nameEntryWidget;
actions = [
TextButton(
onPressed: _closeScreen, // 닫기
child: const Text('나중에 하기'),
),
ElevatedButton(
onPressed: () => _submitRank(), // 👈 [수정] 인자 없이 호출
child: const Text('랭킹 등록'),
),
];
}
else if (_rankStep == _RankSubmissionStep.submitting) {
titleText = '랭킹 등록 중...';
content = const Center(child: CircularProgressIndicator());
// 로딩 중에는 버튼 없음
}
else { // _RankSubmissionStep.showList
titleText = '🏆 랭킹 (${widget.args.contextId})';
content = rankDisplaySection;
actions = [
TextButton(
onPressed: _closeScreen, // 닫기
child: const Text('닫기'),
)
];
}
// [수정] AlertDialog가 아닌 전체 화면 Scaffold로 변경
return Scaffold(
appBar: AppBar(
title: Text(titleText),
automaticallyImplyLeading: false, // 뒤로가기 버튼 숨김
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: content,
),
bottomNavigationBar: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: actions,
),
),
);
}
}