Skip to content

Commit

Permalink
Add comma_value function for pretty formatting big numbers (#1085)
Browse files Browse the repository at this point in the history
This also applies the function to a number of labels in the skills window.
  • Loading branch information
AscuasFunkeln authored May 5, 2020
1 parent 63f5351 commit 714c6b8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
20 changes: 10 additions & 10 deletions modules/game_skills/skills.lua
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,11 @@ function onSkillButtonClick(button)
end

function onExperienceChange(localPlayer, value)
setSkillValue('experience', value)
setSkillValue('experience', comma_value(value))
end

function onLevelChange(localPlayer, value, percent)
setSkillValue('level', value)
setSkillValue('level', comma_value(value))
local text = tr('You have %s percent to go', 100 - percent) .. '\n' ..
tr('%s of experience left', expToAdvance(localPlayer:getLevel(), localPlayer:getExperience()))

Expand All @@ -300,7 +300,7 @@ function onLevelChange(localPlayer, value, percent)
local hoursLeft = (nextLevelExp - localPlayer:getExperience()) / expPerHour
local minutesLeft = math.floor((hoursLeft - math.floor(hoursLeft))*60)
hoursLeft = math.floor(hoursLeft)
text = text .. '\n' .. tr('%d of experience per hour', expPerHour)
text = text .. '\n' .. tr('%d of experience per hour', comma_value(expPerHour))
text = text .. '\n' .. tr('Next level in %d hours and %d minutes', hoursLeft, minutesLeft)
end
end
Expand All @@ -309,21 +309,21 @@ function onLevelChange(localPlayer, value, percent)
end

function onHealthChange(localPlayer, health, maxHealth)
setSkillValue('health', health)
setSkillValue('health', comma_value(health))
checkAlert('health', health, maxHealth, 30)
end

function onManaChange(localPlayer, mana, maxMana)
setSkillValue('mana', mana)
setSkillValue('mana', comma_value(mana))
checkAlert('mana', mana, maxMana, 30)
end

function onSoulChange(localPlayer, soul)
setSkillValue('soul', soul)
setSkillValue('soul', comma_value(soul))
end

function onFreeCapacityChange(localPlayer, freeCapacity)
setSkillValue('capacity', freeCapacity)
setSkillValue('capacity', comma_value(freeCapacity))
checkAlert('capacity', freeCapacity, localPlayer:getTotalCapacity(), 20)
end

Expand Down Expand Up @@ -397,7 +397,7 @@ function onRegenerationChange(localPlayer, regenerationTime)
end

function onSpeedChange(localPlayer, speed)
setSkillValue('speed', speed)
setSkillValue('speed', comma_value(speed))

onBaseSpeedChange(localPlayer, localPlayer:getBaseSpeed())
end
Expand All @@ -407,7 +407,7 @@ function onBaseSpeedChange(localPlayer, baseSpeed)
end

function onMagicLevelChange(localPlayer, magiclevel, percent)
setSkillValue('magiclevel', magiclevel)
setSkillValue('magiclevel', comma_value(magiclevel))
setSkillPercent('magiclevel', percent, tr('You have %s percent to go', 100 - percent))

onBaseMagicLevelChange(localPlayer, localPlayer:getBaseMagicLevel())
Expand All @@ -418,7 +418,7 @@ function onBaseMagicLevelChange(localPlayer, baseMagicLevel)
end

function onSkillChange(localPlayer, id, level, percent)
setSkillValue('skillId' .. id, level)
setSkillValue('skillId' .. id, comma_value(level))
setSkillPercent('skillId' .. id, percent, tr('You have %s percent to go', 100 - percent))

onBaseSkillChange(localPlayer, id, localPlayer:getSkillBaseLevel(id))
Expand Down
7 changes: 6 additions & 1 deletion modules/gamelib/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ function dirtostring(dir)
return k
end
end
end
end

function comma_value(n)
local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end

0 comments on commit 714c6b8

Please sign in to comment.