Skip to content

Commit 8f18f11

Browse files
Merge pull request #15 from thamaraiselvam/daily_weekly_leaderboard
weekly and daily leaderboard added
2 parents 639bae5 + 655393e commit 8f18f11

File tree

6 files changed

+99
-32
lines changed

6 files changed

+99
-32
lines changed

lib/component/LeaderboardCard.dart

+15-5
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,31 @@ import 'package:numbers/widgets/dashedLine.dart';
55
import 'package:numbers/widgets/leadershipBoardTable.dart';
66

77
class LeaderboardCard extends StatefulWidget {
8+
final String title;
9+
final String type;
10+
final int limit;
11+
12+
const LeaderboardCard({Key key, this.title, this.type, this.limit}): super(key: key);
13+
14+
@override
815
_LeaderboardCardState createState() => _LeaderboardCardState();
916
}
1017

1118
class _LeaderboardCardState extends State<LeaderboardCard> {
12-
String title= 'Leaderboard';
1319
List scores = [];
1420

1521
@override
1622
void initState() {
1723
super.initState();
18-
_getRecentScore();
24+
_getScores();
25+
}
26+
27+
void _getScores() {
28+
this.getScore();
1929
}
2030

21-
void _getRecentScore() {
22-
LeaderboardService().getData().then((score) {
31+
void getScore(){
32+
LeaderboardService().getData(limit: widget.limit, leaderboardType: widget.type).then((score) {
2333
setState(() {
2434
this.scores = score;
2535
});
@@ -42,7 +52,7 @@ class _LeaderboardCardState extends State<LeaderboardCard> {
4252
Icons.show_chart,
4353
color: primaryColor,
4454
),
45-
title: Text(title,
55+
title: Text(widget.title,
4656
style: TextStyle(
4757
color: primaryColor, fontWeight: FontWeight.bold)),
4858
),

lib/component/RecentScoreCard.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class RecentScoreBoard extends StatefulWidget {
1212
class _RecentScoreBoardState extends State<RecentScoreBoard> {
1313
List recentScore = [];
1414
Map scoreCardMeta = {};
15-
String title = 'Recent Scores';
15+
String title = 'Your Recent Scores';
1616

1717
@override
1818
void initState() {

lib/screens/HomeScreen/HomeScreen.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ class _HomeScreenState extends State<HomeScreen> {
6565
],
6666
),
6767
),
68-
LeaderboardCard(),
68+
LeaderboardCard(title: "Today's Leaders", type: 'daily', limit: 6,),
69+
LeaderboardCard(title: "Leaders for Week", type: 'weekly', limit: 6,),
70+
LeaderboardCard(title: "All-Time Leaders", type: 'overall', limit: 6,),
6971
RecentScoreBoard(),
7072
],
7173
),

lib/screens/SplashScreen.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import 'package:flutter/material.dart';
44
import 'package:numbers/utils/constants.dart';
55

66
class SplashScreen extends StatefulWidget {
7+
78
const SplashScreen({Key key}) : super(key: key);
89

910
@override
1011
_SplashScreenState createState() => _SplashScreenState();
1112
}
12-
1313
class _SplashScreenState extends State<SplashScreen> {
1414

1515
Timer _timer;
@@ -19,7 +19,7 @@ class _SplashScreenState extends State<SplashScreen> {
1919

2020
loadData();
2121
}
22-
22+
2323

2424
Future<Timer> loadData() async {
2525
_timer = new Timer(Duration(seconds: 2), onDoneLoading);

lib/service/leaderboardService.dart

+71-16
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ import 'package:numbers/utils/Common.dart';
55
class 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
}

pubspec.lock

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ packages:
2121
name: async
2222
url: "https://pub.dartlang.org"
2323
source: hosted
24-
version: "2.2.0"
24+
version: "2.3.0"
2525
boolean_selector:
2626
dependency: transitive
2727
description:
2828
name: boolean_selector
2929
url: "https://pub.dartlang.org"
3030
source: hosted
31-
version: "1.0.4"
31+
version: "1.0.5"
3232
charcode:
3333
dependency: transitive
3434
description:
@@ -136,21 +136,21 @@ packages:
136136
name: meta
137137
url: "https://pub.dartlang.org"
138138
source: hosted
139-
version: "1.1.6"
139+
version: "1.1.7"
140140
path:
141141
dependency: transitive
142142
description:
143143
name: path
144144
url: "https://pub.dartlang.org"
145145
source: hosted
146-
version: "1.6.2"
146+
version: "1.6.4"
147147
pedantic:
148148
dependency: transitive
149149
description:
150150
name: pedantic
151151
url: "https://pub.dartlang.org"
152152
source: hosted
153-
version: "1.7.0"
153+
version: "1.8.0+1"
154154
petitparser:
155155
dependency: transitive
156156
description:
@@ -164,7 +164,7 @@ packages:
164164
name: quiver
165165
url: "https://pub.dartlang.org"
166166
source: hosted
167-
version: "2.0.3"
167+
version: "2.0.5"
168168
shared_preferences:
169169
dependency: "direct main"
170170
description:
@@ -204,7 +204,7 @@ packages:
204204
name: string_scanner
205205
url: "https://pub.dartlang.org"
206206
source: hosted
207-
version: "1.0.4"
207+
version: "1.0.5"
208208
term_glyph:
209209
dependency: transitive
210210
description:

0 commit comments

Comments
 (0)