mchess-client/lib/api/websocket_message.dart
Marco c213d9b1f3 Use position to build board
Now the client considers the position sent by the server to build the
position on the board.
2023-08-14 17:04:25 +02:00

75 lines
1.7 KiB
Dart

import 'package:mchess/api/move.dart';
enum MessageType {
move,
invalidMove,
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;
final String? reason;
final String? position;
ApiWebsocketMessage({
required this.type,
required this.move,
required this.color,
required this.reason,
required this.position,
});
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']),
reason: null,
position: null,
);
break;
case MessageType.move:
ret = ApiWebsocketMessage(
type: type,
move: ApiMove.fromJson(json['move']),
color: null,
reason: null,
position: json['position'],
);
break;
case MessageType.invalidMove:
ret = ApiWebsocketMessage(
type: type,
move: ApiMove.fromJson(json['move']),
color: null,
reason: json['reason'],
position: null,
);
}
return ret;
}
Map<String, dynamic> toJson() => {
'messageType': type,
'move': move,
'color': color,
};
}