Guest User

TSMAPI:MoneyToString

a guest
Aug 4th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. function TSMAPI:MoneyToString(money, ...)
  2. money = tonumber(money)
  3. if not money then return end
  4. local color, isIcon, pad, trim, disabled, isCompact = nil, nil, nil, nil, nil, nil
  5. for i=1, select('#', ...) do
  6. local opt = select(i, ...)
  7. if type(opt) == "string" then
  8. if opt == "OPT_ICON" then -- use texture icons instead of letters for denominations
  9. isIcon = true
  10. elseif opt == "OPT_COMPACT" then
  11. isCompact = true
  12. elseif opt == "OPT_PAD" then -- left-pad all but the highest denomination with zeros (i.e. "1g 00s 02c" instead of "1g 0s 2c")
  13. pad = true
  14. elseif opt == "OPT_TRIM" then -- removes any 0 valued denominations (i.e. "1g" instead of "1g 0s 0c") - 0 will still be represented as "0c"
  15. trim = true
  16. elseif opt == "OPT_DISABLE" then -- removes color from denomination text - NOTE: this is not allowed if OPT_ICON is set
  17. disabled = true
  18. elseif strmatch(strlower(opt), "^|c[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]$") then -- color the numbers
  19. color = opt
  20. else
  21. TSMAPI:Assert(false, "Invalid option: "..opt)
  22. end
  23. end
  24. end
  25.  
  26. TSMAPI:Assert(not (isIcon and disabled), "Setting both OPT_ICON and OPT_DISABLE is not allowed")
  27.  
  28. local isNegative = money < 0
  29. money = abs(money)
  30. local gold = floor(money / COPPER_PER_GOLD)
  31. local silver = floor((money % COPPER_PER_GOLD) / COPPER_PER_SILVER)
  32. local copper = floor(money % COPPER_PER_SILVER)
  33. local goldText, silverText, copperText = nil, nil, nil
  34. if isIcon then
  35. goldText, silverText, copperText = GOLD_ICON, SILVER_ICON, COPPER_ICON
  36. elseif disabled then
  37. goldText, silverText, copperText = "g", "s", "c"
  38. else
  39. goldText, silverText, copperText = TSM.GOLD_TEXT, TSM.SILVER_TEXT, TSM.COPPER_TEXT
  40. end
  41.  
  42. if money == 0 then
  43. return private:FormatNumber(0, false, color)..copperText
  44. end
  45.  
  46. local text = nil
  47. local shouldPad = false
  48. if trim then
  49. wipe(private.textMoneyParts) -- avoid creating a new table every time
  50. -- add gold
  51. if gold > 0 then
  52. tinsert(private.textMoneyParts, private:FormatNumber(gold, false, color)..goldText)
  53. shouldPad = pad
  54. end
  55. -- add silver
  56. if silver > 0 then
  57. tinsert(private.textMoneyParts, private:FormatNumber(silver, shouldPad, color)..silverText)
  58. shouldPad = pad
  59. end
  60. -- add copper
  61. if copper > 0 then
  62. tinsert(private.textMoneyParts, private:FormatNumber(copper, shouldPad, color)..copperText)
  63. shouldPad = pad
  64. end
  65. text = table.concat(private.textMoneyParts, " ")
  66. elseif isCompact then
  67. if ( gold >= 100 ) then
  68. gold = floor(gold + (silver+copper/100)/100 + 0.5)
  69. text = private:FormatNumber(gold, false, color)..goldText
  70. elseif gold > 0 then
  71. silver = floor(silver+copper/100+0.5)
  72. text = private:FormatNumber(gold, false, color)..goldText..private:FormatNumber(silver, false, color)..silverText
  73. elseif silver > 0 then
  74. text = private:FormatNumber(silver, false, color)..silverText..private:FormatNumber(copper, false, color)..copperText
  75. else
  76. text = private:FormatNumber(copper, false, color)..copperText
  77. end
  78. else
  79. if gold > 0 then
  80. text = private:FormatNumber(gold, false, color)..goldText.." "..private:FormatNumber(silver, pad, color)..silverText.." "..private:FormatNumber(copper, pad, color)..copperText
  81. elseif silver > 0 then
  82. text = private:FormatNumber(silver, false, color)..silverText.." "..private:FormatNumber(copper, pad, color)..copperText
  83. else
  84. text = private:FormatNumber(copper, false, color)..copperText
  85. end
  86. end
  87.  
  88. if isNegative then
  89. if color then
  90. return color.."-|r"..text
  91. else
  92. return "-"..text
  93. end
  94. else
  95. return text
  96. end
  97. end
Add Comment
Please, Sign In to add comment