Added mongodb word counter
This commit is contained in:
parent
713b21a719
commit
5f1d770604
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Db> 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<void> 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<String, dynamic> ret;
|
||||
if (res == null) {
|
||||
ret = await _collection.insert(<String, dynamic>{
|
||||
await _collection.insert(<String, dynamic>{
|
||||
'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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue