InsanichessGameEvent.fromJson constructor

InsanichessGameEvent.fromJson(
  1. Map<String, dynamic> json
)

Creates a concrete implementer of InsanichessGameEvent based on the type provided in json.

Implementation

factory InsanichessGameEvent.fromJson(Map<String, dynamic> json) {
  final GameEventType type =
      gameEventTypeFromJson(json[InsanichessGameEventJsonKey.type]);
  switch (type) {
    case GameEventType.movePlayed:
      return MovePlayedGameEvent(
        move: insanichess.Move.fromICString(
          json[InsanichessGameEventJsonKey.movePlayed],
        ),
        timeSpent: json[InsanichessGameEventJsonKey.timeSpent] == null
            ? null
            : Duration(
                milliseconds: json[InsanichessGameEventJsonKey.timeSpent]),
        player: json[InsanichessGameEventJsonKey.color] == null
            ? null
            : pieceColorFromJson(json[InsanichessGameEventJsonKey.color]),
      );
    case GameEventType.disbanded:
      return const DisbandedGameEvent();
    case GameEventType.drawOffered:
      return const DrawOfferedGameEvent();
    case GameEventType.drawOfferCancelled:
      return const DrawOfferCancelledGameEvent();
    case GameEventType.drawOfferResponded:
      return DrawOfferRespondedGameEvent(
        accept: json[InsanichessGameEventJsonKey.accept],
      );
    case GameEventType.resigned:
      return ResignedGameEvent(
        player: json[InsanichessGameEventJsonKey.color] == null
            ? null
            : pieceColorFromJson(json[InsanichessGameEventJsonKey.color]),
      );
    case GameEventType.flagged:
      return FlaggedGameEvent(
        player: pieceColorFromJson(json[InsanichessGameEventJsonKey.color]),
      );
    case GameEventType.undoRequested:
      return const UndoRequestedGameEvent();
    case GameEventType.undoCancelled:
      return const UndoCancelledGameEvent();
    case GameEventType.undoRequestResponded:
      return UndoRespondedGameEvent(
        accept: json[InsanichessGameEventJsonKey.accept],
      );
  }
}