From 6f427f1953f189302d81b659fc28100df3fe33d4 Mon Sep 17 00:00:00 2001 From: Luis Burgos Date: Sun, 18 Sep 2022 00:37:28 -0500 Subject: [PATCH] feat: start example project improve --- example/packages/core/lib/data_model.dart | 114 ++++++++++++++++++ example/packages/core/pubspec.yaml | 5 +- .../packages/core/test/data_model_test.dart | 91 ++++++++++++++ example/pubspec.lock | 2 +- 4 files changed, 208 insertions(+), 4 deletions(-) create mode 100644 example/packages/core/lib/data_model.dart create mode 100644 example/packages/core/test/data_model_test.dart diff --git a/example/packages/core/lib/data_model.dart b/example/packages/core/lib/data_model.dart new file mode 100644 index 0000000..7073ec2 --- /dev/null +++ b/example/packages/core/lib/data_model.dart @@ -0,0 +1,114 @@ +class Trivia { + Trivia({ + required this.id, + required this.host, + required this.name, + required this.description, + required this.mainQuestion, + required this.joinCode, + this.game, + }); + + final String id; + final Player host; + final String name; + final String description; + final String mainQuestion; + final String joinCode; + + Game? game; + + void init() { + game = Game(id: 'game-1'); + addPlayer(host); + } + + void addPlayer(Player player) { + // + game?.players = List.from(game?.players ?? [])..add(player); + } + + void start() { + game?.start(); + } +} + +class Player { + Player({ + required this.id, + required this.name, + required this.answer, + }); + + final String id; + final String name; + final String answer; +} + +enum GameStatus { + initial, + started, + finished, +} + +class Game { + Game({ + required this.id, + this.status = GameStatus.initial, + this.players = const [], + }); + + final String id; + GameStatus status; + List players; + int get playersCount => players.length; + + void start() { + status = GameStatus.started; + } + + void finish() { + status = GameStatus.finished; + } +} + +class Ballot { + Ballot({ + required this.id, + required this.player, + this.status = BallotStatus.initial, + this.answers = const [], + }); + + final String id; + final Player player; + BallotStatus status; + List answers; + + void submit() { + status = BallotStatus.submitted; + } + + void addAnswer(QuestionAnswer answer) { + answers = List.from(answers)..add(answer); + } +} + +enum BallotStatus { + initial, + submitted, +} + +class Question { + Question(this.value); + final String value; +} + +class QuestionAnswer { + QuestionAnswer({ + required this.question, + required this.answer, + }); + final Question question; + final String answer; +} diff --git a/example/packages/core/pubspec.yaml b/example/packages/core/pubspec.yaml index 40c5e54..6428e26 100644 --- a/example/packages/core/pubspec.yaml +++ b/example/packages/core/pubspec.yaml @@ -1,7 +1,7 @@ name: core description: Example shared core components version: 0.0.1 -homepage: +homepage: none environment: sdk: ">=2.17.0 <3.0.0" @@ -13,9 +13,8 @@ dependencies: rxdart: 0.27.4 dev_dependencies: - flutter_test: - sdk: flutter flutter_lints: ^2.0.0 + test: 1.21.4 # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec diff --git a/example/packages/core/test/data_model_test.dart b/example/packages/core/test/data_model_test.dart new file mode 100644 index 0000000..39adb5f --- /dev/null +++ b/example/packages/core/test/data_model_test.dart @@ -0,0 +1,91 @@ +import 'package:core/data_model.dart'; +import 'package:test/test.dart'; + +final player1 = Player( + id: 'player-1', + name: 'Luis', + answer: 'ANSWER-1', +); +final player2 = Player( + id: 'player-2', + name: 'Javier', + answer: 'ANSWER-2', +); +final player3 = Player( + id: 'player-3', + name: 'Juan', + answer: 'ANSWER-3', +); +final player4 = Player( + id: 'player-4', + name: 'Silvia', + answer: 'ANSWER-4', +); + +void main() { + test('Trivia', () { + /// Create a new trivia + final trivia = Trivia( + id: 'trivia-1', + host: player1, + name: 'High school times', + description: + 'Search the song on the Spotify app, and paste the share link!', + mainQuestion: 'What is that song that reminds you of high school times?', + joinCode: 'a4ds-f49m-kq', + ); + trivia.init(); + expect(trivia.game?.status, GameStatus.initial); + expect(trivia.game?.playersCount, 1); + + /// Players Join + trivia.addPlayer(player2); + trivia.addPlayer(player3); + trivia.addPlayer(player4); + + expect(trivia.game?.status, GameStatus.initial); + expect(trivia.game?.playersCount, 4); + + /// Last Minute Player + trivia.addPlayer( + Player( + id: 'player-5', + name: 'LastMinutePlayer', + answer: 'ANSWER-5', + ), + ); + + expect(trivia.game?.status, GameStatus.initial); + expect(trivia.game?.playersCount, 5); + + /// Host start the trivia + trivia.start(); + expect(trivia.game?.status, GameStatus.started); + + //TODO: Test cannot join the game + }); + + test('Ballot', () { + /// Create a new trivia + final ballot = Ballot( + id: 'ballot-1', + player: player1, + ); + + expect(ballot.status, BallotStatus.initial); + + /// Add a guess + /// TODO: The options by question should be the list of players. + /// TODO: So we should be able to add answers to a map. + ballot.addAnswer( + QuestionAnswer( + question: Question('ANSWER-3'), + answer: 'Luis', + ), + ); + + // Submit the ballot + ballot.submit(); + expect(ballot.status, BallotStatus.submitted); + }); +} diff --git a/example/pubspec.lock b/example/pubspec.lock index b3deac3..4a48df9 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -21,7 +21,7 @@ packages: path: ".." relative: true source: path - version: "0.0.6" + version: "0.1.0" characters: dependency: transitive description: