From e7071bac63a264cfa99fee67f7158cdf1a68f735 Mon Sep 17 00:00:00 2001 From: Calcitem Date: Sun, 11 Jul 2021 21:43:16 +0800 Subject: [PATCH] rule: Set default rule to Twelve Men's Morris if country is Iran --- src/ui/flutter_app/lib/common/config.dart | 14 +++++++++----- src/ui/flutter_app/lib/engine/engine.dart | 3 ++- .../flutter_app/lib/engine/native_engine.dart | 4 +++- src/ui/flutter_app/lib/l10n/resources.dart | 17 +++++++++++++++++ src/ui/flutter_app/lib/main.dart | 2 ++ src/ui/flutter_app/lib/mill/rule.dart | 8 +++++--- src/ui/flutter_app/lib/widgets/game_page.dart | 6 +++--- .../lib/widgets/rule_settings_page.dart | 4 +++- 8 files changed, 44 insertions(+), 14 deletions(-) diff --git a/src/ui/flutter_app/lib/common/config.dart b/src/ui/flutter_app/lib/common/config.dart index 2513bc53..14029b43 100644 --- a/src/ui/flutter_app/lib/common/config.dart +++ b/src/ui/flutter_app/lib/common/config.dart @@ -17,6 +17,7 @@ */ import 'package:sanmill/common/constants.dart'; +import 'package:sanmill/l10n/resources.dart'; import 'package:sanmill/mill/rule.dart'; import 'package:sanmill/style/app_theme.dart'; @@ -67,10 +68,11 @@ class Config { static int messageColor = AppTheme.messageColor.value; // Rules - static int piecesCount = 9; + static int piecesCount = specialCountryAndRegion == "Iran" ? 12 : 9; static int flyPieceCount = 3; static int piecesAtLeastCount = 3; - static bool hasDiagonalLines = false; + static bool hasDiagonalLines = + specialCountryAndRegion == "Iran" ? true : false; static bool hasBannedLocations = false; static bool mayMoveInPlacingPhase = false; static bool isDefenderMoveFirst = false; @@ -139,12 +141,14 @@ class Config { settings['MessageColor'] ?? AppTheme.messageColor.value; // Rules - rule.piecesCount = Config.piecesCount = settings['PiecesCount'] ?? 9; + rule.piecesCount = Config.piecesCount = + settings['PiecesCount'] ?? (specialCountryAndRegion == "Iran" ? 12 : 9); rule.flyPieceCount = Config.flyPieceCount = settings['FlyPieceCount'] ?? 3; rule.piecesAtLeastCount = Config.piecesAtLeastCount = settings['PiecesAtLeastCount'] ?? 3; - rule.hasDiagonalLines = - Config.hasDiagonalLines = settings['HasDiagonalLines'] ?? false; + rule.hasDiagonalLines = Config.hasDiagonalLines = + settings['HasDiagonalLines'] ?? + (specialCountryAndRegion == "Iran" ? true : false); rule.hasBannedLocations = Config.hasBannedLocations = settings['HasBannedLocations'] ?? false; rule.mayMoveInPlacingPhase = Config.mayMoveInPlacingPhase = diff --git a/src/ui/flutter_app/lib/engine/engine.dart b/src/ui/flutter_app/lib/engine/engine.dart index 9ff62fb1..6d946eed 100644 --- a/src/ui/flutter_app/lib/engine/engine.dart +++ b/src/ui/flutter_app/lib/engine/engine.dart @@ -16,6 +16,7 @@ along with this program. If not, see . */ +import 'package:flutter/material.dart'; import 'package:sanmill/mill/position.dart'; enum EngineType { @@ -35,7 +36,7 @@ class EngineResponse { } abstract class Engine { - Future setOptions() async {} + Future setOptions(BuildContext context) async {} Future startup() async {} Future shutdown() async {} Future search(Position? position); diff --git a/src/ui/flutter_app/lib/engine/native_engine.dart b/src/ui/flutter_app/lib/engine/native_engine.dart index 539e8aa4..7a665716 100644 --- a/src/ui/flutter_app/lib/engine/native_engine.dart +++ b/src/ui/flutter_app/lib/engine/native_engine.dart @@ -18,8 +18,10 @@ import 'dart:async'; +import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:sanmill/common/config.dart'; +import 'package:sanmill/l10n/resources.dart'; import 'package:sanmill/mill/position.dart'; import 'package:sanmill/mill/types.dart'; @@ -131,7 +133,7 @@ class NativeEngine extends Engine { await send('stop'); } - Future setOptions() async { + Future setOptions(BuildContext context) async { if (Config.settingsLoaded == false) { print("[engine] Settings is not loaded yet, now load settings..."); await Config.loadSettings(); diff --git a/src/ui/flutter_app/lib/l10n/resources.dart b/src/ui/flutter_app/lib/l10n/resources.dart index ff2eea60..d9fe49c4 100644 --- a/src/ui/flutter_app/lib/l10n/resources.dart +++ b/src/ui/flutter_app/lib/l10n/resources.dart @@ -565,3 +565,20 @@ Bidirectionality getBidirectionality(BuildContext context) { return Bidirectionality.leftToRight; } } + +String specialCountryAndRegion = ""; + +setSpecialCountryAndRegion(BuildContext context) { + Locale currentLocale = Localizations.localeOf(context); + + switch (currentLocale.countryCode) { + case "IR": + specialCountryAndRegion = "Iran"; + break; + default: + specialCountryAndRegion = ""; + break; + } + + print("Set Special Country and Region to $specialCountryAndRegion."); +} diff --git a/src/ui/flutter_app/lib/main.dart b/src/ui/flutter_app/lib/main.dart index aaac24ae..be3ef5f1 100644 --- a/src/ui/flutter_app/lib/main.dart +++ b/src/ui/flutter_app/lib/main.dart @@ -126,6 +126,8 @@ class _SanmillAppState extends State { @override Widget build(BuildContext context) { + setSpecialCountryAndRegion(context); + return MaterialApp( /// Add navigator key from Catcher. /// It will be used to navigate user to report page or to show dialog. diff --git a/src/ui/flutter_app/lib/mill/rule.dart b/src/ui/flutter_app/lib/mill/rule.dart index 388b1a57..10aef8d3 100644 --- a/src/ui/flutter_app/lib/mill/rule.dart +++ b/src/ui/flutter_app/lib/mill/rule.dart @@ -16,13 +16,15 @@ along with this program. If not, see . */ +import 'package:sanmill/l10n/resources.dart'; + class Rule { - String name = "Nine Men's Morris"; + String name = "Default Rule"; String description = ""; - int piecesCount = 9; + int piecesCount = specialCountryAndRegion == "Iran" ? 12 : 9; int flyPieceCount = 3; int piecesAtLeastCount = 3; - bool hasDiagonalLines = false; + bool hasDiagonalLines = specialCountryAndRegion == "Iran" ? true : false; bool hasBannedLocations = false; bool mayMoveInPlacingPhase = false; bool isDefenderMoveFirst = false; diff --git a/src/ui/flutter_app/lib/widgets/game_page.dart b/src/ui/flutter_app/lib/widgets/game_page.dart index 5d02c5cb..eede453f 100644 --- a/src/ui/flutter_app/lib/widgets/game_page.dart +++ b/src/ui/flutter_app/lib/widgets/game_page.dart @@ -1631,7 +1631,7 @@ class _GamePageState extends State void didPush() async { final route = ModalRoute.of(context)!.settings.name; print('$tag Game Page didPush route: $route'); - await widget.engine.setOptions(); + await widget.engine.setOptions(context); if (Config.languageCode != Constants.defaultLanguageCodeName) { S.load(Locale(Config.languageCode)); setState(() {}); @@ -1642,7 +1642,7 @@ class _GamePageState extends State void didPopNext() async { final route = ModalRoute.of(context)!.settings.name; print('$tag Game Page didPopNext route: $route'); - await widget.engine.setOptions(); + await widget.engine.setOptions(context); if (Config.languageCode != Constants.defaultLanguageCodeName) { S.load(Locale(Config.languageCode)); } @@ -1652,7 +1652,7 @@ class _GamePageState extends State void didPushNext() async { final route = ModalRoute.of(context)!.settings.name; print('$tag Game Page didPushNext route: $route'); - await widget.engine.setOptions(); + await widget.engine.setOptions(context); if (Config.languageCode != Constants.defaultLanguageCodeName) { S.load(Locale(Config.languageCode)); } diff --git a/src/ui/flutter_app/lib/widgets/rule_settings_page.dart b/src/ui/flutter_app/lib/widgets/rule_settings_page.dart index 4af017f4..d6ca3089 100644 --- a/src/ui/flutter_app/lib/widgets/rule_settings_page.dart +++ b/src/ui/flutter_app/lib/widgets/rule_settings_page.dart @@ -19,6 +19,7 @@ import 'package:flutter/material.dart'; import 'package:sanmill/common/config.dart'; import 'package:sanmill/generated/l10n.dart'; +import 'package:sanmill/l10n/resources.dart'; import 'package:sanmill/mill/rule.dart'; import 'package:sanmill/style/app_theme.dart'; import 'package:sanmill/widgets/settings_card.dart'; @@ -200,7 +201,8 @@ class _RuleSettingsPageState extends State { Navigator.of(context).pop(); setState(() { - rule.piecesCount = Config.piecesCount = piecesCount ?? 9; + rule.piecesCount = Config.piecesCount = + piecesCount ?? (specialCountryAndRegion == "Iran" ? 12 : 9); }); print("[config] rule.piecesCount: ${rule.piecesCount}");