Tidied things up, avoid doing things on the invoker thread that should be done
on the dobj thread. git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@408 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
@@ -27,11 +27,12 @@ import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import com.samskivert.io.PersistenceException;
|
||||
import com.samskivert.jdbc.RepositoryUnit;
|
||||
|
||||
import com.samskivert.util.ArrayIntSet;
|
||||
import com.samskivert.util.ArrayUtil;
|
||||
import com.samskivert.util.HashIntMap;
|
||||
import com.samskivert.util.IntMap;
|
||||
import com.samskivert.util.Invoker;
|
||||
|
||||
import com.threerings.crowd.data.PlaceObject;
|
||||
import com.threerings.crowd.server.CrowdServer;
|
||||
@@ -48,22 +49,15 @@ import com.threerings.util.Name;
|
||||
/**
|
||||
* Rates players after each game and handles persisting the results.
|
||||
*/
|
||||
public abstract class RatingManagerDelegate
|
||||
extends GameManagerDelegate
|
||||
public abstract class RatingManagerDelegate extends GameManagerDelegate
|
||||
{
|
||||
/**
|
||||
* The minimum rating value.
|
||||
*/
|
||||
/** The minimum rating value. */
|
||||
public static final int MINIMUM_RATING = 1000;
|
||||
|
||||
/**
|
||||
* The default rating value.
|
||||
*/
|
||||
/** The default rating value. */
|
||||
public static final int DEFAULT_RATING = 1200;
|
||||
|
||||
/**
|
||||
* The maximum rating value.
|
||||
*/
|
||||
/** The maximum rating value. */
|
||||
public static final int MAXIMUM_RATING = 3000;
|
||||
|
||||
/**
|
||||
@@ -73,19 +67,17 @@ public abstract class RatingManagerDelegate
|
||||
{
|
||||
super(gmgr);
|
||||
_gmgr = gmgr;
|
||||
|
||||
_repo = getRatingRepository();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override // from PlaceManagerDelegate
|
||||
public void didStartup (PlaceObject plobj)
|
||||
{
|
||||
super.didStartup(plobj);
|
||||
|
||||
_gobj = (GameObject) plobj;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override // from GameManagerDelegate
|
||||
public void gameWillStart ()
|
||||
{
|
||||
super.gameWillStart();
|
||||
@@ -93,31 +85,26 @@ public abstract class RatingManagerDelegate
|
||||
// note the time at which we started
|
||||
_startStamp = (int) (System.currentTimeMillis() / 1000);
|
||||
|
||||
// read ratings from persistent store
|
||||
CrowdServer.invoker.postUnit(new Invoker.Unit() {
|
||||
public boolean invoke () {
|
||||
int gameId = _gmgr.getGameConfig().getGameId();
|
||||
final int gameId = _gmgr.getGameConfig().getGameId();
|
||||
|
||||
// enumerate our non-guest players
|
||||
Integer[] allPlayers = new Integer[_gmgr.getPlayerCount()];
|
||||
Integer[] ratedPlayers = new Integer[allPlayers.length];
|
||||
int jj = 0;
|
||||
final Integer[] allPlayers = new Integer[_gmgr.getPlayerCount()];
|
||||
ArrayIntSet ratedSet = new ArrayIntSet();
|
||||
for (int ii = 0; ii < allPlayers.length; ii ++) {
|
||||
allPlayers[ii] = _gmgr.getPlayerPersistentId(_gmgr.getPlayer(ii));
|
||||
if (allPlayers[ii] != 0) {
|
||||
// this is a player with a persistent id, a player we can rate
|
||||
ratedPlayers[jj ++] = allPlayers[ii];
|
||||
ratedSet.add(allPlayers[ii]);
|
||||
}
|
||||
}
|
||||
ratedPlayers = ArrayUtil.splice(ratedPlayers, jj);
|
||||
final Integer[] ratedPlayers = ratedSet.toArray(new Integer[ratedSet.size()]);
|
||||
|
||||
try {
|
||||
// fetch the previous ratings of our persistent users, for this game
|
||||
List<RatingRecord> records = _repo.getRatings(gameId, ratedPlayers);
|
||||
|
||||
// and make it easy to look them up by their id
|
||||
// read ratings from persistent store
|
||||
CrowdServer.invoker.postUnit(new RepositoryUnit("loadRatings") {
|
||||
public void invokePersist () throws Exception {
|
||||
// fetch the previous ratings of our persistent users and map them by player id
|
||||
IntMap<RatingRecord> map = new HashIntMap<RatingRecord>();
|
||||
for (RatingRecord record : records) {
|
||||
for (RatingRecord record : _repo.getRatings(gameId, ratedPlayers)) {
|
||||
map.put(record.playerId, record);
|
||||
}
|
||||
|
||||
@@ -125,32 +112,30 @@ public abstract class RatingManagerDelegate
|
||||
_ratings = new Rating[allPlayers.length];
|
||||
for (int ii = 0; ii < _ratings.length; ii ++) {
|
||||
if (allPlayers[ii] == 0) {
|
||||
// for guests we let the slot remain null
|
||||
continue;
|
||||
continue; // for guests we let the slot remain null
|
||||
}
|
||||
RatingRecord record = map.get(allPlayers[ii]);
|
||||
// if the player had no previous record, initiate them at default values
|
||||
_ratings[ii] = record != null ?
|
||||
new Rating(allPlayers[ii], record.rating, record.experience) :
|
||||
new Rating(allPlayers[ii]);
|
||||
_ratings[ii] = (record == null) ? new Rating(allPlayers[ii]) :
|
||||
new Rating(allPlayers[ii], record.rating, record.experience);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
} catch (PersistenceException pe) {
|
||||
log.log(Level.WARNING,
|
||||
"Failed to load ratings [where=" + where() + ", id=" + gameId + "].", pe);
|
||||
return false;
|
||||
public void handleSuccess () {
|
||||
// stick our ratings into our manager
|
||||
_ratings = _tratings;
|
||||
}
|
||||
};
|
||||
|
||||
public void handleResult () {
|
||||
// nothing to do here?
|
||||
public void handleFailure (Exception e) {
|
||||
log.log(Level.WARNING, "Failed to load ratings [where=" + where() +
|
||||
", id=" + gameId + "].", e);
|
||||
}
|
||||
|
||||
protected Rating[] _tratings;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override // from GameManagerDelegate
|
||||
public void gameDidEnd ()
|
||||
{
|
||||
super.gameDidEnd();
|
||||
@@ -188,34 +173,28 @@ public abstract class RatingManagerDelegate
|
||||
|
||||
// else persist the result
|
||||
final int gameId = _gmgr.getGameConfig().getGameId();
|
||||
CrowdServer.invoker.postUnit(new Invoker.Unit() {
|
||||
public boolean invoke () {
|
||||
try {
|
||||
for (int ii = 0; ii < _ratings.length; ii ++) {
|
||||
if (_ratings[ii] != null) {
|
||||
// for each rated player, update or create the rating record
|
||||
// TODO: reorganize things so this can be a single db request?
|
||||
_repo.setRating(gameId, _ratings[ii].playerId,
|
||||
_ratings[ii].rating, _ratings[ii].experience);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
} catch (PersistenceException pe) {
|
||||
log.log(Level.WARNING,
|
||||
"Failed to load ratings [where=" + where() + ", id=" + gameId + "].", pe);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
public void handleResult () {
|
||||
// let subclasses store away the new ratings if they so desire
|
||||
CrowdServer.invoker.postUnit(new RepositoryUnit("updateRatings") {
|
||||
public void invokePersist () throws Exception {
|
||||
for (Rating rating : _ratings) {
|
||||
if (rating != null) {
|
||||
_repo.setRating(gameId, rating.playerId, rating.rating, rating.experience);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void handleSuccess () {
|
||||
for (int ii = 0; ii < _ratings.length; ii++) {
|
||||
if (_ratings[ii] != null) {
|
||||
// let subclasses publish the new ratings if they so desire
|
||||
updateRatingInMemory(gameId, _gmgr.getPlayerName(ii), _ratings[ii]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void handleFailure (Exception e) {
|
||||
log.log(Level.WARNING, "Failed to update ratings [where=" + where() +
|
||||
", id=" + gameId + "].", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -285,6 +264,23 @@ public abstract class RatingManagerDelegate
|
||||
return MathUtil.bound(MINIMUM_RATING, nrat, MAXIMUM_RATING);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the minimum time (in seconds) a game must've lasted for it to count towards rating.
|
||||
*/
|
||||
protected abstract int minimumRatedDuration ();
|
||||
|
||||
/**
|
||||
* Return a reference to the {@link RatingRepository} instance we should use to persist.
|
||||
*/
|
||||
protected abstract RatingRepository getRatingRepository ();
|
||||
|
||||
/**
|
||||
* Optionally store update ratings in memory e.g. in the user object.
|
||||
*
|
||||
* This method is called on the dobj thread.
|
||||
*/
|
||||
protected abstract void updateRatingInMemory (int gameId, Name playerName, Rating rating);
|
||||
|
||||
/**
|
||||
* Simply encapsulates the rating/experience tuple representing a player's rating for a game.
|
||||
*/
|
||||
@@ -320,24 +316,6 @@ public abstract class RatingManagerDelegate
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the minimum time (in seconds) a game must've lasted for it to count towards rating.
|
||||
*/
|
||||
protected abstract int minimumRatedDuration ();
|
||||
|
||||
/**
|
||||
* Return a reference to the {@link RatingRepository} instance we should use to persist.
|
||||
*/
|
||||
protected abstract RatingRepository getRatingRepository ();
|
||||
|
||||
/**
|
||||
* Optionally store update ratings in memory e.g. in the user object.
|
||||
*
|
||||
* This method is called on the dobj thread.
|
||||
*/
|
||||
protected abstract void updateRatingInMemory (int gameId, Name playerName, Rating rating);
|
||||
|
||||
|
||||
/** An appropriately casted reference to our GameManager. */
|
||||
protected GameManager _gmgr;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user