PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Interface/AddOns/TradeSkillMaster/Util/Money.lua

https://github.com/ELLIOTTCABLE/Warcrack
Lua | 232 lines | 178 code | 21 blank | 33 comment | 38 complexity | d07411acaf9cb5d6844dab5bf2e7cb51 MD5 | raw file
  1. -- ------------------------------------------------------------------------------ --
  2. -- TradeSkillMaster --
  3. -- http://www.curse.com/addons/wow/tradeskillmaster_warehousing --
  4. -- --
  5. -- A TradeSkillMaster Addon (http://tradeskillmaster.com) --
  6. -- All Rights Reserved* - Detailed license information included with addon. --
  7. -- ------------------------------------------------------------------------------ --
  8. -- This file contains various money APIs
  9. local TSM = select(2, ...)
  10. TSM.GOLD_TEXT = "|cffffd700g|r"
  11. TSM.SILVER_TEXT = "|cffc7c7cfs|r"
  12. TSM.COPPER_TEXT = "|cffeda55fc|r"
  13. local private = {}
  14. TSMAPI:RegisterForTracing(private, "TradeSkillMaster.Money_private")
  15. local GOLD_ICON = "|TInterface\\MoneyFrame\\UI-GoldIcon:0|t"
  16. local SILVER_ICON = "|TInterface\\MoneyFrame\\UI-SilverIcon:0|t"
  17. local COPPER_ICON = "|TInterface\\MoneyFrame\\UI-CopperIcon:0|t"
  18. function private:PadNumber(num, pad)
  19. if num < 10 and pad then
  20. return format("%02d", num)
  21. end
  22. return tostring(num)
  23. end
  24. --- Creates a formatted money string from a copper value.
  25. -- @param money The money value in copper.
  26. -- @param color The color to make the money text (minus the 'g'/'s'/'c'). If nil, will not add any extra color formatting.
  27. -- @param pad If true, the formatted string will be left padded.
  28. -- @param trim If true, will remove any 0 valued tokens. For example, "1g" instead of "1g0s0c". If money is zero, will return "0c".
  29. -- @param disabled If true, the g/s/c text will not be colored.
  30. -- @return Returns the formatted money text according to the parameters.
  31. function TSMAPI:FormatTextMoney(money, color, pad, trim, disabled)
  32. local money = tonumber(money)
  33. if not money then return end
  34. local isNegative = money < 0
  35. money = abs(money)
  36. local gold = floor(money / COPPER_PER_GOLD)
  37. local silver = floor((money - (gold * COPPER_PER_GOLD)) / COPPER_PER_SILVER)
  38. local copper = floor(money%COPPER_PER_SILVER)
  39. local text = ""
  40. local isFirst = true
  41. -- Trims 0 silver and/or 0 copper from the text
  42. if trim then
  43. if gold > 0 then
  44. if color then
  45. text = format("%s%s ", color..private:PadNumber(gold, pad and not isFirst).."|r", disabled and "g" or TSM.GOLD_TEXT)
  46. else
  47. text = format("%s%s ", private:PadNumber(gold, pad and not isFirst), disabled and "g" or TSM.GOLD_TEXT)
  48. end
  49. isFirst = false
  50. end
  51. if silver > 0 then
  52. if color then
  53. text = format("%s%s%s ", text, color..private:PadNumber(silver, pad and not isFirst).."|r", disabled and "s" or TSM.SILVER_TEXT)
  54. else
  55. text = format("%s%s%s ", text, private:PadNumber(silver, pad and not isFirst), disabled and "s" or TSM.SILVER_TEXT)
  56. end
  57. isFirst = false
  58. end
  59. if copper > 0 then
  60. if color then
  61. text = format("%s%s%s ", text, color..private:PadNumber(copper, pad and not isFirst).."|r", disabled and "c" or TSM.COPPER_TEXT)
  62. else
  63. text = format("%s%s%s ", text, private:PadNumber(copper, pad and not isFirst), disabled and "c" or TSM.COPPER_TEXT)
  64. end
  65. isFirst = false
  66. end
  67. if money == 0 then
  68. if color then
  69. text = format("%s%s%s ", text, color..private:PadNumber(copper, pad and not isFirst).."|r", disabled and "c" or TSM.COPPER_TEXT)
  70. else
  71. text = format("%s%s%s ", text, private:PadNumber(copper, pad and not isFirst), disabled and "c" or TSM.COPPER_TEXT)
  72. end
  73. isFirst = false
  74. end
  75. else
  76. -- Add gold
  77. if gold > 0 then
  78. if color then
  79. text = format("%s%s ", color..private:PadNumber(gold, pad and not isFirst).."|r", disabled and "g" or TSM.GOLD_TEXT)
  80. else
  81. text = format("%s%s ", private:PadNumber(gold, pad and not isFirst), disabled and "g" or TSM.GOLD_TEXT)
  82. end
  83. isFirst = false
  84. end
  85. -- Add silver
  86. if gold > 0 or silver > 0 then
  87. if color then
  88. text = format("%s%s%s ", text, color..private:PadNumber(silver, pad and not isFirst).."|r", disabled and "s" or TSM.SILVER_TEXT)
  89. else
  90. text = format("%s%s%s ", text, private:PadNumber(silver, pad and not isFirst), disabled and "s" or TSM.SILVER_TEXT)
  91. end
  92. isFirst = false
  93. end
  94. -- Add copper
  95. if color then
  96. text = format("%s%s%s ", text, color..private:PadNumber(copper, pad and not isFirst).."|r", disabled and "c" or TSM.COPPER_TEXT)
  97. else
  98. text = format("%s%s%s ", text, private:PadNumber(copper, pad and not isFirst), disabled and "c" or TSM.COPPER_TEXT)
  99. end
  100. end
  101. if isNegative then
  102. if color then
  103. return color .. "-|r" .. text:trim()
  104. else
  105. return "-" .. text:trim()
  106. end
  107. else
  108. return text:trim()
  109. end
  110. end
  111. --- Creates a formatted money string from a copper value and uses coin icon.
  112. -- @param money The money value in copper.
  113. -- @param color The color to make the money text (minus the coin icons). If nil, will not add any extra color formatting.
  114. -- @param pad If true, the formatted string will be left padded.
  115. -- @param trim If true, will not remove any 0 valued tokens. For example, "1g" instead of "1g0s0c". If money is zero, will return "0c".
  116. -- @return Returns the formatted money text according to the parameters.
  117. function TSMAPI:FormatTextMoneyIcon(money, color, pad, trim)
  118. local money = tonumber(money)
  119. if not money then return end
  120. local isNegative = money < 0
  121. money = abs(money)
  122. local gold = floor(money / COPPER_PER_GOLD)
  123. local silver = floor((money - (gold * COPPER_PER_GOLD)) / COPPER_PER_SILVER)
  124. local copper = floor(money%COPPER_PER_SILVER)
  125. local text = ""
  126. local isFirst = true
  127. -- Trims 0 silver and/or 0 copper from the text
  128. if trim then
  129. if gold > 0 then
  130. if color then
  131. text = format("%s%s ", color..private:PadNumber(gold, pad and not isFirst).."|r", GOLD_ICON)
  132. else
  133. text = format("%s%s ", private:PadNumber(gold, pad and not isFirst), GOLD_ICON)
  134. end
  135. isFirst = false
  136. end
  137. if silver > 0 then
  138. if color then
  139. text = format("%s%s%s ", text, color..private:PadNumber(silver, pad and not isFirst).."|r", SILVER_ICON)
  140. else
  141. text = format("%s%s%s ", text, private:PadNumber(silver, pad and not isFirst), SILVER_ICON)
  142. end
  143. isFirst = false
  144. end
  145. if copper > 0 then
  146. if color then
  147. text = format("%s%s%s ", text, color..private:PadNumber(copper, pad and not isFirst).."|r", COPPER_ICON)
  148. else
  149. text = format("%s%s%s ", text, private:PadNumber(copper, pad and not isFirst), COPPER_ICON)
  150. end
  151. isFirst = false
  152. end
  153. if money == 0 then
  154. if color then
  155. text = format("%s%s%s ", text, color..private:PadNumber(copper, pad and not isFirst).."|r", COPPER_ICON)
  156. else
  157. text = format("%s%s%s ", text, private:PadNumber(copper, pad and not isFirst), COPPER_ICON)
  158. end
  159. isFirst = false
  160. end
  161. else
  162. -- Add gold
  163. if gold > 0 then
  164. if color then
  165. text = format("%s%s ", color..private:PadNumber(gold, pad and not isFirst).."|r", GOLD_ICON)
  166. else
  167. text = format("%s%s ", private:PadNumber(gold, pad and not isFirst), GOLD_ICON)
  168. end
  169. isFirst = false
  170. end
  171. -- Add silver
  172. if gold > 0 or silver > 0 then
  173. if color then
  174. text = format("%s%s%s ", text, color..private:PadNumber(silver, pad and not isFirst).."|r", SILVER_ICON)
  175. else
  176. text = format("%s%s%s ", text, private:PadNumber(silver, pad and not isFirst), SILVER_ICON)
  177. end
  178. isFirst = false
  179. end
  180. -- Add copper
  181. if color then
  182. text = format("%s%s%s ", text, color..private:PadNumber(copper, pad and not isFirst).."|r", COPPER_ICON)
  183. else
  184. text = format("%s%s%s ", text, private:PadNumber(copper, pad and not isFirst), COPPER_ICON)
  185. end
  186. end
  187. if isNegative then
  188. if color then
  189. return color .. "-|r" .. text:trim()
  190. else
  191. return "-" .. text:trim()
  192. end
  193. else
  194. return text:trim()
  195. end
  196. end
  197. -- Converts a formated money string back to the copper value
  198. function TSMAPI:UnformatTextMoney(value)
  199. -- remove any colors
  200. value = gsub(value, "|cff([0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])", "")
  201. value = gsub(value, "|r", "")
  202. -- extract gold/silver/copper values
  203. local gold = tonumber(string.match(value, "([0-9]+)g"))
  204. local silver = tonumber(string.match(value, "([0-9]+)s"))
  205. local copper = tonumber(string.match(value, "([0-9]+)c"))
  206. if gold or silver or copper then
  207. -- Convert it all into copper
  208. copper = (copper or 0) + ((gold or 0) * COPPER_PER_GOLD) + ((silver or 0) * COPPER_PER_SILVER)
  209. end
  210. return copper
  211. end