diff --git a/api/packages/crossword_repository/lib/src/crossword_repository.dart b/api/packages/crossword_repository/lib/src/crossword_repository.dart index 12a10a0d9..f3bda5939 100644 --- a/api/packages/crossword_repository/lib/src/crossword_repository.dart +++ b/api/packages/crossword_repository/lib/src/crossword_repository.dart @@ -14,9 +14,11 @@ class CrosswordRepository { final DbClient _dbClient; + static const _sectionsCollection = 'boardChunks'; + /// Fetches all sections from the board. Future> listAllSections() async { - final sections = await _dbClient.listAll('boardSections'); + final sections = await _dbClient.listAll(_sectionsCollection); return sections.map((sectionDoc) { return BoardSection.fromJson({ @@ -29,7 +31,7 @@ class CrosswordRepository { /// Fetches a section by its position. Future findSectionByPosition(int x, int y) async { final result = await _dbClient.find( - 'boardSections', + _sectionsCollection, { 'position.x': x, 'position.y': y, @@ -49,7 +51,7 @@ class CrosswordRepository { /// Updates a section. Future updateSection(BoardSection section) async { await _dbClient.update( - 'boardSections', + _sectionsCollection, DbEntityRecord( id: section.id, data: section.toJson()..remove('id'), diff --git a/api/packages/crossword_repository/test/src/crossword_repository_test.dart b/api/packages/crossword_repository/test/src/crossword_repository_test.dart index 66c4b662b..63948d3cb 100644 --- a/api/packages/crossword_repository/test/src/crossword_repository_test.dart +++ b/api/packages/crossword_repository/test/src/crossword_repository_test.dart @@ -14,6 +14,8 @@ void main() { group('CrosswordRepository', () { late DbClient dbClient; + const sectionsCollection = 'boardChunks'; + setUpAll(() { registerFallbackValue(_MockDbEntityRecord()); }); @@ -37,7 +39,7 @@ void main() { 'borderWords': const [], }, ); - when(() => dbClient.listAll('boardSections')) + when(() => dbClient.listAll(sectionsCollection)) .thenAnswer((_) async => [record]); final repository = CrosswordRepository(dbClient: dbClient); final sections = await repository.listAllSections(); @@ -65,7 +67,7 @@ void main() { ); when( () => dbClient.find( - 'boardSections', + sectionsCollection, { 'position.x': 1, 'position.y': 1, @@ -90,7 +92,7 @@ void main() { test('findSectionByPosition returns null when empty', () async { when( () => dbClient.find( - 'boardSections', + sectionsCollection, { 'position.x': 1, 'position.y': 1, @@ -106,7 +108,7 @@ void main() { test('updateSection updates the section in the db', () async { when( () => dbClient.update( - 'boardSections', + sectionsCollection, any(that: isA()), ), ).thenAnswer((_) async {}); @@ -121,7 +123,7 @@ void main() { await repository.updateSection(section); final captured = verify( () => dbClient.update( - 'boardSections', + sectionsCollection, captureAny(), ), ).captured.single as DbEntityRecord; @@ -168,7 +170,7 @@ void main() { ); when( () => dbClient.find( - 'boardSections', + sectionsCollection, { 'position.x': 1, 'position.y': 1, @@ -177,7 +179,7 @@ void main() { ).thenAnswer((_) async => [record]); when( () => dbClient.update( - 'boardSections', + sectionsCollection, any(that: isA()), ), ).thenAnswer((_) async {}); @@ -193,7 +195,7 @@ void main() { expect(valid, isTrue); final captured = verify( () => dbClient.update( - 'boardSections', + sectionsCollection, captureAny(), ), ).captured.single as DbEntityRecord; @@ -228,7 +230,7 @@ void main() { test('answerWord returns false if section does not exist', () async { when( () => dbClient.find( - 'boardSections', + sectionsCollection, { 'position.x': 0, 'position.y': 0, diff --git a/packages/board_generator/lib/create_sections.dart b/packages/board_generator/lib/create_sections.dart index 166282dd7..f7012eaa5 100644 --- a/packages/board_generator/lib/create_sections.dart +++ b/packages/board_generator/lib/create_sections.dart @@ -118,9 +118,11 @@ void main(List args) async { } } + print('Uploading answers...'); await crosswordRepository.addAnswers(answers); print('Added all answers to the database.'); + print('Uploading sections...'); await crosswordRepository.addSections(sections); print('Added all ${sections.length} section to the database.'); } diff --git a/packages/board_generator/lib/src/crossword_repository.dart b/packages/board_generator/lib/src/crossword_repository.dart index dc5e9198f..28372fbe5 100644 --- a/packages/board_generator/lib/src/crossword_repository.dart +++ b/packages/board_generator/lib/src/crossword_repository.dart @@ -1,3 +1,4 @@ +import 'package:collection/collection.dart'; import 'package:dart_firebase_admin/firestore.dart'; import 'package:game_domain/game_domain.dart'; @@ -11,18 +12,29 @@ class CrosswordRepository { /// The firestore instance. final Firestore firestore; - /// Adds a list of word answers to the database. + /// Adds a map of word id: answer to the database. Future addAnswers(Map answers) async { final answersCollection = firestore.collection('answers'); - for (final answer in answers.entries) { - await answersCollection.doc(answer.key).set({'answer': answer.value}); + + // Firestore has a limit that prevents having a document with more than + // 20000 answers. + const size = 20000; + for (final subset in answers.entries.slices(size)) { + final map = subset.fold>( + {}, + (previousValue, element) { + previousValue[element.key] = element.value; + return previousValue; + }, + ); + await answersCollection.add(map); } } /// Adds a list of sections to the database. Future addSections(List sections) async { for (final section in sections) { - await firestore.collection('newBoardSections').add(section.toJson()); + await firestore.collection('boardChunks').add(section.toJson()); } } diff --git a/packages/crossword_repository/lib/src/crossword_repository.dart b/packages/crossword_repository/lib/src/crossword_repository.dart index f4fe67a66..3f96cf3b3 100644 --- a/packages/crossword_repository/lib/src/crossword_repository.dart +++ b/packages/crossword_repository/lib/src/crossword_repository.dart @@ -9,7 +9,7 @@ class CrosswordRepository { CrosswordRepository({ required this.db, }) { - sectionCollection = db.collection('boardSections'); + sectionCollection = db.collection('boardChunks'); } /// The [FirebaseFirestore] instance. diff --git a/packages/crossword_repository/test/src/crossword_repository_test.dart b/packages/crossword_repository/test/src/crossword_repository_test.dart index 6e2789873..1f9d2c3af 100644 --- a/packages/crossword_repository/test/src/crossword_repository_test.dart +++ b/packages/crossword_repository/test/src/crossword_repository_test.dart @@ -25,7 +25,7 @@ void main() { ], borderWords: const [], ); - const sectionsCollection = 'boardSections'; + const sectionsCollection = 'boardChunks'; late FirebaseFirestore firebaseFirestore; late CrosswordRepository crosswordRepository; diff --git a/pubspec.yaml b/pubspec.yaml index e1d599d16..f98e58cf7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -61,13 +61,13 @@ dev_dependencies: very_good_analysis: ^5.1.0 dependency_overrides: + # Adds support for wasm while flame_audio does not update this dependency + audioplayers: 6.0.0 flame: git: url: https://github.com/flame-engine/flame.git ref: main path: packages/flame - # Adds support for wasm while flame_audio does not update this dependency - audioplayers: 6.0.0 flutter: uses-material-design: true