Skip to content

Commit

Permalink
Generate and display leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
yeojoey committed Apr 6, 2017
1 parent e11f643 commit 5a7b1a7
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 84 deletions.
84 changes: 32 additions & 52 deletions public/javascripts/dashboard/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ angular.module("dashboardApp").controller ('userCtrl', function ($scope, $http)
url: '/api/dashboard/getUserInfo'
}).then(function successCallback(response) {
var userInfo = response.data.data;
userInfo.tutorials = setLevelInfo(userInfo.tutorials);
userInfo.name = toTitleCase(userInfo.name)
$scope.userInfo = userInfo;
}, function errorCallback(response) {
Expand All @@ -16,28 +15,28 @@ angular.module("dashboardApp").controller ('userCtrl', function ($scope, $http)

});

angular.module("dashboardApp").controller ('moduleCtrl', function ($scope, $http, $q) {
angular.module("dashboardApp").controller ('moduleCtrl', function ($scope, $http, $q, $window) {

var promises = [];
var tuts = [];

promises.push($http({
// promises.push($http({

method: 'POST',
url: '/api/dashboard/forceSyncIVLE'
// method: 'POST',
// url: '/api/dashboard/forceSyncIVLE'

}).then(function successCallback(response) {
// }).then(function successCallback(response) {

}, function errorCallback(response) {
console.log('Error: ' + response.message);
}));
// }, function errorCallback(response) {
// console.log('Error: ' + response.message);
// }));

promises.push(
$http({
method: 'POST',
method: 'POST',
url: '/api/dashboard/getTutorials'
}, function successCallback(response) {
console.log(response);
//console.log(response);
}, function errorCallback(response) {
console.log('Error: ' + response.message);
})
Expand All @@ -49,59 +48,40 @@ angular.module("dashboardApp").controller ('moduleCtrl', function ($scope, $http
} else {
tuts = responseArray[1].data.data.rows;
}
for (i = 0; i < tuts.length; i++) { // set leaderboard visibility to false
tuts[i].leaderboardIsVisible = false;
tuts[i].index = i;
}
$scope.tuts = tuts;
});

$scope.leaderboardIsVisible = false;

$scope.redirect = function(tut) {
$('#form').attr('action', 'lobby/'+tut.coursecode+'/'+tut.name)
$window.location.href = 'lobby/'+tut.coursecode+'/'+tut.name;
}

$scope.toggleLeaderboard = function(tut) {
$scope.leaderboardIsVisible = !$scope.leaderboardIsVisible;
if (!$scope.tuts[tut.index].leaderboardIsVisible) {
getTopStudents(tut);
}
$scope.tuts[tut.index].leaderboardIsVisible = !$scope.tuts[tut.index].leaderboardIsVisible;
}

});

/**
* Calculates level-related user info
* @param user
* @return user
*/
var setLevelInfo = function(tutArray) {
var constant = 0.1;
for (i = 0; i < tutArray.length; i++) {
var tutObj = tutArray[i];
var exp = tutObj.exp;
tutObj.level = calculateLevel(exp);
tutObj.currExp = exp - calculateExp(tutObj.level - 1);
tutObj.totalToNext = calculateExp(tutObj.level + 1); - calculateExp(tutObj.level);
tutObj.percentage = Math.floor(tutObj.currExp/tutObj.totalToNext * 100);
var getTopStudents = function(tut) {
$http({
method: 'POST',
url: '/api/dashboard/getTopUsers',
data: { tid: tut.id }
}).then( function successCallback(response) {
$scope.tuts[tut.index].students = response.data.data;
console.log(tuts[tut.index]);
console.log($scope.tuts[tut.index].students);
}, function errorCallback(response) {
console.log('Error: ' + response.message);
});
}
return tutArray;
}

var constant = 0.1;

/**
* Calculates level based on exp
* @param {Integer} exp
* @return {Integer} level
*/
var calculateLevel = function (exp) {
// Level = Constant * Sqrt(EXP)
return Math.floor(constant * Math.sqrt(exp)) + 1;
}
});

/**
* Calculates total exp needed to reach this level
* @param {Integer} level
* @return {Integer}
*/
var calculateExp = function (level) {
return Math.floor(Math.pow(level/constant, 2));
}


/**
Expand Down
85 changes: 82 additions & 3 deletions source/controller/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ var getUserInfo = function (req, res, next) {
var user = req.body.auth.decoded;
Tutorial.getUserTutorials(user.id).then(function (result) {
userTuts = processUserInfo(result);
console.log(userTuts);
res.json({success: true, message: 'Success', data: userTuts});
});
} else {
Expand Down Expand Up @@ -163,13 +162,93 @@ var processUserInfo = function (result) {
}
}
console.log(tutArray);
returnObject.tutorials = tutArray;
returnObject.tutorials = setLevelInfo(tutArray);
return returnObject;
}


var getTopUsers = function (req, res, next) {
var tid = req.body.tid;
Tutorial.findAndCountAllUsersInTutorial(tid).then(function (result) {
var result = processTopUsers(result);
res.json({success: true, message: 'Success', data: result});
});
}

var processTopUsers = function (data) {
var userArray = [];
for (i = 0; i < data.rows.length; i++) {
var user = data.rows[i];
var exp = user.dataValues.tutorials[0].userTutorial.exp;
userArray.push({
name: user.dataValues.name,
exp: exp,
level: calculateLevel(exp)
});
}
userArray.sort(sort_by('exp', true, parseInt));
return userArray;
}

/**
* Calculates level-related user info
* @param user
* @return user
*/
var setLevelInfo = function(tutArray) {
var constant = 0.1;
for (i = 0; i < tutArray.length; i++) {
var tutObj = tutArray[i];
var exp = tutObj.exp;
tutObj.level = calculateLevel(exp);
tutObj.currExp = exp - calculateExp(tutObj.level - 1);
tutObj.totalToNext = calculateExp(tutObj.level + 1); - calculateExp(tutObj.level);
tutObj.percentage = Math.floor(tutObj.currExp/tutObj.totalToNext * 100);
}
return tutArray;
}

var constant = 0.1;

/**
* Calculates level based on exp
* @param {Integer} exp
* @return {Integer} level
*/
var calculateLevel = function (exp) {
// Level = Constant * Sqrt(EXP)
return Math.floor(constant * Math.sqrt(exp)) + 1;
}

/**
* Calculates total exp needed to reach this level
* @param {Integer} level
* @return {Integer}
*/
var calculateExp = function (level) {
return Math.floor(Math.pow(level/constant, 2));
}


/**
* JSON object sorting function
*/
var sort_by = function(field, reverse, primer){

var key = primer ?
function(x) {return primer(x[field])} :
function(x) {return x[field]};

reverse = !reverse ? 1 : -1;

return function (a, b) {
return a = key(a), b = key(b), reverse * ((a > b) - (b > a));
}
}

module.exports.get = get;
module.exports.forceSyncIVLE = forceSyncIVLE;
module.exports.getTutorials = getTutorials;
module.exports.syncUser = syncUser;
module.exports.getUserInfo = getUserInfo;
module.exports.getUserInfo = getUserInfo;
module.exports.getTopUsers = getTopUsers;
1 change: 1 addition & 0 deletions source/controller/lobby.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var get = function (req, res, next)
var userId = user.id;
var moduleId = req.params.moduleId;
var tutorialId = req.params.tutorialId;
var tid = req.body.tut.id;

res.render ('lobby/lobby', {
title: 'Lobby UI',
Expand Down
31 changes: 24 additions & 7 deletions source/model/Tutorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,13 @@ var forceSyncIVLE = function (uid) {
* @return {Promise} [description]
*/
var findTopUsersInTutorial = function (tid) {
return userTutorial.findAndCountAll({
where:{
tutorialId: tid
},
return User.findAndCountAll({
include: [{
model: tutorial,
where: { id: tid }
}],
order: [
['exp','DESC']
[tutorial, 'exp', 'DESC']
]
});
}
Expand All @@ -412,8 +413,6 @@ var getUserTutorials = function (uid) {
});
}



/**
* Gets user info by uid
* @param uid
Expand Down Expand Up @@ -443,6 +442,24 @@ var getTutorialByCoursecodeAndName = function (coursecode, name) {
});
}

/**
* Change user EXP
* @param uid
* @param {int} amount [Amount of points to increase/decrease by]
* @return {Promise}
*/
var changeExp = function (uid, tid, amount) {
return userTutorial.findOne({
where: {
userId: uid,
tutorialId: tid
}
}).then(function (result) {
return result.increment(['exp'], { by: amount });
});
}


module.exports = tutorial;
module.exports.forceSyncIVLE = forceSyncIVLE;
module.exports.findTutorialSession = findTutorialSession;
Expand Down
1 change: 1 addition & 0 deletions source/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ router.post('/api/dashboard/forceSyncIVLE', auth.ensureAuth, dashboard.forceSync
router.post('/api/dashboard/getTutorials', auth.ensureAuth, dashboard.getTutorials);
router.post('/api/dashboard/syncUser', auth.ensureAuth, dashboard.syncUser);
router.post('/api/dashboard/getUserInfo', auth.ensureAuth, dashboard.getUserInfo);
router.post('/api/dashboard/getTopUsers', auth.ensureAuth, dashboard.getTopUsers);

router.post('/api/lobby/enterLobby', auth.ensureAuth, lobby.enterLobby);
router.post('/api/lobby/getUsersInTutorial', auth.ensureAuth, lobby.getUsersInTutorial);
Expand Down
48 changes: 26 additions & 22 deletions source/view/dashboard.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,33 @@

<h2>Modules</h2>
<div class="list-group" ng-controller="moduleCtrl">
<form id="form" style="display: hidden" action="" method='POST'>
<ul id="tutorials">
<li ng-repeat="t in tuts">
<div class="container">
<div class="module-container">
<div class="row">
{{t.coursecode}} {{t.coursename}}
</div>
<div class="row">
<button class="btn btn-primary lobby-button" ng-click="redirect(t)" name="tut-id" value="{{t.id}}"> Join Class </button>
<button class="btn btn-primary" ng-click="toggleLeaderboard()"> View Leaderboard </button>
</div>
<div class="row">
<table class="table" ng-repeat="s in students" ng-Show="leaderboardIsVisible">
<th>Name</th>
<th>Level</th>
</table>
</div>
</div>
<ul id="tutorials">
<li ng-repeat="t in tuts">
<div class="container">
<div class="module-container">
<div class="row">
{{t.coursecode}} {{t.coursename}}
</div>
</li>
</ul>
</form>
<div class="row">
<button class="btn btn-primary lobby-button" ng-click="redirect(t)" name="tut-id" value="{{t.id}}"> Join Class </button>
<button class="btn btn-primary" ng-click="toggleLeaderboard(t)"> View Leaderboard </button>
</div>
<div class="row">
<table class="table" ng-show="t.leaderboardIsVisible">
<tr>
<th>Name</th>
<th>Level</th>
</tr>
<tr ng-repeat="s in t.students">
<td>{{ s.name }}</td>
<td>{{ s.level }}</td>
</tr>
</table>
</div>
</div>
</div>
</li>
</ul>
</div>

</div>
Expand Down

0 comments on commit 5a7b1a7

Please sign in to comment.