Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dieCount = 0
- dice = {}
- results = {}
- numberOfRolls = 0
- greenRGB = {r=0, g=1, b=0}
- redRGB = {r=1, g=0, b=0}
- --Returns real table size
- function getRealTableSize(tbl)
- count = 0
- for _ in pairs(tbl) do
- count = count + 1
- end
- return count
- end
- --This detects when a die is placed on the notecard and registers it.
- -- A die can only be registered once.
- function onCollisionEnter(collision_info)
- l_colObj = collision_info.collision_object
- if l_colObj.tag ~= "Dice" then
- print("Can only register dice!")
- return
- end
- if dieCount > 0 then
- for i = 1, dieCount do
- if l_colObj.guid == dice[i].guid then
- print("This die is already registered!")
- return
- end
- end
- end
- dieCount = dieCount + 1
- extendResults(#l_colObj.getRotationValues())
- dice[dieCount] = l_colObj
- infiltrateDie(l_colObj, dieCount)
- broadcastToAll("New die registered, " .. (dieCount) .. " dice registered", greenRGB)
- end
- function onLoad()
- self.scale({x=1, y=0.1, z=1})
- updateXml()
- end
- function onDiceRolled(dice)
- dice.setVar("rolledRecently", true)
- end
- -- Injects custom code into the dices scripts
- function infiltrateDie(die, dieN)
- stuffToAdd = "getObjectFromGUID(\"" .. self.getGUID() .. "\").call(\"onDiceRolled\", self)"
- dieScript = die.getLuaScript()
- scrStart, scrEnd = string.find(dieScript, "function onRandomize%(%)")
- if(scrStart != nil) then
- scriptBefore = string.sub(dieScript, 1, scrEnd)
- scriptAfter = string.sub(dieScript, scrEnd + 1, #dieScript)
- dieScript = scriptBefore .. "\n " .. stuffToAdd .. scriptAfter
- else
- dieScript = dieScript .. "\nfunction onRandomize()\n " .. stuffToAdd .. "\nend"
- end
- die.setLuaScript(dieScript)
- newDieReference = die.reload() --only call once!
- dice[dieN] = newDieReference
- end
- -- Updates the UI when necessary
- function update()
- if dieCount == 0 then
- return -- Returns when no die has been registered yet
- end
- if(dice[1].getVar("rolledRecently")) then
- if(dice[1].resting) then
- dice[1].setVar("rolledRecently", false)
- sum = 0
- for i = 1, dieCount do
- sum = sum + dice[i].getValue()
- end
- results[sum] = results[sum] + 1
- numberOfRolls = numberOfRolls + 1
- updateXml()
- end
- end
- end
- -- Extends the list of results by n
- function extendResults(n)
- for i = (#results+1), (#results+n) do
- results[i] = 0
- end
- end
- -- Updates the UIs XML
- function updateXml()
- panelHeight = 90 + (#results*40)
- if dieCount > 1 then
- panelHeight = panelHeight - (dieCount-1)*40
- end
- panelAttributes = {
- height = panelHeight,
- width = 500,
- color = "rgba(0,0,0,0.7)",
- }
- xmlTable = {}
- xmlTableLayout = generateTag("TableLayout", panelAttributes)
- topRowLayout = generateTag("VerticalLayout", {})
- topRow = generateRow(90)
- children = {}
- table.insert(children, topRowLayout)
- appendChild(topRow, generateTagWithChildren("cell", {columnSpan=5}, children))
- appendChild(topRowLayout, generateText(50, "blue", "DICE STATS"))
- appendChild(topRowLayout, generateText(25, "red", "Place a die on top of this panel to track it"))
- appendChild(xmlTableLayout, topRow)
- if dieCount > 0 then
- for i = dieCount, #results do
- appendChild(xmlTableLayout, generateStatRow(i))
- end
- end
- table.insert(xmlTable, xmlTableLayout)
- self.UI.setXmlTable(xmlTable)
- end
- -- Generates a row with statistics for the given number
- function generateStatRow(n)
- percent = results[n]*100/numberOfRolls
- descriptionCell = generateTag("cell", {})
- appendChild(descriptionCell, generateTextDesc("" .. n))
- graphCell = generateTag("cell", {columnSpan=4})
- graph = generateTag("ProgressBar", {showPercentageText="false", color="#FFFFFF33", percentage=percent, raycastTarget=false})
- appendChild(graph, generateText(25, "#000000FF", "" .. results[n]))
- appendChild(graphCell, graph)
- row = generateRow(40)
- appendChild(row, descriptionCell)
- appendChild(row, graphCell)
- return row
- end
- -- Generates a table row
- function generateRow(height)
- return generateTag("row", {preferredHeight=height})
- end
- -- Generates XmlTagTable
- function generateTag(uTag, uAttributes)
- if uTag == nil then
- return nil
- end
- tagTable = {
- tag = uTag,
- }
- if #uAttributes ~= nil then
- tagTable["attributes"] = uAttributes
- end
- return tagTable
- end
- --Generates Tag with children
- function generateTagWithChildren(uTag, uAttr, children)
- tag = generateTag(uTag, uAttr)
- tag["children"] = children
- return tag
- end
- -- Generates white text
- function generateTextDesc(text)
- return generateText(25, "white", text)
- end
- -- Generates a text tag
- function generateText(size, textColor, content)
- textAttributes = {
- alignment="UpperCenter",
- fontSize=size,
- color=textColor,
- text=content,
- }
- return generateTag("Text", textAttributes)
- end
- -- Appends given TagTable to children of parent
- function appendChild(parent, child)
- if parent["children"] == nil then
- parent["children"] = {}
- end
- table.insert(parent["children"], child)
- end
Add Comment
Please, Sign In to add comment