Impementing toJson() and fromJson() Method in GameSession and Round class
This commit is contained in:
@@ -38,6 +38,30 @@ class GameSession {
|
|||||||
'winner: $winner]');
|
'winner: $winner]');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Converts the GameSession object to a JSON map.
|
||||||
|
Map<String, dynamic> toJson() => {
|
||||||
|
'gameTitle': gameTitle,
|
||||||
|
'gameHasPointLimit': gameHasPointLimit,
|
||||||
|
'players': players,
|
||||||
|
'playerScores': playerScores,
|
||||||
|
'roundNumber': roundNumber,
|
||||||
|
'isGameFinished': isGameFinished,
|
||||||
|
'winner': winner,
|
||||||
|
'roundList': roundList.map((e) => e.toJson()).toList()
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Creates a GameSession object from a JSON map.
|
||||||
|
GameSession.fromJson(Map<String, dynamic> json)
|
||||||
|
: gameTitle = json['gameTitle'],
|
||||||
|
gameHasPointLimit = json['gameHasPointLimit'],
|
||||||
|
players = List<String>.from(json['players']),
|
||||||
|
playerScores = List<int>.from(json['playerScores']),
|
||||||
|
roundNumber = json['roundNumber'],
|
||||||
|
isGameFinished = json['isGameFinished'],
|
||||||
|
winner = json['winner'],
|
||||||
|
roundList =
|
||||||
|
(json['roundList'] as List).map((e) => Round.fromJson(e)).toList();
|
||||||
|
|
||||||
/// Returns the length of all player names combined.
|
/// Returns the length of all player names combined.
|
||||||
int getLengthOfPlayerNames() {
|
int getLengthOfPlayerNames() {
|
||||||
int length = 0;
|
int length = 0;
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import 'package:cabo_counter/data/game_session.dart';
|
||||||
|
|
||||||
/// This class represents a single round in the game.
|
/// This class represents a single round in the game.
|
||||||
/// It is stored within the [GameSession] class.
|
/// It is stored within the [GameSession] class.
|
||||||
/// [roundNum] is the number of the round its reppresenting.
|
/// [roundNum] is the number of the round its reppresenting.
|
||||||
@@ -16,4 +18,19 @@ class Round {
|
|||||||
required this.scores,
|
required this.scores,
|
||||||
required this.scoreUpdates,
|
required this.scoreUpdates,
|
||||||
this.kamikazePlayerIndex});
|
this.kamikazePlayerIndex});
|
||||||
|
|
||||||
|
/// Converts the Round object to a JSON map.
|
||||||
|
Map<String, dynamic> toJson() => {
|
||||||
|
'roundNum': roundNum,
|
||||||
|
'scores': scores,
|
||||||
|
'scoreUpdates': scoreUpdates,
|
||||||
|
'kamikazePlayerIndex': kamikazePlayerIndex,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Creates a Round object from a JSON map.
|
||||||
|
Round.fromJson(Map<String, dynamic> json)
|
||||||
|
: roundNum = json['roundNum'],
|
||||||
|
scores = List<int>.from(json['scores']),
|
||||||
|
scoreUpdates = List<int>.from(json['scoreUpdates']),
|
||||||
|
kamikazePlayerIndex = json['kamikazePlayerIndex'];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user