@@ -5,17 +5,21 @@ import 'package:numbers/utils/Common.dart';
5
5
class LeaderboardService {
6
6
final String collection = 'leaderboard' ;
7
7
final String orderByKey = 'score' ;
8
+ final Map <String , int > leaderBoardTypes = {
9
+ 'overall' : 0 ,
10
+ 'weekly' : 7 ,
11
+ 'daily' : 1
12
+ };
8
13
9
14
setData (Map gameState) async {
10
15
Map newData = await this ._formatSetData (gameState);
11
16
Firestore .instance.collection (this .collection).document ().setData (newData);
12
17
}
13
18
14
19
_formatSetData (Map gameState) async {
15
-
16
20
String name = await SettingsStore ().getKey ('name' );
17
21
18
- if (name == null ){
22
+ if (name == null ) {
19
23
name = Common .getRandomName ();
20
24
}
21
25
@@ -30,28 +34,79 @@ class LeaderboardService {
30
34
};
31
35
}
32
36
33
- getData ({int limit = 6 }) async {
37
+ getData ({int limit = 6 , leaderboardType = 'overall' }) async {
34
38
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);
40
49
41
- return this ._formatGetData (resultData);
50
+ } else {
51
+ resultData = await _getOverallData (limit);
52
+ }
53
+
54
+ return this ._formatGetData (resultData, limit);
42
55
} catch (error) {
43
- print (error);
56
+ // print(error);
44
57
}
45
58
}
46
59
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
+ }
49
74
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) {
50
98
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;
54
109
}
55
- return formattedData ;
110
+ return list. sublist (start, end) ;
56
111
}
57
112
}
0 commit comments