Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Logic added to cope with cases where no such user is present in leade…

…r board
  • Loading branch information...
commit 8697b94908a6beceb2784f9398141a1894945026 1 parent 9db1cda
@sneakybeaky sneakybeaky authored czarneckid committed
View
86 src/main/java/com/agoragames/leaderboard/Leaderboard.java
@@ -1,15 +1,10 @@
package com.agoragames.leaderboard;
-import java.util.ArrayList;
-import java.util.Hashtable;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.Transaction;
-import redis.clients.jedis.TransactionBlock;
public class Leaderboard {
@@ -17,8 +12,9 @@
public static final int DEFAULT_PAGE_SIZE = 25;
public static final String DEFAULT_REDIS_HOST = "localhost";
public static final int DEFAULT_REDIS_PORT = 6379;
+ public static final List<LeaderData> EMPTY_LEADER_DATA = Collections.emptyList();
- private Jedis _jedis;
+ private Jedis _jedis;
private String _leaderboardName;
private int _pageSize;
@@ -209,21 +205,23 @@ public long rankMemberIn(String leaderboardName, String member, double score) {
/**
* Retrieve the score for a member in the current leaderboard
*
- * @param member Member
- * @return Member score
+ *
+ * @param member Member
+ * @return Member score
*/
- public double scoreFor(String member) {
+ public Double scoreFor(String member) {
return scoreForIn(_leaderboardName, member);
}
/**
* Retrieve the score for a member in the named leaderboard
*
- * @param leaderboardName Leaderboard
- * @param member Member
- * @return Member score
+ *
+ * @param leaderboardName Leaderboard
+ * @param member Member
+ * @return Member score
*/
- public double scoreForIn(String leaderboardName, String member) {
+ public Double scoreForIn(String leaderboardName, String member) {
return _jedis.zscore(leaderboardName, member);
}
@@ -274,28 +272,39 @@ public boolean checkMemberIn(String leaderboardName, String member) {
/**
* Retrieve the rank for a member in the current leaderboard
*
- * @param member Member
- * @param useZeroIndexForRank Use zero-based index for rank
- * @return Rank for member in the current leaderboard
+ *
+ * @param member Member
+ * @param useZeroIndexForRank Use zero-based index for rank
+ * @return Rank for member in the current leaderboard
*/
- public long rankFor(String member, boolean useZeroIndexForRank) {
+ public Long rankFor(String member, boolean useZeroIndexForRank) {
return rankForIn(_leaderboardName, member, useZeroIndexForRank);
}
/**
* Retrieve the rank for a member in the named leaderboard
*
- * @param leaderboardName Leaderboard
- * @param member Member
- * @param useZeroIndexForRank Use zero-based index for rank
- * @return Rank for member in the named leaderboard
- */
- public long rankForIn(String leaderboardName, String member, boolean useZeroIndexForRank) {
- if (useZeroIndexForRank) {
- return _jedis.zrevrank(leaderboardName, member);
- } else {
- return (_jedis.zrevrank(leaderboardName, member) + 1);
- }
+ *
+ * @param leaderboardName Leaderboard
+ * @param member Member
+ * @param useZeroIndexForRank Use zero-based index for rank
+ * @return Rank for member in the named leaderboard
+ */
+ public Long rankForIn(String leaderboardName, String member, boolean useZeroIndexForRank) {
+
+ Long result = null;
+
+ Long redisRank = _jedis.zrevrank(leaderboardName, member);
+
+ if (redisRank != null) {
+ if (useZeroIndexForRank) {
+ result = redisRank;
+ } else {
+ result = (redisRank + 1);
+ }
+ }
+
+ return result;
}
/**
@@ -424,13 +433,18 @@ public long removeMembersInScoreRangeIn(String leaderboardName, double minScore,
* @return Leaders around a given member in the named leaderboard as a list of LeaderData
*/
public List<LeaderData> aroundMeIn(String leaderboardName, String member, boolean useZeroIndexForRank, int pageSize) {
- long reverseRankForMember = _jedis.zrevrank(leaderboardName, member);
+ Long reverseRankForMember = _jedis.zrevrank(leaderboardName, member);
+
+ if (reverseRankForMember == null) {
+ return EMPTY_LEADER_DATA;
+ }
+
if (pageSize < 1) {
pageSize = DEFAULT_PAGE_SIZE;
}
- int startingOffset = (int) reverseRankForMember - (pageSize / 2);
+ int startingOffset = reverseRankForMember.intValue() - (pageSize / 2);
if (startingOffset < 0) {
startingOffset = 0;
}
@@ -465,8 +479,14 @@ public long removeMembersInScoreRangeIn(String leaderboardName, double minScore,
Iterator<String> membersIterator = members.iterator();
while (membersIterator.hasNext()) {
String member = membersIterator.next();
- LeaderData memberData = new LeaderData(member, scoreForIn(leaderboardName, member), rankForIn(leaderboardName, member, useZeroIndexForRank));
- leaderData.add(memberData);
+ Double score = scoreForIn(leaderboardName, member);
+
+ if (score != null) {
+
+ long rank = rankForIn(leaderboardName, member, useZeroIndexForRank);
+ LeaderData memberData = new LeaderData(member, score, rank);
+ leaderData.add(memberData);
+ }
}
return leaderData;
View
160 src/test/java/com/agoragames/leaderboard/LeaderboardTest.java
@@ -1,11 +1,8 @@
package com.agoragames.leaderboard;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.Hashtable;
-import java.util.Iterator;
import java.util.List;
-import java.util.ListIterator;
import junit.framework.TestCase;
import redis.clients.jedis.Jedis;
@@ -14,17 +11,17 @@
private Jedis _jedis;
private Leaderboard _leaderboard;
-
+
protected void setUp() throws Exception {
super.setUp();
-
+
_leaderboard = new Leaderboard("name");
_jedis = new Jedis(Leaderboard.DEFAULT_REDIS_HOST, Leaderboard.DEFAULT_REDIS_PORT);
}
protected void tearDown() throws Exception {
super.tearDown();
-
+
_jedis.flushDB();
_leaderboard.disconnect();
_jedis.disconnect();
@@ -33,102 +30,111 @@ protected void tearDown() throws Exception {
public void testVersion() {
assertEquals("2.0.0", Leaderboard.VERSION);
}
-
+
public void testGetLeaderboardName() {
assertEquals("name", _leaderboard.getLeaderboardName());
}
-
+
public void testDeleteLeaderboard() {
rankMembersInLeaderboard(5);
assertTrue(_jedis.exists("name"));
_leaderboard.deleteLeaderboard();
- assertFalse(_jedis.exists("name"));
+ assertFalse(_jedis.exists("name"));
}
-
+
public void testSetPageSize() {
- _leaderboard.setPageSize(10);
+ _leaderboard.setPageSize(10);
assertEquals(10, _leaderboard.getPageSize());
-
+
_leaderboard.setPageSize(0);
assertEquals(Leaderboard.DEFAULT_PAGE_SIZE, _leaderboard.getPageSize());
}
-
+
public void testRankMemberAndTotalMembers() {
rankMembersInLeaderboard(5);
-
+
assertEquals(5, _leaderboard.totalMembers());
}
-
+
public void testTotalPages() {
rankMembersInLeaderboard(Leaderboard.DEFAULT_PAGE_SIZE + 2);
-
+
assertEquals(2, _leaderboard.totalPages());
}
-
+
public void testTotalMembersInScoreRange() {
rankMembersInLeaderboard(5);
-
+
assertEquals(3, _leaderboard.totalMembersInScoreRange(2, 4));
}
-
+
public void testScoreFor() {
- _leaderboard.rankMember("member", 76);
- assertEquals(76, (int) _leaderboard.scoreFor("member"));
+ Double expectedScore = (double) 76;
+ _leaderboard.rankMember("member", expectedScore);
+ assertEquals(expectedScore, _leaderboard.scoreFor("member"));
}
-
+
+ public void testScoreForNoSuchMember() {
+ assertNull("No such member in leader board, so should have a null result",_leaderboard.scoreFor("member"));
+ }
+
public void testChangeScoreFor() {
_leaderboard.rankMember("member", 5);
- assertEquals(5, (int) _leaderboard.scoreFor("member"));
-
+ assertEquals((double) 5, _leaderboard.scoreFor("member"));
+
_leaderboard.changeScoreFor("member", 5);
- assertEquals(10, (int) _leaderboard.scoreFor("member"));
-
+ assertEquals((double) 10, _leaderboard.scoreFor("member"));
+
_leaderboard.changeScoreFor("member", -5);
- assertEquals(5, (int) _leaderboard.scoreFor("member"));
+ assertEquals((double) 5, _leaderboard.scoreFor("member"));
}
-
+
public void testCheckMember() {
rankMembersInLeaderboard(5);
-
+
assertEquals(true, _leaderboard.checkMember("member_1"));
assertEquals(false, _leaderboard.checkMember("member_8"));
}
-
+
public void testRankFor() {
- rankMembersInLeaderboard(5);
-
- assertEquals(2, _leaderboard.rankFor("member_4", false));
- assertEquals(1, _leaderboard.rankFor("member_4", true));
- }
-
+ rankMembersInLeaderboard(5);
+
+ assertEquals(2, (long)_leaderboard.rankFor("member_4", false));
+ assertEquals(1, (long)_leaderboard.rankFor("member_4", true));
+ }
+
+ public void testRankForNoSuchMember() {
+ assertNull("No such member in leader board so should return a null result",_leaderboard.rankFor("no_such_member", false));
+ }
+
public void testRemoveMembersInScoreRange() {
rankMembersInLeaderboard(5);
-
+
assertEquals(5, _leaderboard.totalMembers());
-
+
_leaderboard.rankMember("cheater_1", 100);
_leaderboard.rankMember("cheater_2", 101);
_leaderboard.rankMember("cheater_3", 102);
-
+
assertEquals(8, _leaderboard.totalMembers());
-
+
_leaderboard.removeMembersInScoreRange(100, 102);
assertEquals(5, _leaderboard.totalMembers());
}
-
+
public void testScoreAndRankFor() {
rankMembersInLeaderboard(5);
-
+
Hashtable<String, Object> data = _leaderboard.scoreAndRankFor("member_1", false);
-
+
assertEquals("member_1", data.get("member"));
- assertEquals(1.0, ((Double) data.get("score")).doubleValue());
+ assertEquals(1.0, data.get("score"));
assertEquals(5, ((Long) data.get("rank")).longValue());
}
-
+
public void testLeadersIn() {
rankMembersInLeaderboard(25);
-
+
List<LeaderData> leaders = _leaderboard.leadersIn(1, false);
assertEquals(25, leaders.size());
assertEquals("member_25", leaders.get(0).getMember());
@@ -136,10 +142,10 @@ public void testLeadersIn() {
assertEquals(1, (int) leaders.get(leaders.size() - 1).getScore());
assertEquals(25, leaders.get(leaders.size() - 1).getRank());
}
-
+
public void testLeadersWithMultiplePages() {
rankMembersInLeaderboard(Leaderboard.DEFAULT_PAGE_SIZE * 3 + 1);
-
+
assertEquals(Leaderboard.DEFAULT_PAGE_SIZE * 3 + 1, _leaderboard.totalMembers());
List<LeaderData> leaders = _leaderboard.leadersIn(1, false);
@@ -153,49 +159,81 @@ public void testLeadersWithMultiplePages() {
leaders = _leaderboard.leadersIn(4, false);
assertEquals(1, leaders.size());
-
+
leaders = _leaderboard.leadersIn(_leaderboard.getLeaderboardName(), 1, false, 10);
assertEquals(10, leaders.size());
}
-
+
public void testAroundMe() {
rankMembersInLeaderboard(Leaderboard.DEFAULT_PAGE_SIZE * 3 + 1);
-
+
assertEquals(Leaderboard.DEFAULT_PAGE_SIZE * 3 + 1, _leaderboard.totalMembers());
-
+
List<LeaderData> leadersAroundMe = _leaderboard.aroundMe("member_30", false);
assertEquals(_leaderboard.getPageSize() / 2, leadersAroundMe.size() / 2);
-
+
leadersAroundMe = _leaderboard.aroundMe("member_1", false);
assertEquals(_leaderboard.getPageSize() / 2 + 1, leadersAroundMe.size());
-
+
leadersAroundMe = _leaderboard.aroundMe("member_76", false);
assertEquals(_leaderboard.getPageSize() / 2, leadersAroundMe.size() / 2);
}
-
+
+ public void testAroundMeNoSuchMember() {
+
+ rankMembersInLeaderboard(Leaderboard.DEFAULT_PAGE_SIZE);
+
+ List<LeaderData> leadersAroundMe = _leaderboard.aroundMe("no_such_member", false);
+ assertEquals("No such member in the leaderboard, so should have an empty list returned",0,leadersAroundMe.size());
+ }
+
public void testRankedInList() {
rankMembersInLeaderboard(Leaderboard.DEFAULT_PAGE_SIZE);
-
+
assertEquals(Leaderboard.DEFAULT_PAGE_SIZE, _leaderboard.totalMembers());
-
+
List<String> members = new ArrayList<String>();
members.add("member_1");
members.add("member_5");
members.add("member_10");
-
+
List<LeaderData> rankedMembers = _leaderboard.rankedInList(members, false);
assertEquals(3, rankedMembers.size());
-
+
assertEquals(25, rankedMembers.get(0).getRank());
assertEquals(1.0, rankedMembers.get(0).getScore());
-
+
assertEquals(21, rankedMembers.get(1).getRank());
assertEquals(5.0, rankedMembers.get(1).getScore());
assertEquals(16, rankedMembers.get(2).getRank());
assertEquals(10.0, rankedMembers.get(2).getScore());
}
-
+
+ public void testRankedInListNoSuchMember() {
+ rankMembersInLeaderboard(Leaderboard.DEFAULT_PAGE_SIZE);
+
+ assertEquals(Leaderboard.DEFAULT_PAGE_SIZE, _leaderboard.totalMembers());
+
+ List<String> members = new ArrayList<String>();
+ members.add("member_1");
+ members.add("member_5");
+ members.add("member_10");
+ members.add("member_" + (Leaderboard.DEFAULT_PAGE_SIZE+1));
+
+ List<LeaderData> rankedMembers = _leaderboard.rankedInList(members, false);
+ assertEquals(3, rankedMembers.size());
+
+ assertEquals(25, rankedMembers.get(0).getRank());
+ assertEquals(1.0, rankedMembers.get(0).getScore());
+
+ assertEquals(21, rankedMembers.get(1).getRank());
+ assertEquals(5.0, rankedMembers.get(1).getScore());
+
+ assertEquals(16, rankedMembers.get(2).getRank());
+ assertEquals(10.0, rankedMembers.get(2).getScore());
+ }
+
private void rankMembersInLeaderboard(int totalMembers) {
for (int i = 1; i <= totalMembers; i++) {
_leaderboard.rankMember("member_" + i, i);
Please sign in to comment.
Something went wrong with that request. Please try again.