Skip to content
Snippets Groups Projects
Select Git revision
  • 5533743f80386973bc123b5b9c968ad2703571bf
  • master default protected
  • 13-improve-app-metadata
  • 2-display-midi-input
  • Release_0.8.2_27 protected
  • Release_0.8.1_26 protected
  • Release_0.8.0_25 protected
  • Release_0.7.2_24 protected
  • Release_0.7.1_23 protected
  • Release_0.7.0_22 protected
  • Release_0.6.0_21 protected
  • Release_0.5.0_20 protected
  • Release_0.4.0_19 protected
  • Release_0.3.2_18 protected
  • Release_0.3.1_17 protected
  • Release_0.3.0_16 protected
  • Release_0.2.1_15 protected
  • Release_0.2.0_14 protected
  • Release_0.1.1_13 protected
  • Release_0.1.0_12 protected
  • Release_0.0.11_11 protected
  • Release_0.0.10_10 protected
  • Release_0.0.9_9 protected
  • Release_0.0.8_8 protected
24 results

about.dart

Blame
  • game.dart 5.41 KiB
    import 'package:wordguessing/config/default_game_settings.dart';
    import 'package:wordguessing/data/fetch_data_helper.dart';
    import 'package:wordguessing/models/data/word.dart';
    import 'package:wordguessing/models/settings/settings_game.dart';
    import 'package:wordguessing/models/settings/settings_global.dart';
    import 'package:wordguessing/utils/tools.dart';
    
    class Game {
      Game({
        // Settings
        required this.gameSettings,
        required this.globalSettings,
    
        // State
        this.isRunning = false,
        this.isStarted = false,
        this.isFinished = false,
        this.animationInProgress = false,
    
        // Base data
        required this.word,
        required this.otherWords,
        required this.images,
    
        // Game data
        this.recentWordsKeys = const [],
        this.questionsCount = 0,
        this.goodAnswers = 0,
        this.wrongAnswers = 0,
      });
    
      // Settings
      final GameSettings gameSettings;
      final GlobalSettings globalSettings;
    
      // State
      bool isRunning;
      bool isStarted;
      bool isFinished;
      bool animationInProgress;
    
      // Base data
      Word word;
      List<Word> otherWords;
      List<Word> images;
    
      // Game data
      List<String> recentWordsKeys;
      int questionsCount;
      int goodAnswers;
      int wrongAnswers;
    
      factory Game.createEmpty() {
        return Game(
          // Settings
          gameSettings: GameSettings.createDefault(),
          globalSettings: GlobalSettings.createDefault(),
          // Base data
          word: Word.createEmpty(),
          otherWords: [],
          images: [],
          // Game data
          recentWordsKeys: [],
        );
      }
    
      factory Game.createNew({
        GameSettings? gameSettings,
        GlobalSettings? globalSettings,
      }) {
        final GameSettings newGameSettings = gameSettings ?? GameSettings.createDefault();
        final GlobalSettings newGlobalSettings = globalSettings ?? GlobalSettings.createDefault();
    
        return Game(
          // Settings
          gameSettings: newGameSettings,
          globalSettings: newGlobalSettings,
          // State
          isRunning: true,
          // Base data
          word: Word.createEmpty(),
          otherWords: [],
          images: [],
          // Game data
          recentWordsKeys: [],
        );
      }
    
      bool get canBeResumed => isStarted && !isFinished;
    
      bool get gameWon => isRunning && isStarted && isFinished;
    
      void updateWord(Word newWord) {
        word = newWord;
        if (newWord.key != '') {
          recentWordsKeys.insert(0, newWord.key);
          recentWordsKeys = recentWordsKeys.take(DefaultGameSettings.recentWordsCount).toList();
        }
      }
    
      void pickNewWord() {
        switch (gameSettings.gameType) {
          case DefaultGameSettings.gameTypeValuePickImage:
            pickDataForGamePickImage();
            break;
          case DefaultGameSettings.gameTypeValuePickWord:
            pickDataForGamePickWord();
            break;
          default:
        }
    
        questionsCount++;
      }
    
      bool isRecentlyPicked(String key) {
        return recentWordsKeys.contains(key);
      }
    
      void pickDataForGamePickImage() {
        Word word;
    
        int attempts = 0;
        do {
          final List<Word> words = FetchDataHelper().getWords(
            lang: gameSettings.lang,
            count: DefaultGameSettings.itemsCount,
          );
    
          word = words.take(1).toList()[0];
    
          if ((words.length == DefaultGameSettings.itemsCount) && !isRecentlyPicked(word.key)) {
            updateWord(word);
    
            final List<Word> pickedImages = [];
            for (var element in words) {
              pickedImages.add(element);
            }
    
            images = pickedImages;
          }
    
          attempts++;
        } while (word != word && attempts < 10);
      }
    
      void pickDataForGamePickWord() {
        Word word;
    
        int attempts = 0;
        do {
          final List<Word> words = FetchDataHelper().getWords(
            lang: gameSettings.lang,
            count: DefaultGameSettings.itemsCount,
          );
    
          word = words.take(1).toList()[0];
    
          if ((words.length >= DefaultGameSettings.itemsCount) && !isRecentlyPicked(word.key)) {
            updateWord(word);
            otherWords = words.skip(1).toList();
            images = [word];
          }
    
          attempts++;
        } while (word != word && attempts < 10);
      }
    
      void dump() {
        printlog('');
        printlog('## Current game dump:');
        printlog('');
        printlog('$Game:');
        printlog('  Settings');
        gameSettings.dump();
        globalSettings.dump();
        printlog('  State');
        printlog('    isRunning: $isRunning');
        printlog('    isStarted: $isStarted');
        printlog('    isFinished: $isFinished');
        printlog('    animationInProgress: $animationInProgress');
        printlog('  Base data');
        printlog('    word: $word');
        printlog('    otherWords: $otherWords');
        printlog('    images: $images');
        printlog('  Game data');
        printlog('    recentWordsKeys: $recentWordsKeys');
        printlog('    questionsCount: $questionsCount');
        printlog('    goodAnswers: $goodAnswers');
        printlog('    wrongAnswers: $wrongAnswers');
        printlog('');
      }
    
      @override
      String toString() {
        return '$Game(${toJson()})';
      }
    
      Map<String, dynamic>? toJson() {
        return <String, dynamic>{
          // Settings
          'gameSettings': gameSettings.toJson(),
          'globalSettings': globalSettings.toJson(),
          // State
          'isRunning': isRunning,
          'isStarted': isStarted,
          'isFinished': isFinished,
          'animationInProgress': animationInProgress,
          // Base data
          'word': word.toJson(),
          'otherWords': otherWords,
          'images': images,
          // Game data
          'recentWordsKeys': recentWordsKeys,
          'questionsCount': questionsCount,
          'goodAnswers': goodAnswers,
          'wrongAnswers': wrongAnswers,
        };
      }
    }