2023-06-08 15:10:48 +00:00
|
|
|
import 'package:mchess/api/move.dart';
|
|
|
|
|
|
|
|
enum MessageType {
|
2023-12-09 19:34:52 +00:00
|
|
|
boardState,
|
2023-06-08 18:23:00 +00:00
|
|
|
move,
|
2023-06-28 10:37:59 +00:00
|
|
|
invalidMove,
|
2023-06-08 15:10:48 +00:00
|
|
|
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;
|
2023-12-09 19:34:52 +00:00
|
|
|
final ApiColor? turnColor;
|
|
|
|
final ApiColor? playerColor;
|
2023-06-28 10:37:59 +00:00
|
|
|
final String? reason;
|
2023-08-14 15:04:25 +00:00
|
|
|
final String? position;
|
2023-09-04 19:39:51 +00:00
|
|
|
final ApiCoordinate? squareInCheck;
|
2023-06-08 15:10:48 +00:00
|
|
|
|
2023-08-14 15:04:25 +00:00
|
|
|
ApiWebsocketMessage({
|
|
|
|
required this.type,
|
|
|
|
required this.move,
|
2023-12-09 19:34:52 +00:00
|
|
|
required this.turnColor,
|
|
|
|
required this.playerColor,
|
2023-08-14 15:04:25 +00:00
|
|
|
required this.reason,
|
|
|
|
required this.position,
|
2023-09-04 19:39:51 +00:00
|
|
|
required this.squareInCheck,
|
2023-08-14 15:04:25 +00:00
|
|
|
});
|
2023-06-08 15:10:48 +00:00
|
|
|
|
|
|
|
factory ApiWebsocketMessage.fromJson(Map<String, dynamic> json) {
|
|
|
|
final type = MessageType.fromJson(json['messageType']);
|
|
|
|
ApiWebsocketMessage ret;
|
|
|
|
switch (type) {
|
2023-12-09 19:34:52 +00:00
|
|
|
case MessageType.boardState:
|
|
|
|
ret = ApiWebsocketMessage(
|
|
|
|
type: type,
|
2023-12-25 16:50:58 +00:00
|
|
|
move: ApiMove.fromJson(json['move']),
|
2023-12-09 19:34:52 +00:00
|
|
|
turnColor: ApiColor.fromJson(json['turnColor']),
|
|
|
|
reason: null,
|
|
|
|
position: json['position'],
|
|
|
|
squareInCheck: null,
|
|
|
|
playerColor: ApiColor.fromJson(json['playerColor']),
|
|
|
|
);
|
2023-06-08 15:10:48 +00:00
|
|
|
case MessageType.colorDetermined:
|
|
|
|
ret = ApiWebsocketMessage(
|
2023-08-14 15:04:25 +00:00
|
|
|
type: type,
|
|
|
|
move: null,
|
2023-12-09 19:34:52 +00:00
|
|
|
turnColor: null,
|
2023-08-14 15:04:25 +00:00
|
|
|
reason: null,
|
|
|
|
position: null,
|
2023-09-04 19:39:51 +00:00
|
|
|
squareInCheck: null,
|
2023-12-09 19:34:52 +00:00
|
|
|
playerColor: ApiColor.fromJson(json['playerColor']),
|
2023-08-14 15:04:25 +00:00
|
|
|
);
|
2023-06-08 15:10:48 +00:00
|
|
|
break;
|
2023-06-08 18:23:00 +00:00
|
|
|
case MessageType.move:
|
2023-06-08 15:10:48 +00:00
|
|
|
ret = ApiWebsocketMessage(
|
2023-12-09 19:34:52 +00:00
|
|
|
type: type,
|
|
|
|
move: ApiMove.fromJson(json['move']),
|
|
|
|
turnColor: null,
|
|
|
|
reason: null,
|
|
|
|
position: json['position'],
|
|
|
|
squareInCheck: json['squareInCheck'],
|
|
|
|
playerColor: null);
|
2023-06-28 10:37:59 +00:00
|
|
|
break;
|
|
|
|
case MessageType.invalidMove:
|
|
|
|
ret = ApiWebsocketMessage(
|
|
|
|
type: type,
|
|
|
|
move: ApiMove.fromJson(json['move']),
|
2023-12-09 19:34:52 +00:00
|
|
|
turnColor: null,
|
2023-06-28 10:37:59 +00:00
|
|
|
reason: json['reason'],
|
2023-08-14 15:04:25 +00:00
|
|
|
position: null,
|
2023-09-04 19:39:51 +00:00
|
|
|
squareInCheck: json['squareInCheck'],
|
2023-12-09 19:34:52 +00:00
|
|
|
playerColor: null,
|
2023-06-28 10:37:59 +00:00
|
|
|
);
|
2023-06-08 15:10:48 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2023-06-08 18:23:00 +00:00
|
|
|
|
2023-07-03 17:41:12 +00:00
|
|
|
Map<String, dynamic> toJson() => {
|
|
|
|
'messageType': type,
|
|
|
|
'move': move,
|
2023-12-09 19:34:52 +00:00
|
|
|
'color': turnColor,
|
2023-07-03 17:41:12 +00:00
|
|
|
};
|
2023-06-08 15:10:48 +00:00
|
|
|
}
|