-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: update word placement algo * feat: update board saving and logging * feat: save board to firestore * chore: add models package * feat: update board generation * feat: create and upload sections * docs: update readme * feat: improve readability * chore: update min coverage
- Loading branch information
1 parent
df89b89
commit 0dd3900
Showing
15 changed files
with
874 additions
and
339 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,7 @@ | |
.dart_tool/ | ||
.packages | ||
build/ | ||
pubspec.lock | ||
pubspec.lock | ||
|
||
allWords.json | ||
board.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// ignore_for_file: avoid_print | ||
|
||
import 'dart:io'; | ||
|
||
import 'package:board_generator/src/crossword_repository.dart'; | ||
import 'package:csv/csv.dart'; | ||
import 'package:dart_firebase_admin/dart_firebase_admin.dart'; | ||
import 'package:dart_firebase_admin/firestore.dart'; | ||
import 'package:game_domain/game_domain.dart'; | ||
|
||
void main(List<String> args) async { | ||
final serviceAccountPath = | ||
Platform.environment['GOOGLE_APPLICATION_CREDENTIALS']; | ||
if (serviceAccountPath == null) { | ||
throw Exception('Service account path not found'); | ||
} | ||
|
||
final admin = FirebaseAdminApp.initializeApp( | ||
'io-crossword-dev', | ||
Credential.fromServiceAccount(File(serviceAccountPath)), | ||
); | ||
final firestore = Firestore(admin); | ||
final crosswordRepository = CrosswordRepository(firestore: firestore); | ||
|
||
// Read the file | ||
final fileString = File('assets/board.txt').readAsStringSync(); | ||
final rows = const CsvToListConverter(eol: '\n').convert(fileString); | ||
|
||
// Convert to custom object | ||
final words = rows.map((row) { | ||
return Word( | ||
position: Point(row[0] as int, row[1] as int), | ||
answer: row[2] as String, | ||
clue: 'The answer is: ${row[2]}', | ||
hints: const [], | ||
visible: false, | ||
axis: row[3] == 'horizontal' ? Axis.horizontal : Axis.vertical, | ||
solvedTimestamp: null, | ||
); | ||
}).toList(); | ||
|
||
// Get crossword size | ||
final maxX = words | ||
.map((e) => e.position.x) | ||
.reduce((value, element) => value > element ? value : element); | ||
final maxY = words | ||
.map((e) => e.position.y) | ||
.reduce((value, element) => value > element ? value : element); | ||
final minX = words | ||
.map((e) => e.position.x) | ||
.reduce((value, element) => value < element ? value : element); | ||
final minY = words | ||
.map((e) => e.position.y) | ||
.reduce((value, element) => value < element ? value : element); | ||
|
||
final boardHeight = maxY - minY; | ||
final boardWidth = maxX - minX; | ||
|
||
print('Crossword size: $boardWidth x $boardHeight.'); | ||
|
||
final sections = <BoardSection>[]; | ||
const sectionSize = 300; | ||
|
||
var sectionX = minX; | ||
while (sectionX < maxX) { | ||
var sectionY = minY; | ||
while (sectionY < maxY) { | ||
final sectionWords = words.where((word) { | ||
return word.isStartInSection(sectionX, sectionY, sectionSize); | ||
}).toList(); | ||
|
||
final borderWords = words.where((word) { | ||
final isStartInSection = | ||
word.isStartInSection(sectionX, sectionY, sectionSize); | ||
final isEndInSection = | ||
word.isEndInSection(sectionX, sectionY, sectionSize); | ||
return !isStartInSection && isEndInSection; | ||
}).toList(); | ||
|
||
final section = BoardSection( | ||
id: '', | ||
position: Point(sectionX, sectionY), | ||
size: sectionSize, | ||
words: sectionWords, | ||
borderWords: borderWords, | ||
); | ||
sections.add(section); | ||
|
||
sectionY += sectionSize; | ||
} | ||
sectionX += sectionSize; | ||
} | ||
|
||
await crosswordRepository.addSections(sections); | ||
|
||
print('Added all ${sections.length} section to the database.'); | ||
} | ||
|
||
/// An extension on [Word] to check if it is in a section. | ||
extension SectionBelonging on Word { | ||
/// Returns true if the word starting letter is in the section. | ||
bool isStartInSection(int sectionX, int sectionY, int sectionSize) { | ||
return position.x >= sectionX && | ||
position.x < sectionX + sectionSize && | ||
position.y >= sectionY && | ||
position.y < sectionY + sectionSize; | ||
} | ||
|
||
/// Returns true if the word ending letter is in the section. | ||
bool isEndInSection(int sectionX, int sectionY, int sectionSize) { | ||
final (endX, endY) = axis == Axis.horizontal | ||
? (position.x + answer.length - 1, position.y) | ||
: (position.x, position.y + answer.length - 1); | ||
return endX >= sectionX && | ||
endX < sectionX + sectionSize && | ||
endY >= sectionY && | ||
endY < sectionY + sectionSize; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:board_generator/src/board_generator.dart'; | ||
|
||
void main(List<String> args) async { | ||
final file = File('assets/allWords.json'); | ||
|
||
final string = await file.readAsString(); | ||
final map = jsonDecode(string) as Map<String, dynamic>; | ||
final words = | ||
(map['words'] as List<dynamic>).map((e) => e as Map<String, dynamic>); | ||
final parsedWords = words.map((word) { | ||
return word['answer'] as String; | ||
}).toList(); | ||
|
||
final regex = RegExp(r'^[a-z]+$'); | ||
final filteredWords = [...parsedWords]..removeWhere( | ||
(element) => !regex.hasMatch(element), | ||
); | ||
|
||
generateCrossword(filteredWords, 'assets/board.txt'); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.