Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Score
- include MongoMapper::Document
- key :user_id, ObjectId, :index => true
- key :stage, String, :index => true
- key :score, Integer
- timestamps!
- def self.high_score_map
- <<-MAP
- function() {
- emit(this.stage, { user_id : this.user_id, score: this.score });
- }
- MAP
- end
- def self.high_score_reduce
- <<-REDUCE
- function(k, v) {
- var top = 0;
- var user_id = '';
- for (var i in v) {
- if (v[i]["score"] > top) {
- top = v[i]["score"];
- user_id = v[i]["user_id"];
- }
- }
- return { user_id: user_id, score: top };
- }
- REDUCE
- end
- def self.high_score_build(opts)
- Score.collection.map_reduce(high_score_map, high_score_reduce, opts).find()
- end
- def self.high_score(opts={})
- data = []
- Score.high_score_build(opts).each do |score|
- data << { :user_id => score["value"]["user_id"], :stage => score["_id"], :score => score["value"]["score"] }
- end
- data
- end
- def self.high_score_daily(opts={})
- opts[:query][:created_at] = { "$gt" => Time.now - 1.day }
- Score.high_score(opts)
- end
- def self.high_score_weekly(opts={})
- opts[:query][:created_at] = { "$gt" => Time.now - 1.week }
- Score.high_score(opts)
- end
- def self.high_score_monthly(opts={})
- opts[:query][:created_at] = { "$gt" => Time.now - 1.month }
- Score.high_score(opts)
- end
- end
- class TopScore
- include MongoMapper::Document
- key :_id, String
- key :value, Hash
- def self.compute_top_scores(opts={})
- opts[:out] = "top_scores"
- Score.high_score(opts)
- end
- end
Add Comment
Please, Sign In to add comment