Added uuid to gameSession class & json scheme
This commit is contained in:
@@ -5,6 +5,9 @@
|
|||||||
"items": {
|
"items": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
"createdAt": {
|
"createdAt": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import 'package:cabo_counter/data/round.dart';
|
import 'package:cabo_counter/data/round.dart';
|
||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
|
import 'package:uuid/uuid.dart';
|
||||||
|
|
||||||
/// This class represents a game session for Cabo game.
|
/// This class represents a game session for Cabo game.
|
||||||
/// [createdAt] is the timestamp of when the game session was created.
|
/// [createdAt] is the timestamp of when the game session was created.
|
||||||
@@ -12,6 +13,7 @@ import 'package:flutter/cupertino.dart';
|
|||||||
/// [isGameFinished] is a boolean indicating if the game has ended yet.
|
/// [isGameFinished] is a boolean indicating if the game has ended yet.
|
||||||
/// [winner] is the name of the player who won the game.
|
/// [winner] is the name of the player who won the game.
|
||||||
class GameSession extends ChangeNotifier {
|
class GameSession extends ChangeNotifier {
|
||||||
|
late String id;
|
||||||
final DateTime createdAt;
|
final DateTime createdAt;
|
||||||
final String gameTitle;
|
final String gameTitle;
|
||||||
final List<String> players;
|
final List<String> players;
|
||||||
@@ -33,17 +35,21 @@ class GameSession extends ChangeNotifier {
|
|||||||
required this.isPointsLimitEnabled,
|
required this.isPointsLimitEnabled,
|
||||||
}) {
|
}) {
|
||||||
playerScores = List.filled(players.length, 0);
|
playerScores = List.filled(players.length, 0);
|
||||||
|
var uuid = const Uuid();
|
||||||
|
id = uuid.v1();
|
||||||
|
print('GameSession created with ID: $id');
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
toString() {
|
toString() {
|
||||||
return ('GameSession: [createdAt: $createdAt, gameTitle: $gameTitle, '
|
return ('GameSession: [id: $id, createdAt: $createdAt, gameTitle: $gameTitle, '
|
||||||
'isPointsLimitEnabled: $isPointsLimitEnabled, pointLimit: $pointLimit, caboPenalty: $caboPenalty,'
|
'isPointsLimitEnabled: $isPointsLimitEnabled, pointLimit: $pointLimit, caboPenalty: $caboPenalty,'
|
||||||
' players: $players, playerScores: $playerScores, roundList: $roundList, winner: $winner]');
|
' players: $players, playerScores: $playerScores, roundList: $roundList, winner: $winner]');
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts the GameSession object to a JSON map.
|
/// Converts the GameSession object to a JSON map.
|
||||||
Map<String, dynamic> toJson() => {
|
Map<String, dynamic> toJson() => {
|
||||||
|
'id': id,
|
||||||
'createdAt': createdAt.toIso8601String(),
|
'createdAt': createdAt.toIso8601String(),
|
||||||
'gameTitle': gameTitle,
|
'gameTitle': gameTitle,
|
||||||
'players': players,
|
'players': players,
|
||||||
@@ -59,7 +65,8 @@ class GameSession extends ChangeNotifier {
|
|||||||
|
|
||||||
/// Creates a GameSession object from a JSON map.
|
/// Creates a GameSession object from a JSON map.
|
||||||
GameSession.fromJson(Map<String, dynamic> json)
|
GameSession.fromJson(Map<String, dynamic> json)
|
||||||
: createdAt = DateTime.parse(json['createdAt']),
|
: id = json['id'] ?? const Uuid().v1(),
|
||||||
|
createdAt = DateTime.parse(json['createdAt']),
|
||||||
gameTitle = json['gameTitle'],
|
gameTitle = json['gameTitle'],
|
||||||
players = List<String>.from(json['players']),
|
players = List<String>.from(json['players']),
|
||||||
pointLimit = json['pointLimit'],
|
pointLimit = json['pointLimit'],
|
||||||
|
|||||||
Reference in New Issue
Block a user