Skip to content
Snippets Groups Projects
Select Git revision
  • d5ae06a92a1b200376e8f576b06e6f9fe9148729
  • master default protected
  • 38-upgrade-framework-and-dependencies
  • 20-improve-app-metadata
  • Release_0.8.0_32 protected
  • Release_0.7.2_31 protected
  • Release_0.7.1_30 protected
  • Release_0.7.0_29 protected
  • Release_0.6.0_28 protected
  • Release_0.5.0_27 protected
  • Release_0.4.0_26 protected
  • Release_0.3.2_25 protected
  • Release_0.3.1_24 protected
  • Release_0.3.0_23 protected
  • Release_0.2.1_22 protected
  • Release_0.2.0_21 protected
  • Release_0.1.2_20 protected
  • Release_0.1.1_19 protected
  • Release_0.1.0_18 protected
  • Release_0.0.17_17 protected
  • Release_0.0.16_16 protected
  • Release_0.0.15_15 protected
  • Release_0.0.14_14 protected
  • Release_0.0.13_13 protected
24 results

settings_global.dart

Blame
  • game_cubit.dart 4.55 KiB
    import 'package:equatable/equatable.dart';
    import 'package:flutter/material.dart';
    import 'package:hydrated_bloc/hydrated_bloc.dart';
    
    import 'package:awale/models/game/game.dart';
    import 'package:awale/models/settings/settings_game.dart';
    import 'package:awale/models/settings/settings_global.dart';
    import 'package:awale/utils/tools.dart';
    
    part 'game_state.dart';
    
    class GameCubit extends HydratedCubit<GameState> {
      GameCubit()
          : super(GameState(
              currentGame: Game.createNull(),
            ));
    
      void updateState(Game game) {
        emit(GameState(
          currentGame: game,
        ));
      }
    
      void refresh() {
        final Game game = Game(
          // Settings
          gameSettings: state.currentGame.gameSettings,
          globalSettings: state.currentGame.globalSettings,
          // State
          isRunning: state.currentGame.isRunning,
          isStarted: state.currentGame.isStarted,
          isFinished: state.currentGame.isFinished,
          animationInProgress: state.currentGame.animationInProgress,
          // Base data
          board: state.currentGame.board,
          // Game data
          currentPlayer: state.currentGame.currentPlayer,
          scores: state.currentGame.scores,
        );
        // game.dump();
    
        updateState(game);
      }
    
      void startNewGame({
        required GameSettings gameSettings,
        required GlobalSettings globalSettings,
      }) {
        final Game newGame = Game.createNew(
          // Settings
          gameSettings: gameSettings,
          globalSettings: globalSettings,
        );
    
        newGame.dump();
    
        updateState(newGame);
        refresh();
      }
    
      void quitGame() {
        state.currentGame.isRunning = false;
        refresh();
      }
    
      void resumeSavedGame() {
        state.currentGame.isRunning = true;
        refresh();
      }
    
      void deleteSavedGame() {
        state.currentGame.isRunning = false;
        state.currentGame.isFinished = true;
        refresh();
      }
    
      void toggleCurrentPlayer() {
        state.currentGame.currentPlayer = 1 - state.currentGame.currentPlayer;
        refresh();
      }
    
      void tapOnCell(int cellIndex) {
        printlog('tapOnCell: $cellIndex');
    
        if (!state.currentGame.isCurrentPlayerHouse(cellIndex)) {
          printlog('not allowed');
    
          return;
        }
    
        if (state.currentGame.board.cells[cellIndex] == 0) {
          printlog('empty cell');
    
          return;
        }
    
        if (!state.currentGame.isMoveAllowed(cellIndex)) {
          printlog('not allowed (need to give at least one seed to other player)');
    
          return;
        }
    
        state.currentGame.animationInProgress = true;
        refresh();
    
        final int lastCellIndex = animateSeedsDistribution(cellIndex);
        animateSeedsEarning(lastCellIndex);
    
        toggleCurrentPlayer();
    
        if (!state.currentGame.canPlay()) {
          printlog('user has no more move to play');
          state.currentGame.isFinished = true;
          refresh();
        }
    
        state.currentGame.animationInProgress = false;
        refresh();
      }
    
      int animateSeedsDistribution(int sourceCellIndex) {
        printlog('animateSeedsDistribution / sourceCellIndex: $sourceCellIndex');
    
        final int seedsCount = state.currentGame.board.cells[sourceCellIndex];
    
        // empty source cell
        state.currentGame.board.cells[sourceCellIndex] = 0;
        printlog('animateSeedsDistribution / empty source cell');
        refresh();
    
        int cellIndex = sourceCellIndex;
        for (int i = 0; i < seedsCount; i++) {
          cellIndex = state.currentGame.getNextCellIndex(cellIndex, sourceCellIndex);
          state.currentGame.board.cells[cellIndex] += 1;
          refresh();
        }
    
        refresh();
    
        return cellIndex;
      }
    
      void animateSeedsEarning(int lastCellIndex) {
        printlog('animateSeedsEarning / lastCellIndex: $lastCellIndex');
    
        if (state.currentGame.isOpponentHouse(lastCellIndex)) {
          final int seedsCount = state.currentGame.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;
            refresh();
    
            // (recursively) check previous cells
            printlog('-> dispatch to previous cell');
            animateSeedsEarning(state.currentGame.getPreviousCellIndex(lastCellIndex));
          } else {
            printlog('-> nothing to do');
          }
        }
      }
    
      @override
      GameState? fromJson(Map<String, dynamic> json) {
        final Game currentGame = json['currentGame'] as Game;
    
        return GameState(
          currentGame: currentGame,
        );
      }
    
      @override
      Map<String, dynamic>? toJson(GameState state) {
        return <String, dynamic>{
          'currentGame': state.currentGame.toJson(),
        };
      }
    }