Mongodb test files

This commit is contained in:
Морозов Андрей 2022-09-24 21:49:59 +04:00
parent 4a5e2d7c84
commit 813658f934
4 changed files with 206 additions and 1 deletions

View File

@ -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/keyboard_keys.dart';
import 'package:guessing_words/core/data/enums/message_types.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/presentation/home/cubit/home_cubit.dart';
import 'package:guessing_words/core/data/mongodb.dart';
class DataSingleton { class DataSingleton {
static final DataSingleton _dataSingleton = DataSingleton._internal(); static final DataSingleton _dataSingleton = DataSingleton._internal();
@ -15,12 +16,14 @@ class DataSingleton {
List<String> gridData = [""]; List<String> gridData = [""];
Map<String, Color> coloredLetters = {}; Map<String, Color> coloredLetters = {};
int currentWordIndex = 0; int currentWordIndex = 0;
late DBConnection _mongo;
factory DataSingleton() { factory DataSingleton() {
return _dataSingleton; return _dataSingleton;
} }
DataSingleton._internal() { DataSingleton._internal() {
_mongo.startConnection();
if (secretWord.isEmpty) { if (secretWord.isEmpty) {
createWord(); createWord();
} }
@ -61,7 +64,8 @@ class DataSingleton {
.decode(utf8.encode("${gridData[currentWordIndex]}\r").toList()) .decode(utf8.encode("${gridData[currentWordIndex]}\r").toList())
.toString(); .toString();
if (input == secretWord) { if (input == secretWord) {
nextWord(); //nextWord();
_mongo.insertWord(secretWord, true);
return WinGameState(); return WinGameState();
} }
if (allWords.contains(secretWord)) { if (allWords.contains(secretWord)) {
@ -76,6 +80,7 @@ class DataSingleton {
MessageTypes.error, TextConstants.errorWrongWordLength); MessageTypes.error, TextConstants.errorWrongWordLength);
} }
} else { } else {
_mongo.insertWord(secretWord, false);
return LoseGameState(); return LoseGameState();
} }
} }

View File

@ -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<Db> 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<void> 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<String, dynamic> ret;
if (res == null) {
ret = await _collection.insert(<String, dynamic>{
'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
}
}

View File

@ -8,6 +8,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.9.0" version: "2.9.0"
basic_utils:
dependency: transitive
description:
name: basic_utils
url: "https://pub.dartlang.org"
source: hosted
version: "4.5.2"
bloc: bloc:
dependency: "direct main" dependency: "direct main"
description: description:
@ -22,6 +29,20 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.0" 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: characters:
dependency: transitive dependency: transitive
description: description:
@ -50,6 +71,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.16.0" version: "1.16.0"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.2"
crypto: crypto:
dependency: transitive dependency: transitive
description: description:
@ -64,6 +92,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.0.4" version: "1.0.4"
decimal:
dependency: transitive
description:
name: decimal
url: "https://pub.dartlang.org"
source: hosted
version: "2.3.0"
desktop_window: desktop_window:
dependency: "direct main" dependency: "direct main"
description: description:
@ -99,6 +134,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "6.1.4" version: "6.1.4"
fixnum:
dependency: transitive
description:
name: fixnum
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
flutter: flutter:
dependency: "direct main" dependency: "direct main"
description: flutter description: flutter
@ -184,6 +226,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.6.4" version: "0.6.4"
json_annotation:
dependency: transitive
description:
name: json_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "4.7.0"
lints: lints:
dependency: transitive dependency: transitive
description: description:
@ -191,6 +240,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.0" version: "2.0.0"
logging:
dependency: transitive
description:
name: logging
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
matcher: matcher:
dependency: transitive dependency: transitive
description: description:
@ -212,6 +268,20 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.8.0" 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: nested:
dependency: transitive dependency: transitive
description: description:
@ -289,6 +359,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.5" version: "2.0.5"
pedantic:
dependency: transitive
description:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.11.1"
petitparser: petitparser:
dependency: transitive dependency: transitive
description: description:
@ -310,6 +387,20 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.2" 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: process:
dependency: transitive dependency: transitive
description: description:
@ -324,6 +415,27 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "6.0.2" 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: shared_preferences:
dependency: "direct main" dependency: "direct main"
description: description:
@ -448,6 +560,20 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.3.0" 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: vector_math:
dependency: transitive dependency: transitive
description: description:
@ -455,6 +581,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.3" 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: win32:
dependency: transitive dependency: transitive
description: description:

View File

@ -23,6 +23,7 @@ dependencies:
fullscreen: ^1.0.3 fullscreen: ^1.0.3
file: ^6.1.4 file: ^6.1.4
bloc: ^8.1.0 bloc: ^8.1.0
mongo_dart: ^0.8.0-1.0.beta
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: