This commit is contained in:
2025-11-24 17:53:00 +09:00
parent e992a5ca5e
commit dde81cab65
34 changed files with 3718 additions and 469 deletions
+52
View File
@@ -0,0 +1,52 @@
import 'dart:convert';
/// 패킷의 종류 (라우팅 기준)
enum PacketType {
system, // 시스템 (레디, 시작, 종료, 핸드셰이크 등)
chat, // 채팅 (GlobalChatManager로 전달)
game, // 게임 로직 (GameController로 전달)
media,
unknown
}
class PlayPacket {
final PacketType type;
final String senderId; // 보낸 사람 ID
final dynamic payload; // 실제 데이터 (Map, List, String 등)
final int timestamp;
PlayPacket({
required this.type,
required this.senderId,
required this.payload,
required this.timestamp,
});
// JSON -> 객체
factory PlayPacket.fromJson(Map<String, dynamic> json) {
return PlayPacket(
type: _parseType(json['type']),
senderId: json['senderId'] ?? 'unknown',
payload: json['payload'],
timestamp: json['timestamp'] ?? DateTime.now().millisecondsSinceEpoch,
);
}
// 객체 -> JSON
Map<String, dynamic> toJson() {
return {
'type': type.name, // enum을 문자열로 ('chat', 'game'...)
'senderId': senderId,
'payload': payload,
'timestamp': timestamp,
};
}
static PacketType _parseType(String? typeStr) {
for (var t in PacketType.values) {
if (t.name == typeStr) return t;
}
// 호환성: 기존 레거시 메시지(PING, ANSWER_SUBMIT 등)는 'unknown'이나 별도 처리
return PacketType.unknown;
}
}
+10 -7
View File
@@ -3,51 +3,54 @@ import 'package:equatable/equatable.dart';
class UserInfo extends Equatable {
final String id;
final String nickname;
final int avatarIndex; // 프로필 이미지 대신 사용할 아바타 번호 (0~9 등)
final int colorValue; // 유저 고유 컬러 (ARGB int)
final int avatarIndex;
final int colorValue;
final bool isReady; // [추가] 준비 상태
const UserInfo({
required this.id,
required this.nickname,
this.avatarIndex = 0,
this.colorValue = 0xFF2196F3, // 기본값 Blue
this.colorValue = 0xFF2196F3,
this.isReady = false, // 기본값 false
});
/// JSON -> Object 변환 (네트워크 수신 시)
factory UserInfo.fromJson(Map<String, dynamic> json) {
return UserInfo(
id: json['id'] as String,
nickname: json['nickname'] as String,
avatarIndex: json['avatarIndex'] as int? ?? 0,
colorValue: json['colorValue'] as int? ?? 0xFF2196F3,
isReady: json['isReady'] as bool? ?? false, // JSON 파싱 추가
);
}
/// Object -> JSON 변환 (네트워크 전송 시)
Map<String, dynamic> toJson() {
return {
'id': id,
'nickname': nickname,
'avatarIndex': avatarIndex,
'colorValue': colorValue,
'isReady': isReady, // JSON 변환 추가
};
}
/// 복사본 생성 (불변 객체 수정용)
UserInfo copyWith({
String? id,
String? nickname,
int? avatarIndex,
int? colorValue,
bool? isReady, // copyWith 추가
}) {
return UserInfo(
id: id ?? this.id,
nickname: nickname ?? this.nickname,
avatarIndex: avatarIndex ?? this.avatarIndex,
colorValue: colorValue ?? this.colorValue,
isReady: isReady ?? this.isReady,
);
}
@override
List<Object?> get props => [id, nickname, avatarIndex, colorValue];
List<Object?> get props => [id, nickname, avatarIndex, colorValue, isReady];
}