WIP ws messages, TODO backend receiving frontend messages
This commit is contained in:
@@ -27,12 +27,11 @@ class CreateRoomResponse {
|
||||
sealed class GameRoomMessage {
|
||||
GameRoomMessage(this.roomUuid);
|
||||
final String roomUuid;
|
||||
abstract final String type;
|
||||
abstract String? type;
|
||||
|
||||
factory GameRoomMessage.fromJson(Map<String, dynamic> json) {
|
||||
return switch (json['type']) {
|
||||
'ping' => PingMessage.fromJson(json),
|
||||
'roomPing' => RoomPingMessage.fromJson(json),
|
||||
'playerVote' => PlayerVoteMessage.fromJson(json),
|
||||
_ => throw Exception('Unknown message type'),
|
||||
};
|
||||
@@ -45,41 +44,24 @@ enum PingDestination { client, server }
|
||||
|
||||
@JsonSerializable()
|
||||
class PingMessage extends GameRoomMessage {
|
||||
DateTime timestamp;
|
||||
late final DateTime timestamp;
|
||||
final PingDestination dest;
|
||||
final String userUuid;
|
||||
String userUuid;
|
||||
|
||||
PingMessage(super.roomUuid, {required this.dest, required this.userUuid}) {
|
||||
timestamp = DateTime.now();
|
||||
PingMessage(super.roomUuid, {required this.dest, required this.userUuid, required this.timestamp}) {
|
||||
type = 'ping';
|
||||
}
|
||||
|
||||
factory PingMessage.now(String roomUuid, {required PingDestination dest, required String userUuid}) =>
|
||||
PingMessage(roomUuid, userUuid: userUuid, dest: dest, timestamp: DateTime.now());
|
||||
|
||||
factory PingMessage.fromJson(Map<String, dynamic> json) => _$PingMessageFromJson(json);
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() => _$PingMessageToJson(this);
|
||||
|
||||
@override
|
||||
late final String type;
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
class RoomPingMessage extends GameRoomMessage {
|
||||
late final DateTime timestamp;
|
||||
final PingDestination dest;
|
||||
|
||||
RoomPingMessage(super.roomUuid, {required this.dest}) {
|
||||
timestamp = DateTime.now();
|
||||
type = 'roomPing';
|
||||
}
|
||||
|
||||
factory RoomPingMessage.fromJson(Map<String, dynamic> json) => _$RoomPingMessageFromJson(json);
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() => _$RoomPingMessageToJson(this);
|
||||
|
||||
@override
|
||||
late final String type;
|
||||
String? type;
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
@@ -97,5 +79,5 @@ class PlayerVoteMessage extends GameRoomMessage {
|
||||
Map<String, dynamic> toJson() => _$PlayerVoteMessageToJson(this);
|
||||
|
||||
@override
|
||||
late final String type;
|
||||
String? type;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user