Phil_N

DiceStats v1.3a

Apr 10th, 2020 (edited)
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.53 KB | None | 0 0
  1. dieCount = 0
  2. dice = {}
  3. results = {}
  4. numberOfRolls = 0
  5. greenRGB = {r=0, g=1, b=0}
  6. redRGB = {r=1, g=0, b=0}
  7.  
  8. --Returns real table size
  9. function getRealTableSize(tbl)
  10.     count = 0
  11.     for _ in pairs(tbl) do
  12.         count = count + 1
  13.     end
  14.     return count
  15. end
  16.  
  17. --This detects when a die is placed on the notecard and registers it.
  18. --    A die can only be registered once.
  19.  
  20. function onCollisionEnter(collision_info)
  21.     l_colObj = collision_info.collision_object
  22.    
  23.     if l_colObj.tag ~= "Dice" then
  24.        print("Can only register dice!")
  25.        return
  26.     end
  27.     if dieCount > 0 then
  28.         for i = 1, dieCount do
  29.             if l_colObj.guid == dice[i].guid then
  30.                 print("This die is already registered!")
  31.                 return
  32.             end
  33.         end
  34.     end
  35.    
  36.     dieCount = dieCount + 1
  37.  
  38.     extendResults(#l_colObj.getRotationValues())
  39.     dice[dieCount] = l_colObj
  40.     infiltrateDie(l_colObj, dieCount)
  41.  
  42.    
  43.     broadcastToAll("New die registered, " .. (dieCount) .. " dice registered", greenRGB)
  44.  
  45. end
  46.  
  47.  
  48. function onLoad()
  49.     self.scale({x=1, y=0.1, z=1})
  50.     updateXml()
  51.  
  52. end
  53.  
  54.  
  55. function onDiceRolled(dice)
  56.     dice.setVar("rolledRecently", true)
  57. end
  58.  
  59. -- Injects custom code into the dices scripts
  60. function infiltrateDie(die, dieN)
  61.     stuffToAdd = "getObjectFromGUID(\"" .. self.getGUID() .. "\").call(\"onDiceRolled\", self)"
  62.     dieScript = die.getLuaScript()
  63.     scrStart, scrEnd = string.find(dieScript, "function onRandomize%(%)")
  64.  
  65.     if(scrStart != nil) then
  66.         scriptBefore = string.sub(dieScript, 1, scrEnd)
  67.         scriptAfter = string.sub(dieScript, scrEnd + 1, #dieScript)
  68.         dieScript = scriptBefore .. "\n    " .. stuffToAdd .. scriptAfter        
  69.     else
  70.         dieScript = dieScript .. "\nfunction onRandomize()\n    " .. stuffToAdd .. "\nend"
  71.  
  72.     end
  73.  
  74.     die.setLuaScript(dieScript)
  75.     newDieReference = die.reload() --only call once!
  76.     dice[dieN] = newDieReference
  77. end
  78.  
  79. -- Updates the UI when necessary
  80. function update()
  81.     if dieCount == 0 then
  82.         return -- Returns when no die has been registered yet
  83.     end
  84.     if(dice[1].getVar("rolledRecently")) then
  85.         if(dice[1].resting) then
  86.  
  87.             dice[1].setVar("rolledRecently", false)
  88.             sum = 0
  89.  
  90.             for i = 1, dieCount do
  91.                 sum = sum + dice[i].getValue()
  92.             end
  93.  
  94.             results[sum] = results[sum] + 1
  95.             numberOfRolls = numberOfRolls + 1
  96.             updateXml()
  97.         end
  98.     end
  99. end
  100.  
  101. -- Extends the list of results by n
  102. function extendResults(n)
  103.     for i = (#results+1), (#results+n) do
  104.         results[i] = 0
  105.     end
  106. end
  107.  
  108.  
  109. -- Updates the UIs XML
  110. function updateXml()
  111.    
  112.     panelHeight = 90 + (#results*40)
  113.     if dieCount > 1 then
  114.         panelHeight = panelHeight - (dieCount-1)*40
  115.     end
  116.    
  117.     panelAttributes = {
  118.         height = panelHeight,
  119.  
  120.         width = 500,
  121.         color = "rgba(0,0,0,0.7)",
  122.     }
  123.    
  124.     xmlTable = {}
  125.     xmlTableLayout = generateTag("TableLayout", panelAttributes)
  126.    
  127.     topRowLayout = generateTag("VerticalLayout", {})
  128.     topRow = generateRow(90)
  129.    
  130.     children = {}
  131.     table.insert(children, topRowLayout)
  132.     appendChild(topRow, generateTagWithChildren("cell", {columnSpan=5}, children))
  133.     appendChild(topRowLayout, generateText(50, "blue", "DICE STATS"))
  134.     appendChild(topRowLayout, generateText(25, "red", "Place a die on top of this panel to track it"))
  135.    
  136.     appendChild(xmlTableLayout, topRow)
  137.    
  138.     if dieCount > 0 then
  139.         for i = dieCount, #results do
  140.             appendChild(xmlTableLayout, generateStatRow(i))
  141.         end
  142.     end
  143.  
  144.     table.insert(xmlTable, xmlTableLayout)
  145.     self.UI.setXmlTable(xmlTable)
  146. end
  147.  
  148. -- Generates a row with statistics for the given number
  149. function generateStatRow(n)
  150.    
  151.     percent = results[n]*100/numberOfRolls
  152.     descriptionCell = generateTag("cell", {})
  153.     appendChild(descriptionCell, generateTextDesc("" .. n))
  154.     graphCell = generateTag("cell", {columnSpan=4})
  155.     graph = generateTag("ProgressBar", {showPercentageText="false", color="#FFFFFF33", percentage=percent, raycastTarget=false})
  156.     appendChild(graph, generateText(25, "#000000FF", "" .. results[n]))
  157.     appendChild(graphCell, graph)
  158.    
  159.     row = generateRow(40)
  160.     appendChild(row, descriptionCell)
  161.     appendChild(row, graphCell)
  162.     return row
  163. end
  164.  
  165.  
  166. -- Generates a table row
  167. function generateRow(height)
  168.     return generateTag("row", {preferredHeight=height})
  169. end
  170.  
  171. -- Generates XmlTagTable
  172. function generateTag(uTag, uAttributes)
  173.     if uTag == nil then
  174.         return nil
  175.     end
  176.     tagTable = {
  177.         tag = uTag,
  178.     }
  179.     if #uAttributes ~= nil then
  180.         tagTable["attributes"] = uAttributes
  181.     end
  182.     return tagTable
  183. end
  184.  
  185. --Generates Tag with children
  186. function generateTagWithChildren(uTag, uAttr, children)
  187.     tag = generateTag(uTag, uAttr)
  188.     tag["children"] = children
  189.     return tag
  190. end
  191.  
  192. -- Generates white text
  193. function generateTextDesc(text)
  194.    return generateText(25, "white", text)
  195. end
  196.  
  197. -- Generates a text tag
  198. function generateText(size, textColor, content)
  199.     textAttributes = {
  200.         alignment="UpperCenter",
  201.         fontSize=size,
  202.         color=textColor,
  203.         text=content,
  204.     }
  205.     return generateTag("Text", textAttributes)
  206. end
  207.  
  208. -- Appends given TagTable to children of parent
  209. function appendChild(parent, child)
  210.     if parent["children"] == nil then
  211.         parent["children"] = {}
  212.     end
  213.     table.insert(parent["children"], child)
  214. end
  215.  
Add Comment
Please, Sign In to add comment