Skip to content

Commit

Permalink
feat: save mascot when answering word
Browse files Browse the repository at this point in the history
  • Loading branch information
jsgalarraga committed Apr 1, 2024
1 parent 1fdb6a1 commit 516e3ca
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:clock/clock.dart';
import 'package:collection/collection.dart';
import 'package:db_client/db_client.dart';
import 'package:game_domain/game_domain.dart';

Expand Down Expand Up @@ -64,24 +65,27 @@ class CrosswordRepository {
int sectionY,
int wordX,
int wordY,
Mascots mascot,
String answer,
) async {
final section = await findSectionByPosition(
sectionX,
sectionY,
);
final section = await findSectionByPosition(sectionX, sectionY);

if (section == null) {
return false;
}

final word = section.words.firstWhere(
final word = section.words.firstWhereOrNull(
(element) => element.position.x == wordX && element.position.y == wordY,
);

if (word == null) {
return false;
}

if (answer == word.answer) {
final solvedWord = word.copyWith(
solvedTimestamp: clock.now().millisecondsSinceEpoch,
mascot: mascot,
);
final newSection = section.copyWith(
words: [...section.words..remove(word), solvedWord],
Expand Down
1 change: 1 addition & 0 deletions api/packages/crossword_repository/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ environment:

dependencies:
clock: ^1.1.1
collection: ^1.18.0
db_client:
path: ../db_client
game_domain:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ void main() {
hints: const [],
solvedTimestamp: null,
);

setUp(() {
final record = _MockDbEntityRecord();
when(() => record.id).thenReturn('id');
Expand Down Expand Up @@ -184,7 +185,8 @@ void main() {
final time = DateTime.now();
final clock = Clock.fixed(time);
await withClock(clock, () async {
final valid = await repository.answerWord(1, 1, 1, 1, 'flutter');
final valid =
await repository.answerWord(1, 1, 1, 1, Mascots.dino, 'flutter');
expect(valid, isTrue);
final captured = verify(
() => dbClient.update(
Expand All @@ -201,7 +203,10 @@ void main() {
'size': 300,
'words': [
word
.copyWith(solvedTimestamp: time.millisecondsSinceEpoch)
.copyWith(
solvedTimestamp: time.millisecondsSinceEpoch,
mascot: Mascots.dino,
)
.toJson(),
],
'borderWords': <String>[],
Expand All @@ -212,7 +217,8 @@ void main() {
});

test('answerWord returns false if answer is incorrect', () async {
final valid = await repository.answerWord(1, 1, 1, 1, 'android');
final valid =
await repository.answerWord(1, 1, 1, 1, Mascots.dino, 'android');
expect(valid, isFalse);
});

Expand All @@ -227,7 +233,14 @@ void main() {
),
).thenAnswer((_) async => []);

final valid = await repository.answerWord(0, 0, 1, 1, 'flutter');
final valid =
await repository.answerWord(0, 0, 1, 1, Mascots.dino, 'flutter');
expect(valid, isFalse);
});

test('answerWord returns false if word is not in section', () async {
final valid =
await repository.answerWord(1, 1, -1, -1, Mascots.dino, 'flutter');
expect(valid, isFalse);
});
});
Expand Down
1 change: 1 addition & 0 deletions api/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies:
board_renderer:
path: packages/board_renderer
clock: ^1.1.1
collection: ^1.18.0
crossword_repository:
path: packages/crossword_repository
dart_frog: ^1.1.0
Expand Down
7 changes: 6 additions & 1 deletion api/routes/game/answer.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'dart:io';

import 'package:api/extensions/path_param_to_position.dart';
import 'package:collection/collection.dart';
import 'package:crossword_repository/crossword_repository.dart';
import 'package:dart_frog/dart_frog.dart';
import 'package:game_domain/game_domain.dart';

Future<Response> onRequest(RequestContext context) async {
if (context.request.method == HttpMethod.post) {
Expand All @@ -18,9 +20,11 @@ Future<Response> _onPost(RequestContext context) async {
final json = await context.request.json() as Map<String, dynamic>;
final sectionId = json['sectionId'] as String?;
final wordPosition = json['wordPosition'] as String?;
final mascot = json['mascot'] as String?;
final mascotName = json['mascot'] as String?;
final answer = json['answer'] as String?;

final mascot = Mascots.values.firstWhereOrNull((e) => e.name == mascotName);

if (sectionId == null ||
wordPosition == null ||
mascot == null ||
Expand Down Expand Up @@ -49,6 +53,7 @@ Future<Response> _onPost(RequestContext context) async {
sectionY,
wordX,
wordY,
mascot,
answer,
);

Expand Down
15 changes: 9 additions & 6 deletions api/test/routes/game/answer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'dart:io';

import 'package:crossword_repository/crossword_repository.dart';
import 'package:dart_frog/dart_frog.dart';
import 'package:game_domain/game_domain.dart';
import 'package:mocktail/mocktail.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -55,14 +56,15 @@ void main() {

test('returns Response with valid to true if answer is correct',
() async {
when(() => crosswordRepository.answerWord(1, 1, 1, 1, 'flutter'))
.thenAnswer((_) async => true);
when(
() => crosswordRepository.answerWord(1, 1, 1, 1, Mascots.dash, 'sun'),
).thenAnswer((_) async => true);
when(() => request.json()).thenAnswer(
(_) async => {
'sectionId': '1,1',
'wordPosition': '1,1',
'mascot': 'dash',
'answer': 'flutter',
'answer': 'sun',
},
);

Expand All @@ -74,14 +76,15 @@ void main() {

test('returns Response with valid to false if answer is incorrect',
() async {
when(() => crosswordRepository.answerWord(1, 1, 1, 1, 'android'))
.thenAnswer((_) async => false);
when(
() => crosswordRepository.answerWord(1, 1, 1, 1, Mascots.dash, 'sun'),
).thenAnswer((_) async => false);
when(() => request.json()).thenAnswer(
(_) async => {
'sectionId': '1,1',
'wordPosition': '1,1',
'mascot': 'dash',
'answer': 'android',
'answer': 'sun',
},
);

Expand Down

0 comments on commit 516e3ca

Please sign in to comment.