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 to leaderboard streak and mascot team #222

Merged
merged 6 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -1,4 +1,5 @@
import 'package:equatable/equatable.dart';
import 'package:game_domain/src/models/mascots.dart';
import 'package:json_annotation/json_annotation.dart';

part 'leaderboard_player.g.dart';
Expand All @@ -13,6 +14,8 @@ class LeaderboardPlayer extends Equatable {
required this.userId,
required this.initials,
required this.score,
required this.streak,
required this.mascot,
});

/// {@macro leaderboard_player}
Expand All @@ -28,13 +31,21 @@ class LeaderboardPlayer extends Equatable {
@JsonKey()
final int score;

/// Number of streaks.
@JsonKey()
final int streak;

/// Initials of the player.
@JsonKey()
final String initials;

/// The player mascot.
@JsonKey()
final Mascots mascot;

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

@override
List<Object?> get props => [userId, score, initials];
List<Object?> get props => [userId, score, initials, streak, mascot];
}

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 @@ -11,6 +11,8 @@ void main() {
userId: 'id',
initials: 'TST',
score: 10,
mascot: Mascots.android,
streak: 2,
),
isNotNull,
);
Expand All @@ -20,6 +22,8 @@ void main() {
userId: 'id',
initials: 'TST',
score: 20,
mascot: Mascots.android,
streak: 2,
);

test('toJson returns the instance as json', () {
Expand All @@ -29,32 +33,52 @@ void main() {
'userId': 'id',
'initials': 'TST',
'score': 20,
'mascot': Mascots.android.name,
'streak': 2,
}),
);
});

test('fromJson returns the correct instance', () {
expect(
LeaderboardPlayer.fromJson(const {
LeaderboardPlayer.fromJson({
'userId': 'id',
'initials': 'TST',
'score': 20,
'mascot': Mascots.android.name,
'streak': 2,
}),
equals(leaderboardPlayer),
);
});

test('supports equality', () {
expect(
LeaderboardPlayer(userId: '', initials: 'TST', score: 20),
equals(LeaderboardPlayer(userId: '', initials: 'TST', score: 20)),
LeaderboardPlayer(
userId: '',
initials: 'TST',
score: 20,
mascot: Mascots.android,
streak: 2,
),
equals(
LeaderboardPlayer(
userId: '',
initials: 'TST',
score: 20,
mascot: Mascots.android,
streak: 2,
),
),
);

expect(
LeaderboardPlayer(
userId: '',
initials: 'TST',
score: 20,
mascot: Mascots.android,
streak: 2,
),
isNot(
equals(leaderboardPlayer),
Expand All @@ -66,6 +90,34 @@ void main() {
userId: 'id',
initials: 'WOW',
score: 20,
mascot: Mascots.android,
streak: 2,
),
isNot(
equals(leaderboardPlayer),
),
);

expect(
LeaderboardPlayer(
userId: 'id',
initials: 'TST',
score: 20,
mascot: Mascots.dash,
streak: 2,
),
isNot(
equals(leaderboardPlayer),
),
);

expect(
LeaderboardPlayer(
userId: 'id',
initials: 'TST',
score: 20,
mascot: Mascots.android,
streak: 3,
),
isNot(
equals(leaderboardPlayer),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,27 @@ void main() {
userId: 'id',
initials: 'AAA',
score: 20,
mascot: Mascots.android,
streak: 3,
);
const playerTwo = LeaderboardPlayer(
userId: 'id2',
initials: 'BBB',
score: 10,
mascot: Mascots.dash,
streak: 2,
);

when(() => dbClient.orderBy('leaderboard', 'score'))
.thenAnswer((_) async {
return [
DbEntityRecord(
id: 'id',
data: {
'initials': playerOne.initials,
'score': 20,
},
data: playerOne.toJson()..remove('userId'),
),
DbEntityRecord(
id: 'id2',
data: {
'initials': playerTwo.initials,
'score': 10,
},
data: playerTwo.toJson()..remove('userId'),
),
];
});
Expand All @@ -93,14 +91,13 @@ void main() {
userId: 'user-id',
initials: 'initials',
score: 40,
mascot: Mascots.dash,
streak: 2,
);

final record = DbEntityRecord(
id: 'user-id',
data: {
'initials': 'initials',
'score': 40,
},
data: leaderboardPlayer.toJson()..remove('userId'),
);

when(() => dbClient.set('leaderboard', record))
Expand Down
38 changes: 38 additions & 0 deletions api/test/routes/game/leaderboard/initials/index_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ void main() {
userId: 'user-id',
initials: 'AAA',
score: 10,
mascot: Mascots.dash,
streak: 2,
);

when(
Expand All @@ -71,6 +73,8 @@ void main() {
userId: 'user-id',
initials: 'AAA',
score: 10,
mascot: Mascots.dash,
streak: 2,
);

when(() => request.method).thenReturn(HttpMethod.post);
Expand All @@ -87,6 +91,32 @@ void main() {
expect(response.statusCode, equals(HttpStatus.noContent));
});

test('responds with a 400 when mascot is not correct', () async {
final leaderboardPlayer = LeaderboardPlayer(
userId: 'user-id',
initials: 'AAA',
score: 10,
mascot: Mascots.dash,
streak: 2,
);

when(() => request.method).thenReturn(HttpMethod.post);

when(
() => leaderboardRepository.addPlayerToLeaderboard(
leaderboardPlayer: leaderboardPlayer,
),
).thenAnswer((_) async {});

when(request.json).thenAnswer(
(_) async => leaderboardPlayer.toJson()
..update('mascot', (value) => 'no-real-mascot'),
);

final response = await route.onRequest(context);
expect(response.statusCode, equals(HttpStatus.badRequest));
});

test('responds with a 400 when request is invalid', () async {
when(() => request.method).thenReturn(HttpMethod.post);
when(request.json).thenAnswer((_) async => {'test': 'test'});
Expand All @@ -102,6 +132,8 @@ void main() {
'userId': 'user-id',
'initials': 'CCC',
'score': 10,
'mascot': 'dash',
'streak': 2,
},
);

Expand All @@ -117,6 +149,8 @@ void main() {
'userId': 'user-id',
'initials': 'ccc',
'score': 10,
'mascot': 'dash',
'streak': 2,
},
);

Expand All @@ -133,6 +167,8 @@ void main() {
'userId': 'user-id',
'initials': 'aa',
'score': 10,
'mascot': 'dash',
'streak': 2,
},
);

Expand All @@ -150,6 +186,8 @@ void main() {
'userId': 'user-id',
'initials': 'aaaa',
'score': 10,
'mascot': 'dash',
'streak': 2,
},
);

Expand Down
12 changes: 12 additions & 0 deletions api/test/routes/game/leaderboard/results/index_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ void main() {
userId: 'id',
score: 1,
initials: 'AAA',
mascot: Mascots.dash,
streak: 2,
),
LeaderboardPlayer(
userId: 'id2',
score: 2,
initials: 'BBB',
mascot: Mascots.android,
streak: 3,
),
LeaderboardPlayer(
userId: 'id3',
score: 3,
initials: 'CCC',
mascot: Mascots.sparky,
streak: 4,
),
];

Expand Down Expand Up @@ -88,16 +94,22 @@ void main() {
'userId': 'id',
'score': 1,
'initials': 'AAA',
'mascot': 'dash',
'streak': 2,
},
{
'userId': 'id2',
'score': 2,
'initials': 'BBB',
'mascot': 'android',
'streak': 3,
},
{
'userId': 'id3',
'score': 3,
'initials': 'CCC',
'mascot': 'sparky',
'streak': 4,
},
],
}),
Expand Down
2 changes: 2 additions & 0 deletions lib/leaderboard/bloc/leaderboard_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class LeaderboardBloc extends Bloc<LeaderboardEvent, LeaderboardState> {
userId: '',
initials: 'AAA',
score: 0,
streak: 0,
mascot: Mascots.dash,
),
),
),
Expand Down
18 changes: 9 additions & 9 deletions lib/leaderboard/view/leaderboard_success.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,13 @@ class UserLeaderboardRanking extends StatelessWidget {

@override
Widget build(BuildContext context) {
// TODO(Ayad): show the correct style based on the players team
// https://very-good-ventures-team.monday.com/boards/6004820050/pulses/6400331391
final style = IoPlayerAliasStyle(
backgroundColor: rank.isOdd
? IoCrosswordColors.androidGreen
: IoCrosswordColors.flutterBlue,
backgroundColor: switch (player.mascot) {
Mascots.dash => IoCrosswordColors.flutterBlue,
Mascots.sparky => IoCrosswordColors.sparkyYellow,
Mascots.dino => IoCrosswordColors.chromeRed,
Mascots.android => IoCrosswordColors.androidGreen,
},
textStyle: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.w700,
Expand Down Expand Up @@ -204,11 +205,10 @@ class UserLeaderboardRanking extends StatelessWidget {
],
),
),

// TODO(Ayad): add missing streak in [LeaderboardPlayer]
// https://very-good-ventures-team.monday.com/boards/6004820050/pulses/6400331391
Expanded(
child: Text(1524.toDisplayNumber()),
child: Text(
player.streak.toDisplayNumber(),
),
),
Expanded(
child: Text(
Expand Down
Loading
Loading