Files
game-tracker/lib/data/dto/game.dart
2025-11-18 23:16:57 +01:00

42 lines
1.1 KiB
Dart

import 'package:game_tracker/data/dto/group.dart';
import 'package:game_tracker/data/dto/player.dart';
import 'package:uuid/uuid.dart';
class Game {
final String id;
final String name;
final List<Player>? players;
final Group? group;
final String winner;
Game({
String? id,
required this.name,
this.players,
this.group,
this.winner = '',
}) : id = id ?? const Uuid().v4();
@override
String toString() {
return 'Game{\n\tid: $id,\n\tname: $name,\n\tplayers: $players,\n\tgroup: $group,\n\twinner: $winner\n}';
}
/// Creates a Game instance from a JSON object.
Game.fromJson(Map<String, dynamic> json)
: id = json['id'],
name = json['name'],
players = json['players'] != null
? (json['players'] as List)
.map((playerJson) => Player.fromJson(playerJson))
.toList()
: null,
group = json['group'] != null ? Group.fromJson(json['group']) : null,
winner = json['winner'] ?? '';
/// Converts the Game instance to a JSON object.
String toJson() {
return 'Game{id: $id,name: $name,players: $players,group: $group,winner: $winner}';
}
}