@@ -3,6 +3,7 @@ import 'package:numbers/store/BestScore.dart';
33import 'package:numbers/store/RecentScoreStore.dart' ;
44import 'package:numbers/schema/BlockSchema.dart' ;
55import 'package:numbers/screens/GameScreen/summaryModel.dart' ;
6+ import 'package:numbers/utils/Config.dart' ;
67import 'package:numbers/widgets/bgGradient.dart' ;
78import 'package:numbers/provider/BlockDataStream.dart' ;
89import 'package:flutter/material.dart' ;
@@ -20,9 +21,16 @@ class _GameScreenState extends State<GameScreen> {
2021 List <BlockSchema > blocks;
2122 int currentTotal;
2223 Timer gameTimerObject;
23- int secCounter = 5 ;
24+ int secCounter = gameDuration ;
2425 bool isTimeUp = false ;
25- Map <String , int > gameHistory = {"total" : 0 , "success" : 0 , "fail" : 0 };
26+ Map <String , int > gameHistory = {
27+ "total" : 0 ,
28+ "success" : 0 ,
29+ "fail" : 0 ,
30+ "score" : 0 ,
31+ "selectedBlocks" : 0 ,
32+ "tmpSelectedBlocks" : 0
33+ };
2634 BlockDataStream blockDataStream = BlockDataStream ();
2735
2836 @override
@@ -66,18 +74,30 @@ class _GameScreenState extends State<GameScreen> {
6674 setState (() {
6775 this .secCounter-- ;
6876 if (this .secCounter < 1 ) {
69- this .isTimeUp = true ;
70- this .gameTimerObject.cancel ();
71- showSummary (context, gameHistory);
72- updateScores ();
77+ this .postGameWorks ();
7378 }
7479 });
7580 });
7681 }
7782
78- void updateScores (){
79- RecentScoreStore ().updateRecentScore (gameHistory['success' ]);
80- BestScoreStore ().updateScore (gameHistory['success' ]);
83+ void postGameWorks () {
84+ this .isTimeUp = true ;
85+ this .gameTimerObject.cancel ();
86+ showSummary (context, gameHistory);
87+ updateScores ();
88+ }
89+
90+ void calculateScore () {
91+ int score = (this .gameHistory['selectedBlocks' ] * costs['block' ]) +
92+ (this .gameHistory['success' ] * costs['success' ]) +
93+ (this .gameHistory['fail' ] * costs['fail' ]);
94+ this .gameHistory['score' ] = score > 0 ? (score) : 0 ;
95+ print (this .gameHistory);
96+ }
97+
98+ void updateScores () {
99+ RecentScoreStore ().updateRecentScore (gameHistory['score' ]);
100+ BestScoreStore ().updateScore (gameHistory['score' ]);
81101 }
82102
83103 void fillBlocksData () {
@@ -109,6 +129,8 @@ class _GameScreenState extends State<GameScreen> {
109129
110130 this .blocks[selectedIndex].isSelected = true ;
111131
132+ this .gameHistory['tmpSelectedBlocks' ]++ ;
133+
112134 this .currentTotal += blockData['value' ];
113135
114136 if (this .currentTotal < this .blockSchema.target) {
@@ -126,13 +148,18 @@ class _GameScreenState extends State<GameScreen> {
126148
127149 void _wrongAnswer (selectedIndex) {
128150 this .gameHistory['fail' ]++ ;
151+ this .gameHistory['tmpSelectedBlocks' ] = 0 ; //reset
152+ calculateScore ();
129153 _changeBlockColor (selectedIndex, Colors .red);
130154 _showStatusAlert ('Wrong !!!' , Icons .clear, Colors .red, false );
131155 _closePopUpAndShuffle ();
132156 }
133157
134158 void _correctAnswer (selectedIndex) {
135159 this .gameHistory['success' ]++ ;
160+ this .gameHistory['selectedBlocks' ] += this .gameHistory['tmpSelectedBlocks' ];
161+ this .gameHistory['tmpSelectedBlocks' ] = 0 ; //reset
162+ calculateScore ();
136163 _changeBlockColor (selectedIndex, Colors .green);
137164 _showStatusAlert ('Correct !!!' , Icons .check, Colors .green, true );
138165 _closePopUpAndShuffle ();
0 commit comments