Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds a list of collided_word.dart to answer.dart #406

Merged
merged 16 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ class CrosswordRepository {
return false;
}

final sectionX = correctAnswer.section.x;
final sectionY = correctAnswer.section.y;
// TODO(Ayad): update all the sections
// https://very-good-ventures-team.monday.com/boards/6004820050/pulses/6530206382
final sectionX = correctAnswer.sections.first.x;
final sectionY = correctAnswer.sections.first.y;
final section = await findSectionByPosition(sectionX, sectionY);

if (section == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ void main() {
when(() => answersRecord.id).thenReturn('1');
when(() => answersRecord.data).thenReturn({
'answer': 'flutter',
'section': {'x': 1, 'y': 1},
'sections': [
{'x': 1, 'y': 1},
],
'collidedWords': <Map<String, dynamic>>[],
});
when(
() => dbClient.getById(answersCollection, '1'),
Expand Down Expand Up @@ -271,7 +274,10 @@ void main() {
when(() => answersRecord.id).thenReturn('fake');
when(() => answersRecord.data).thenReturn({
'answer': 'flutter',
'section': {'x': 1, 'y': 1},
'sections': [
{'x': 1, 'y': 1},
],
'collidedWords': <Map<String, dynamic>>[],
});
when(
() => dbClient.getById(answersCollection, 'fake'),
Expand Down
15 changes: 10 additions & 5 deletions api/packages/game_domain/lib/src/models/answer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Answer extends Equatable {
const Answer({
required this.id,
required this.answer,
required this.section,
required this.sections,
required this.collidedWords,
});

/// {@macro answer}
Expand All @@ -29,14 +30,18 @@ class Answer extends Equatable {
@JsonKey()
final String answer;

/// The section of the board where the word is located.
/// The sections of the board where the word is located.
@JsonKey()
@PointConverter()
final Point<int> section;
@ListPointConverter()
final List<Point<int>> sections;

/// The words that collide with the current [Answer].
@JsonKey()
final List<CollidedWord> collidedWords;

/// Returns a json representation from this instance.
Map<String, dynamic> toJson() => _$AnswerToJson(this);

@override
List<Object?> get props => [id, answer, section];
List<Object?> get props => [id, answer, sections, collidedWords];
}
10 changes: 7 additions & 3 deletions api/packages/game_domain/lib/src/models/answer.g.dart

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

42 changes: 42 additions & 0 deletions api/packages/game_domain/lib/src/models/collided_word.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:equatable/equatable.dart';
import 'package:game_domain/game_domain.dart';
import 'package:json_annotation/json_annotation.dart';

part 'collided_word.g.dart';

/// {@template collided_word}
/// A model that represents collision of a word incating the [wordId], the
/// [position] of the character and the [character].
/// {@endtemplate}
@JsonSerializable()
class CollidedWord extends Equatable {
/// {@macro collided_word}
const CollidedWord({
required this.wordId,
required this.position,
required this.character,
});

/// {@macro collided_word}
factory CollidedWord.fromJson(Map<String, dynamic> json) =>
_$CollidedWordFromJson(json);

/// Returns a json representation from this instance.
Map<String, dynamic> toJson() => _$CollidedWordToJson(this);

/// The id of [Word].
@JsonKey()
final String wordId;

/// The position of the character where it collided.
/// First character 0, second character 1, etc.
@JsonKey()
final int position;

/// The character that collided.
@JsonKey()
final String character;
AyadLaouissi marked this conversation as resolved.
Show resolved Hide resolved

@override
List<Object?> get props => [wordId, position, character];
}
20 changes: 20 additions & 0 deletions api/packages/game_domain/lib/src/models/collided_word.g.dart

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

1 change: 1 addition & 0 deletions api/packages/game_domain/lib/src/models/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export 'dart:math' show Point;

export 'answer.dart';
export 'board_section.dart';
export 'collided_word.dart';
export 'hint.dart';
export 'mascots.dart';
export 'player.dart';
Expand Down
34 changes: 34 additions & 0 deletions api/packages/game_domain/lib/src/models/point_converter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,37 @@ class PointConverter extends JsonConverter<Point<int>, Map<String, dynamic>> {
};
}
}

/// {@template point_converter}
/// A converter that converts a [List] of [Point] to a [List] of [Map]
/// and vice versa.
/// {@endtemplate}
class ListPointConverter
extends JsonConverter<List<Point<int>>, List<Map<String, dynamic>>> {
/// {@macro point_converter}
const ListPointConverter();

@override
List<Point<int>> fromJson(List<Map<String, dynamic>> json) {
return json
.map(
(j) => Point<int>(
(j['x'] as num).toInt(),
(j['y'] as num).toInt(),
),
)
.toList();
}

@override
List<Map<String, dynamic>> toJson(List<Point<int>> object) {
return object
.map(
(o) => {
'x': o.x,
'y': o.y,
},
)
.toList();
}
}
66 changes: 60 additions & 6 deletions api/packages/game_domain/test/src/models/answer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,34 @@ import 'package:test/test.dart';
void main() {
group('Answer', () {
test('creates correct json object from Answer object', () {
final answer = Answer(id: 'id', answer: 'answer', section: Point(1, 2));
final answer = Answer(
id: 'id',
answer: 'answer',
sections: const [Point(1, 2)],
collidedWords: const [
CollidedWord(
wordId: 'word-id',
position: 3,
character: 'b',
),
],
);
final json = answer.toJson();

expect(
json,
equals({
'answer': 'answer',
'section': {'x': 1, 'y': 2},
'sections': [
{'x': 1, 'y': 2},
],
'collidedWords': [
{
'wordId': 'word-id',
'position': 3,
'character': 'b',
},
],
}),
);
});
Expand All @@ -22,13 +42,33 @@ void main() {
final json = {
'id': 'id',
'answer': 'answer',
'section': {'x': 1, 'y': 2},
'sections': [
{'x': 1, 'y': 2},
],
'collidedWords': [
{
'wordId': 'word-id',
'position': 3,
'character': 'b',
},
],
};
final answer = Answer.fromJson(json);
expect(
answer,
equals(
Answer(id: 'id', answer: 'answer', section: Point(1, 2)),
Answer(
id: 'id',
answer: 'answer',
sections: const [Point(1, 2)],
collidedWords: const [
CollidedWord(
wordId: 'word-id',
position: 3,
character: 'b',
),
],
),
),
);
});
Expand All @@ -37,12 +77,26 @@ void main() {
final firstAnswer = Answer(
id: 'id',
answer: 'answer',
section: Point(1, 2),
sections: const [Point(1, 2)],
collidedWords: const [
CollidedWord(
wordId: 'word-id',
position: 3,
character: 'b',
),
],
);
final secondAnswer = Answer(
id: 'id',
answer: 'answer',
section: Point(1, 2),
sections: const [Point(1, 2)],
collidedWords: const [
CollidedWord(
wordId: 'word-id',
position: 3,
character: 'b',
),
],
);
expect(firstAnswer, equals(secondAnswer));
});
Expand Down
9 changes: 6 additions & 3 deletions api/test/routes/game/hint_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ void main() {
return Answer(
id: 'wordId',
answer: 'answer',
section: Point(1, 1),
sections: [Point(1, 1)],
collidedWords: [],
);
},
);
Expand Down Expand Up @@ -172,7 +173,8 @@ void main() {
return Answer(
id: 'wordId',
answer: 'answer',
section: Point(1, 1),
sections: [Point(1, 1)],
collidedWords: [],
);
},
);
Expand Down Expand Up @@ -214,7 +216,8 @@ void main() {
return Answer(
id: 'wordId',
answer: 'answer',
section: Point(1, 1),
sections: [Point(1, 1)],
collidedWords: [],
);
},
);
Expand Down
Loading
Loading