couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Davis <paul.joseph.da...@gmail.com>
Subject Re: Looking for help/ideas on how to fetch a Rank from a CouchDB based Highscores system
Date Wed, 26 Aug 2009 22:59:33 GMT
> First, change your scores to a numeric instead of a string to make math easier.
>
> map:
> function(doc) {
>    emit(doc.score, doc.interesting_info);
> }
>
> getRank(score):
>    SCORE = user_score+min_score_increment
>    results = GET("http://127.0.0.1:5984/score_db/_design/high_scores/_view/find_range?startkey=$(SCORE)")
>    return results.offset
>
> HTH,
> Paul Davis
>

Whoops! Totally forgot that we wanted this descending.

getRank(score):
    results = GET("http://127.0.0.1:5984/score_db/_design/high_scores/_view/find_rank?startkey=$(score)&descending=true")
    return results.total_rows - results.offset + 1

Pretty sure that math is right. Also, I didn't double check that
offsets are calculated in reverse from the beginning. Ie, for
?descending=true&limit=0, offset==total_rows I don't think we do the
math inside CouchDB, but I'm too distracted to look. It'd be a simple
test.

HTH,
Paul Davis

Mime
View raw message