Break out autoAllocate to its own function

This commit is contained in:
Sabe Jones
2013-12-30 11:39:53 -06:00
parent 958ec99f1a
commit 9f4efc31ed
2 changed files with 100 additions and 92 deletions
+33 -29
View File
@@ -941,6 +941,37 @@ api.wrap = (user) ->
{stats} new stats
{update} if aggregated changes, pass in userObj as update. otherwise commits will be made immediately
###
autoAllocate: ->
switch user.preferences.allocationMode
when "flat"
suggested = Math.min(user.stats.str, user.stats.int, user.stats.con, user.stats.per)
if user.stats.int is suggested # In case of ties, favor INT first, to get the next point sooner
return "int"
else if user.stats.per is suggested # Then favor PER, it's a god stat
return "per"
else if user.stats.str is suggested # Then favor STR, everyone loves crits
return "per"
else
return "con" # CON, the unsexiest of attributes
when "classbased"
# Attributes get 3:2:1:1 per 7 levels.
ideal = [(user.stats.lvl / 7 * 3), (user.stats.lvl / 7 * 2), (user.stats.lvl / 7), (user.stats.lvl / 7)]
# Primary, secondary etc. attributes aren't explicitly defined, so hardcode them. In order as above
switch user.stats.class
when "wizard" then preference = ["int", "per", "con", "str"]
when "rogue" then preference = ["per", "str", "int", "con"]
when "healer" then preference = ["con", "int", "str", "per"]
else preference = ["str", "con", "per", "int"]
# Get the difference between the ideal attribute spread according to level, and the user's current spread.
diff = [(user.stats[preference[0]]-ideal[0]),(user.stats[preference[1]]-ideal[1]),(user.stats[preference[2]]-ideal[2]),(user.stats[preference[3]]-ideal[3])]
suggested = _.findIndex(diff, ((val) -> if val is _.min(diff) then true)) # Returns the index of the first attribute that's furthest behind the ideal
if suggested is -1 then return "str" else return preference[suggested] # If _findIndex failed, we'd get a -1...
when "taskbased" # old logic, temp
tallies = _.reduce user.tasks, ((m,v)-> m[v.attribute or 'str'] += v.value;m), {str:0,int:0,con:0,per:0}
suggested = _.reduce tallies, ((m,v,k)-> if v>tallies[m] then k else m), 'str'
return suggested
else return "str" # if all else fails, dump into STR
updateStats: (stats) ->
# Game Over
return user.stats.hp=0 if stats.hp <= 0
@@ -966,35 +997,8 @@ api.wrap = (user) ->
# Auto-allocate a point, or give them a new manual point
if user.preferences.automaticAllocation
switch user.preferences.allocationMode
when "flat"
suggested = Math.min(user.stats.str, user.stats.int, user.stats.con, user.stats.per)
if user.stats.int is suggested # In case of ties, favor INT first, to get the next point sooner
user.stats.int++
else if user.stats.per is suggested # Then favor PER, it's a god stat
user.stats.per++
else if user.stats.str is suggested # Then favor STR, everyone loves crits
user.stats.str++
else
user.stats.con++ # CON, the unsexiest of attributes
when "classbased"
# Attributes get 3:2:1:1 per 7 levels.
ideal = [(user.stats.lvl / 7 * 3), (user.stats.lvl / 7 * 2), (user.stats.lvl / 7), (user.stats.lvl / 7)]
# Primary, secondary etc. attributes aren't explicitly defined, so hardcode them. In order per above
switch user.stats.class
when "wizard" then preference = ["int", "per", "con", "str"]
when "rogue" then preference = ["per", "str", "int", "con"]
when "healer" then preference = ["con", "int", "str", "per"]
else preference = ["str", "con", "per", "int"]
# Get the difference between the ideal attribute spread according to level, and the user's current spread.
diff = [(user.stats[preference[0]]-ideal[0]),(user.stats[preference[1]]-ideal[1]),(user.stats[preference[2]]-ideal[2]),(user.stats[preference[3]]-ideal[3])]
suggested = _.findIndex(diff, ((val) -> if val is _.min(diff) then true)) # Returns the index of the first attribute that's furthest behind the ideal
if suggested is -1 then user.stats.str++ else user.stats[preference[suggested]]++ # If _findIndex failed, we'd get a -1...
when "taskbased" # old logic, temp
tallies = _.reduce user.tasks, ((m,v)-> m[v.attribute or 'str'] += v.value;m), {str:0,int:0,con:0,per:0}
suggested = _.reduce tallies, ((m,v,k)-> if v>tallies[m] then k else m), 'str'
user.stats[suggested]++
else user.stats.str++ # if all else fails, dump into STR
suggested = user.fns.autoAllocate()
user.stats[suggested]++
else
# add new allocatable points. We could do user.stats.points++, but this does a fail-safe just in case
user.stats.points = user.stats.lvl - (user.stats.con + user.stats.str + user.stats.per + user.stats.int);