Added mongodb word counter

This commit is contained in:
Морозов Андрей 2022-09-26 21:13:01 +04:00
parent 713b21a719
commit 5f1d770604
3 changed files with 24 additions and 13 deletions

View File

@ -68,7 +68,7 @@ class DataSingleton {
_mongo.insertWord(secretWord, true); _mongo.insertWord(secretWord, true);
return WinGameState(); return WinGameState();
} }
if (allWords.contains(secretWord)) { if (allWords.contains(input)) {
nextWord(); nextWord();
return GridUpdateState(); return GridUpdateState();
} else { } else {
@ -81,7 +81,6 @@ class DataSingleton {
} }
} else { } else {
_mongo.insertWord(secretWord, false); _mongo.insertWord(secretWord, false);
_mongo.loseConnection();
return LoseGameState(); return LoseGameState();
} }
} }

View File

@ -12,7 +12,7 @@ class Word {
class DBConnection { class DBConnection {
static final DBConnection _instance = 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 _port = "27017";
final String _dbName = "words_data"; final String _dbName = "words_data";
final String _collectionName = "words"; final String _collectionName = "words";
@ -21,8 +21,9 @@ class DBConnection {
startConnection(); startConnection();
} }
late Db _db; late Db
late final DbCollection _collection = _db.collection(_collectionName); _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; static DBConnection getInstance() => _instance;
Db getConnection() => _db; Db getConnection() => _db;
@ -30,18 +31,23 @@ class DBConnection {
Future<Db> startConnection() async { Future<Db> startConnection() async {
try { try {
_db = Db(_getConnectionString()); _db = await Db.create(_getConnectionString());
await _db.open(); await _db.open();
_collection = _db.collection(_collectionName);
} catch (e) { } catch (e) {
_db.close();
print(e); print(e);
} }
return _db; 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(); void loseConnection() => _db.close();
Future<void> insertWord(String word, bool condition) async { Future<void> insertWord(String word, bool condition) async {
await _db.open();
_collection = _db.collection(_collectionName);
int win, lose; int win, lose;
if (condition) { if (condition) {
win = 1; win = 1;
@ -52,19 +58,19 @@ class DBConnection {
} }
var res = await _collection.findOne(where.eq('word', word)); var res = await _collection.findOne(where.eq('word', word));
Map<String, dynamic> ret;
if (res == null) { if (res == null) {
ret = await _collection.insert(<String, dynamic>{ await _collection.insert(<String, dynamic>{
'id': const Uuid().v4obj(), 'id': const Uuid().v4obj(),
'word': word, 'word': word,
'win': win, 'win': win,
'lose': lose 'lose': lose
}); });
} else { } else {
ret = await _collection.update( await _collection.update(
where.eq('word', word), modify.set('win', res['win'] + win)); 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)); where.eq('word', word), modify.set('lose', res['lose'] + lose));
} // Tom - active - 025456da-9e39-4e7c-b1f7-0f5a5e1cb212 }
await _db.close();
} }
} }

View File

@ -69,7 +69,13 @@ class EnterKeyboardKey extends StatelessWidget {
child: InkWell( child: InkWell(
onTap: () { onTap: () {
if (homeCubit.submitWord()) { 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("") .split("")
.asMap() .asMap()
.map((index, e) { .map((index, e) {