diff --git a/app/lib/core/data/data_singleton.dart b/app/lib/core/data/data_singleton.dart index 081541a..df88323 100644 --- a/app/lib/core/data/data_singleton.dart +++ b/app/lib/core/data/data_singleton.dart @@ -7,6 +7,7 @@ import 'package:guessing_words/core/const/text_constants.dart'; import 'package:guessing_words/core/data/enums/keyboard_keys.dart'; import 'package:guessing_words/core/data/enums/message_types.dart'; import 'package:guessing_words/core/presentation/home/cubit/home_cubit.dart'; +import 'package:guessing_words/core/data/mongodb.dart'; class DataSingleton { static final DataSingleton _dataSingleton = DataSingleton._internal(); @@ -15,12 +16,14 @@ class DataSingleton { List gridData = [""]; Map coloredLetters = {}; int currentWordIndex = 0; + late DBConnection _mongo; factory DataSingleton() { return _dataSingleton; } DataSingleton._internal() { + _mongo.startConnection(); if (secretWord.isEmpty) { createWord(); } @@ -61,7 +64,8 @@ class DataSingleton { .decode(utf8.encode("${gridData[currentWordIndex]}\r").toList()) .toString(); if (input == secretWord) { - nextWord(); + //nextWord(); + _mongo.insertWord(secretWord, true); return WinGameState(); } if (allWords.contains(secretWord)) { @@ -76,6 +80,7 @@ class DataSingleton { MessageTypes.error, TextConstants.errorWrongWordLength); } } else { + _mongo.insertWord(secretWord, false); return LoseGameState(); } } diff --git a/app/lib/core/data/mongodb.dart b/app/lib/core/data/mongodb.dart new file mode 100644 index 0000000..c44f06e --- /dev/null +++ b/app/lib/core/data/mongodb.dart @@ -0,0 +1,66 @@ +import 'package:mongo_dart/mongo_dart.dart'; + +class Word { + final ObjectId id; + final String word; + final int win; + final int lose; + + const Word(this.id, this.word, this.win, this.lose); +} + +class DBConnection { + static final DBConnection _instance = DBConnection(); + + final String _host = "DATABASE SERVER"; + final String _port = "DATABASE PORT"; + final String _dbName = "DATABASE NAME"; + final String _collectionName = "WordsTable"; + + late Db _db; + late final DbCollection _collection = _db.collection(_collectionName); + + static DBConnection getInstance() => _instance; + Db getConnection() => _db; + DbCollection getCollection() => _collection; + + Future startConnection() async { + try { + _db = Db(_getConnectionString()); + await _db.open(); + } catch (e) { + print(e); + } + return _db; + } + + String _getConnectionString() => "mongodb://$_host:$_port/$_dbName"; + void loseConnection() => _db.close(); + + Future insertWord(String word, bool condition) async { + int win, lose; + if (condition) { + win = 1; + lose = 0; + } else { + win = 0; + lose = 1; + } + + var res = await _collection.findOne(where.eq('word', word)); + Map ret; + if (res == null) { + ret = await _collection.insert({ + 'id': const Uuid().v4obj(), + 'word': word, + 'win': win, + 'lose': lose + }); + } else { + ret = await _collection.update( + where.eq('word', word), modify.set('win', res['win'] + win)); + ret = await _collection.update( + where.eq('word', word), modify.set('lose', res['lose'] + lose)); + } // Tom - active - 025456da-9e39-4e7c-b1f7-0f5a5e1cb212 + } +} diff --git a/app/pubspec.lock b/app/pubspec.lock index 0192b94..eb6d02e 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -8,6 +8,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.9.0" + basic_utils: + dependency: transitive + description: + name: basic_utils + url: "https://pub.dartlang.org" + source: hosted + version: "4.5.2" bloc: dependency: "direct main" description: @@ -22,6 +29,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.0" + bson: + dependency: transitive + description: + name: bson + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" + buffer: + dependency: transitive + description: + name: buffer + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.1" characters: dependency: transitive description: @@ -50,6 +71,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.16.0" + convert: + dependency: transitive + description: + name: convert + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.2" crypto: dependency: transitive description: @@ -64,6 +92,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.4" + decimal: + dependency: transitive + description: + name: decimal + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.0" desktop_window: dependency: "direct main" description: @@ -99,6 +134,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "6.1.4" + fixnum: + dependency: transitive + description: + name: fixnum + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1" flutter: dependency: "direct main" description: flutter @@ -184,6 +226,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.6.4" + json_annotation: + dependency: transitive + description: + name: json_annotation + url: "https://pub.dartlang.org" + source: hosted + version: "4.7.0" lints: dependency: transitive description: @@ -191,6 +240,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.0" + logging: + dependency: transitive + description: + name: logging + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.2" matcher: dependency: transitive description: @@ -212,6 +268,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.8.0" + mongo_dart: + dependency: "direct main" + description: + name: mongo_dart + url: "https://pub.dartlang.org" + source: hosted + version: "0.8.0-1.0.beta" + mongo_dart_query: + dependency: transitive + description: + name: mongo_dart_query + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" nested: dependency: transitive description: @@ -289,6 +359,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.5" + pedantic: + dependency: transitive + description: + name: pedantic + url: "https://pub.dartlang.org" + source: hosted + version: "1.11.1" petitparser: dependency: transitive description: @@ -310,6 +387,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.2" + pointycastle: + dependency: transitive + description: + name: pointycastle + url: "https://pub.dartlang.org" + source: hosted + version: "3.6.2" + pool: + dependency: transitive + description: + name: pool + url: "https://pub.dartlang.org" + source: hosted + version: "1.5.1" process: dependency: transitive description: @@ -324,6 +415,27 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "6.0.2" + rational: + dependency: transitive + description: + name: rational + url: "https://pub.dartlang.org" + source: hosted + version: "2.2.0" + sasl_scram: + dependency: transitive + description: + name: sasl_scram + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.1" + saslprep: + dependency: transitive + description: + name: saslprep + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.2" shared_preferences: dependency: "direct main" description: @@ -448,6 +560,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.3.0" + unorm_dart: + dependency: transitive + description: + name: unorm_dart + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.0" + uuid: + dependency: transitive + description: + name: uuid + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.6" vector_math: dependency: transitive description: @@ -455,6 +581,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.3" + vy_string_utils: + dependency: transitive + description: + name: vy_string_utils + url: "https://pub.dartlang.org" + source: hosted + version: "0.4.4" win32: dependency: transitive description: diff --git a/app/pubspec.yaml b/app/pubspec.yaml index 637e65d..3149abb 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -23,6 +23,7 @@ dependencies: fullscreen: ^1.0.3 file: ^6.1.4 bloc: ^8.1.0 + mongo_dart: ^0.8.0-1.0.beta dev_dependencies: flutter_test: