Updated prints and json schema
This commit is contained in:
@@ -1,15 +1,15 @@
|
|||||||
{
|
{
|
||||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
"title": "Schema for cabo game data",
|
"title": "Generated schema for cabo game data",
|
||||||
"type": "array",
|
"type": "array",
|
||||||
"items": {
|
"items": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"gameTitle": {
|
"createdAt": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
"gameHasPointLimit": {
|
"gameTitle": {
|
||||||
"type": "boolean"
|
"type": "string"
|
||||||
},
|
},
|
||||||
"players": {
|
"players": {
|
||||||
"type": "array",
|
"type": "array",
|
||||||
@@ -17,21 +17,30 @@
|
|||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"playerScores": {
|
"pointLimit": {
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"type": "number"
|
"type": "number"
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"roundNumber": {
|
"caboPenalty": {
|
||||||
"type": "number"
|
"type": "number"
|
||||||
},
|
},
|
||||||
|
"isPointsLimitEnabled": {
|
||||||
|
"type": "boolean"
|
||||||
|
},
|
||||||
"isGameFinished": {
|
"isGameFinished": {
|
||||||
"type": "boolean"
|
"type": "boolean"
|
||||||
},
|
},
|
||||||
"winner": {
|
"winner": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
|
"roundNumber": {
|
||||||
|
"type": "number"
|
||||||
|
},
|
||||||
|
"playerScores": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "number"
|
||||||
|
}
|
||||||
|
},
|
||||||
"roundList": {
|
"roundList": {
|
||||||
"type": "array",
|
"type": "array",
|
||||||
"items": {
|
"items": {
|
||||||
@@ -43,7 +52,9 @@
|
|||||||
"caboPlayerIndex": {
|
"caboPlayerIndex": {
|
||||||
"type": "number"
|
"type": "number"
|
||||||
},
|
},
|
||||||
"kamikazePlayerIndex": {},
|
"kamikazePlayerIndex": {
|
||||||
|
"type": ["number", "null"]
|
||||||
|
},
|
||||||
"scores": {
|
"scores": {
|
||||||
"type": "array",
|
"type": "array",
|
||||||
"items": {
|
"items": {
|
||||||
@@ -60,7 +71,6 @@
|
|||||||
"required": [
|
"required": [
|
||||||
"roundNum",
|
"roundNum",
|
||||||
"caboPlayerIndex",
|
"caboPlayerIndex",
|
||||||
"kamikazePlayerIndex",
|
|
||||||
"scores",
|
"scores",
|
||||||
"scoreUpdates"
|
"scoreUpdates"
|
||||||
]
|
]
|
||||||
@@ -68,13 +78,16 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"required": [
|
"required": [
|
||||||
|
"createdAt",
|
||||||
"gameTitle",
|
"gameTitle",
|
||||||
"gameHasPointLimit",
|
|
||||||
"players",
|
"players",
|
||||||
"playerScores",
|
"pointLimit",
|
||||||
"roundNumber",
|
"caboPenalty",
|
||||||
|
"isPointsLimitEnabled",
|
||||||
"isGameFinished",
|
"isGameFinished",
|
||||||
"winner",
|
"winner",
|
||||||
|
"roundNumber",
|
||||||
|
"playerScores",
|
||||||
"roundList"
|
"roundList"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,10 +7,14 @@ import 'package:file_picker/file_picker.dart';
|
|||||||
import 'package:file_saver/file_saver.dart';
|
import 'package:file_saver/file_saver.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:json_schema/json_schema.dart';
|
import 'package:json_schema/json_schema.dart';
|
||||||
|
import 'package:logger/logger.dart';
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
|
|
||||||
class LocalStorageService {
|
class LocalStorageService {
|
||||||
static const String _fileName = 'game_data.json';
|
static const String _fileName = 'game_data.json';
|
||||||
|
static var logger = Logger(
|
||||||
|
printer: PrettyPrinter(),
|
||||||
|
);
|
||||||
|
|
||||||
/// Writes the game session list to a JSON file and returns it as string.
|
/// Writes the game session list to a JSON file and returns it as string.
|
||||||
static String getJsonFile() {
|
static String getJsonFile() {
|
||||||
@@ -32,38 +36,38 @@ class LocalStorageService {
|
|||||||
final file = await _getFilePath();
|
final file = await _getFilePath();
|
||||||
final jsonFile = getJsonFile();
|
final jsonFile = getJsonFile();
|
||||||
await file.writeAsString(jsonFile);
|
await file.writeAsString(jsonFile);
|
||||||
print('Daten gespeichert');
|
logger.i('Die Spieldaten wurden zwischengespeichert.');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('Fehler beim Speichern: $e');
|
logger.w('Fehler beim Speichern der Spieldaten. Exception: $e');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Loads the game data from a local JSON file.
|
/// Loads the game data from a local JSON file.
|
||||||
static Future<bool> loadGameSessions() async {
|
static Future<bool> loadGameSessions() async {
|
||||||
print('Versuche, Daten zu laden...');
|
logger.d('Versuche, Daten zu laden...');
|
||||||
try {
|
try {
|
||||||
final file = await _getFilePath();
|
final file = await _getFilePath();
|
||||||
|
|
||||||
if (!await file.exists()) {
|
if (!await file.exists()) {
|
||||||
print('Es existiert noch keine Datei mit Spieldaten');
|
logger.w('Es existiert noch keine Datei mit Spieldaten');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
print('Es existiert bereits eine Datei mit Spieldaten');
|
logger.d('Es existiert bereits eine Datei mit Spieldaten');
|
||||||
final jsonString = await file.readAsString();
|
final jsonString = await file.readAsString();
|
||||||
|
|
||||||
if (jsonString.isEmpty) {
|
if (jsonString.isEmpty) {
|
||||||
print('Die gefundene Datei ist leer');
|
logger.w('Die gefundene Datei ist leer');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!await validateJsonSchema(jsonString)) {
|
if (!await validateJsonSchema(jsonString)) {
|
||||||
print('Die Datei konnte nicht validiert werden');
|
logger.w('Die Datei konnte nicht validiert werden');
|
||||||
Globals.gameList = [];
|
Globals.gameList = [];
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
logger.d('Die gefundene Datei hat Inhalt');
|
||||||
print('Die gefundene Datei ist nicht leer und validiert');
|
logger.d('Die gefundene Datei wurde erfolgreich validiert');
|
||||||
final jsonList = json.decode(jsonString) as List<dynamic>;
|
final jsonList = json.decode(jsonString) as List<dynamic>;
|
||||||
|
|
||||||
Globals.gameList = jsonList
|
Globals.gameList = jsonList
|
||||||
@@ -71,10 +75,11 @@ class LocalStorageService {
|
|||||||
GameSession.fromJson(jsonItem as Map<String, dynamic>))
|
GameSession.fromJson(jsonItem as Map<String, dynamic>))
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
print('Die Daten wurden erfolgreich geladen und verarbeitet');
|
logger.i('Die Spieldaten wurden erfolgreich geladen und verarbeitet');
|
||||||
return true;
|
return true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('Fehler beim Laden der Spieldaten:\n$e');
|
logger.e('Fehler beim Laden der Spieldaten:\n$e',
|
||||||
|
error: 'JSON nicht geladen');
|
||||||
Globals.gameList = [];
|
Globals.gameList = [];
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -91,10 +96,11 @@ class LocalStorageService {
|
|||||||
ext: 'json',
|
ext: 'json',
|
||||||
mimeType: MimeType.json,
|
mimeType: MimeType.json,
|
||||||
);
|
);
|
||||||
print('Datei gespeichert: $result');
|
logger.i('Die Spieldaten wurden exportiert. Dateipfad: $result');
|
||||||
return true;
|
return true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('Fehler beim Speichern: $e');
|
logger.w('Fehler beim Exportieren der Spieldaten. Exception: $e',
|
||||||
|
error: 'JSON nicht exportiert');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,7 +114,7 @@ class LocalStorageService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (result == null) {
|
if (result == null) {
|
||||||
print('Der Dialog wurde abgebrochen');
|
logger.d('Der Filepicker-Dialog wurde abgebrochen');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,17 +125,18 @@ class LocalStorageService {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
final jsonData = json.decode(jsonString) as List<dynamic>;
|
final jsonData = json.decode(jsonString) as List<dynamic>;
|
||||||
print('JSON Inhalt: $jsonData');
|
|
||||||
Globals.gameList = jsonData
|
Globals.gameList = jsonData
|
||||||
.map((jsonItem) =>
|
.map((jsonItem) =>
|
||||||
GameSession.fromJson(jsonItem as Map<String, dynamic>))
|
GameSession.fromJson(jsonItem as Map<String, dynamic>))
|
||||||
.toList();
|
.toList();
|
||||||
|
logger.i('Die Datei wurde erfolgreich Importiertn');
|
||||||
return true;
|
return true;
|
||||||
} on FormatException catch (e) {
|
} on FormatException catch (e) {
|
||||||
print('Ungültiges JSON-Format: $e');
|
logger.e('Ungültiges JSON-Format. Exception: $e', error: 'Formatfehler');
|
||||||
return false;
|
return false;
|
||||||
} on Exception catch (e) {
|
} on Exception catch (e) {
|
||||||
print('Fehler beim Dateizugriff: $e');
|
logger.e('Fehler beim Dateizugriff. Exception: $e',
|
||||||
|
error: 'Dateizugriffsfehler');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -151,13 +158,14 @@ class LocalStorageService {
|
|||||||
final result = schema.validate(jsonData);
|
final result = schema.validate(jsonData);
|
||||||
|
|
||||||
if (result.isValid) {
|
if (result.isValid) {
|
||||||
print('JSON ist erfolgreich validiert.');
|
logger.d('JSON ist erfolgreich validiert.');
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
print('JSON ist nicht gültig: ${result.errors}');
|
logger.w('JSON ist nicht gültig.\nFehler: ${result.errors}');
|
||||||
return false;
|
return false;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('Fehler beim Validieren des JSON-Schemas: $e');
|
logger.e('Fehler beim Validieren des JSON-Schemas: $e',
|
||||||
|
error: 'Validierung fehlgeschlagen');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ name: cabo_counter
|
|||||||
description: "Mobile app for the card game Cabo"
|
description: "Mobile app for the card game Cabo"
|
||||||
publish_to: 'none'
|
publish_to: 'none'
|
||||||
|
|
||||||
version: 0.1.6-alpha+138
|
version: 0.1.6-alpha+145
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ^3.5.4
|
sdk: ^3.5.4
|
||||||
@@ -21,6 +21,7 @@ dependencies:
|
|||||||
url_launcher: any
|
url_launcher: any
|
||||||
json_schema: ^5.2.1
|
json_schema: ^5.2.1
|
||||||
shared_preferences: ^2.5.3
|
shared_preferences: ^2.5.3
|
||||||
|
logger: ^2.5.0
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
@@ -33,3 +34,4 @@ flutter:
|
|||||||
uses-material-design: false
|
uses-material-design: false
|
||||||
assets:
|
assets:
|
||||||
- assets/cabo-counter-logo_rounded.png
|
- assets/cabo-counter-logo_rounded.png
|
||||||
|
- assets/schema.json
|
||||||
|
|||||||
Reference in New Issue
Block a user