@@ -5,17 +5,21 @@ import 'package:numbers/utils/Common.dart';
55class LeaderboardService {
66 final String collection = 'leaderboard' ;
77 final String orderByKey = 'score' ;
8+ final Map <String , int > leaderBoardTypes = {
9+ 'overall' : 0 ,
10+ 'weekly' : 7 ,
11+ 'daily' : 1
12+ };
813
914 setData (Map gameState) async {
1015 Map newData = await this ._formatSetData (gameState);
1116 Firestore .instance.collection (this .collection).document ().setData (newData);
1217 }
1318
1419 _formatSetData (Map gameState) async {
15-
1620 String name = await SettingsStore ().getKey ('name' );
1721
18- if (name == null ){
22+ if (name == null ) {
1923 name = Common .getRandomName ();
2024 }
2125
@@ -30,28 +34,79 @@ class LeaderboardService {
3034 };
3135 }
3236
33- getData ({int limit = 6 }) async {
37+ getData ({int limit = 6 , leaderboardType = 'overall' }) async {
3438 try {
35- var resultData = await Firestore .instance
36- .collection (this .collection)
37- .orderBy (this .orderByKey, descending: true )
38- .limit (limit)
39- .getDocuments ();
39+ int daysToSubtract = this .leaderBoardTypes[leaderboardType];
40+ var resultData;
41+
42+ if (daysToSubtract != 0 ) {
43+ String lastWeekTimeStamp = DateTime .now ()
44+ .subtract (Duration (days: daysToSubtract))
45+ .millisecondsSinceEpoch
46+ .toString ();
47+
48+ resultData = await _getDataByTimeCondition (lastWeekTimeStamp);
4049
41- return this ._formatGetData (resultData);
50+ } else {
51+ resultData = await _getOverallData (limit);
52+ }
53+
54+ return this ._formatGetData (resultData, limit);
4255 } catch (error) {
43- print (error);
56+ // print(error);
4457 }
4558 }
4659
47- _formatGetData (resultData) {
48- List formattedData = [];
60+ _getDataByTimeCondition (String daysToSubtract) async {
61+ return await Firestore .instance
62+ .collection (this .collection)
63+ .where ("time" , isGreaterThanOrEqualTo: daysToSubtract)
64+ .getDocuments ();
65+ }
66+
67+ _getOverallData (limit) async {
68+ return await Firestore .instance
69+ .collection (this .collection)
70+ .orderBy (this .orderByKey, descending: true )
71+ .limit (limit)
72+ .getDocuments ();
73+ }
4974
75+ _formatGetData (collection, limit) {
76+ List list = this ._formList (collection);
77+ _sortList (list);
78+ list = _addRank (list);
79+ return _splitList (list, 0 , limit);
80+ }
81+
82+ List _formList (collection) {
83+ List list = [];
84+ for (var document in collection.documents) {
85+ list.add (document.data);
86+ }
87+
88+ return list;
89+ }
90+
91+ _sortList (List documents) {
92+ documents.sort ((m1, m2) {
93+ return m2["score" ].compareTo (m1["score" ]);
94+ });
95+ }
96+
97+ List _addRank (List documents) {
5098 int counter = 0 ;
51- for (var document in resultData.documents) {
52- document.data['rank' ] = ++ counter;
53- formattedData.add (document.data);
99+ for (var document in documents) {
100+ document['rank' ] = ++ counter;
101+ }
102+
103+ return documents;
104+ }
105+
106+ List _splitList (List list, int start, int end) {
107+ if (list.length < end) {
108+ end = list.length;
54109 }
55- return formattedData ;
110+ return list. sublist (start, end) ;
56111 }
57112}
0 commit comments