45 lines
1.1 KiB
Dart
45 lines
1.1 KiB
Dart
import 'package:mchess/api/move.dart';
|
|
|
|
enum MessageType {
|
|
move,
|
|
colorDetermined;
|
|
|
|
String toJson() => name;
|
|
static MessageType fromJson(String json) => values.byName(json);
|
|
}
|
|
|
|
enum ApiColor {
|
|
black,
|
|
white;
|
|
|
|
String toJson() => name;
|
|
static ApiColor fromJson(String json) => values.byName(json);
|
|
}
|
|
|
|
class ApiWebsocketMessage {
|
|
final MessageType type;
|
|
final ApiMove? move;
|
|
final ApiColor? color;
|
|
|
|
ApiWebsocketMessage(
|
|
{required this.type, required this.move, required this.color});
|
|
|
|
factory ApiWebsocketMessage.fromJson(Map<String, dynamic> json) {
|
|
final type = MessageType.fromJson(json['messageType']);
|
|
ApiWebsocketMessage ret;
|
|
switch (type) {
|
|
case MessageType.colorDetermined:
|
|
ret = ApiWebsocketMessage(
|
|
type: type, move: null, color: ApiColor.fromJson(json['color']));
|
|
break;
|
|
case MessageType.move:
|
|
ret = ApiWebsocketMessage(
|
|
type: type, move: ApiMove.fromJson(json['move']), color: null);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
Map<String, dynamic> toJson() =>
|
|
{'messageType': type, 'move': move, 'color': color};
|
|
}
|