-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Remco
authored and
Remco
committed
Feb 18, 2016
1 parent
68778ad
commit 4902651
Showing
7 changed files
with
239 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
var LevelUP = require("levelup"), | ||
Logger = require("./logger.js").Logger; | ||
|
||
var db = LevelUP('./runtime/databases/beastbank'); | ||
// 1 general error | ||
// 2 low on cash | ||
// 3 already exists | ||
// 4 not found | ||
|
||
fetchSaldo = function(user, callback) { | ||
db.get(user, function(err, reply) { | ||
if (err) { | ||
if (err.notFound) { | ||
return callback(4, null); | ||
} else { | ||
return callback(err, null); | ||
} | ||
} | ||
if (reply && !err) { | ||
return callback(null, reply); | ||
} | ||
}); | ||
}; | ||
|
||
exports.credit = function(user, cash, callback) { | ||
fetchSaldo(user, function(err, reply) { | ||
if (reply) { | ||
// TODO: Do some DB shit here. | ||
return callback(null, Math.round(reply + cash)); | ||
} | ||
if (err) return callback(err, null); | ||
}); | ||
}; | ||
|
||
exports.debit = function(user, cash, callback) { | ||
fetchSaldo(user, function(err, reply) { | ||
if (err) return callback(err, null); | ||
if (reply < cash) { | ||
return callback(2, null); | ||
} else if (reply > cash) { | ||
return callback(null, Math.round(reply - cash)); | ||
} | ||
}); | ||
}; | ||
|
||
exports.register = function(user, callback) { | ||
fetchSaldo(user, function(err, reply) { | ||
if (reply && !err) return callback(3, null); | ||
if (err === 4) { | ||
db.put(user + ':500', function(err) { | ||
if (!err) return callback(0, 500); | ||
if (err) return callback(1, null); | ||
}); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.