Fiddled with creating xp functions

This commit is contained in:
Ruben Vereecken 2014-06-12 19:39:45 +02:00
parent fddca03417
commit dcdcca751f
3 changed files with 21 additions and 10 deletions

View file

@ -79,8 +79,21 @@ module.exports.getByPath = (target, path) ->
module.exports.round = _.curry (digits, n) ->
n = +n.toFixed(digits)
module.exports.createLinearFunc = (params) ->
positify = (func) -> (x) -> if x > 0 then func(x) else 0
# f(x) = ax + b
createLinearFunc = (params) ->
(x) -> (params.a or 1) * x + (params.b or 0)
module.exports.createLogFunc = (params) ->
(x) -> (params.a or 1) * Math.log((params.b or 1) * (x + (params.c or 0)))
# f(x) = ax² + bx + c
createQuadraticFunc = (params) ->
(x) -> (params.a or 1) * x * x + (params.b or 1) * x + (params.c or 0)
# f(x) = a log(b (x + c)) + d
createLogFunc = (params) ->
(x) -> if x > 0 then (params.a or 1) * Math.log((params.b or 1) * (x + (params.c or 0))) + (params.d or 0) else 0
module.exports.functionCreators =
linear: positify(createLinearFunc)
quadratic: positify(createQuadraticFunc)
logarithmic: positify(createLogFunc)

View file

@ -13,5 +13,4 @@ module.exports = class Achievement extends CocoModel
getExpFunction: ->
kind = @get('function')?.kind or @schema.function.default.kind
parameters = @get('function')?.parameters or @schema.function.default.parameters
funcCreator = if kind is 'linear' then util.createLinearFunc else if kind is 'logarithmic' then utils.createLogFunc
return funcCreator(parameters) if funcCreator?
return utils.functionCreators[kind](parameters) if kind of utils.functionCreators

View file

@ -23,11 +23,10 @@ AchievementSchema.methods.objectifyQuery = ->
AchievementSchema.methods.stringifyQuery = ->
@set('query', JSON.stringify(@get('query'))) if typeof @get('query') != "string"
AchievementSchema.methods.getExpFunction = ->
kind = @get('function.kind') or jsonschema.function.default.kind
parameters = @get('function.parameters') or jsonschema.function.default.parameters
funcCreator = if kind is 'linear' then util.createLinearFunc else if kind is 'logarithmic' then util.createLogFunc
return funcCreator(parameters) if funcCreator?
getExpFunction: ->
kind = @get('function')?.kind or jsonschema.function.default.kind
parameters = @get('function')?.parameters or jsonschema.function.default.parameters
return utils.functionCreators[kind](parameters) if kind of utils.functionCreators
AchievementSchema.post('init', (doc) -> doc.objectifyQuery())