Skip to content

Commit

Permalink
feat: update models (#20)
Browse files Browse the repository at this point in the history
* feat: update word model

* feat: update board section model

* test: updated models

* feat: update bloc with new models

* feat: update crossword with new models

* test: fix serialization tests

* fix: word id
  • Loading branch information
jsgalarraga authored Mar 4, 2024
1 parent fafc849 commit b90f444
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 178 deletions.
24 changes: 13 additions & 11 deletions api/packages/game_domain/lib/src/models/board_section.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,37 @@ class BoardSection extends Equatable {
const BoardSection({
required this.id,
required this.position,
required this.width,
required this.height,
required this.size,
required this.words,
required this.borderWords,
});

/// {@macro board_section}
factory BoardSection.fromJson(Map<String, dynamic> json) =>
_$BoardSectionFromJson(json);

/// Unique identifier of board section.
@JsonKey()
///
/// Intentionally left out of serialization to avoid redundancy.
@JsonKey(includeToJson: false)
final String id;

/// Position of the board section in the board. The origin is the top left.
@JsonKey()
@PointConverter()
final Point<int> position;

/// Width of the board section.
/// Size of the squared board section.
@JsonKey()
final int width;
final int size;

/// Height of the board section.
/// The words that start in this board section.
@JsonKey()
final int height;
final List<Word> words;

/// The words that are contained in this board section.
/// The words that end in this board section, but don't start in it.
@JsonKey()
final List<Word> words;
final List<Word> borderWords;

/// Returns a json representation from this instance.
Map<String, dynamic> toJson() => _$BoardSectionToJson(this);
Expand All @@ -51,8 +53,8 @@ class BoardSection extends Equatable {
List<Object?> get props => [
id,
position,
width,
height,
size,
words,
borderWords,
];
}
11 changes: 6 additions & 5 deletions api/packages/game_domain/lib/src/models/board_section.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions api/packages/game_domain/lib/src/models/word.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ part 'word.g.dart';
class Word extends Equatable {
/// {@macro word}
const Word({
required this.id,
required this.position,
required this.axis,
required this.answer,
required this.clue,
required this.hints,
required this.visible,
required this.solvedTimestamp,
});
}) : id = '$position-$axis';

/// {@macro word}
factory Word.fromJson(Map<String, dynamic> json) => _$WordFromJson(json);

/// Unique identifier of the word.
@JsonKey()
/// Unique identifier of the word determined by its position and axis.
///
/// Intentionally left out of serialization to avoid redundancy.
@JsonKey(includeToJson: false)
final String id;

/// Position of the board section in the board. The origin is the top left.
Expand Down
2 changes: 0 additions & 2 deletions api/packages/game_domain/lib/src/models/word.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 12 additions & 16 deletions api/packages/game_domain/test/src/models/board_section_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ void main() {
final boardSection = BoardSection(
id: 'id',
position: Point(1, 2),
width: 3,
height: 4,
size: 200,
words: [
Word(
id: 'id',
position: Point(1, 2),
axis: Axis.horizontal,
answer: 'answer',
Expand All @@ -24,19 +22,17 @@ void main() {
solvedTimestamp: 1234,
),
],
borderWords: [],
);
final json = boardSection.toJson();

expect(
json,
equals({
'id': 'id',
'position': {'x': 1, 'y': 2},
'width': 3,
'height': 4,
'size': 200,
'words': [
{
'id': 'id',
'position': {'x': 1, 'y': 2},
'axis': 'horizontal',
'answer': 'answer',
Expand All @@ -46,6 +42,7 @@ void main() {
'solvedTimestamp': 1234,
},
],
'borderWords': <Map<String, dynamic>>[],
}),
);
});
Expand All @@ -54,8 +51,7 @@ void main() {
final json = {
'id': 'id',
'position': {'x': 1, 'y': 2},
'width': 3,
'height': 4,
'size': 200,
'words': [
{
'id': 'id',
Expand All @@ -68,6 +64,7 @@ void main() {
'solvedTimestamp': 1234,
},
],
'borderWords': <Map<String, dynamic>>[],
};
final boardSection = BoardSection.fromJson(json);
expect(
Expand All @@ -76,11 +73,9 @@ void main() {
BoardSection(
id: 'id',
position: Point(1, 2),
width: 3,
height: 4,
size: 200,
words: [
Word(
id: 'id',
position: Point(1, 2),
axis: Axis.horizontal,
answer: 'answer',
Expand All @@ -90,6 +85,7 @@ void main() {
solvedTimestamp: 1234,
),
],
borderWords: [],
),
),
);
Expand All @@ -99,16 +95,16 @@ void main() {
final firstBoardSection = BoardSection(
id: 'id',
position: Point(1, 2),
width: 3,
height: 4,
size: 300,
words: [],
borderWords: [],
);
final secondBoardSection = BoardSection(
id: 'id',
position: Point(1, 2),
width: 3,
height: 4,
size: 300,
words: [],
borderWords: [],
);

expect(firstBoardSection, equals(secondBoardSection));
Expand Down
5 changes: 0 additions & 5 deletions api/packages/game_domain/test/src/models/word_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ void main() {
group('Word', () {
test('creates correct json object from Word object', () {
final word = Word(
id: 'id',
position: Point(1, 2),
axis: Axis.horizontal,
answer: 'test',
Expand All @@ -21,7 +20,6 @@ void main() {
expect(
json,
equals({
'id': 'id',
'position': {'x': 1, 'y': 2},
'axis': 'horizontal',
'answer': 'test',
Expand All @@ -48,7 +46,6 @@ void main() {
word,
equals(
Word(
id: 'id',
position: Point(1, 2),
axis: Axis.horizontal,
answer: 'test',
Expand All @@ -63,7 +60,6 @@ void main() {

test('supports equality', () {
final firstWord = Word(
id: 'id',
position: Point(1, 2),
axis: Axis.horizontal,
answer: 'test',
Expand All @@ -73,7 +69,6 @@ void main() {
solvedTimestamp: 0,
);
final secondWord = Word(
id: 'id',
position: Point(1, 2),
axis: Axis.horizontal,
answer: 'test',
Expand Down
39 changes: 17 additions & 22 deletions lib/crossword/bloc/crossword_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,22 @@ class CrosswordBloc extends Bloc<CrosswordEvent, CrosswordState> {
InitialBoardLoadRequested event,
Emitter<CrosswordState> emit,
) async {
const section = BoardSection(
final section = BoardSection(
id: '1',
position: Point(2, 2),
width: 40,
height: 40,
position: const Point(2, 2),
size: 40,
words: [
Word(
id: '1',
axis: Axis.horizontal,
position: Point(0, 0),
position: const Point(0, 0),
answer: 'flutter',
clue: 'flutter',
hints: ['dart', 'mobile', 'cross-platform'],
hints: const ['dart', 'mobile', 'cross-platform'],
visible: true,
solvedTimestamp: null,
),
],
borderWords: const [],
);

emit(
Expand All @@ -55,50 +54,46 @@ class CrosswordBloc extends Bloc<CrosswordEvent, CrosswordState> {
final section = BoardSection(
id: '',
position: Point(event.position.$1, event.position.$2),
width: 40,
height: 40,
words: const [
size: 40,
words: [
Word(
id: '',
axis: Axis.horizontal,
position: Point(0, 0),
position: const Point(0, 0),
answer: 'flutter',
clue: 'flutter',
hints: ['dart', 'mobile', 'cross-platform'],
hints: const ['dart', 'mobile', 'cross-platform'],
visible: true,
solvedTimestamp: null,
),
Word(
id: '',
axis: Axis.vertical,
position: Point(4, 1),
position: const Point(4, 1),
answer: 'android',
clue: 'flutter',
hints: ['dart', 'mobile', 'cross-platform'],
hints: const ['dart', 'mobile', 'cross-platform'],
visible: true,
solvedTimestamp: null,
),
Word(
id: '',
axis: Axis.vertical,
position: Point(8, 3),
position: const Point(8, 3),
answer: 'dino',
clue: 'flutter',
hints: ['dart', 'mobile', 'cross-platform'],
hints: const ['dart', 'mobile', 'cross-platform'],
visible: true,
solvedTimestamp: null,
),
Word(
id: '',
position: Point(4, 6),
position: const Point(4, 6),
axis: Axis.horizontal,
answer: 'sparky',
clue: 'flutter',
hints: ['dart', 'mobile', 'cross-platform'],
hints: const ['dart', 'mobile', 'cross-platform'],
visible: true,
solvedTimestamp: null,
),
],
borderWords: const [],
);

emit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:test/test.dart';
void main() {
group('CrosswordRepository', () {
final word = Word(
id: 'id',
position: Point(1, 1),
axis: Axis.horizontal,
answer: 'answer',
Expand All @@ -20,11 +19,11 @@ void main() {
final boardSection1 = BoardSection(
id: 'id',
position: Point(1, 1),
width: 10,
height: 10,
size: 10,
words: [
word,
],
borderWords: const [],
);
const sectionsCollection = 'sections';

Expand Down Expand Up @@ -71,11 +70,11 @@ void main() {
final section = BoardSection(
id: 'id2',
position: Point(1, 1),
width: 10,
height: 10,
size: 10,
words: [
word,
],
borderWords: const [],
);
await crosswordRepository.addSection(section);
expect(
Expand Down
Loading

0 comments on commit b90f444

Please sign in to comment.