-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
90 lines (83 loc) · 2.44 KB
/
Copy pathdatabase.js
File metadata and controls
90 lines (83 loc) · 2.44 KB
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
var USE_DB = true;
var mongojs = USE_DB ? require('mongojs') : null;
var db = USE_DB ? mongojs(process.env.MONGODB_URI, ['account', 'progress']) : null;
Database = {};
///////
Database.saveAccount = function(data, cb){
if(!USE_DB){
return cb({success:true});
}
db.account.findOne({username:data.username, password:data.password}, function(err, res){
if(res){
db.account.update({_id:res._id}, {$set:{playerInfo:res.playerInfo, explorerInfo:res.explorerInfo}}, {}, function(err){
cb({success:true});
});
} else{
cb({success:false, reason:"nonexistant"});
}
});
}
Database.loadAccount = function(data, cb){
if(!USE_DB){
return cb({success:true, explorerInfo:{name:"david", code:"davini"}});
}
if(!(data.username.length > 0 && data.password.length > 0)){
return cb({success:false, reason:"empty"});
}
var finishLoad = function(){
db.account.findOne({username:data.username, password:data.password}, function(err, res){
if(res){
cb({success:true, playerInfo:res.playerInfo, explorerInfo:res.explorerInfo});
} else{
cb({success:false, reason:"notmatching"});
}
});
}
Database.isExistingAccount(data, function(data){
if(data.success){
finishLoad();
} else{
cb({success:false, reason:"nonexistant"});
}
});
}
Database.isExistingAccount = function(data, cb){
if(!(data.username.length > 0 && data.password.length > 0)){
return cb({success:false, reason:"empty"});
}
db.account.findOne({username:data.username}, function(err, res){
if(res){
cb({success:true});
} else{
cb({success:false, reason:"nonexistant"});
}
});
}
Database.createAccount = function(data, cb){
if(!USE_DB){
return cb({success:data.username === "david" && data.password === "davini"});
}
if(!(data.username.length > 0 && data.password.length > 0)){
return cb({success:false, reason:"empty"});
}
var finishCreate = function(){
db.account.insert({username:data.username, password:data.password}, function(err){
//save user progress too...
cb({success:true});
});
}
Database.isExistingAccount(data, function(data){
if(data.success){
cb({success:false, reason:"duplicate"});
} else{
finishCreate();
}
});
}
Database.saveUserProgress = function(data, cb){
cb = cb || function(){}; //optional callback
if(!USE_DB){
return cb();
}
db.account.update({username:'b'}, data, cb);
}