-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e73539
commit 6f427f1
Showing
4 changed files
with
208 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters