From ec11abe2f142c45240fe9ff172aa13a2e089019f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Sat, 4 Sep 2010 02:03:44 -0400 Subject: Add scorePoints to Quotes. ScorePoints is used to order by score on the quote site. See Quote for the formula. --- src/lq/ImportQuotes.java | 3 +-- src/lq/Quote.java | 13 ++++++++++--- src/lq/QuoteUtil.java | 9 ++++++++- 3 files changed, 19 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/lq/ImportQuotes.java b/src/lq/ImportQuotes.java index d750b7b..634521c 100644 --- a/src/lq/ImportQuotes.java +++ b/src/lq/ImportQuotes.java @@ -150,8 +150,7 @@ public class ImportQuotes extends HttpServlet { Vote vote = new Vote(quoteId, rating, ip); vote.setTimestamp(timestamp); - quote.setSumVotes(quote.getSumVotes() + rating); - quote.setNumVotes(quote.getNumVotes() + 1); + QuoteUtil.addVote(quote, vote); return vote; } diff --git a/src/lq/Quote.java b/src/lq/Quote.java index f4dfde0..c859f25 100644 --- a/src/lq/Quote.java +++ b/src/lq/Quote.java @@ -37,10 +37,14 @@ public class Quote { private String ip; @Persistent - private double sumVotes; + private Double sumVotes; @Persistent - private int numVotes; + private Integer numVotes; + + @Persistent + // sum [ (rating-3) * abs((rating-3)) | rating <- votes ] + private Double scorePoints; public Quote(Date quoteDate, String author, String content, String ip) { this.quoteDate = quoteDate; @@ -49,6 +53,7 @@ public class Quote { this.ip = ip; this.sumVotes = 0.0; this.numVotes = 0; + this.scorePoints = 0.0; this.timestamp = new Date(); } @@ -64,7 +69,8 @@ public class Quote { public String getContent() { return content.getValue(); } public String getIp() { return ip; } public Double getSumVotes() { return sumVotes; } - public int getNumVotes() { return numVotes; } + public Integer getNumVotes() { return numVotes; } + public Double getScorePoints() { return scorePoints; } public void setId(Long id) { this.id = id; } public void setTimestamp(Date timestamp) { this.timestamp = timestamp; } @@ -75,4 +81,5 @@ public class Quote { public void setIp(String ip) { this.ip = ip; } public void setSumVotes(double sumVotes) { this.sumVotes = sumVotes; } public void setNumVotes(int numVotes) { this.numVotes = numVotes; } + public void setScorePoints(double scorePoints) { this.scorePoints = scorePoints; } } diff --git a/src/lq/QuoteUtil.java b/src/lq/QuoteUtil.java index 72b94af..37f857b 100644 --- a/src/lq/QuoteUtil.java +++ b/src/lq/QuoteUtil.java @@ -72,7 +72,7 @@ public class QuoteUtil { Collections.sort(quotes, new Comparator() { public int compare(Quote q1, Quote q2) { - throw new RuntimeException("Score ordering not yet implemented"); + return Doubles.signum(q2.getScorePoints() - q1.getScorePoints()); } }); return quotes; @@ -124,4 +124,11 @@ public class QuoteUtil { pm.close(); } } + + public static void addVote(Quote quote, Vote vote) { + quote.setSumVotes(quote.getSumVotes() + vote.getRating()); + quote.setNumVotes(quote.getNumVotes() + 1); + double scorePoints = (vote.getRating()-2.5) * Math.abs((vote.getRating()-2.5)); + quote.setScorePoints(quote.getScorePoints() + scorePoints); + } } -- cgit v1.2.3