Merge pull request #82 from flixcoo/enhance/78-rework-im--and-exporting-game-mechanism

Rework im- and exporting game mechanism
This commit is contained in:
2025-07-08 00:02:22 +02:00
committed by GitHub
10 changed files with 364 additions and 35 deletions

291
assets/game-schema.json Normal file
View File

@@ -0,0 +1,291 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Generated schema for a single cabo counter game",
"type": "object",
"properties": {
"id": {
"type": "string"
},
"createdAt": {
"type": "string"
},
"gameTitle": {
"type": "string"
},
"players": {
"type": "array",
"items": [
{
"type": "string"
},
{
"type": "string"
}
]
},
"pointLimit": {
"type": "integer"
},
"caboPenalty": {
"type": "integer"
},
"isPointsLimitEnabled": {
"type": "boolean"
},
"isGameFinished": {
"type": "boolean"
},
"winner": {
"type": "string"
},
"roundNumber": {
"type": "integer"
},
"playerScores": {
"type": "array",
"items": [
{
"type": "integer"
},
{
"type": "integer"
}
]
},
"roundList": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"roundNum": {
"type": "integer"
},
"caboPlayerIndex": {
"type": "integer"
},
"kamikazePlayerIndex": {
"type": "null"
},
"scores": {
"type": "array",
"items": [
{
"type": "integer"
},
{
"type": "integer"
}
]
},
"scoreUpdates": {
"type": "array",
"items": [
{
"type": "integer"
},
{
"type": "integer"
}
]
}
},
"required": [
"roundNum",
"caboPlayerIndex",
"kamikazePlayerIndex",
"scores",
"scoreUpdates"
]
},
{
"type": "object",
"properties": {
"roundNum": {
"type": "integer"
},
"caboPlayerIndex": {
"type": "integer"
},
"kamikazePlayerIndex": {
"type": "null"
},
"scores": {
"type": "array",
"items": [
{
"type": "integer"
},
{
"type": "integer"
}
]
},
"scoreUpdates": {
"type": "array",
"items": [
{
"type": "integer"
},
{
"type": "integer"
}
]
}
},
"required": [
"roundNum",
"caboPlayerIndex",
"kamikazePlayerIndex",
"scores",
"scoreUpdates"
]
},
{
"type": "object",
"properties": {
"roundNum": {
"type": "integer"
},
"caboPlayerIndex": {
"type": "integer"
},
"kamikazePlayerIndex": {
"type": "null"
},
"scores": {
"type": "array",
"items": [
{
"type": "integer"
},
{
"type": "integer"
}
]
},
"scoreUpdates": {
"type": "array",
"items": [
{
"type": "integer"
},
{
"type": "integer"
}
]
}
},
"required": [
"roundNum",
"caboPlayerIndex",
"kamikazePlayerIndex",
"scores",
"scoreUpdates"
]
},
{
"type": "object",
"properties": {
"roundNum": {
"type": "integer"
},
"caboPlayerIndex": {
"type": "integer"
},
"kamikazePlayerIndex": {
"type": "null"
},
"scores": {
"type": "array",
"items": [
{
"type": "integer"
},
{
"type": "integer"
}
]
},
"scoreUpdates": {
"type": "array",
"items": [
{
"type": "integer"
},
{
"type": "integer"
}
]
}
},
"required": [
"roundNum",
"caboPlayerIndex",
"kamikazePlayerIndex",
"scores",
"scoreUpdates"
]
},
{
"type": "object",
"properties": {
"roundNum": {
"type": "integer"
},
"caboPlayerIndex": {
"type": "integer"
},
"kamikazePlayerIndex": {
"type": "null"
},
"scores": {
"type": "array",
"items": [
{
"type": "integer"
},
{
"type": "integer"
}
]
},
"scoreUpdates": {
"type": "array",
"items": [
{
"type": "integer"
},
{
"type": "integer"
}
]
}
},
"required": [
"roundNum",
"caboPlayerIndex",
"kamikazePlayerIndex",
"scores",
"scoreUpdates"
]
}
]
}
},
"required": [
"id",
"createdAt",
"gameTitle",
"players",
"pointLimit",
"caboPenalty",
"isPointsLimitEnabled",
"isGameFinished",
"winner",
"roundNumber",
"playerScores",
"roundList"
]
}

View File

@@ -1,6 +1,6 @@
{ {
"$schema": "http://json-schema.org/draft-07/schema#", "$schema": "http://json-schema.org/draft-07/schema#",
"title": "Generated schema for cabo game data", "title": "Generated schema for the cabo counter game data",
"type": "array", "type": "array",
"items": { "items": {
"type": "object", "type": "object",

View File

@@ -2,7 +2,6 @@ import 'package:cabo_counter/l10n/app_localizations.dart';
import 'package:cabo_counter/services/config_service.dart'; import 'package:cabo_counter/services/config_service.dart';
import 'package:cabo_counter/services/local_storage_service.dart'; import 'package:cabo_counter/services/local_storage_service.dart';
import 'package:cabo_counter/utility/custom_theme.dart'; import 'package:cabo_counter/utility/custom_theme.dart';
import 'package:cabo_counter/utility/globals.dart';
import 'package:cabo_counter/views/tab_view.dart'; import 'package:cabo_counter/views/tab_view.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
@@ -12,8 +11,8 @@ Future<void> main() async {
await SystemChrome.setPreferredOrientations( await SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]); [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
await ConfigService.initConfig(); await ConfigService.initConfig();
Globals.pointLimit = await ConfigService.getPointLimit(); ConfigService.pointLimit = await ConfigService.getPointLimit();
Globals.caboPenalty = await ConfigService.getCaboPenalty(); ConfigService.caboPenalty = await ConfigService.getCaboPenalty();
runApp(const App()); runApp(const App());
} }

View File

@@ -1,4 +1,3 @@
import 'package:cabo_counter/utility/globals.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
/// This class handles the configuration settings for the app. /// This class handles the configuration settings for the app.
@@ -7,8 +6,12 @@ import 'package:shared_preferences/shared_preferences.dart';
class ConfigService { class ConfigService {
static const String _keyPointLimit = 'pointLimit'; static const String _keyPointLimit = 'pointLimit';
static const String _keyCaboPenalty = 'caboPenalty'; static const String _keyCaboPenalty = 'caboPenalty';
static const int _defaultPointLimit = 100; // Default Value // Actual values used in the app
static const int _defaultCaboPenalty = 5; // Default Value static int pointLimit = 100;
static int caboPenalty = 5;
// Default values
static const int _defaultPointLimit = 100;
static const int _defaultCaboPenalty = 5;
static Future<void> initConfig() async { static Future<void> initConfig() async {
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
@@ -48,8 +51,8 @@ class ConfigService {
/// Resets the configuration to default values. /// Resets the configuration to default values.
static Future<void> resetConfig() async { static Future<void> resetConfig() async {
Globals.pointLimit = _defaultPointLimit; ConfigService.pointLimit = _defaultPointLimit;
Globals.caboPenalty = _defaultCaboPenalty; ConfigService.caboPenalty = _defaultCaboPenalty;
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
await prefs.setInt(_keyPointLimit, _defaultPointLimit); await prefs.setInt(_keyPointLimit, _defaultPointLimit);
await prefs.setInt(_keyCaboPenalty, _defaultCaboPenalty); await prefs.setInt(_keyCaboPenalty, _defaultCaboPenalty);

View File

@@ -70,7 +70,7 @@ class LocalStorageService {
return false; return false;
} }
if (!await validateJsonSchema(jsonString)) { if (!await validateJsonSchema(jsonString, true)) {
print( print(
'[local_storage_service.dart] Die Datei konnte nicht validiert werden'); '[local_storage_service.dart] Die Datei konnte nicht validiert werden');
gameManager.gameList = []; gameManager.gameList = [];
@@ -162,16 +162,28 @@ class LocalStorageService {
try { try {
final jsonString = await _readFileContent(path.files.single); final jsonString = await _readFileContent(path.files.single);
if (!await validateJsonSchema(jsonString)) { if (await validateJsonSchema(jsonString, true)) {
// Checks if the JSON String is in the gameList format
final jsonData = json.decode(jsonString) as List<dynamic>;
List<GameSession> importedList = jsonData
.map((jsonItem) =>
GameSession.fromJson(jsonItem as Map<String, dynamic>))
.toList();
for (GameSession s in importedList) {
importSession(s);
}
} else if (await validateJsonSchema(jsonString, false)) {
// Checks if the JSON String is in the single game format
final jsonData = json.decode(jsonString) as Map<String, dynamic>;
importSession(GameSession.fromJson(jsonData));
} else {
return ImportStatus.validationError; return ImportStatus.validationError;
} }
final jsonData = json.decode(jsonString) as List<dynamic>;
gameManager.gameList = jsonData
.map((jsonItem) =>
GameSession.fromJson(jsonItem as Map<String, dynamic>))
.toList();
print( print(
'[local_storage_service.dart] Die Datei wurde erfolgreich Importiertn'); '[local_storage_service.dart] Die Datei wurde erfolgreich Importiert');
await saveGameSessions(); await saveGameSessions();
return ImportStatus.success; return ImportStatus.success;
} on FormatException catch (e) { } on FormatException catch (e) {
@@ -185,6 +197,18 @@ class LocalStorageService {
} }
} }
/// Imports a single game session into the gameList.
static Future<void> importSession(GameSession session) async {
if (gameManager.gameExistsInGameList(session.id)) {
print(
'[local_storage_service.dart] Die Session mit der ID ${session.id} existiert bereits. Sie wird überschrieben.');
gameManager.removeGameSessionById(session.id);
}
gameManager.addGameSession(session);
print(
'[local_storage_service.dart] Die Session mit der ID ${session.id} wurde erfolgreich importiert.');
}
/// Helper method to read file content from either bytes or path /// Helper method to read file content from either bytes or path
static Future<String> _readFileContent(PlatformFile file) async { static Future<String> _readFileContent(PlatformFile file) async {
if (file.bytes != null) return utf8.decode(file.bytes!); if (file.bytes != null) return utf8.decode(file.bytes!);
@@ -194,15 +218,28 @@ class LocalStorageService {
} }
/// Validates the JSON data against the schema. /// Validates the JSON data against the schema.
static Future<bool> validateJsonSchema(String jsonString) async { /// This method checks if the provided [jsonString] is valid against the
/// JSON schema. It takes a boolean [isGameList] to determine
/// which schema to use (game list or single game).
static Future<bool> validateJsonSchema(
String jsonString, bool isGameList) async {
final String schemaString;
if (isGameList) {
schemaString =
await rootBundle.loadString('assets/game_list-schema.json');
} else {
schemaString = await rootBundle.loadString('assets/game-schema.json');
}
try { try {
final schemaString = await rootBundle.loadString('assets/schema.json');
final schema = JsonSchema.create(json.decode(schemaString)); final schema = JsonSchema.create(json.decode(schemaString));
final jsonData = json.decode(jsonString); final jsonData = json.decode(jsonString);
final result = schema.validate(jsonData); final result = schema.validate(jsonData);
if (result.isValid) { if (result.isValid) {
print('[local_storage_service.dart] JSON ist erfolgreich validiert.'); print(
'[local_storage_service.dart] JSON ist erfolgreich validiert. Typ: ${isGameList ? 'Game List' : 'Single Game'}');
return true; return true;
} }
print( print(

View File

@@ -1,5 +1,3 @@
class Globals { class Globals {
static int pointLimit = 100;
static int caboPenalty = 5;
static String appDevPhase = 'Beta'; static String appDevPhase = 'Beta';
} }

View File

@@ -1,8 +1,8 @@
import 'package:cabo_counter/data/game_manager.dart'; import 'package:cabo_counter/data/game_manager.dart';
import 'package:cabo_counter/data/game_session.dart'; import 'package:cabo_counter/data/game_session.dart';
import 'package:cabo_counter/l10n/app_localizations.dart'; import 'package:cabo_counter/l10n/app_localizations.dart';
import 'package:cabo_counter/services/config_service.dart';
import 'package:cabo_counter/utility/custom_theme.dart'; import 'package:cabo_counter/utility/custom_theme.dart';
import 'package:cabo_counter/utility/globals.dart';
import 'package:cabo_counter/views/active_game_view.dart'; import 'package:cabo_counter/views/active_game_view.dart';
import 'package:cabo_counter/views/mode_selection_view.dart'; import 'package:cabo_counter/views/mode_selection_view.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
@@ -96,7 +96,7 @@ class _CreateGameViewState extends State<CreateGameView> {
_isPointsLimitEnabled == null _isPointsLimitEnabled == null
? AppLocalizations.of(context).select_mode ? AppLocalizations.of(context).select_mode
: (_isPointsLimitEnabled! : (_isPointsLimitEnabled!
? '${Globals.pointLimit} ${AppLocalizations.of(context).points}' ? '${ConfigService.pointLimit} ${AppLocalizations.of(context).points}'
: AppLocalizations.of(context).unlimited), : AppLocalizations.of(context).unlimited),
), ),
const SizedBox(width: 3), const SizedBox(width: 3),
@@ -108,7 +108,7 @@ class _CreateGameViewState extends State<CreateGameView> {
context, context,
CupertinoPageRoute( CupertinoPageRoute(
builder: (context) => ModeSelectionMenu( builder: (context) => ModeSelectionMenu(
pointLimit: Globals.pointLimit, pointLimit: ConfigService.pointLimit,
), ),
), ),
); );
@@ -315,8 +315,8 @@ class _CreateGameViewState extends State<CreateGameView> {
createdAt: DateTime.now(), createdAt: DateTime.now(),
gameTitle: _gameTitleTextController.text, gameTitle: _gameTitleTextController.text,
players: players, players: players,
pointLimit: Globals.pointLimit, pointLimit: ConfigService.pointLimit,
caboPenalty: Globals.caboPenalty, caboPenalty: ConfigService.caboPenalty,
isPointsLimitEnabled: _isPointsLimitEnabled!, isPointsLimitEnabled: _isPointsLimitEnabled!,
); );
final index = await gameManager.addGameSession(gameSession); final index = await gameManager.addGameSession(gameSession);

View File

@@ -1,8 +1,8 @@
import 'package:cabo_counter/data/game_manager.dart'; import 'package:cabo_counter/data/game_manager.dart';
import 'package:cabo_counter/l10n/app_localizations.dart'; import 'package:cabo_counter/l10n/app_localizations.dart';
import 'package:cabo_counter/services/config_service.dart';
import 'package:cabo_counter/services/local_storage_service.dart'; import 'package:cabo_counter/services/local_storage_service.dart';
import 'package:cabo_counter/utility/custom_theme.dart'; import 'package:cabo_counter/utility/custom_theme.dart';
import 'package:cabo_counter/utility/globals.dart';
import 'package:cabo_counter/views/active_game_view.dart'; import 'package:cabo_counter/views/active_game_view.dart';
import 'package:cabo_counter/views/create_game_view.dart'; import 'package:cabo_counter/views/create_game_view.dart';
import 'package:cabo_counter/views/settings_view.dart'; import 'package:cabo_counter/views/settings_view.dart';
@@ -199,7 +199,7 @@ class _MainMenuViewState extends State<MainMenuView> {
/// If [pointLimit] is true, it returns '101 Punkte', otherwise it returns 'Unbegrenzt'. /// If [pointLimit] is true, it returns '101 Punkte', otherwise it returns 'Unbegrenzt'.
String _translateGameMode(bool pointLimit) { String _translateGameMode(bool pointLimit) {
if (pointLimit) { if (pointLimit) {
return '${Globals.pointLimit} ${AppLocalizations.of(context).points}'; return '${ConfigService.pointLimit} ${AppLocalizations.of(context).points}';
} }
return AppLocalizations.of(context).unlimited; return AppLocalizations.of(context).unlimited;
} }

View File

@@ -52,14 +52,14 @@ class _SettingsViewState extends State<SettingsView> {
AppLocalizations.of(context).cabo_penalty_subtitle), AppLocalizations.of(context).cabo_penalty_subtitle),
trailing: Stepper( trailing: Stepper(
key: _stepperKey1, key: _stepperKey1,
initialValue: Globals.caboPenalty, initialValue: ConfigService.caboPenalty,
minValue: 0, minValue: 0,
maxValue: 50, maxValue: 50,
step: 1, step: 1,
onChanged: (newCaboPenalty) { onChanged: (newCaboPenalty) {
setState(() { setState(() {
ConfigService.setCaboPenalty(newCaboPenalty); ConfigService.setCaboPenalty(newCaboPenalty);
Globals.caboPenalty = newCaboPenalty; ConfigService.caboPenalty = newCaboPenalty;
}); });
}, },
), ),
@@ -73,14 +73,14 @@ class _SettingsViewState extends State<SettingsView> {
Text(AppLocalizations.of(context).point_limit_subtitle), Text(AppLocalizations.of(context).point_limit_subtitle),
trailing: Stepper( trailing: Stepper(
key: _stepperKey2, key: _stepperKey2,
initialValue: Globals.pointLimit, initialValue: ConfigService.pointLimit,
minValue: 30, minValue: 30,
maxValue: 1000, maxValue: 1000,
step: 10, step: 10,
onChanged: (newPointLimit) { onChanged: (newPointLimit) {
setState(() { setState(() {
ConfigService.setPointLimit(newPointLimit); ConfigService.setPointLimit(newPointLimit);
Globals.pointLimit = newPointLimit; ConfigService.pointLimit = newPointLimit;
}); });
}, },
), ),

View File

@@ -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.3.7+325 version: 0.3.8+327
environment: environment:
sdk: ^3.5.4 sdk: ^3.5.4
@@ -39,4 +39,5 @@ 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 - assets/game_list-schema.json
- assets/game-schema.json