Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var uid = 0;
- var users = [];
- var transactions = [];
- function User(uid) {
- // If the uid is an object, we copy the properties of
- // that object into this new object.
- if( typeof uid === 'object') {
- for(var prop in uid) {
- this[prop] = uid[prop];
- }
- } else {
- // The unique identifier for this game.
- this.uid = this.uid || uid;
- /*
- * Identification
- */
- this.username = 'admin';
- // default username
- this.password = 'admin';
- // default password
- this.dob = -1;
- // placeholder dob
- this.doc = -1;
- // placeholder doc
- /*
- * Financial
- */
- this.balance = -1;
- this.knownTransactions = [];
- }
- }
- //********************************************************************************//
- function Transaction() {
- this.amount = 0;
- this.to
- this.from
- }
- //********************************************************************************//
- checkPassword = function(user, pass) {
- for(var i = 0; i < users.length; i++) {
- if(users[i].username === user) {
- return (users[i].password === pass);
- }
- }
- return false;
- }
- //********************************************************************************//
- //********************************************************************************//
- //********************************************************************************//
- /*
- * Methods used by the user
- */
- User.prototype.transfer = function(to, amount) {
- var tran = new Transaction(to, this.username, amount);
- var found = 0;
- var complete = 0;
- for(var i = 0; i < users.length; i++) {
- if(users[i].username === to)
- {
- found = 1;
- if (this.balance - amount < 0)
- {
- this.balance -= amount;
- users[i].balance += amount;
- complete = 1;
- }
- }
- }
- if (found === 0)
- {
- // transaction incomplete, invalid id
- // quitting
- }
- if (complete === 0)
- {
- // inssufficient founds
- }
- /*
- * TODO:
- *
- * three states:
- * return:successfully withdrawn
- * return: failure to withdrawn: inssufficient funds
- * return: failure to transfer: invalid recipient
- * */
- path.exists(transDB, function(exists) {
- if(!exists) {
- var transactions = newUser ? [tran] : [];
- var json = JSON.stringify(transactions);
- saveTransDB(transactions, function() {
- res.render('transfer', {
- 'title' : 'transfer'
- });
- });
- } else {
- loadTransDB(function(transactions) {
- transactions.push(tran);
- saveTransDB(transactions, function() {
- res.render('transfer', {
- 'title' : 'transfer'
- });
- });
- });
- }
- });
- }
- User.prototype.withdraw = function(amount) {
- var tran = new Transaction(-1, this.username, amount);
- if (this.balance - amount < 0)
- {
- // return, inssufficient funds
- }
- this.balance -= amount;
- path.exists(transDB, function(exists) {
- if(!exists) {
- var transactions = newUser ? [tran] : [];
- var json = JSON.stringify(transactions);
- saveTransDB(transactions, function() {
- res.render('withdraw', {
- 'title' : 'withdraw'
- });
- });
- } else {
- loadTransDB(function(transactions) {
- transactions.push(tran);
- saveTransDB(transactions, function() {
- res.render('withdraw', {
- 'title' : 'Withdraw'
- });
- });
- });
- }
- });
- /*
- * TODO:
- * add to list of the known transactions
- * return: withdrawn $x
- * return: failure to withdraw: inssufucient funds
- */
- }
- User.prototype.deposit = function(amount) {
- var temp = new Transaction(this.username, -1, amount);
- this.balance += amount;
- path.exists(transDB, function(exists) {
- if(!exists) {
- var transactions = newUser ? [tran] : [];
- var json = JSON.stringify(transactions);
- saveTransDB(transactions, function() {
- res.render('deposit', {
- 'title' : 'deposit'
- });
- });
- } else {
- loadTransDB(function(transactions) {
- transactions.push(tran);
- saveTransDB(transactions, function() {
- res.render('deposit', {
- 'title' : 'deposit'
- });
- });
- });
- }
- });
- /*
- * TODO:
- * add to the list of the known transactions
- *
- */
- }
- //********************************************************************************//
- //********************************************************************************//
- //********************************************************************************//
- /*
- * Methods used backend
- */
- User.prototype.createUser = function(username, password, dob) {
- if (typeof(username) !== 'string' || typeof(password) !== 'string')
- {
- // username or password are not strings
- }
- var patt = new RegExp("\d{1,2}\/\d{1,2}\/\d{1,4}"); // not used yet, very simple regex
- this.username = username;
- this.password = password;
- this.dob = dob;
- this.balance = 0;
- var currentTime = new Date();
- var month = currentTime.getMonth() + 1;
- var day = currentTime.getDate();
- var year = currentTime.getFullYear();
- this.doc = month + "/" + day + "/" + year;
- /*
- * TODO:
- * check date of birth to be in the appropriate format
- *
- * return: user sucessfully created
- * return: error: user already exists
- * return: error: inccorrect format for date of birth
- */
- }
- User.prototype.updateTransactions = function() {
- /*
- * TODO:
- * figure out how to implement this function
- */
- }
- User.prototype.getTransactions = function() {
- var ret = '';
- /*
- * TODO:
- * possibly update transactions
- * return an html object with alternating colors (list items, css) of the list of the transactions
- */
- }
- User.prototype.getName = function() {
- return this.username;
- }
- User.prototype.addTransaction = function(id) {
- /*
- * TODO: given transaction ID add it to array of known transactions
- */
- }
- //********************************************************************************//
- //********************************************************************************//
- //********************************************************************************//
- /*
- * Transactions
- */
- Transaction.prototype.transfer = function(to, from, amount) {
- for(var i = 0; i < users.length; i++) {
- if(users[i].username === to)
- users[i].balance += amount;
- }
- for(var i = 0; i < users.length; i++) {
- if(users[i].username === from)
- users[i].balance -= amount;
- }
- // notify if fail
- /*
- * TODO:
- * find the appropriate users and add transaction to list of transactions
- * change their money
- */
- }
- Transaction.prototype.toString = function() {
- var ret = '';
- if(this.to !== -1 && this.from !== -1)
- ret = 'Transferred $' + this.amount + ' from ' + this.from + ' to ' + this.to;
- else if(this.to === -1 && this.from !== -1)
- ret = 'Withdrawn $' + amount + ' from ' + this.from;
- else if(this.to !== -1 && this.from === 1)
- ret = 'Deposited $' + amount + ' to ' + this.to;
- return ret;
- }
- //********************************************************************************//
- //********************************************************************************//
- //********************************************************************************//
- /*
- * views
- */
- exports.readme = function(req, res) {
- // The instructions for the assignment.
- res.render('readme', {
- title : 'Midterm'
- });
- };
- //********************************************************************************//
- exports.create = function(req, res) {
- var uname = req.query.username;
- var pw = req.query.password;
- var dob = req.query.dob;
- var newUser = new User(uid++);
- newUser.createUser(uname, pw, dob);
- path.exists(usersDB, function(exists) {
- if(!exists) {
- var users = newUser ? [newUser] : [];
- var json = JSON.stringify(users);
- saveUsersDB(users, function() {
- res.render('create', {
- 'title' : 'Create New User'
- });
- });
- } else {
- loadUsersDB(function(users) {
- if(newUser) {
- var dupe = 0;
- for(var i = 0; i < users.length; i++) {
- if(users[i].username === uname) {
- dupe = 1;
- }
- }
- if(dupe === 0)
- users.push(newUser);
- }
- saveUsersDB(users, function() {
- res.render('create', {
- 'title' : 'Create'
- });
- });
- });
- }
- });
- };
- //********************************************************************************//
- exports.login = function(req, res) {
- // req has properties username and password
- path.exists(usersDB, function(exists) {
- if(!exists) {
- var users = [];
- var json = JSON.stringify(users);
- saveUsersDB(users, function() {
- res.render('login', {
- 'title' : 'No userdatabase'
- });
- });
- } else {
- loadUsersDB(function(users) {
- var uname = req.query.username;
- var pw = req.query.password;
- for(var i = 0; i < users.length; i++) {
- if(users[i].username === uname) {
- if(users[i].password === pw)
- ;
- req.session.user = users[i];
- }
- }
- res.render('login', {
- 'title' : 'logged in as ' + req.session.user.username
- });
- });
- }
- // TODO: user login
- });
- }
- //********************************************************************************//
- exports.logout = function(req, res) {
- req.session.destroy();
- // TODO: user logout
- };
- //********************************************************************************//
- exports.transactions = function(req, res) {
- res.render('transactions', {
- title : 'Transactions',
- msg : 'Logged in as ' + req.session.username
- });
- // TODO: transaction list
- };
- //********************************************************************************//
- exports.deposit = function(req, res) {
- res.render('deposit', {
- title : 'Deposit',
- user: req.session.user.username
- });
- var amount = req.query.amt;
- var user = new User(req.session.user);
- // TODO: account deposit
- };
- //********************************************************************************//
- exports.withdraw = function(req, res) {
- var user = new User(req.session.user);
- res.render('withdraw', {
- title : 'Withdraw'
- });
- // TODO: account withdrawal
- };
- //********************************************************************************//
- exports.transfer = function(req, res) {
- res.render('transfer', {
- title : 'Transfer'
- });
- // TODO: account transfer
- };
- //********************************************************************************//
- exports.report = function(req, res) {
- res.render('report', {
- title: 'Report'
- });
- }
- //********************************************************************************//
- //********************************************************************************//
- //********************************************************************************//
- /*
- * Persistance
- */
- var fs = require('fs');
- var path = require('path');
- var usersDB = 'users.json';
- var transDB = 'trans.json';
- function loadUsersDB(cb) {
- fs.readFile(usersDB, function(err, data) {
- if(err) {
- throw new Error('Error reading database: ' + err);
- }
- cb(JSON.parse(data));
- });
- }
- function loadTransDB(cb) {
- fs.readFile(trans, function(err, data) {
- if(err) {
- throw new Error('Error reading database: ' + err);
- }
- cb(JSON.parse(data));
- });
- }
- function saveUsersDB(users, cb) {
- var json = JSON.stringify(users);
- fs.writeFile(usersDB, json, function(err) {
- if(err) {
- throw new Error('Could not write ' + dbfile);
- }
- cb();
- });
- }
- function saveTransDB(transactions, cb) {
- var json = JSON.stringify(transactions);
- fs.writeFile(transDB, json, function(err) {
- if(err) {
- throw new Error('Could not write ' + dbfile);
- }
- cb();
- });
- }
- //********************************************************************************//
- //********************************************************************************//
- //********************************************************************************//
- // Random stuff
- // Generate list of Users
- function toList(users) {
- var s = '<ul>';
- for(var i = 0; i < users.length; i++)
- s += '<li>' + users[i].username + '</li>';
- s += '</ul>';
- return s;
- }
Add Comment
Please, Sign In to add comment