Select Git revision
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,
};
}
}