diff --git a/src/java/com/threerings/parlor/rating/data/RatingCodes.java b/src/java/com/threerings/parlor/rating/data/RatingCodes.java new file mode 100644 index 00000000..2f34ef2b --- /dev/null +++ b/src/java/com/threerings/parlor/rating/data/RatingCodes.java @@ -0,0 +1,37 @@ +// +// $Id$ +// +// Vilya library - tools for developing networked games +// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/vilya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.parlor.rating.data; + +/** + * Constants relating to the rating services. + */ +public interface RatingCodes +{ + /** The minimum rating value. */ + public static final int MINIMUM_RATING = 1000; + + /** The default rating value. */ + public static final int DEFAULT_RATING = 1200; + + /** The maximum rating value. */ + public static final int MAXIMUM_RATING = 3000; +} diff --git a/src/java/com/threerings/parlor/rating/server/RatingManagerDelegate.java b/src/java/com/threerings/parlor/rating/server/RatingManagerDelegate.java index d54da4ad..e0bbae22 100644 --- a/src/java/com/threerings/parlor/rating/server/RatingManagerDelegate.java +++ b/src/java/com/threerings/parlor/rating/server/RatingManagerDelegate.java @@ -50,6 +50,7 @@ import com.threerings.parlor.game.data.GameObject; import com.threerings.parlor.game.server.GameManager; import com.threerings.parlor.game.server.GameManagerDelegate; +import com.threerings.parlor.rating.data.RatingCodes; import com.threerings.parlor.rating.server.persist.RatingRecord; import com.threerings.parlor.rating.server.persist.RatingRepository; @@ -57,16 +58,8 @@ import com.threerings.parlor.rating.server.persist.RatingRepository; * Rates players after each game and handles persisting the results. */ public abstract class RatingManagerDelegate extends GameManagerDelegate + implements RatingCodes { - /** The minimum rating value. */ - public static final int MINIMUM_RATING = 1000; - - /** The default rating value. */ - public static final int DEFAULT_RATING = 1200; - - /** The maximum rating value. */ - public static final int MAXIMUM_RATING = 3000; - /** * Constructs a rating manager delegate. */ 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 83cf5db9..e3b50a42 100644 --- a/src/java/com/threerings/parlor/rating/server/persist/RatingRepository.java +++ b/src/java/com/threerings/parlor/rating/server/persist/RatingRepository.java @@ -31,6 +31,7 @@ import java.util.Set; import com.samskivert.io.PersistenceException; import com.samskivert.util.HashIntMap; import com.samskivert.util.IntMap; +import com.samskivert.util.IntSet; import com.samskivert.jdbc.depot.DepotRepository; import com.samskivert.jdbc.depot.EntityMigration; @@ -40,6 +41,7 @@ import com.samskivert.jdbc.depot.clause.Limit; import com.samskivert.jdbc.depot.clause.OrderBy; import com.samskivert.jdbc.depot.clause.QueryClause; import com.samskivert.jdbc.depot.clause.Where; +import com.samskivert.jdbc.depot.expression.SQLExpression; import com.samskivert.jdbc.depot.operator.Conditionals.*; import com.samskivert.jdbc.depot.operator.Logic.And; @@ -56,11 +58,6 @@ public class RatingRepository extends DepotRepository public RatingRepository (PersistenceContext ctx) { super(ctx); - - // TEMP - ctx.registerMigration(PercentileRecord.class, new EntityMigration.Retype(2, "data")); - ctx.registerMigration(PercentileRecord.class, new EntityMigration.Drop(3, "type")); - // END TEMP } /** @@ -115,6 +112,30 @@ public class RatingRepository extends DepotRepository return findAll(RatingRecord.class, clauses.toArray(new QueryClause[clauses.size()])); } + /** + * Returns the top-ratings for the specified game. Players with equal rating will be sorted + * most recently played first. + * + * @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) + 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)))); + } + 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()])); + } + /** * Set the rating and experience for a given player and game. This method will either update * or create a row.