Added attributes to GameSession Class, edited toJson and fromJson Method
This commit is contained in:
@@ -11,20 +11,25 @@ import 'package:cabo_counter/data/round.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 {
|
class GameSession {
|
||||||
final DateTime createdAt = DateTime.now();
|
final DateTime createdAt;
|
||||||
final String gameTitle;
|
final String gameTitle;
|
||||||
final bool isPointsLimitEnabled;
|
|
||||||
final List<String> players;
|
final List<String> players;
|
||||||
late List<int> playerScores;
|
final int pointLimit;
|
||||||
List<Round> roundList = [];
|
final int caboPenalty;
|
||||||
int roundNumber = 1;
|
final bool isPointsLimitEnabled;
|
||||||
bool isGameFinished = false;
|
bool isGameFinished = false;
|
||||||
String winner = '';
|
String winner = '';
|
||||||
|
int roundNumber = 1;
|
||||||
|
late List<int> playerScores;
|
||||||
|
List<Round> roundList = [];
|
||||||
|
|
||||||
GameSession({
|
GameSession({
|
||||||
|
required this.createdAt,
|
||||||
required this.gameTitle,
|
required this.gameTitle,
|
||||||
required this.isPointsLimitEnabled,
|
|
||||||
required this.players,
|
required this.players,
|
||||||
|
required this.pointLimit,
|
||||||
|
required this.caboPenalty,
|
||||||
|
required this.isPointsLimitEnabled,
|
||||||
}) {
|
}) {
|
||||||
playerScores = List.filled(players.length, 0);
|
playerScores = List.filled(players.length, 0);
|
||||||
}
|
}
|
||||||
@@ -32,33 +37,37 @@ class GameSession {
|
|||||||
@override
|
@override
|
||||||
toString() {
|
toString() {
|
||||||
return ('GameSession: [createdAt: $createdAt, gameTitle: $gameTitle, '
|
return ('GameSession: [createdAt: $createdAt, gameTitle: $gameTitle, '
|
||||||
'isPointsLimitEnabled: $isPointsLimitEnabled, players: $players, '
|
'isPointsLimitEnabled: $isPointsLimitEnabled, pointLimit: $pointLimit, caboPenalty: $caboPenalty,'
|
||||||
'playerScores: $playerScores, roundList: $roundList, '
|
' players: $players, playerScores: $playerScores, roundList: $roundList, winner: $winner]');
|
||||||
'roundNumber: $roundNumber, isGameFinished: $isGameFinished, '
|
|
||||||
'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() => {
|
||||||
|
'createdAt': createdAt.toIso8601String(),
|
||||||
'gameTitle': gameTitle,
|
'gameTitle': gameTitle,
|
||||||
'gameHasPointLimit': isPointsLimitEnabled,
|
|
||||||
'players': players,
|
'players': players,
|
||||||
'playerScores': playerScores,
|
'pointLimit': pointLimit,
|
||||||
'roundNumber': roundNumber,
|
'caboPenalty': caboPenalty,
|
||||||
|
'isPointsLimitEnabled': isPointsLimitEnabled,
|
||||||
'isGameFinished': isGameFinished,
|
'isGameFinished': isGameFinished,
|
||||||
'winner': winner,
|
'winner': winner,
|
||||||
|
'roundNumber': roundNumber,
|
||||||
|
'playerScores': playerScores,
|
||||||
'roundList': roundList.map((e) => e.toJson()).toList()
|
'roundList': roundList.map((e) => e.toJson()).toList()
|
||||||
};
|
};
|
||||||
|
|
||||||
/// 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)
|
||||||
: gameTitle = json['gameTitle'],
|
: createdAt = DateTime.parse(json['createdAt']),
|
||||||
isPointsLimitEnabled = json['gameHasPointLimit'],
|
gameTitle = json['gameTitle'],
|
||||||
players = List<String>.from(json['players']),
|
players = List<String>.from(json['players']),
|
||||||
playerScores = List<int>.from(json['playerScores']),
|
pointLimit = json['pointLimit'],
|
||||||
roundNumber = json['roundNumber'],
|
caboPenalty = json['caboPenalty'],
|
||||||
|
isPointsLimitEnabled = json['gameHasPointLimit'],
|
||||||
isGameFinished = json['isGameFinished'],
|
isGameFinished = json['isGameFinished'],
|
||||||
winner = json['winner'],
|
winner = json['winner'],
|
||||||
|
roundNumber = json['roundNumber'],
|
||||||
|
playerScores = List<int>.from(json['playerScores']),
|
||||||
roundList =
|
roundList =
|
||||||
(json['roundList'] as List).map((e) => Round.fromJson(e)).toList();
|
(json['roundList'] as List).map((e) => Round.fromJson(e)).toList();
|
||||||
|
|
||||||
@@ -76,7 +85,7 @@ class GameSession {
|
|||||||
void applyKamikaze(int roundNum, int kamikazePlayerIndex) {
|
void applyKamikaze(int roundNum, int kamikazePlayerIndex) {
|
||||||
List<int> roundScores = List.generate(players.length, (_) => 0);
|
List<int> roundScores = List.generate(players.length, (_) => 0);
|
||||||
List<int> scoreUpdates = List.generate(players.length, (_) => 0);
|
List<int> scoreUpdates = List.generate(players.length, (_) => 0);
|
||||||
for (int i = 0; i < roundScores.length; i++) {
|
for (int i = 0; i < scoreUpdates.length; i++) {
|
||||||
if (i != kamikazePlayerIndex) {
|
if (i != kamikazePlayerIndex) {
|
||||||
scoreUpdates[i] += 50;
|
scoreUpdates[i] += 50;
|
||||||
}
|
}
|
||||||
@@ -206,13 +215,13 @@ class GameSession {
|
|||||||
/// It then checks if any player has exceeded 100 points. If so, it sets
|
/// It then checks if any player has exceeded 100 points. If so, it sets
|
||||||
/// isGameFinished to true and calls the _setWinner() method to determine
|
/// isGameFinished to true and calls the _setWinner() method to determine
|
||||||
/// the winner.
|
/// the winner.
|
||||||
void updatePoints() {
|
Future<void> updatePoints() async {
|
||||||
_sumPoints();
|
_sumPoints();
|
||||||
if (isPointsLimitEnabled) {
|
if (isPointsLimitEnabled) {
|
||||||
_checkHundredPointsReached();
|
_checkHundredPointsReached();
|
||||||
|
|
||||||
for (int i = 0; i < playerScores.length; i++) {
|
for (int i = 0; i < playerScores.length; i++) {
|
||||||
if (playerScores[i] > 100) {
|
if (playerScores[i] > pointLimit) {
|
||||||
isGameFinished = true;
|
isGameFinished = true;
|
||||||
print('${players[i]} hat die 100 Punkte ueberschritten, '
|
print('${players[i]} hat die 100 Punkte ueberschritten, '
|
||||||
'deswegen wurde das Spiel beendet');
|
'deswegen wurde das Spiel beendet');
|
||||||
@@ -238,7 +247,7 @@ class GameSession {
|
|||||||
/// the corresponding round update.
|
/// the corresponding round update.
|
||||||
void _checkHundredPointsReached() {
|
void _checkHundredPointsReached() {
|
||||||
for (int i = 0; i < players.length; i++) {
|
for (int i = 0; i < players.length; i++) {
|
||||||
if (playerScores[i] == 100) {
|
if (playerScores[i] == pointLimit) {
|
||||||
print('${players[i]} hat genau 100 Punkte erreicht und bekommt '
|
print('${players[i]} hat genau 100 Punkte erreicht und bekommt '
|
||||||
'deswegen 50 Punkte abgezogen');
|
'deswegen 50 Punkte abgezogen');
|
||||||
roundList[roundNumber - 1].scoreUpdates[i] -= 50;
|
roundList[roundNumber - 1].scoreUpdates[i] -= 50;
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ dependencies:
|
|||||||
path_provider: ^2.1.1
|
path_provider: ^2.1.1
|
||||||
typed_data: ^1.3.2
|
typed_data: ^1.3.2
|
||||||
url_launcher: any
|
url_launcher: any
|
||||||
|
shared_preferences: ^2.5.3
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|||||||
Reference in New Issue
Block a user