...
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'dart:convert'; // 👈 [추가]
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
@@ -5,7 +6,10 @@ import 'package:uuid/uuid.dart';
|
||||
class IdentityService {
|
||||
static const String _userIdKey = 'app_user_id';
|
||||
static const String _userNameKey = 'app_user_name';
|
||||
static const String _maxLevelKey = 'max_unlocked_level'; // 👈 [추가]
|
||||
static const String _maxLevelKey = 'max_unlocked_level';
|
||||
|
||||
// 🔽 [수정] 랭킹 정보를 Map<int, int> 형태로 저장하기 위한 Key
|
||||
static const String _lastRankMapKey = 'last_checked_rank_map';
|
||||
|
||||
// 1. 앱-고유 ID 가져오기 (없으면 생성)
|
||||
Future<String> getOrCreateUserId() async {
|
||||
@@ -13,7 +17,6 @@ class IdentityService {
|
||||
String? userId = prefs.getString(_userIdKey);
|
||||
|
||||
if (userId == null) {
|
||||
// ID가 없으면 V4 UUID 생성
|
||||
userId = const Uuid().v4();
|
||||
await prefs.setString(_userIdKey, userId);
|
||||
}
|
||||
@@ -32,16 +35,46 @@ class IdentityService {
|
||||
await prefs.setString(_userNameKey, name);
|
||||
}
|
||||
|
||||
// 4. 🔽 [추가] 현재 잠금 해제된 최고 레벨 가져오기
|
||||
// 4. 현재 잠금 해제된 최고 레벨 가져오기
|
||||
Future<int> getMaxUnlockedLevel() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
// 최초 실행 시 1 (L1) 반환, 9레벨 클리어 시 99 반환
|
||||
return prefs.getInt(_maxLevelKey) ?? 1;
|
||||
}
|
||||
|
||||
// 5. 🔽 [추가] 새 레벨 잠금 해제
|
||||
// 5. 새 레벨 잠금 해제
|
||||
Future<void> saveMaxUnlockedLevel(int level) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt(_maxLevelKey, level);
|
||||
}
|
||||
|
||||
// 6. 🔽 [수정] 모든 레벨의 랭킹 맵(Map<int, int>) 가져오기
|
||||
Future<Map<int, int>> getLastSavedRankMap() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
String? jsonString = prefs.getString(_lastRankMapKey);
|
||||
|
||||
if (jsonString == null) {
|
||||
return {}; // 저장된 맵이 없으면 빈 맵 반환
|
||||
}
|
||||
|
||||
try {
|
||||
// JSON은 Map<String, dynamic>이므로, 키를 int로 변환
|
||||
final Map<String, dynamic> decodedMap = jsonDecode(jsonString);
|
||||
return decodedMap.map((key, value) => MapEntry(int.parse(key), value as int));
|
||||
} catch (e) {
|
||||
// JSON 파싱 실패 시 빈 맵 반환
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
// 7. 🔽 [수정] 모든 레벨의 랭킹 맵(Map<int, int>) 저장하기
|
||||
Future<void> saveLastRankMap(Map<int, int> rankMap) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
// JSON 저장을 위해 키를 String으로 변환
|
||||
final Map<String, int> stringKeyMap =
|
||||
rankMap.map((key, value) => MapEntry(key.toString(), value));
|
||||
|
||||
String jsonString = jsonEncode(stringKeyMap);
|
||||
await prefs.setString(_lastRankMapKey, jsonString);
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,7 @@ class PuzzleService {
|
||||
}
|
||||
|
||||
// 랭킹 등록
|
||||
Future<void> submitRank(UnifiedRankDto rankDto) async {
|
||||
Future<List<GameRankDto>> submitRank(UnifiedRankDto rankDto) async {
|
||||
|
||||
final requestBody = jsonEncode(rankDto.toJson());
|
||||
log(">>> 랭킹 등록 요청: $requestBody");
|
||||
@@ -55,7 +55,19 @@ class PuzzleService {
|
||||
body: requestBody,
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
// 🔽 [수정] 성공(200) 시, 서버가 반환한 랭킹 리스트를 파싱하여 반환
|
||||
if (response.statusCode == 200) {
|
||||
log("<<< 랭킹 등록 성공: 200 OK (랭킹 목록 반환됨)");
|
||||
try {
|
||||
final List<dynamic> data = jsonDecode(utf8.decode(response.bodyBytes));
|
||||
return data.map((json) => GameRankDto.fromJson(json)).toList();
|
||||
} catch (e) {
|
||||
log("<<< 랭킹 등록 성공했으나, 반환된 랭킹 목록 파싱 실패: $e");
|
||||
throw Exception('랭킹 목록 파싱 실패: $e');
|
||||
}
|
||||
}
|
||||
// 🔽 [수정] 실패 시, 기존 로직과 동일하게 에러 처리
|
||||
else {
|
||||
log("<<< 랭킹 등록 실패: ${response.statusCode}");
|
||||
try {
|
||||
final errorBody = utf8.decode(response.bodyBytes);
|
||||
@@ -65,7 +77,6 @@ class PuzzleService {
|
||||
throw Exception('랭킹 등록 실패: ${response.reasonPhrase}');
|
||||
}
|
||||
}
|
||||
log("<<< 랭킹 등록 성공: 200 OK");
|
||||
}
|
||||
|
||||
// 랭킹 조회
|
||||
|
||||
Reference in New Issue
Block a user