Skip to content

Commit

Permalink
first version done
Browse files Browse the repository at this point in the history
  • Loading branch information
KhalafA committed May 6, 2019
1 parent 3836783 commit a512dfb
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 31 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@ whilst only having to maintain a single codebase.

Sonner than later:

Multiplayer backened
- lots of cleanup.

Responsiveness (smaller screens might get a pixel overflow)
- design should also work in protait mode
- set resizeToAvoidBottomPadding: false on most Scaffold widgets

DeathMatch
- make a design in AdobeXD

later:
- REMOVE debug printouts
Expand Down
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ android {

defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.pro"
applicationId "dk.khalaf.puiz"
minSdkVersion 21
targetSdkVersion 27
versionCode flutterVersionCode.toInteger()
Expand Down
1 change: 1 addition & 0 deletions lib/config/app_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class AppComponentState extends State<AppComponent> {
Widget build(BuildContext context) {
final app = new MaterialApp(
title: constants.appTitle,
debugShowCheckedModeBanner: false,
theme: new ThemeData(
primarySwatch: Colors.blue,
),
Expand Down
13 changes: 8 additions & 5 deletions lib/data/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ class Database {
}

// ------------------------------------- Messages Collection -------------------------------------
bool isCreator = false;
User player;
User opponent;
Game currentGame;
Expand All @@ -287,8 +286,10 @@ class Database {
await getQuestions(gameID);
player = await getPlayer();
opponent = await getOpponent(gameID);

}


Future<Game> buildGame(String gameID, {String state = constants.gameState}) async {

DocumentSnapshot documentSnapshot = await getGame(gameID);
Expand Down Expand Up @@ -324,9 +325,6 @@ class Database {
}


getIsCreator() {
return isCreator;
}

Future<User> getPlayer() async {
if(player == null){
Expand All @@ -345,7 +343,6 @@ class Database {
player = await getPlayer();

if(player.displayName == gameSnapshot.data[constants.gameCreatorName].toString()){
isCreator = true;
opponentID = gameSnapshot.data[constants.gameJoinerID].toString();
}else {
opponentID = gameSnapshot.data[constants.gameCreatorID].toString();
Expand Down Expand Up @@ -425,4 +422,10 @@ class Database {
Future<void> addMessage(String gameID, ChatMessage message) async {
await firestore.collection(constants.messagesCollection).document(gameID).collection(constants.chatCollections).add(message.toMap());
}

Future<String> getGameWinner(String gameID) async {
DocumentSnapshot doc = await firestore.collection(constants.messagesCollection).document(gameID).get();

return doc.data['winnerID'];
}
}
9 changes: 7 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import 'package:flutter/material.dart';
import 'package:pro/config/app_component.dart';
import 'package:flutter/services.dart';

void main() => runApp(new AppComponent());

void main() {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
.then((_) {
runApp(new AppComponent());
});
}

115 changes: 105 additions & 10 deletions lib/pages/multiplayer_tab/game_quiz.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'dart:ui';
import 'dart:async';

import 'package:pro/data/constants.dart' as constants;
import 'package:pro/widgets/custom_widgets.dart' as customWidgets;

class GameQuiz extends StatefulWidget {
final gameID;
Expand All @@ -28,14 +29,16 @@ class _GameQuizState extends State<GameQuiz> {
User player;
User opponent;

int ownScore;
int opponentScore;

var unescape = new HtmlUnescape();

bool showAnswer = false;
bool isCreator = false;
int timeLeft;

bool gameHasStarted = false;
bool gameHasEnded = false;

@override
void initState() {
Expand All @@ -50,11 +53,11 @@ class _GameQuizState extends State<GameQuiz> {
await Database().setUpGame(widget.gameID);
player = await Database().getPlayer();
opponent = await Database().getOpponent(widget.gameID);
isCreator = await Database().getIsCreator();

questions = await Database().getQuestions(widget.gameID);
game = await Database().getCurrentGame(widget.gameID);

isCreator = game.creatorID == player.id;
setState(() {
isLoading = false;
});
Expand All @@ -80,11 +83,10 @@ class _GameQuizState extends State<GameQuiz> {
return Container(
child: Stack(
children: <Widget>[
(!gameHasEnded) ? quiz() : Container(),
quiz(),

(!gameHasStarted || gameHasEnded) ? blur() : Container(),
(!gameHasStarted) ? blur() : Container(),
(!gameHasStarted) ? startScreen() : Container(),
(gameHasEnded) ? endScreen() : Container()
],
),
);
Expand Down Expand Up @@ -124,7 +126,8 @@ Widget quiz() {
if(snap.data[constants.gameCurrentRound] <= 5){
return placeholder(snap.data[constants.gameCurrentRound]);
}else {
gameHasEnded = true;
setScores(snap.data[constants.gameCreatorScore], snap.data[constants.gameJoinerScore]);
return endScreen();
}
},
);
Expand Down Expand Up @@ -227,6 +230,8 @@ Widget quiz() {
String ownAnswer;
String opponentAnswer;



if(isCreator){
ownAnswer = ownerAnswer;
opponentAnswer = joinerAnswer;
Expand Down Expand Up @@ -295,7 +300,6 @@ Widget quiz() {
print("ownAnswer: " + answer);
print("opponentAnswer: " + opponentAnswer);


Question q = Question.answer(
question: questions[currenQuestionIndex].question,
correctAnswer: questions[currenQuestionIndex].correctAnswer,
Expand Down Expand Up @@ -324,10 +328,14 @@ Widget quiz() {
);
}

setScores(int creatorScore, int joinerScore){
ownScore = isCreator ? creatorScore : joinerScore;
opponentScore = isCreator ? joinerScore : creatorScore;
}

Widget scoreBoard(int creatorScore, int joinerScore){

int ownScore = isCreator ? creatorScore : joinerScore;
int opponentScore = isCreator ? joinerScore : creatorScore;
setScores(creatorScore, joinerScore);

return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
Expand Down Expand Up @@ -376,7 +384,94 @@ Widget quiz() {


Widget endScreen(){
return Container();

String winnderID = (ownScore >= opponentScore) ? player.id : opponent.id;

User winner;
User loser;
String mainComment;
String subComment;

if(player.id == winnderID){
winner = player;
loser = opponent;
mainComment = "C o n g r a t u l a t i o n!";
subComment = "${opponent.displayName} was no match to you!";
}else {
winner = opponent;
loser = player;
mainComment = "You lost :(";
subComment = "${opponent.displayName} was too good this time";
}

return Container(
padding: EdgeInsets.only(top:45.0),
child: Column(
children: <Widget>[
customWidgets.titleWidget(mainComment.toUpperCase(), size: 25.0, color: constants.themeBlue, fontWeight: FontWeight.w800),
customWidgets.titleWidget(subComment, size: 15.0, color: Colors.black),


Center(
child: Container(
margin: EdgeInsets.only(top:25.0, bottom: 35.0),
child: Column(
children: <Widget>[
customWidgets.titleWidget('WINNER', size: 25.0, color: constants.themeBlue, fontWeight: FontWeight.w600),
Column(
children: <Widget>[
Container(
//margin: EdgeInsets.only(bottom:25.0),
width: 200.0,
height: 200.0,
decoration: new BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
fit: BoxFit.fill,
image: new NetworkImage(winner.imgPath)
)
),
),

ownScore >= opponentScore ? customWidgets.titleWidget('Score: $ownScore', size: 25.0, color: constants.themeBlue) : customWidgets.titleWidget('Score: $opponentScore', size: 25.0, color: constants.themeBlue) //TODO: handle ties
],
)
],
),
),
),
Container(
child: Column(
children: <Widget>[
Column(
children: <Widget>[
Container(
width: 100.0,
height: 100.0,
decoration: new BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
fit: BoxFit.fill,
image: new NetworkImage(loser.imgPath)
)
),
),
ownScore < opponentScore ? customWidgets.titleWidget('Score: $ownScore', size: 15.0, color: constants.themeBlue) : customWidgets.titleWidget('Score: $opponentScore', size: 15.0, color: constants.themeBlue)
],
)
],
),
),
Container(
margin: EdgeInsets.only(top: 25.0),
child: RaisedButton(
child: Text("Go back"),
onPressed: () => Navigator.pop(context),
)
)
],
),
);
}


Expand Down
24 changes: 17 additions & 7 deletions lib/pages/navigation_controller.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import 'package:flutter/material.dart';

import 'package:pro/pages/settings_page.dart';
import 'package:pro/pages/home_page.dart';
import 'package:pro/pages/quiz_tab/quiz_page.dart';
import 'package:pro/pages/deathmatch_tab/deathmatch_page.dart';
import 'package:pro/pages/multiplayer_tab/multiplayer_page.dart';

import 'package:pro/data/constants.dart' as constants;

import 'package:pro/data/database.dart';


class NavigationController extends StatefulWidget {
final subject;
Expand All @@ -29,7 +30,6 @@ class _NavigationControllerState extends State<NavigationController> {
MultiplayerPage multiplayerPage;
DeathMatchPage deathMatchPage;
QuizPage quizzesPage;
SettingsPage settingsPage;

List<Widget> pages;
List<String> pageTitles;
Expand All @@ -43,10 +43,9 @@ class _NavigationControllerState extends State<NavigationController> {
multiplayerPage = MultiplayerPage(1);
deathMatchPage = DeathMatchPage(2);
quizzesPage = QuizPage(3);
settingsPage = SettingsPage(4);

pageTitles = [constants.homePageTitle, constants.multiPlayerPageTitle, constants.deathmatchPageTitle, constants.quizPageTitle, constants.settingsPageTitle];
pages = [homePage,multiplayerPage, deathMatchPage, quizzesPage, settingsPage];
pageTitles = [constants.homePageTitle, constants.multiPlayerPageTitle, constants.deathmatchPageTitle, constants.quizPageTitle, 'LogOut'];
pages = [homePage,multiplayerPage, deathMatchPage, quizzesPage];

if(widget.subject == null){
currentPage = homePage;
Expand All @@ -71,6 +70,11 @@ class _NavigationControllerState extends State<NavigationController> {

updateCurrentTab(int index, String title){

if(index == 4){
print("Logout");
_signOut();
}else{

if(title == null){
setState(() {
currentTab = index;
Expand All @@ -85,7 +89,7 @@ class _NavigationControllerState extends State<NavigationController> {
currentTitle = title;
});
}

}


}
Expand All @@ -105,9 +109,15 @@ class _NavigationControllerState extends State<NavigationController> {
BottomNavigationBarItem(icon: Icon(Icons.people), title: Text(constants.multiPlayerPageTitle)),
BottomNavigationBarItem(icon: Icon(Icons.face), title: Text(constants.deathmatchPageTitle)),
BottomNavigationBarItem(icon: Icon(Icons.question_answer), title: Text(constants.quizPageTitle)),
BottomNavigationBarItem(icon: Icon(Icons.settings), title: Text(constants.settingsPageTitle))
BottomNavigationBarItem(icon: Icon(Icons.exit_to_app), title: Text('LogOut'))
],
),
);
}

void _signOut() {
Database().signOut();

Navigator.of(context).pushNamedAndRemoveUntil('/', (Route<dynamic> route) => false);
}
}

0 comments on commit a512dfb

Please sign in to comment.