Skip to content

Instantly share code, notes, and snippets.

@drexel-ue
Created July 22, 2019 18:54
Show Gist options
  • Save drexel-ue/60bf344cb43f1af32cf8cf261b7481c6 to your computer and use it in GitHub Desktop.
Save drexel-ue/60bf344cb43f1af32cf8cf261b7481c6 to your computer and use it in GitHub Desktop.
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart';
import 'package:trivia_black/events/game_event.dart';
import 'package:trivia_black/models/answer.dart';
import 'package:trivia_black/models/game.dart';
import 'package:trivia_black/models/point.dart';
import 'package:trivia_black/models/question.dart';
import 'package:trivia_black/models/user.dart';
import 'package:http/http.dart' as http;
import 'package:trivia_black/utils.dart';
import 'package:trivia_black/views/gameplay.dart';
export 'package:trivia_black/events/game_event.dart';
enum Guess { correct, incorrect }
class GameBloc {
Game currentGame;
Question currentQuestion = Question();
List<Answer> currentOptions = [];
String currentGuess;
final guessSubject = BehaviorSubject<Guess>();
Stream<Guess> get guessStream => guessSubject.stream;
final eventSubject = BehaviorSubject<GameEvent>();
GameBloc() {
eventSubject.listen((GameEvent event) {
if (event is SubmitGuess) {
_submitGuess(event.context, event.identifier);
} else if (event is ResetColors) {
guessSubject.add(null);
} else if (event is StartGame) {
_startGame(event.type, event.user, event.context);
} else if (event is LoadQuestion) {
_loadQuestion(event.user, event.context, event.category);
}
});
}
void _submitGuess(BuildContext context, String identifier) {
guessSubject.add(identifier == currentQuestion.answer.identifier
? Guess.correct
: Guess.incorrect);
}
void _startGame(GameType type, User user, BuildContext context,
{String friendIdentifier}) async {
currentOptions.clear();
String url = '';
if (type == GameType.random) {
url = '$currentIP/game/new';
} else {
url = '$currentIP/game/new/$friendIdentifier';
}
final header = {
"Authorization": "Bearer ${user.accessToken}",
'friendIdentifier': json.encode(friendIdentifier),
};
final response = await http.get(url, headers: header);
if (response.statusCode == 200) {
Map<String, dynamic> deats = await json.decode(response.body);
TODO:
print(deats);
final turnPlayer = User(
identifier: deats['turnPlayer']['identifier'],
firstName: deats['turnPlayer']['firstName'],
fullName: deats['turnPlayer']['fullName'],
lastName: deats['turnPlayer']['lastName'],
);
final waitPlayer = User(
identifier: deats['waitPlayer']['identifier'],
firstName: deats['waitPlayer']['firstName'],
fullName: deats['waitPlayer']['fullName'],
lastName: deats['waitPlayer']['lastName'],
);
final challengerPoints = <Point>[];
deats['challengerPoints'].forEach(
(point) {
challengerPoints.add(Point(
category: point['category'],
identifier: point['identifier'],
scored: point['scored'],
));
},
);
final challengeePoints = <Point>[];
deats['challengeePoints'].forEach(
(point) {
challengeePoints.add(Point(
category: point['category'],
identifier: point['identifier'],
scored: point['scored'],
));
},
);
currentGame = Game(
challengeeIdentifier: deats['challengee']['identifier'],
challengerIdentifier: deats['challenger']['identifier'],
turnPlayer: turnPlayer,
waitPlayer: waitPlayer,
challengeePoints: challengeePoints,
challengerPoints: challengerPoints,
);
showDialog(
barrierDismissible: false,
context: context,
builder: (BuildContext context) => AlertDialog(
titlePadding: const EdgeInsets.all(0.0),
contentPadding: const EdgeInsets.all(0.0),
content: Container(
height: 200.0,
width: 200.0,
child: Center(
child: Transform.scale(
scale: 1.5,
child: CircularProgressIndicator(),
),
),
),
),
);
_awaitLoad(context);
} else {
showMessage(context, response.reasonPhrase);
}
}
void _loadQuestion(User user, BuildContext context, String category) async {
currentOptions.clear();
final url = '$currentIP/question/$category';
final header = {"Authorization": "Bearer ${user.accessToken}"};
final response = await http.get(url, headers: header);
if (response.statusCode == 200) {
//TODO: print(await json.decode(response.body));
final deats = await json.decode(response.body);
List options = deats['options'];
Map<String, dynamic> question = deats['question'];
options.forEach((option) {
currentOptions.add(Answer()
..body = option['body']
..category = option['category']
..identifier = option['identifier']);
});
currentQuestion
..answer = currentOptions.firstWhere(
(option) => option.identifier == question['answer']['identifier'])
..body = question['body']
..category = question['category']
..optionA = currentOptions[0]
..optionB = currentOptions[1]
..optionC = currentOptions[2]
..optionD = currentOptions[3];
} else {
showMessage(context, response.reasonPhrase);
}
}
void _awaitLoad(BuildContext context) async {
if (currentGame.loaded) {
Navigator.push(
context,
MaterialPageRoute(builder: (BuildContext context) => GamePlay()),
);
} else {
await Future.delayed(Duration(milliseconds: 500));
_awaitLoad(context);
}
}
void showPowerUpDialog(BuildContext context) {}
void dispose() {
guessSubject?.drain();
eventSubject?.drain();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment