From 24455b4ab8b9124b7d2035d03bdb467cf496ae22 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 2 Sep 2008 19:34:54 +0000 Subject: [PATCH] Allow filtering of top-ratings list by recency of play. git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@737 c613c5cb-e716-0410-b11b-feb51c14d237 --- .../server/persist/RatingRepository.java | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/java/com/threerings/parlor/rating/server/persist/RatingRepository.java b/src/java/com/threerings/parlor/rating/server/persist/RatingRepository.java index a45564df..2795035f 100644 --- a/src/java/com/threerings/parlor/rating/server/persist/RatingRepository.java +++ b/src/java/com/threerings/parlor/rating/server/persist/RatingRepository.java @@ -28,8 +28,9 @@ import java.util.Collections; import java.util.List; import java.util.Set; -import com.google.inject.Singleton; +import com.google.common.collect.Lists; import com.google.inject.Inject; +import com.google.inject.Singleton; import com.samskivert.io.PersistenceException; import com.samskivert.util.IntSet; @@ -110,31 +111,35 @@ public class RatingRepository extends DepotRepository clauses.add(new Limit(0, count)); } clauses.add(OrderBy.descending(RatingRecord.LAST_UPDATED_C)); - return findAll(RatingRecord.class, clauses.toArray(new QueryClause[clauses.size()])); + return findAll(RatingRecord.class, clauses); } /** * Returns the top-ratings for the specified game. Players with equal rating will be sorted * most recently played first. * + * @param since an absolute number of milliseconds (ie. 10*24*60*60*1000L). Players that have + * not updated their rating within this many milliseconds in the past will be omitted from the + * results. Supply zero to omit this filter. * @param playerIds an optional list of player ids to which to limit the top-rankings search. */ - public List getTopRatings (int gameId, int limit, IntSet playerIds) + public List getTopRatings (int gameId, int limit, long since, IntSet playerIds) throws PersistenceException { - ArrayList clauses = new ArrayList(); - if (playerIds == null) { - clauses.add(new Where(RatingRecord.GAME_ID_C, gameId)); - } else { - Integer[] pids = playerIds.toArray(new Integer[playerIds.size()]); - clauses.add(new Where(new And(new Equals(RatingRecord.GAME_ID_C, gameId), - new In(RatingRecord.PLAYER_ID_C, pids)))); + List where = Lists.newArrayList(); + where.add(new Equals(RatingRecord.GAME_ID_C, gameId)); + if (since > 0L) { + where.add(new GreaterThan(RatingRecord.LAST_UPDATED_C, + new Timestamp(System.currentTimeMillis() - since))); } - clauses.add(new Limit(0, limit)); - clauses.add(new OrderBy( - new SQLExpression[] { RatingRecord.RATING_C, RatingRecord.LAST_UPDATED_C }, - new OrderBy.Order[] { OrderBy.Order.DESC, OrderBy.Order.DESC })); - return findAll(RatingRecord.class, clauses.toArray(new QueryClause[clauses.size()])); + if (playerIds != null) { + where.add(new In(RatingRecord.PLAYER_ID_C, playerIds)); + } + + OrderBy ob = new OrderBy( + new SQLExpression[] { RatingRecord.RATING_C, RatingRecord.LAST_UPDATED_C }, + new OrderBy.Order[] { OrderBy.Order.DESC, OrderBy.Order.DESC }); + return findAll(RatingRecord.class, new Where(new And(where)), new Limit(0, limit), ob); } /**