Skip to content
Snippets Groups Projects
Commit 6f739c53 authored by Benoît Harrault's avatar Benoît Harrault
Browse files

Merge branch '20-normalize-activity-application-architecture' into 'master'

Resolve "Normalize Activity application architecture"

Closes #20

See merge request !18
parents 5669f4e0 6775c64b
No related branches found
No related tags found
1 merge request!18Resolve "Normalize Activity application architecture"
Pipeline #6780 passed
Showing
with 659 additions and 44 deletions
{
"app_name": "Oware",
"page_home": "Home",
"page_game": "Game",
"settings_title": "Settings",
"settings_label_theme": "Theme mode",
......
{
"app_name": "Awalé",
"page_home": "Accueil",
"page_game": "Jeu",
"settings_title": "Réglages",
"settings_label_theme": "Thème de couleurs",
......
Normalize Activity application architecture.
Harmonisation des applications en Activity.
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:awale/ui/screens/page_about.dart';
import 'package:awale/ui/screens/page_game.dart';
import 'package:awale/ui/screens/page_settings.dart';
import 'package:awale/common/ui/pages/game.dart';
import 'package:awale/common/ui/pages/parameters.dart';
class MenuItem {
class ActivityPageItem {
final String code;
final Icon icon;
final Widget page;
const MenuItem({
const ActivityPageItem({
required this.code,
required this.icon,
required this.page,
});
}
class Menu {
static const indexGame = 0;
static const menuItemGame = MenuItem(
icon: Icon(UniconsLine.home),
page: PageGame(),
);
class ActivityPage {
static const bool displayBottomNavBar = false;
static const indexSettings = 1;
static const menuItemSettings = MenuItem(
icon: Icon(UniconsLine.setting),
page: PageSettings(),
static const indexHome = 0;
static const pageHome = ActivityPageItem(
code: 'page_home',
icon: Icon(UniconsLine.home),
page: PageParameters(),
);
static const indexAbout = 2;
static const menuItemAbout = MenuItem(
icon: Icon(UniconsLine.info_circle),
page: PageAbout(),
static const indexGame = 1;
static const pageGame = ActivityPageItem(
code: 'page_game',
icon: Icon(UniconsLine.star),
page: PageGame(),
);
static Map<int, MenuItem> items = {
indexGame: menuItemGame,
indexSettings: menuItemSettings,
indexAbout: menuItemAbout,
static const Map<int, ActivityPageItem> items = {
indexHome: pageHome,
indexGame: pageGame,
};
static int defaultPageIndex = indexHome;
static bool isIndexAllowed(int pageIndex) {
return items.keys.contains(pageIndex);
}
static Widget getPageWidget(int pageIndex) {
return items[pageIndex]?.page ?? menuItemGame.page;
static Widget getWidget(int pageIndex) {
return items[pageIndex]?.page ?? pageHome.page;
}
static int itemsCount = Menu.items.length;
}
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:awale/common/ui/screens/about.dart';
import 'package:awale/common/ui/screens/activity.dart';
import 'package:awale/common/ui/screens/settings.dart';
class ScreenItem {
final String code;
final Icon icon;
final Widget screen;
const ScreenItem({
required this.code,
required this.icon,
required this.screen,
});
}
class Screen {
static const indexActivity = 0;
static const screenActivity = ScreenItem(
code: 'screen_activity',
icon: Icon(UniconsLine.home),
screen: ScreenActivity(),
);
static const indexSettings = 1;
static const screenSettings = ScreenItem(
code: 'screen_settings',
icon: Icon(UniconsLine.setting),
screen: ScreenSettings(),
);
static const indexAbout = 2;
static const screenAbout = ScreenItem(
code: 'screen_about',
icon: Icon(UniconsLine.info_circle),
screen: ScreenAbout(),
);
static Map<int, ScreenItem> items = {
indexActivity: screenActivity,
indexSettings: screenSettings,
indexAbout: screenAbout,
};
static bool isIndexAllowed(int screenIndex) {
return items.keys.contains(screenIndex);
}
static Widget getWidget(int screenIndex) {
return items[screenIndex]?.screen ?? screenActivity.screen;
}
}
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:awale/config/menu.dart';
import 'package:awale/common/config/activity_page.dart';
class NavCubit extends HydratedCubit<int> {
NavCubit() : super(0);
class NavCubitPage extends HydratedCubit<int> {
NavCubitPage() : super(0);
void updateIndex(int index) {
if (Menu.isIndexAllowed(index)) {
if (ActivityPage.isIndexAllowed(index)) {
emit(index);
} else {
goToGamePage();
emit(ActivityPage.indexHome);
}
}
void goToGamePage() {
emit(Menu.indexGame);
void goToPageHome() {
updateIndex(ActivityPage.indexHome);
}
void goToSettingsPage() {
emit(Menu.indexSettings);
}
void goToAboutPage() {
emit(Menu.indexAbout);
void goToPageGame() {
updateIndex(ActivityPage.indexGame);
}
@override
int fromJson(Map<String, dynamic> json) {
return Menu.indexGame;
return ActivityPage.indexHome;
}
@override
Map<String, dynamic>? toJson(int state) {
return <String, int>{'pageIndex': state};
return <String, int>{'index': state};
}
}
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:awale/common/config/screen.dart';
class NavCubitScreen extends HydratedCubit<int> {
NavCubitScreen() : super(0);
void updateIndex(int index) {
if (Screen.isIndexAllowed(index)) {
emit(index);
} else {
goToScreenActivity();
}
}
void goToScreenActivity() {
emit(Screen.indexActivity);
}
void goToScreenSettings() {
emit(Screen.indexSettings);
}
void goToScreenAbout() {
emit(Screen.indexAbout);
}
@override
int fromJson(Map<String, dynamic> json) {
return Screen.indexActivity;
}
@override
Map<String, dynamic>? toJson(int state) {
return <String, int>{'index': state};
}
}
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:awale/common/config/activity_page.dart';
import 'package:awale/common/cubit/nav/nav_cubit_pages.dart';
class BottomNavBar extends StatelessWidget {
const BottomNavBar({super.key});
@override
Widget build(BuildContext context) {
return Card(
margin: const EdgeInsets.only(top: 1, right: 4, left: 4),
elevation: 4,
shadowColor: Theme.of(context).colorScheme.shadow,
color: Theme.of(context).colorScheme.surfaceContainerHighest,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(16),
topRight: Radius.circular(16),
),
),
child: BlocBuilder<NavCubitPage, int>(builder: (BuildContext context, int state) {
final List<BottomNavigationBarItem> items = [];
ActivityPage.items.forEach((int pageIndex, ActivityPageItem item) {
items.add(BottomNavigationBarItem(
icon: item.icon,
label: tr(item.code),
));
});
return BottomNavigationBar(
currentIndex: state,
onTap: (int index) => BlocProvider.of<NavCubitPage>(context).updateIndex(index),
type: BottomNavigationBarType.fixed,
elevation: 0,
backgroundColor: Colors.transparent,
selectedItemColor: Theme.of(context).colorScheme.primary,
unselectedItemColor: Theme.of(context).textTheme.bodySmall!.color,
items: items,
);
}),
);
}
}
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:awale/config/menu.dart';
import 'package:awale/cubit/game_cubit.dart';
import 'package:awale/cubit/nav_cubit.dart';
import 'package:awale/models/game/game.dart';
import 'package:awale/common/config/screen.dart';
import 'package:awale/common/cubit/nav/nav_cubit_pages.dart';
import 'package:awale/common/cubit/nav/nav_cubit_screens.dart';
import 'package:awale/cubit/activity/activity_cubit.dart';
import 'package:awale/models/activity/activity.dart';
class GlobalAppBar extends StatelessWidget implements PreferredSizeWidget {
const GlobalAppBar({super.key});
@override
Widget build(BuildContext context) {
return BlocBuilder<GameCubit, GameState>(
builder: (BuildContext context, GameState gameState) {
return BlocBuilder<NavCubit, int>(
return BlocBuilder<ActivityCubit, ActivityState>(
builder: (BuildContext context, ActivityState activityState) {
return BlocBuilder<NavCubitScreen, int>(
builder: (BuildContext context, int pageIndex) {
final Game currentGame = gameState.currentGame;
final Activity currentActivity = activityState.currentActivity;
final List<Widget> menuActions = [];
if (currentGame.isRunning && !currentGame.isFinished) {
if (currentActivity.isRunning && !currentActivity.isFinished) {
menuActions.add(StyledButton(
color: Colors.red,
onPressed: () {},
onLongPress: () {
BlocProvider.of<GameCubit>(context).quitGame();
BlocProvider.of<ActivityCubit>(context).quitActivity();
BlocProvider.of<NavCubitPage>(context).goToPageHome();
},
child: const Image(
image: AssetImage('assets/ui/button_back.png'),
......@@ -32,38 +35,38 @@ class GlobalAppBar extends StatelessWidget implements PreferredSizeWidget {
),
));
} else {
if (pageIndex == Menu.indexGame) {
if (pageIndex == Screen.indexActivity) {
// go to Settings page
menuActions.add(ElevatedButton(
onPressed: () {
BlocProvider.of<NavCubit>(context).goToSettingsPage();
BlocProvider.of<NavCubitScreen>(context).goToScreenSettings();
},
style: ElevatedButton.styleFrom(
shape: const CircleBorder(),
),
child: Menu.menuItemSettings.icon,
child: Screen.screenSettings.icon,
));
// go to About page
menuActions.add(ElevatedButton(
onPressed: () {
BlocProvider.of<NavCubit>(context).goToAboutPage();
BlocProvider.of<NavCubitScreen>(context).goToScreenAbout();
},
style: ElevatedButton.styleFrom(
shape: const CircleBorder(),
),
child: Menu.menuItemAbout.icon,
child: Screen.screenAbout.icon,
));
} else {
// back to Home page
menuActions.add(ElevatedButton(
onPressed: () {
BlocProvider.of<NavCubit>(context).goToGamePage();
BlocProvider.of<NavCubitScreen>(context).goToScreenActivity();
},
style: ElevatedButton.styleFrom(
shape: const CircleBorder(),
),
child: Menu.menuItemGame.icon,
child: Screen.screenActivity.icon,
));
}
}
......
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:awale/cubit/game_cubit.dart';
import 'package:awale/models/game/game.dart';
import 'package:awale/cubit/activity/activity_cubit.dart';
import 'package:awale/models/activity/activity.dart';
import 'package:awale/ui/widgets/game/game_board.dart';
import 'package:awale/ui/widgets/game/game_end.dart';
import 'package:awale/ui/widgets/game/game_player.dart';
class GameLayout extends StatelessWidget {
const GameLayout({super.key});
class PageGame extends StatelessWidget {
const PageGame({super.key});
@override
Widget build(BuildContext context) {
return BlocBuilder<GameCubit, GameState>(
builder: (BuildContext context, GameState gameState) {
final Game currentGame = gameState.currentGame;
return BlocBuilder<ActivityCubit, ActivityState>(
builder: (BuildContext context, ActivityState activityState) {
final Activity currentActivity = activityState.currentActivity;
var screenSize = MediaQuery.of(context).size;
......@@ -35,7 +35,7 @@ class GameLayout extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
currentGame.isFinished
currentActivity.isFinished
? GameEndWidget(
playerIndex: 0,
widgetSize: baseSize,
......@@ -45,7 +45,7 @@ class GameLayout extends StatelessWidget {
widgetSize: baseSize,
),
GameBoardWidget(houseSize: baseSize),
currentGame.isFinished
currentActivity.isFinished
? GameEndWidget(
playerIndex: 1,
widgetSize: baseSize,
......
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:awale/config/default_game_settings.dart';
import 'package:awale/common/ui/parameters/parameter_widget.dart';
import 'package:awale/config/default_activity_settings.dart';
import 'package:awale/config/default_global_settings.dart';
import 'package:awale/cubit/settings_game_cubit.dart';
import 'package:awale/cubit/settings_global_cubit.dart';
import 'package:awale/cubit/activity/activity_cubit.dart';
import 'package:awale/cubit/settings/settings_activity_cubit.dart';
import 'package:awale/cubit/settings/settings_global_cubit.dart';
import 'package:awale/models/activity/activity.dart';
import 'package:awale/ui/widgets/actions/button_delete_saved_game.dart';
import 'package:awale/ui/widgets/actions/button_game_start_new.dart';
import 'package:awale/ui/widgets/actions/button_resume_saved_game.dart';
import 'package:awale/ui/parameters/parameter_widget.dart';
class ParametersLayout extends StatelessWidget {
const ParametersLayout({super.key, required this.canResume});
final bool canResume;
class PageParameters extends StatelessWidget {
const PageParameters({super.key});
final double separatorHeight = 8.0;
@override
Widget build(BuildContext context) {
return BlocBuilder<ActivityCubit, ActivityState>(
builder: (BuildContext context, ActivityState activityState) {
final Activity currentActivity = activityState.currentActivity;
final List<Widget> lines = [];
// Game settings
for (String code in DefaultGameSettings.availableParameters) {
for (String code in DefaultActivitySettings.availableParameters) {
lines.add(Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: buildParametersLine(
......@@ -38,7 +43,7 @@ class ParametersLayout extends StatelessWidget {
child: SizedBox(height: separatorHeight),
));
if (canResume == false) {
if (currentActivity.canBeResumed == false) {
// Start new game
lines.add(
const AspectRatio(
......@@ -77,6 +82,8 @@ class ParametersLayout extends StatelessWidget {
return Column(
children: lines,
);
},
);
}
List<Widget> buildParametersLine({
......@@ -87,25 +94,25 @@ class ParametersLayout extends StatelessWidget {
final List<String> availableValues = isGlobal
? DefaultGlobalSettings.getAvailableValues(code)
: DefaultGameSettings.getAvailableValues(code);
: DefaultActivitySettings.getAvailableValues(code);
if (availableValues.length <= 1) {
return [];
}
for (String value in availableValues) {
final Widget parameterButton = BlocBuilder<GameSettingsCubit, GameSettingsState>(
builder: (BuildContext context, GameSettingsState gameSettingsState) {
final Widget parameterButton = BlocBuilder<ActivitySettingsCubit, ActivitySettingsState>(
builder: (BuildContext context, ActivitySettingsState activitySettingsState) {
return BlocBuilder<GlobalSettingsCubit, GlobalSettingsState>(
builder: (BuildContext context, GlobalSettingsState globalSettingsState) {
final GameSettingsCubit gameSettingsCubit =
BlocProvider.of<GameSettingsCubit>(context);
final ActivitySettingsCubit activitySettingsCubit =
BlocProvider.of<ActivitySettingsCubit>(context);
final GlobalSettingsCubit globalSettingsCubit =
BlocProvider.of<GlobalSettingsCubit>(context);
final String currentValue = isGlobal
? globalSettingsCubit.getParameterValue(code)
: gameSettingsCubit.getParameterValue(code);
: activitySettingsCubit.getParameterValue(code);
final bool isSelected = (value == currentValue);
......@@ -119,12 +126,12 @@ class ParametersLayout extends StatelessWidget {
value: value,
isSelected: isSelected,
size: itemWidth,
gameSettings: gameSettingsState.settings,
activitySettings: activitySettingsState.settings,
globalSettings: globalSettingsState.settings,
onPressed: () {
isGlobal
? globalSettingsCubit.setParameterValue(code, value)
: gameSettingsCubit.setParameterValue(code, value);
: activitySettingsCubit.setParameterValue(code, value);
},
),
);
......
......@@ -3,20 +3,20 @@ import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:awale/models/settings/settings_game.dart';
import 'package:awale/models/settings/settings_activity.dart';
import 'package:awale/models/settings/settings_global.dart';
class ParameterPainter extends CustomPainter {
const ParameterPainter({
required this.code,
required this.value,
required this.gameSettings,
required this.activitySettings,
required this.globalSettings,
});
final String code;
final String value;
final GameSettings gameSettings;
final ActivitySettings activitySettings;
final GlobalSettings globalSettings;
@override
......
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:awale/config/default_game_settings.dart';
import 'package:awale/models/settings/settings_game.dart';
import 'package:awale/config/default_activity_settings.dart';
import 'package:awale/models/settings/settings_activity.dart';
import 'package:awale/models/settings/settings_global.dart';
class ParameterWidget extends StatelessWidget {
......@@ -12,7 +12,7 @@ class ParameterWidget extends StatelessWidget {
required this.value,
required this.isSelected,
required this.size,
required this.gameSettings,
required this.activitySettings,
required this.globalSettings,
required this.onPressed,
});
......@@ -21,7 +21,7 @@ class ParameterWidget extends StatelessWidget {
final String value;
final bool isSelected;
final double size;
final GameSettings gameSettings;
final ActivitySettings activitySettings;
final GlobalSettings globalSettings;
final VoidCallback onPressed;
......@@ -35,7 +35,7 @@ class ParameterWidget extends StatelessWidget {
Widget content = const SizedBox.shrink();
switch (code) {
case DefaultGameSettings.parameterCodeGameMode:
case DefaultActivitySettings.parameterCodeGameMode:
content = getGameModeParameterItem();
break;
default:
......@@ -72,19 +72,19 @@ class ParameterWidget extends StatelessWidget {
Color baseColor = Colors.grey;
switch (value) {
case DefaultGameSettings.gameModeHumanVsHuman:
case DefaultActivitySettings.gameModeHumanVsHuman:
text = '🧑 🧑';
baseColor = Colors.green;
break;
case DefaultGameSettings.gameModeHumanVsRobot:
case DefaultActivitySettings.gameModeHumanVsRobot:
text = '🧑 🤖';
baseColor = Colors.pink;
break;
case DefaultGameSettings.gameModeRobotVsHuman:
case DefaultActivitySettings.gameModeRobotVsHuman:
text = '🤖 🧑';
baseColor = Colors.pink;
break;
case DefaultGameSettings.gameModeRobotVsRobot:
case DefaultActivitySettings.gameModeRobotVsRobot:
text = '🤖 🤖';
baseColor = Colors.brown;
break;
......
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
class PageAbout extends StatelessWidget {
const PageAbout({super.key});
class ScreenAbout extends StatelessWidget {
const ScreenAbout({super.key});
@override
Widget build(BuildContext context) {
......
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:awale/common/config/activity_page.dart';
import 'package:awale/common/cubit/nav/nav_cubit_pages.dart';
class ScreenActivity extends StatelessWidget {
const ScreenActivity({super.key});
@override
Widget build(BuildContext context) {
return BlocBuilder<NavCubitPage, int>(
builder: (BuildContext context, int pageIndex) {
return ActivityPage.getWidget(pageIndex);
},
);
}
}
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
class PageSettings extends StatelessWidget {
const PageSettings({super.key});
class ScreenSettings extends StatelessWidget {
const ScreenSettings({super.key});
@override
Widget build(BuildContext context) {
......
class ApplicationConfig {
static const String appTitle = 'Awale';
}
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
class DefaultGameSettings {
class DefaultActivitySettings {
// available game parameters codes
static const String parameterCodeGameMode = 'gameMode';
static const List<String> availableParameters = [
......@@ -25,7 +25,7 @@ class DefaultGameSettings {
static List<String> getAvailableValues(String parameterCode) {
switch (parameterCode) {
case parameterCodeGameMode:
return DefaultGameSettings.allowedGameModeValues;
return DefaultActivitySettings.allowedGameModeValues;
}
printlog('Did not find any available value for game parameter "$parameterCode".');
......
......@@ -3,91 +3,93 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:awale/models/game/game.dart';
import 'package:awale/models/settings/settings_game.dart';
import 'package:awale/models/activity/activity.dart';
import 'package:awale/models/settings/settings_activity.dart';
import 'package:awale/models/settings/settings_global.dart';
import 'package:awale/robot/robot_player.dart';
part 'game_state.dart';
part 'activity_state.dart';
class GameCubit extends HydratedCubit<GameState> {
GameCubit()
: super(GameState(
currentGame: Game.createNull(),
class ActivityCubit extends HydratedCubit<ActivityState> {
ActivityCubit()
: super(ActivityState(
currentActivity: Activity.createNull(),
));
void updateState(Game game) {
emit(GameState(
currentGame: game,
void updateState(Activity activity) {
emit(ActivityState(
currentActivity: activity,
));
}
void refresh() {
final Game game = Game(
final Activity activity = Activity(
// Settings
gameSettings: state.currentGame.gameSettings,
globalSettings: state.currentGame.globalSettings,
activitySettings: state.currentActivity.activitySettings,
globalSettings: state.currentActivity.globalSettings,
// State
isRunning: state.currentGame.isRunning,
isStarted: state.currentGame.isStarted,
isFinished: state.currentGame.isFinished,
animationInProgress: state.currentGame.animationInProgress,
isRunning: state.currentActivity.isRunning,
isStarted: state.currentActivity.isStarted,
isFinished: state.currentActivity.isFinished,
animationInProgress: state.currentActivity.animationInProgress,
// Base data
board: state.currentGame.board,
board: state.currentActivity.board,
// Game data
currentPlayer: state.currentGame.currentPlayer,
scores: state.currentGame.scores,
currentHand: state.currentGame.currentHand,
currentPlayer: state.currentActivity.currentPlayer,
scores: state.currentActivity.scores,
currentHand: state.currentActivity.currentHand,
);
// game.dump();
updateState(game);
updateState(activity);
}
void startNewGame({
required GameSettings gameSettings,
void startNewActivity({
required ActivitySettings activitySettings,
required GlobalSettings globalSettings,
}) {
final Game newGame = Game.createNew(
final Activity newActivity = Activity.createNew(
// Settings
gameSettings: gameSettings,
activitySettings: activitySettings,
globalSettings: globalSettings,
);
newGame.dump();
newActivity.dump();
updateState(newGame);
updateState(newActivity);
refresh();
robotPlay();
}
void quitGame() {
state.currentGame.isRunning = false;
void quitActivity() {
state.currentActivity.isRunning = false;
refresh();
}
void resumeSavedGame() {
state.currentGame.isRunning = true;
void resumeSavedActivity() {
state.currentActivity.isRunning = true;
refresh();
}
void deleteSavedGame() {
state.currentGame.isRunning = false;
state.currentGame.isFinished = true;
void deleteSavedActivity() {
state.currentActivity.isRunning = false;
state.currentActivity.isFinished = true;
refresh();
}
void toggleCurrentPlayer() {
state.currentGame.currentPlayer = 1 - state.currentGame.currentPlayer;
state.currentActivity.currentPlayer = 1 - state.currentActivity.currentPlayer;
refresh();
robotPlay();
}
void robotPlay() async {
if (!state.currentGame.isFinished && !state.currentGame.isCurrentPlayerHuman()) {
final int? pickedCell = RobotPlayer.pickCell(state.currentGame);
if (state.currentActivity.isRunning &&
!state.currentActivity.isFinished &&
!state.currentActivity.isCurrentPlayerHuman()) {
final int? pickedCell = RobotPlayer.pickCell(state.currentActivity);
await Future.delayed(const Duration(milliseconds: 500));
if (pickedCell != null) {
......@@ -99,25 +101,25 @@ class GameCubit extends HydratedCubit<GameState> {
void tapOnCell(int cellIndex) async {
printlog('tapOnCell: $cellIndex');
if (!state.currentGame.isCurrentPlayerHouse(cellIndex)) {
if (!state.currentActivity.isCurrentPlayerHouse(cellIndex)) {
printlog('not allowed');
return;
}
if (state.currentGame.board.cells[cellIndex] == 0) {
if (state.currentActivity.board.cells[cellIndex] == 0) {
printlog('empty cell');
return;
}
if (!state.currentGame.isMoveAllowed(cellIndex)) {
if (!state.currentActivity.isMoveAllowed(cellIndex)) {
printlog('not allowed (need to give at least one seed to other player)');
return;
}
state.currentGame.animationInProgress = true;
state.currentActivity.animationInProgress = true;
refresh();
final int lastCellIndex = await animateSeedsDistribution(cellIndex);
......@@ -125,34 +127,34 @@ class GameCubit extends HydratedCubit<GameState> {
toggleCurrentPlayer();
if (!state.currentGame.canPlay()) {
if (!state.currentActivity.canPlay()) {
printlog('user has no more move to play');
state.currentGame.isFinished = true;
state.currentActivity.isFinished = true;
refresh();
}
state.currentGame.animationInProgress = false;
state.currentActivity.animationInProgress = false;
refresh();
}
Future<int> animateSeedsDistribution(int sourceCellIndex) async {
printlog('animateSeedsDistribution / sourceCellIndex: $sourceCellIndex');
final int seedsCount = state.currentGame.board.cells[sourceCellIndex];
final int seedsCount = state.currentActivity.board.cells[sourceCellIndex];
// empty source cell
printlog('animateSeedsDistribution / empty source cell');
state.currentGame.board.cells[sourceCellIndex] = 0;
state.currentGame.currentHand = seedsCount;
state.currentActivity.board.cells[sourceCellIndex] = 0;
state.currentActivity.currentHand = seedsCount;
refresh();
await Future.delayed(const Duration(milliseconds: 200));
int cellIndex = sourceCellIndex;
for (int i = 0; i < seedsCount; i++) {
cellIndex = state.currentGame.getNextCellIndex(cellIndex, sourceCellIndex);
state.currentGame.currentHand--;
state.currentGame.board.cells[cellIndex] += 1;
cellIndex = state.currentActivity.getNextCellIndex(cellIndex, sourceCellIndex);
state.currentActivity.currentHand--;
state.currentActivity.board.cells[cellIndex] += 1;
refresh();
await Future.delayed(const Duration(milliseconds: 300));
}
......@@ -167,15 +169,15 @@ class GameCubit extends HydratedCubit<GameState> {
int earnedSeedsCount = 0;
if (state.currentGame.isOpponentHouse(lastCellIndex)) {
final int seedsCount = state.currentGame.board.cells[lastCellIndex];
if (state.currentActivity.isOpponentHouse(lastCellIndex)) {
final int seedsCount = state.currentActivity.board.cells[lastCellIndex];
printlog('found $seedsCount seed(s) on final house');
if ([2, 3].contains(seedsCount)) {
printlog('-> ok will earn these seeds');
state.currentGame.board.cells[lastCellIndex] = 0;
state.currentGame.scores[state.currentGame.currentPlayer] += seedsCount;
state.currentActivity.board.cells[lastCellIndex] = 0;
state.currentActivity.scores[state.currentActivity.currentPlayer] += seedsCount;
earnedSeedsCount += seedsCount;
refresh();
......@@ -183,7 +185,8 @@ class GameCubit extends HydratedCubit<GameState> {
// (recursively) check previous cells
printlog('-> dispatch to previous cell');
final int previousCellIndex = state.currentGame.getPreviousCellIndex(lastCellIndex);
final int previousCellIndex =
state.currentActivity.getPreviousCellIndex(lastCellIndex);
earnedSeedsCount += await animateSeedsEarning(previousCellIndex);
} else {
printlog('-> nothing to do');
......@@ -194,18 +197,18 @@ class GameCubit extends HydratedCubit<GameState> {
}
@override
GameState? fromJson(Map<String, dynamic> json) {
final Game currentGame = json['currentGame'] as Game;
ActivityState? fromJson(Map<String, dynamic> json) {
final Activity currentActivity = json['currentActivity'] as Activity;
return GameState(
currentGame: currentGame,
return ActivityState(
currentActivity: currentActivity,
);
}
@override
Map<String, dynamic>? toJson(GameState state) {
Map<String, dynamic>? toJson(ActivityState state) {
return <String, dynamic>{
'currentGame': state.currentGame.toJson(),
'currentActivity': state.currentActivity.toJson(),
};
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment