Added getTopRatings(), moved constants into RatingCodes.

git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@451 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Michael Bayne
2007-10-15 19:28:20 +00:00
parent e8bd79d1e3
commit 1b7f810246
3 changed files with 65 additions and 14 deletions
@@ -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;
}
@@ -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.
*/
@@ -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<RatingRecord> getTopRatings (int gameId, int limit, IntSet playerIds)
throws PersistenceException
{
ArrayList<QueryClause> clauses = new ArrayList<QueryClause>();
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.