Skip to content

Commit

Permalink
Merge branch 'main' into feat/remove-hints
Browse files Browse the repository at this point in the history
  • Loading branch information
jsgalarraga authored Apr 2, 2024
2 parents c612bef + e24bada commit 251636c
Show file tree
Hide file tree
Showing 13 changed files with 527 additions and 145 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 @@ -148,6 +148,7 @@ void main() {
clue: '',
solvedTimestamp: null,
);

setUp(() {
final record = _MockDbEntityRecord();
when(() => record.id).thenReturn('id');
Expand Down Expand Up @@ -183,7 +184,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 @@ -200,7 +202,10 @@ void main() {
'size': 300,
'words': [
word
.copyWith(solvedTimestamp: time.millisecondsSinceEpoch)
.copyWith(
solvedTimestamp: time.millisecondsSinceEpoch,
mascot: Mascots.dino,
)
.toJson(),
],
'borderWords': <String>[],
Expand All @@ -211,7 +216,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 @@ -226,7 +232,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
9 changes: 9 additions & 0 deletions api/packages/game_domain/lib/src/models/word.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Word extends Equatable {
required this.answer,
required this.clue,
required this.solvedTimestamp,
this.mascot,
}) : id = '$position-$axis';

/// {@macro word}
Expand Down Expand Up @@ -49,6 +50,11 @@ class Word extends Equatable {
@JsonKey()
final int? solvedTimestamp;

/// The mascot of the user that first solved the word.
/// If the word is not solved, this value is null.
@JsonKey()
final Mascots? mascot;

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

Expand All @@ -61,13 +67,15 @@ class Word extends Equatable {
String? answer,
String? clue,
int? solvedTimestamp,
Mascots? mascot,
}) {
return Word(
position: position ?? this.position,
axis: axis ?? this.axis,
answer: answer ?? this.answer,
clue: clue ?? this.clue,
solvedTimestamp: solvedTimestamp ?? this.solvedTimestamp,
mascot: mascot ?? this.mascot,
);
}

Expand All @@ -79,6 +87,7 @@ class Word extends Equatable {
answer,
clue,
solvedTimestamp,
mascot,
];
}

Expand Down
9 changes: 9 additions & 0 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.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void main() {
answer: 'answer',
clue: 'clue',
solvedTimestamp: 1234,
mascot: Mascots.android,
),
],
borderWords: [],
Expand All @@ -36,6 +37,7 @@ void main() {
'answer': 'answer',
'clue': 'clue',
'solvedTimestamp': 1234,
'mascot': 'android',
},
],
'borderWords': <Map<String, dynamic>>[],
Expand All @@ -57,6 +59,7 @@ void main() {
'answer': 'answer',
'clue': 'clue',
'solvedTimestamp': 1234,
'mascot': 'android',
},
],
'borderWords': <Map<String, dynamic>>[],
Expand All @@ -77,6 +80,7 @@ void main() {
answer: 'answer',
clue: 'clue',
solvedTimestamp: 1234,
mascot: Mascots.android,
),
],
borderWords: [],
Expand Down
4 changes: 4 additions & 0 deletions api/packages/game_domain/test/src/models/word_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void main() {
answer: 'test',
clue: 'clue',
solvedTimestamp: 0,
mascot: Mascots.sparky,
);
final json = word.toJson();

Expand All @@ -23,6 +24,7 @@ void main() {
'answer': 'test',
'clue': 'clue',
'solvedTimestamp': 0,
'mascot': 'sparky',
}),
);
});
Expand All @@ -32,6 +34,7 @@ void main() {
'id': 'id',
'position': {'x': 1, 'y': 2},
'axis': 'horizontal',
'mascot': 'sparky',
'answer': 'test',
'clue': 'clue',
};
Expand All @@ -45,6 +48,7 @@ void main() {
answer: 'test',
clue: 'clue',
solvedTimestamp: null,
mascot: Mascots.sparky,
),
),
);
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
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
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,
String sectionId,
String wordPosition,
) async {
Future<Response> onRequest(RequestContext context) async {
if (context.request.method == HttpMethod.post) {
return _onPost(context, sectionId, wordPosition);
return _onPost(context);
} else {
return Response(statusCode: HttpStatus.methodNotAllowed);
}
}

Future<Response> _onPost(
RequestContext context,
String sectionId,
String wordPosition,
) async {
Future<Response> _onPost(RequestContext context) async {
final crosswordRepository = context.read<CrosswordRepository>();

final json = await context.request.json() as Map<String, dynamic>;
final sectionId = json['sectionId'] as String?;
final wordPosition = json['wordPosition'] 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 ||
answer == null) {
return Response(statusCode: HttpStatus.badRequest);
}

final posSection = sectionId.parseToPosition();
final sectionX = posSection?.$1;
final sectionY = posSection?.$2;
Expand All @@ -39,14 +48,12 @@ Future<Response> _onPost(
return Response(statusCode: HttpStatus.badRequest);
}

final json = await context.request.json() as Map<String, dynamic>;
final answer = json['answer'] as String;

final valid = await crosswordRepository.answerWord(
sectionX,
sectionY,
wordX,
wordY,
mascot,
answer,
);

Expand Down
Loading

0 comments on commit 251636c

Please sign in to comment.