moved tally functionality into scoring file

This commit is contained in:
Tyler Renelle 2012-07-27 21:11:53 -04:00
parent 52a96eb5e3
commit b9a9239bbd
4 changed files with 72 additions and 217 deletions

View file

@ -1,5 +1,5 @@
// 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');
@ -11,7 +11,7 @@ derby.use(require('../../ui'));
content = require('./content');
score = require('./score');
Scoring = require('./scoring');
view.fn('taskClasses', function(type, completed, value) {
var classes;
@ -74,6 +74,7 @@ view.fn("silver", function(num) {
get('/:uidParam?', function(page, model, _arg) {
var uidParam;
uidParam = _arg.uidParam;
console.log(Scoring);
return model.fetch('users', function(err, users) {
var guid, newUser, task, user, userId, _i, _len, _ref1;
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.');
};
task = model.at("_user.tasks." + i);
score({
Scoring.score({
user: model.at('_user'),
task: task,
direction: direction()
@ -343,56 +344,23 @@ ready(function(model) {
}
user = model.at('_user');
task = model.at($(el).parents('li')[0]);
return score({
return Scoring.score({
user: user,
task: task,
direction: direction
});
};
exports.endOfDayTally = endOfDayTally = function(e, el) {
var absVal, completed, expTally, key, lvl, task, todoTally, user, value, _ref3;
user = model.at('_user');
todoTally = 0;
for (key in model.get('_user.tasks')) {
task = model.at("_user.tasks." + key);
_ref3 = [task.get('type'), task.get('value'), task.get('completed')], type = _ref3[0], value = _ref3[1], completed = _ref3[2];
if (type === 'todo' || type === 'daily') {
if (!completed) {
score({
user: user,
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.revive = function(e, el) {
var stats;
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.poormanscron = poormanscron = function() {
var DAY, daysPassed, lastCron, n, today, _k, _results;
@ -420,17 +388,8 @@ ready(function(model) {
setInterval((function() {
return poormanscron();
}), 3600000);
exports.revive = function(e, el) {
var stats;
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.endOfDayTally = endOfDayTally = function(e, el) {
return Scoring.tally(model);
};
exports.shortcuts = function(e) {
var code, command;

View file

@ -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;
};

View file

@ -3,9 +3,9 @@ derby = require('derby')
derby.use require('derby-ui-boot')
derby.use(require('../../ui'))
content = require('./content')
score = require('./score')
Scoring = require('./scoring')
## VIEW HELPERS ##
# ========== VIEW HELPERS ==========
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
@ -41,9 +41,10 @@ view.fn "silver", (num) ->
else
return "0"
## ROUTES ##
# ========== ROUTES ==========
get '/:uidParam?', (page, model, {uidParam}) ->
console.log Scoring
model.fetch 'users', (err, users) ->
@ -113,7 +114,7 @@ getHabits = (page, model, userId) ->
page.render()
## CONTROLLER FUNCTIONS ##
# ========== CONTROLLER FUNCTIONS ==========
ready (model) ->
@ -171,7 +172,7 @@ ready (model) ->
# Score the user based on todo task
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
if task.get('type') == 'todo'
@ -275,41 +276,18 @@ ready (model) ->
user = model.at('_user')
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
# At end of day, add value to all incomplete Daily & Todo tasks (further incentive)
# For incomplete Dailys, deduct experience
#TODO: remove from exports when cron implemented
exports.endOfDayTally = endOfDayTally = (e, el) ->
# 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 }
exports.revive = (e, el) ->
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]
model.set '_items.weapon', content.items.weapon[1]
# 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
exports.poormanscron = poormanscron = ->
model.setNull('_user.lastCron', new Date())
@ -327,15 +305,12 @@ ready (model) ->
poormanscron()
), 3600000
exports.revive = (e, el) ->
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]
model.set '_items.weapon', content.items.weapon[1]
# Note: Set 12am daily cron for this
#TODO: remove from exports when cron implemented
exports.endOfDayTally = endOfDayTally = (e, el) ->
Scoring.tally(model)
## SHORTCUTS ##
# ========== SHORTCUTS ==========
exports.shortcuts = (e) ->
return unless e.metaKey || e.ctrlKey

View file

@ -47,8 +47,8 @@ updateStats = (user, stats) ->
if stats.money?
money = 0.0 if (!money? or money<0)
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]
# For negative values, use a line: something like y=-.1x+1
@ -100,4 +100,34 @@ module.exports = (spec = {user:null, task:null, direction:null, cron:null}) ->
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 }