Skip to content

Commit

Permalink
feat: start example project improve
Browse files Browse the repository at this point in the history
  • Loading branch information
luisburgos committed Sep 18, 2022
1 parent 5e73539 commit 6f427f1
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 4 deletions.
114 changes: 114 additions & 0 deletions example/packages/core/lib/data_model.dart
Original file line number Diff line number Diff line change
@@ -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<Player> 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<QuestionAnswer> 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;
}
5 changes: 2 additions & 3 deletions example/packages/core/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down
91 changes: 91 additions & 0 deletions example/packages/core/test/data_model_test.dart
Original file line number Diff line number Diff line change
@@ -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);
});
}
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.6"
version: "0.1.0"
characters:
dependency: transitive
description:
Expand Down

0 comments on commit 6f427f1

Please sign in to comment.