-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.js
116 lines (104 loc) · 3.22 KB
/
control.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var Promise = require("es6-promise").Promise;
var _ = require("underscore");
var moment = require("moment/moment");
var co = require("co");
var userSchema = new Schema({
name: String,
score: Number,
time: String
});
var User = mongoose.model('User', userSchema);
mongoose.connect('mongodb://localhost/games');
exports.rankHandle = function *(next){
var req = this.request.body || {};
var _user = new User({
name: req.name || "",
score: req.score || 0,
time: moment().format('l')
});
var ctx = this,
user, users;
yield _user.save(function(err, u){
if(err){
console.log(err);
}
user = u;
console.log('save user id: ' + u.id);
});
yield next;
ctx.redirect('/game-three/list/' + user.id);
};
exports.listHandle = function *(next){
console.log("list here" + this.params.id);
var id = this.params.id,
val = 0,
ctx = this,
users, user;
user = yield User.findOne({_id: id}, function(err, u){
if(err){
console.log(err);
}else{
console.log(u);
console.log('find user, id: ' + u.id);
return u;
}
return null;
});
yield User.find({}, function(err, us){
if(err){
console.log(err);
}
users = us;
console.log('find users, length: ' + us.length);
});
var val = 0;
users = _.sortBy(users, function(u){ // sort by desc
return 0 - u.score;
});
_.find(users, function(u, i){ //calculate rank
val = i + 1;
return user.score >= u.score;
});
users = _.each(users, function(u, i){
users[i].rank = i + 1;
if(i > 0 && users[i].score === users[i-1].score){
users[i].rank = users[i-1].rank;
}
});
ctx.render("rank", {users: users.length > 10 ? users.splice(0, 10) : users, rank: val}, true);
ctx.set('Cache-Control', 'no-cache');
// this.body = 'hello world';
// yield new Promise(function(resolve, reject){
// User.find({}, function(err, users){
// if(err) {
// console.log(err);
// reject(err);
// }
// return resolve(users);
// });
// }).then(function(users){
// User.findOne({_id: id}, function(err, user){
// if(err){
// console.log(err);
// }
// users = _.sortBy(users, function(u){ // sort by desc
// return 0 - u.score;
// });
// _.find(users, function(u, i){ //calculate rank
// val = i + 1;
// return user.score >= u.score;
// });
// users = _.each(users, function(u, i){
// users[i].rank = i + 1;
// if(i > 0 && users[i].score === users[i-1].score){
// users[i].rank = users[i-1].rank;
// }
// });
// console.log(user);
// console.log(val);
// return ctx.render("rank", {users: users.length > 10 ? users.splice(0, 10) : users, rank: val}, true);
// });
// });
};