Initial web socket client support
This commit is contained in:
@@ -23,3 +23,51 @@ class CreateRoomResponse {
|
||||
|
||||
Map<String, dynamic> toJson() => _$CreateRoomResponseToJson(this);
|
||||
}
|
||||
|
||||
sealed class GameRoomMessage {
|
||||
const GameRoomMessage();
|
||||
|
||||
factory GameRoomMessage.fromJson(Map<String, dynamic> json) {
|
||||
return switch (json['type']) {
|
||||
'ping' => PingMessage.fromJson(json),
|
||||
'playerVote' => PlayerVoteMessage.fromJson(json),
|
||||
_ => throw Exception('Unknown message type'),
|
||||
};
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson();
|
||||
}
|
||||
|
||||
class PingMessage extends GameRoomMessage {
|
||||
final DateTime timestamp;
|
||||
|
||||
const PingMessage({required this.timestamp});
|
||||
|
||||
factory PingMessage.fromJson(Map<String, dynamic> json) =>
|
||||
PingMessage(timestamp: DateTime.parse(json['timestamp'] as String));
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() => {
|
||||
'type': 'ping',
|
||||
'timestamp': timestamp.toIso8601String(),
|
||||
};
|
||||
}
|
||||
|
||||
class PlayerVoteMessage extends GameRoomMessage {
|
||||
final String playerUuid;
|
||||
final int vote;
|
||||
|
||||
const PlayerVoteMessage({required this.playerUuid, required this.vote});
|
||||
|
||||
factory PlayerVoteMessage.fromJson(Map<String, dynamic> json) => PlayerVoteMessage(
|
||||
playerUuid: json['playerUuid'] as String,
|
||||
vote: json['vote'] as int,
|
||||
);
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() => {
|
||||
'type': 'playerVote',
|
||||
'playerUuid': playerUuid,
|
||||
'vote': vote,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user