From 5f1d7706047aa8fbf8006adf0cc5b0dae0506401 Mon Sep 17 00:00:00 2001 From: lnd212 Date: Mon, 26 Sep 2022 21:13:01 +0400 Subject: [PATCH] Added mongodb word counter --- app/lib/core/data/data_singleton.dart | 3 +-- app/lib/core/data/mongodb.dart | 26 ++++++++++++------- .../home/widget/keyboard_key.dart | 8 +++++- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/app/lib/core/data/data_singleton.dart b/app/lib/core/data/data_singleton.dart index 450f19e..92a1f32 100644 --- a/app/lib/core/data/data_singleton.dart +++ b/app/lib/core/data/data_singleton.dart @@ -68,7 +68,7 @@ class DataSingleton { _mongo.insertWord(secretWord, true); return WinGameState(); } - if (allWords.contains(secretWord)) { + if (allWords.contains(input)) { nextWord(); return GridUpdateState(); } else { @@ -81,7 +81,6 @@ class DataSingleton { } } else { _mongo.insertWord(secretWord, false); - _mongo.loseConnection(); return LoseGameState(); } } diff --git a/app/lib/core/data/mongodb.dart b/app/lib/core/data/mongodb.dart index 90cf4b4..5658643 100644 --- a/app/lib/core/data/mongodb.dart +++ b/app/lib/core/data/mongodb.dart @@ -12,7 +12,7 @@ class Word { class DBConnection { static final DBConnection _instance = DBConnection(); - final String _host = "192.168.1.71"; + final String _host = "ac-atedki8-shard-00-01.8ustfpd.mongodb.net"; final String _port = "27017"; final String _dbName = "words_data"; final String _collectionName = "words"; @@ -21,8 +21,9 @@ class DBConnection { startConnection(); } - late Db _db; - late final DbCollection _collection = _db.collection(_collectionName); + late Db + _db; //= Db.create("mongodb+srv://lnd:b1gexlv4ESFpeLbQ@ac-atedki8-shard-00-01.8ustfpd.mongodb.net:27017/words_data") as Db; + late DbCollection _collection; static DBConnection getInstance() => _instance; Db getConnection() => _db; @@ -30,18 +31,23 @@ class DBConnection { Future startConnection() async { try { - _db = Db(_getConnectionString()); + _db = await Db.create(_getConnectionString()); await _db.open(); + _collection = _db.collection(_collectionName); } catch (e) { + _db.close(); print(e); } return _db; } - String _getConnectionString() => "mongodb://$_host:$_port/$_dbName"; + String _getConnectionString() => + "mongodb+srv://testuser:DyYDrSFhQg0D8Chd@cluster0.8ustfpd.mongodb.net/$_dbName?retryWrites=true&w=majority"; void loseConnection() => _db.close(); Future insertWord(String word, bool condition) async { + await _db.open(); + _collection = _db.collection(_collectionName); int win, lose; if (condition) { win = 1; @@ -52,19 +58,19 @@ class DBConnection { } var res = await _collection.findOne(where.eq('word', word)); - Map ret; if (res == null) { - ret = await _collection.insert({ + await _collection.insert({ 'id': const Uuid().v4obj(), 'word': word, 'win': win, 'lose': lose }); } else { - ret = await _collection.update( + await _collection.update( where.eq('word', word), modify.set('win', res['win'] + win)); - ret = await _collection.update( + await _collection.update( where.eq('word', word), modify.set('lose', res['lose'] + lose)); - } // Tom - active - 025456da-9e39-4e7c-b1f7-0f5a5e1cb212 + } + await _db.close(); } } diff --git a/app/lib/core/presentation/home/widget/keyboard_key.dart b/app/lib/core/presentation/home/widget/keyboard_key.dart index 9ded8e6..d5f3191 100644 --- a/app/lib/core/presentation/home/widget/keyboard_key.dart +++ b/app/lib/core/presentation/home/widget/keyboard_key.dart @@ -69,7 +69,13 @@ class EnterKeyboardKey extends StatelessWidget { child: InkWell( onTap: () { if (homeCubit.submitWord()) { - data.gridData[data.currentWordIndex - 1] + int ind; + if (data.currentWordIndex == 0) { + ind = 1; + } else { + ind = data.currentWordIndex; + } + data.gridData[ind-1] .split("") .asMap() .map((index, e) {