mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-08-02 07:49:39 +00:00
moved tally functionality into scoring file
This commit is contained in:
parent
52a96eb5e3
commit
b9a9239bbd
4 changed files with 72 additions and 217 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
// Generated by CoffeeScript 1.3.3
|
// Generated by CoffeeScript 1.3.3
|
||||||
var content, derby, get, getHabits, ready, score, view, _ref;
|
var Scoring, content, derby, get, getHabits, ready, view, _ref;
|
||||||
|
|
||||||
derby = require('derby');
|
derby = require('derby');
|
||||||
|
|
||||||
|
|
@ -11,7 +11,7 @@ derby.use(require('../../ui'));
|
||||||
|
|
||||||
content = require('./content');
|
content = require('./content');
|
||||||
|
|
||||||
score = require('./score');
|
Scoring = require('./scoring');
|
||||||
|
|
||||||
view.fn('taskClasses', function(type, completed, value) {
|
view.fn('taskClasses', function(type, completed, value) {
|
||||||
var classes;
|
var classes;
|
||||||
|
|
@ -74,6 +74,7 @@ view.fn("silver", function(num) {
|
||||||
get('/:uidParam?', function(page, model, _arg) {
|
get('/:uidParam?', function(page, model, _arg) {
|
||||||
var uidParam;
|
var uidParam;
|
||||||
uidParam = _arg.uidParam;
|
uidParam = _arg.uidParam;
|
||||||
|
console.log(Scoring);
|
||||||
return model.fetch('users', function(err, users) {
|
return model.fetch('users', function(err, users) {
|
||||||
var guid, newUser, task, user, userId, _i, _len, _ref1;
|
var guid, newUser, task, user, userId, _i, _len, _ref1;
|
||||||
if ((uidParam != null) && users.get(uidParam)) {
|
if ((uidParam != null) && users.get(uidParam)) {
|
||||||
|
|
@ -208,7 +209,7 @@ ready(function(model) {
|
||||||
return console.error('Error: direction neither "up" nor "down" on checkbox set.');
|
return console.error('Error: direction neither "up" nor "down" on checkbox set.');
|
||||||
};
|
};
|
||||||
task = model.at("_user.tasks." + i);
|
task = model.at("_user.tasks." + i);
|
||||||
score({
|
Scoring.score({
|
||||||
user: model.at('_user'),
|
user: model.at('_user'),
|
||||||
task: task,
|
task: task,
|
||||||
direction: direction()
|
direction: direction()
|
||||||
|
|
@ -343,56 +344,23 @@ ready(function(model) {
|
||||||
}
|
}
|
||||||
user = model.at('_user');
|
user = model.at('_user');
|
||||||
task = model.at($(el).parents('li')[0]);
|
task = model.at($(el).parents('li')[0]);
|
||||||
return score({
|
return Scoring.score({
|
||||||
user: user,
|
user: user,
|
||||||
task: task,
|
task: task,
|
||||||
direction: direction
|
direction: direction
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
exports.endOfDayTally = endOfDayTally = function(e, el) {
|
exports.revive = function(e, el) {
|
||||||
var absVal, completed, expTally, key, lvl, task, todoTally, user, value, _ref3;
|
var stats;
|
||||||
user = model.at('_user');
|
stats = model.at('_user.stats');
|
||||||
todoTally = 0;
|
stats.set('hp', 50);
|
||||||
for (key in model.get('_user.tasks')) {
|
stats.set('lvl', 1);
|
||||||
task = model.at("_user.tasks." + key);
|
stats.set('exp', 0);
|
||||||
_ref3 = [task.get('type'), task.get('value'), task.get('completed')], type = _ref3[0], value = _ref3[1], completed = _ref3[2];
|
stats.set('money', 0);
|
||||||
if (type === 'todo' || type === 'daily') {
|
model.set('_user.items.armor', 0);
|
||||||
if (!completed) {
|
model.set('_user.items.weapon', 0);
|
||||||
score({
|
model.set('_items.armor', content.items.armor[1]);
|
||||||
user: user,
|
return model.set('_items.weapon', content.items.weapon[1]);
|
||||||
task: task,
|
|
||||||
direction: 'down',
|
|
||||||
cron: true
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (type === 'daily') {
|
|
||||||
task.push("history", {
|
|
||||||
date: new Date(),
|
|
||||||
value: value
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
absVal = completed ? Math.abs(value) : value;
|
|
||||||
todoTally += absVal;
|
|
||||||
}
|
|
||||||
if (type === 'daily') {
|
|
||||||
task.set('completed', false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
model.push('_user.history.todos', {
|
|
||||||
date: new Date(),
|
|
||||||
value: todoTally
|
|
||||||
});
|
|
||||||
expTally = user.get('stats.exp');
|
|
||||||
lvl = 0;
|
|
||||||
while (lvl < (user.get('stats.lvl') - 1)) {
|
|
||||||
lvl++;
|
|
||||||
expTally += 50 * Math.pow(lvl, 2) - 150 * lvl + 200;
|
|
||||||
}
|
|
||||||
return model.push('_user.history.exp', {
|
|
||||||
date: new Date(),
|
|
||||||
value: expTally
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
exports.poormanscron = poormanscron = function() {
|
exports.poormanscron = poormanscron = function() {
|
||||||
var DAY, daysPassed, lastCron, n, today, _k, _results;
|
var DAY, daysPassed, lastCron, n, today, _k, _results;
|
||||||
|
|
@ -420,17 +388,8 @@ ready(function(model) {
|
||||||
setInterval((function() {
|
setInterval((function() {
|
||||||
return poormanscron();
|
return poormanscron();
|
||||||
}), 3600000);
|
}), 3600000);
|
||||||
exports.revive = function(e, el) {
|
exports.endOfDayTally = endOfDayTally = function(e, el) {
|
||||||
var stats;
|
return Scoring.tally(model);
|
||||||
stats = model.at('_user.stats');
|
|
||||||
stats.set('hp', 50);
|
|
||||||
stats.set('lvl', 1);
|
|
||||||
stats.set('exp', 0);
|
|
||||||
stats.set('money', 0);
|
|
||||||
model.set('_user.items.armor', 0);
|
|
||||||
model.set('_user.items.weapon', 0);
|
|
||||||
model.set('_items.armor', content.items.armor[1]);
|
|
||||||
return model.set('_items.weapon', content.items.weapon[1]);
|
|
||||||
};
|
};
|
||||||
exports.shortcuts = function(e) {
|
exports.shortcuts = function(e) {
|
||||||
var code, command;
|
var code, command;
|
||||||
|
|
|
||||||
109
lib/app/score.js
109
lib/app/score.js
|
|
@ -1,109 +0,0 @@
|
||||||
// Generated by CoffeeScript 1.3.3
|
|
||||||
var content, expModifier, hpModifier, updateStats;
|
|
||||||
|
|
||||||
content = require('./content');
|
|
||||||
|
|
||||||
expModifier = function(user, value) {
|
|
||||||
var dmg, modified;
|
|
||||||
dmg = user.get('items.weapon') * .03;
|
|
||||||
dmg += user.get('stats.lvl') * .03;
|
|
||||||
modified = value + (value * dmg);
|
|
||||||
return modified;
|
|
||||||
};
|
|
||||||
|
|
||||||
hpModifier = function(user, value) {
|
|
||||||
var ac, modified;
|
|
||||||
ac = user.get('items.armor') * .03;
|
|
||||||
ac += user.get('stats.lvl') * .03;
|
|
||||||
modified = value - (value * ac);
|
|
||||||
return modified;
|
|
||||||
};
|
|
||||||
|
|
||||||
updateStats = function(user, stats) {
|
|
||||||
var money, tnl;
|
|
||||||
if (stats.hp != null) {
|
|
||||||
if (stats.hp < 0) {
|
|
||||||
user.set('stats.lvl', 0);
|
|
||||||
} else {
|
|
||||||
user.set('stats.hp', stats.hp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (stats.exp != null) {
|
|
||||||
tnl = user.get('_tnl');
|
|
||||||
if (stats.exp >= tnl) {
|
|
||||||
stats.exp -= tnl;
|
|
||||||
user.set('stats.lvl', user.get('stats.lvl') + 1);
|
|
||||||
}
|
|
||||||
if (!user.get('items.itemsEnabled') && stats.exp >= 50) {
|
|
||||||
user.set('items.itemsEnabled', true);
|
|
||||||
$('ul.items').popover({
|
|
||||||
title: content.items.unlockedMessage.title,
|
|
||||||
placement: 'left',
|
|
||||||
trigger: 'manual',
|
|
||||||
html: true,
|
|
||||||
content: "<div class='item-store-popover'> <img src='/img/BrowserQuest/chest.png' /> " + content.items.unlockedMessage.content + " <a href='#' onClick=\"$('ul.items').popover('hide');return false;\">[Close]</a> </div>"
|
|
||||||
});
|
|
||||||
$('ul.items').popover('show');
|
|
||||||
}
|
|
||||||
user.set('stats.exp', stats.exp);
|
|
||||||
}
|
|
||||||
if (stats.money != null) {
|
|
||||||
if (!(typeof money !== "undefined" && money !== null) || money < 0) {
|
|
||||||
money = 0.0;
|
|
||||||
}
|
|
||||||
return user.set('stats.money', stats.money);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = function(spec) {
|
|
||||||
var adjustvalue, cron, delta, direction, exp, hp, lvl, money, sign, task, type, user, value, _ref, _ref1;
|
|
||||||
if (spec == null) {
|
|
||||||
spec = {
|
|
||||||
user: null,
|
|
||||||
task: null,
|
|
||||||
direction: null,
|
|
||||||
cron: null
|
|
||||||
};
|
|
||||||
}
|
|
||||||
_ref = [spec.user, spec.task, spec.direction, spec.cron], user = _ref[0], task = _ref[1], direction = _ref[2], cron = _ref[3];
|
|
||||||
sign = direction === "up" ? 1 : -1;
|
|
||||||
value = task.get('value');
|
|
||||||
delta = value < 0 ? (-0.1 * value + 1) * sign : (Math.pow(0.9, value)) * sign;
|
|
||||||
type = task.get('type');
|
|
||||||
adjustvalue = type !== 'reward';
|
|
||||||
if ((type === 'habit') && (task.get("up") === false || task.get("down") === false)) {
|
|
||||||
adjustvalue = false;
|
|
||||||
}
|
|
||||||
if (adjustvalue) {
|
|
||||||
value += delta;
|
|
||||||
}
|
|
||||||
if (type === 'habit') {
|
|
||||||
if (task.get('value') !== value) {
|
|
||||||
task.push('history', {
|
|
||||||
date: new Date(),
|
|
||||||
value: value
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
task.set('value', value);
|
|
||||||
_ref1 = [user.get('stats.money'), user.get('stats.hp'), user.get('stats.exp'), user.get('stats.lvl')], money = _ref1[0], hp = _ref1[1], exp = _ref1[2], lvl = _ref1[3];
|
|
||||||
if (type === 'reward') {
|
|
||||||
money -= task.get('value');
|
|
||||||
if (money < 0) {
|
|
||||||
hp += money;
|
|
||||||
money = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (delta > 0 || ((type === 'daily' || type === 'todo') && !cron)) {
|
|
||||||
exp += expModifier(user, delta);
|
|
||||||
money += delta;
|
|
||||||
} else if (type !== 'reward' && type !== 'todo') {
|
|
||||||
hp += hpModifier(user, delta);
|
|
||||||
}
|
|
||||||
updateStats(user, {
|
|
||||||
hp: hp,
|
|
||||||
exp: exp,
|
|
||||||
money: money
|
|
||||||
});
|
|
||||||
return delta;
|
|
||||||
};
|
|
||||||
|
|
@ -3,9 +3,9 @@ derby = require('derby')
|
||||||
derby.use require('derby-ui-boot')
|
derby.use require('derby-ui-boot')
|
||||||
derby.use(require('../../ui'))
|
derby.use(require('../../ui'))
|
||||||
content = require('./content')
|
content = require('./content')
|
||||||
score = require('./score')
|
Scoring = require('./scoring')
|
||||||
|
|
||||||
## VIEW HELPERS ##
|
# ========== VIEW HELPERS ==========
|
||||||
|
|
||||||
view.fn 'taskClasses', (type, completed, value) ->
|
view.fn 'taskClasses', (type, completed, value) ->
|
||||||
#TODO figure out how to just pass in the task model, so i can access all these properties from one object
|
#TODO figure out how to just pass in the task model, so i can access all these properties from one object
|
||||||
|
|
@ -41,9 +41,10 @@ view.fn "silver", (num) ->
|
||||||
else
|
else
|
||||||
return "0"
|
return "0"
|
||||||
|
|
||||||
## ROUTES ##
|
# ========== ROUTES ==========
|
||||||
|
|
||||||
get '/:uidParam?', (page, model, {uidParam}) ->
|
get '/:uidParam?', (page, model, {uidParam}) ->
|
||||||
|
console.log Scoring
|
||||||
|
|
||||||
model.fetch 'users', (err, users) ->
|
model.fetch 'users', (err, users) ->
|
||||||
|
|
||||||
|
|
@ -113,7 +114,7 @@ getHabits = (page, model, userId) ->
|
||||||
|
|
||||||
page.render()
|
page.render()
|
||||||
|
|
||||||
## CONTROLLER FUNCTIONS ##
|
# ========== CONTROLLER FUNCTIONS ==========
|
||||||
|
|
||||||
ready (model) ->
|
ready (model) ->
|
||||||
|
|
||||||
|
|
@ -171,7 +172,7 @@ ready (model) ->
|
||||||
|
|
||||||
# Score the user based on todo task
|
# Score the user based on todo task
|
||||||
task = model.at("_user.tasks.#{i}")
|
task = model.at("_user.tasks.#{i}")
|
||||||
score({user:model.at('_user'), task:task, direction:direction()})
|
Scoring.score({user:model.at('_user'), task:task, direction:direction()})
|
||||||
|
|
||||||
# Then move the todos to/from _todoList/_completedList
|
# Then move the todos to/from _todoList/_completedList
|
||||||
if task.get('type') == 'todo'
|
if task.get('type') == 'todo'
|
||||||
|
|
@ -275,40 +276,17 @@ ready (model) ->
|
||||||
user = model.at('_user')
|
user = model.at('_user')
|
||||||
task = model.at $(el).parents('li')[0]
|
task = model.at $(el).parents('li')[0]
|
||||||
|
|
||||||
score({user:user, task:task, direction:direction})
|
Scoring.score({user:user, task:task, direction:direction})
|
||||||
|
|
||||||
# Note: Set 12am daily cron for this
|
exports.revive = (e, el) ->
|
||||||
# At end of day, add value to all incomplete Daily & Todo tasks (further incentive)
|
stats = model.at '_user.stats'
|
||||||
# For incomplete Dailys, deduct experience
|
stats.set 'hp', 50; stats.set 'lvl', 1; stats.set 'exp', 0; stats.set 'money', 0
|
||||||
#TODO: remove from exports when cron implemented
|
model.set '_user.items.armor', 0
|
||||||
exports.endOfDayTally = endOfDayTally = (e, el) ->
|
model.set '_user.items.weapon', 0
|
||||||
# users = model.at('users') #TODO this isn't working, iterate over all users
|
model.set '_items.armor', content.items.armor[1]
|
||||||
# for user in users
|
model.set '_items.weapon', content.items.weapon[1]
|
||||||
user = model.at '_user'
|
|
||||||
todoTally = 0
|
|
||||||
for key of model.get '_user.tasks'
|
|
||||||
task = model.at "_user.tasks.#{key}"
|
|
||||||
[type, value, completed] = [task.get('type'), task.get('value'), task.get('completed')]
|
|
||||||
if type in ['todo', 'daily']
|
|
||||||
# Deduct experience for missed Daily tasks,
|
|
||||||
# but not for Todos (just increase todo's value)
|
|
||||||
score({user:user, task:task, direction:'down', cron:true}) unless completed
|
|
||||||
if type == 'daily'
|
|
||||||
task.push "history", { date: new Date(), value: value }
|
|
||||||
else
|
|
||||||
absVal = if (completed) then Math.abs(value) else value
|
|
||||||
todoTally += absVal
|
|
||||||
task.set('completed', false) if type == 'daily'
|
|
||||||
model.push '_user.history.todos', { date: new Date(), value: todoTally }
|
|
||||||
|
|
||||||
# tally experience
|
|
||||||
expTally = user.get 'stats.exp'
|
|
||||||
lvl = 0 #iterator
|
|
||||||
while lvl < (user.get('stats.lvl')-1)
|
|
||||||
lvl++
|
|
||||||
expTally += 50 * Math.pow(lvl, 2) - 150 * lvl + 200
|
|
||||||
model.push '_user.history.exp', { date: new Date(), value: expTally }
|
|
||||||
|
|
||||||
|
# ========== CRON ==========
|
||||||
|
|
||||||
#TODO: remove when cron implemented
|
#TODO: remove when cron implemented
|
||||||
exports.poormanscron = poormanscron = ->
|
exports.poormanscron = poormanscron = ->
|
||||||
|
|
@ -327,15 +305,12 @@ ready (model) ->
|
||||||
poormanscron()
|
poormanscron()
|
||||||
), 3600000
|
), 3600000
|
||||||
|
|
||||||
exports.revive = (e, el) ->
|
# Note: Set 12am daily cron for this
|
||||||
stats = model.at '_user.stats'
|
#TODO: remove from exports when cron implemented
|
||||||
stats.set 'hp', 50; stats.set 'lvl', 1; stats.set 'exp', 0; stats.set 'money', 0
|
exports.endOfDayTally = endOfDayTally = (e, el) ->
|
||||||
model.set '_user.items.armor', 0
|
Scoring.tally(model)
|
||||||
model.set '_user.items.weapon', 0
|
|
||||||
model.set '_items.armor', content.items.armor[1]
|
|
||||||
model.set '_items.weapon', content.items.weapon[1]
|
|
||||||
|
|
||||||
## SHORTCUTS ##
|
# ========== SHORTCUTS ==========
|
||||||
|
|
||||||
exports.shortcuts = (e) ->
|
exports.shortcuts = (e) ->
|
||||||
return unless e.metaKey || e.ctrlKey
|
return unless e.metaKey || e.ctrlKey
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ updateStats = (user, stats) ->
|
||||||
money = 0.0 if (!money? or money<0)
|
money = 0.0 if (!money? or money<0)
|
||||||
user.set 'stats.money', stats.money
|
user.set 'stats.money', stats.money
|
||||||
|
|
||||||
module.exports = (spec = {user:null, task:null, direction:null, cron:null}) ->
|
exports.score = score = (spec = {user:null, task:null, direction:null, cron:null}) ->
|
||||||
[user, task, direction, cron] = [spec.user, spec.task, spec.direction, spec.cron]
|
[user, task, direction, cron] = [spec.user, spec.task, spec.direction, spec.cron]
|
||||||
|
|
||||||
# For negative values, use a line: something like y=-.1x+1
|
# For negative values, use a line: something like y=-.1x+1
|
||||||
|
|
@ -101,3 +101,33 @@ module.exports = (spec = {user:null, task:null, direction:null, cron:null}) ->
|
||||||
updateStats(user, {hp: hp, exp: exp, money: money})
|
updateStats(user, {hp: hp, exp: exp, money: money})
|
||||||
|
|
||||||
return delta
|
return delta
|
||||||
|
|
||||||
|
# At end of day, add value to all incomplete Daily & Todo tasks (further incentive)
|
||||||
|
# For incomplete Dailys, deduct experience
|
||||||
|
exports.tally = tally = (model) ->
|
||||||
|
# users = model.at('users') #TODO this isn't working, iterate over all users
|
||||||
|
# for user in users
|
||||||
|
user = model.at '_user'
|
||||||
|
todoTally = 0
|
||||||
|
for key of model.get '_user.tasks'
|
||||||
|
task = model.at "_user.tasks.#{key}"
|
||||||
|
[type, value, completed] = [task.get('type'), task.get('value'), task.get('completed')]
|
||||||
|
if type in ['todo', 'daily']
|
||||||
|
# Deduct experience for missed Daily tasks,
|
||||||
|
# but not for Todos (just increase todo's value)
|
||||||
|
score({user:user, task:task, direction:'down', cron:true}) unless completed
|
||||||
|
if type == 'daily'
|
||||||
|
task.push "history", { date: new Date(), value: value }
|
||||||
|
else
|
||||||
|
absVal = if (completed) then Math.abs(value) else value
|
||||||
|
todoTally += absVal
|
||||||
|
task.set('completed', false) if type == 'daily'
|
||||||
|
model.push '_user.history.todos', { date: new Date(), value: todoTally }
|
||||||
|
|
||||||
|
# tally experience
|
||||||
|
expTally = user.get 'stats.exp'
|
||||||
|
lvl = 0 #iterator
|
||||||
|
while lvl < (user.get('stats.lvl')-1)
|
||||||
|
lvl++
|
||||||
|
expTally += 50 * Math.pow(lvl, 2) - 150 * lvl + 200
|
||||||
|
model.push '_user.history.exp', { date: new Date(), value: expTally }
|
||||||
Loading…
Reference in a new issue