Skip to content
Snippets Groups Projects
Commit 3ed7459e authored by Sunny Singh's avatar Sunny Singh
Browse files

fixed bonus question errors

parent 1f651d73
No related branches found
No related tags found
No related merge requests found
Pipeline #376725 failed
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"cloud_firestore","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\cloud_firestore-4.17.5\\\\","native_build":true,"dependencies":["firebase_core"]},{"name":"firebase_core","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\firebase_core-2.32.0\\\\","native_build":true,"dependencies":[]},{"name":"flutter_compass","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\flutter_compass-0.8.1\\\\","native_build":true,"dependencies":[]},{"name":"sensors_plus","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\sensors_plus-6.1.1\\\\","native_build":true,"dependencies":[]}],"android":[{"name":"cloud_firestore","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\cloud_firestore-4.17.5\\\\","native_build":true,"dependencies":["firebase_core"]},{"name":"firebase_core","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\firebase_core-2.32.0\\\\","native_build":true,"dependencies":[]},{"name":"flutter_compass","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\flutter_compass-0.8.1\\\\","native_build":true,"dependencies":[]},{"name":"sensors_plus","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\sensors_plus-6.1.1\\\\","native_build":true,"dependencies":[]}],"macos":[{"name":"cloud_firestore","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\cloud_firestore-4.17.5\\\\","native_build":true,"dependencies":["firebase_core"]},{"name":"firebase_core","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\firebase_core-2.32.0\\\\","native_build":true,"dependencies":[]}],"linux":[],"windows":[{"name":"cloud_firestore","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\cloud_firestore-4.17.5\\\\","native_build":true,"dependencies":["firebase_core"]},{"name":"firebase_core","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\firebase_core-2.32.0\\\\","native_build":true,"dependencies":[]}],"web":[{"name":"cloud_firestore_web","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\cloud_firestore_web-3.12.5\\\\","dependencies":["firebase_core_web"]},{"name":"firebase_core_web","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\firebase_core_web-2.18.2\\\\","dependencies":[]},{"name":"sensors_plus","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\sensors_plus-6.1.1\\\\","dependencies":[]}]},"dependencyGraph":[{"name":"cloud_firestore","dependencies":["cloud_firestore_web","firebase_core"]},{"name":"cloud_firestore_web","dependencies":["firebase_core","firebase_core_web"]},{"name":"firebase_core","dependencies":["firebase_core_web"]},{"name":"firebase_core_web","dependencies":[]},{"name":"flutter_compass","dependencies":[]},{"name":"sensors_plus","dependencies":[]}],"date_created":"2025-01-10 15:32:53.728377","version":"3.24.5","swift_package_manager_enabled":false}
\ No newline at end of file
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"cloud_firestore","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\cloud_firestore-4.17.5\\\\","native_build":true,"dependencies":["firebase_core"]},{"name":"firebase_core","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\firebase_core-2.32.0\\\\","native_build":true,"dependencies":[]},{"name":"flutter_compass","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\flutter_compass-0.8.1\\\\","native_build":true,"dependencies":[]},{"name":"sensors_plus","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\sensors_plus-6.1.1\\\\","native_build":true,"dependencies":[]}],"android":[{"name":"cloud_firestore","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\cloud_firestore-4.17.5\\\\","native_build":true,"dependencies":["firebase_core"]},{"name":"firebase_core","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\firebase_core-2.32.0\\\\","native_build":true,"dependencies":[]},{"name":"flutter_compass","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\flutter_compass-0.8.1\\\\","native_build":true,"dependencies":[]},{"name":"sensors_plus","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\sensors_plus-6.1.1\\\\","native_build":true,"dependencies":[]}],"macos":[{"name":"cloud_firestore","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\cloud_firestore-4.17.5\\\\","native_build":true,"dependencies":["firebase_core"]},{"name":"firebase_core","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\firebase_core-2.32.0\\\\","native_build":true,"dependencies":[]}],"linux":[],"windows":[{"name":"cloud_firestore","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\cloud_firestore-4.17.5\\\\","native_build":true,"dependencies":["firebase_core"]},{"name":"firebase_core","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\firebase_core-2.32.0\\\\","native_build":true,"dependencies":[]}],"web":[{"name":"cloud_firestore_web","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\cloud_firestore_web-3.12.5\\\\","dependencies":["firebase_core_web"]},{"name":"firebase_core_web","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\firebase_core_web-2.18.2\\\\","dependencies":[]},{"name":"sensors_plus","path":"C:\\\\Users\\\\Sunny\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\sensors_plus-6.1.1\\\\","dependencies":[]}]},"dependencyGraph":[{"name":"cloud_firestore","dependencies":["cloud_firestore_web","firebase_core"]},{"name":"cloud_firestore_web","dependencies":["firebase_core","firebase_core_web"]},{"name":"firebase_core","dependencies":["firebase_core_web"]},{"name":"firebase_core_web","dependencies":[]},{"name":"flutter_compass","dependencies":[]},{"name":"sensors_plus","dependencies":[]}],"date_created":"2025-01-10 15:53:18.968782","version":"3.24.5","swift_package_manager_enabled":false}
\ No newline at end of file
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:sensors_plus/sensors_plus.dart';
import 'dart:async';
class QuestionScreen extends StatefulWidget {
final String gameCode;
......@@ -26,12 +28,17 @@ class _QuestionScreenState extends State<QuestionScreen> {
double? _correctAnswer;
int? _pointsGained;
List<DocumentSnapshot> questions = [];
bool _isBonusQuestion = false;
double _maxAcceleration = 0.0;
StreamSubscription<AccelerometerEvent>? _accelerometerSubscription;
@override
void initState() {
super.initState();
if (widget.isHost) {
_initializeQuestions();
} else {
_fetchQuestions();
}
}
......@@ -81,11 +88,32 @@ class _QuestionScreenState extends State<QuestionScreen> {
'status': 'playing',
'currentQuestionIndex': 0
});
_fetchQuestions();
} catch (e) {
debugPrint('Error initializing questions: $e');
}
}
Future<void> _fetchQuestions() async {
try {
final questionsSnapshot = await FirebaseFirestore.instance
.collection('games')
.doc(widget.gameCode)
.collection('questions')
.orderBy('createdAt')
.get();
setState(() {
questions = questionsSnapshot.docs;
});
debugPrint('Questions fetched: ${questions.length}');
} catch (e) {
debugPrint('Error fetching questions: $e');
}
}
Future<void> _submitAnswer() async {
if (_hasAnswered) return;
......@@ -147,7 +175,6 @@ class _QuestionScreenState extends State<QuestionScreen> {
int _calculateScore(double userAnswer, double correctAnswer) {
const maxScore = 100;
const decayRate = 0.1; // Adjust this value to control the rate of score decay
final distance = (userAnswer - correctAnswer).abs();
final score = (maxScore * (1 / (1 + decayRate * distance))).round();
......@@ -162,6 +189,11 @@ class _QuestionScreenState extends State<QuestionScreen> {
_wasCorrect = null;
_answerController.clear();
});
} else if (!_isBonusQuestion) {
setState(() {
_isBonusQuestion = true;
_startBonusQuestion();
});
} else {
// Mark player as finished
final playerRef = FirebaseFirestore.instance
......@@ -174,6 +206,7 @@ class _QuestionScreenState extends State<QuestionScreen> {
if (playerSnapshot.docs.isNotEmpty) {
await playerSnapshot.docs.first.reference.update({
'finished': true,
'bonusScore': _maxAcceleration,
});
}
......@@ -207,6 +240,25 @@ class _QuestionScreenState extends State<QuestionScreen> {
}
}
void _startBonusQuestion() {
_maxAcceleration = 0.0;
_accelerometerSubscription = accelerometerEvents.listen((event) {
final acceleration = event.x * event.x + event.y * event.y + event.z * event.z;
if (acceleration > _maxAcceleration) {
setState(() {
_maxAcceleration = acceleration;
});
}
});
Future.delayed(Duration(seconds: 5), () {
_accelerometerSubscription?.cancel();
setState(() {
_hasAnswered = true;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
......@@ -222,7 +274,8 @@ class _QuestionScreenState extends State<QuestionScreen> {
if (!snapshot.hasData || snapshot.data!.docs.isEmpty) {
return const Text('Score: 0');
}
final playerData = snapshot.data!.docs.first.data() as Map<String, dynamic>?; // Cast to Map<String, dynamic>
final playerData = snapshot.data!.docs.first.data() as Map<String, dynamic>?;
final score = playerData != null ? playerData['score'] ?? 0 : 0;
return Text('Score: $score');
},
......@@ -242,7 +295,7 @@ class _QuestionScreenState extends State<QuestionScreen> {
if (gameData == null) {
return const Center(child: Text('Error: Game data is null'));
}
if (gameData['status'] == 'finished') {
WidgetsBinding.instance.addPostFrameCallback((_) {
Navigator.pushReplacement(
......@@ -263,10 +316,26 @@ class _QuestionScreenState extends State<QuestionScreen> {
style: Theme.of(context).textTheme.titleLarge,
),
),
Text(
gameData['questions'][_currentQuestionIndex]['text'] as String,
style: Theme.of(context).textTheme.headlineMedium,
),
if (_isBonusQuestion)
Column(
children: [
Text(
'Bonus Question: Flap your wings as hard as you can (shake your phone fast) for 5 secs',
style: Theme.of(context).textTheme.headlineMedium,
),
Text(
'Max Acceleration: $_maxAcceleration',
style: Theme.of(context).textTheme.headlineMedium,
),
],
)
else if (questions.isNotEmpty)
Text(
questions[_currentQuestionIndex]['text'] as String,
style: Theme.of(context).textTheme.headlineMedium,
)
else
const Center(child: Text('No questions available')),
if (_wasCorrect != null)
Column(
children: [
......@@ -284,16 +353,17 @@ class _QuestionScreenState extends State<QuestionScreen> {
),
],
),
Padding(
padding: const EdgeInsets.all(16.0),
child: TextField(
controller: _answerController,
decoration: const InputDecoration(
labelText: 'Your Answer',
if (!_isBonusQuestion)
Padding(
padding: const EdgeInsets.all(16.0),
child: TextField(
controller: _answerController,
decoration: const InputDecoration(
labelText: 'Your Answer',
),
keyboardType: TextInputType.number,
),
keyboardType: TextInputType.number,
),
),
if (!_hasAnswered)
ElevatedButton(
onPressed: _submitAnswer,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment