joadson

Client Side

Mar 3rd, 2021
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.21 KB | None | 0 0
  1. local opcode = 103
  2.  local KEY = "Shift+C"
  3. local MainWindow, CreateWindow, SpeedUpWindow
  4. local RankPanel, CraftListPanel
  5. local CraftItemCollect, CraftItemCreate, SpeedUpCraft
  6. local lastPlayerPosition = nil
  7. local WorkNivel = 0
  8.  
  9. function init()
  10.   connect(g_game, {
  11.     onGameStart = offline,
  12.     onGameEnd = offline,
  13.     onWalk = check,
  14.     onAutoWalk = check
  15.   })
  16.  
  17.   MainWindow = g_ui.loadUI('craft', modules.game_interface.getRootPanel())
  18.  
  19.   RankPanel = MainWindow:getChildById('rankPanel')
  20.   CraftListPanel = MainWindow:getChildById('craftListPanel')
  21.   connect(CraftListPanel, {onChildFocusChange = function(self, focusedChild, unfocusedChild, reason)
  22.     if focusedChild == nil then return end
  23.     updateCraftPanel(self, focusedChild, unfocusedChild, reason)
  24.   end})
  25.  
  26.   CraftItemCollect = MainWindow:getChildById('craftItemCollect')
  27.   CraftItemCreate = MainWindow:getChildById('craftItemCreate')
  28.   SpeedUpCraft = MainWindow:getChildById('speedUpCraft')
  29.  
  30.   MainWindow:hide()
  31.  
  32.   CreateWindow = g_ui.createWidget('CreateWindow', modules.game_interface.getRootPanel())
  33.   for slot=1, 16 do
  34.     local recipeItem = g_ui.createWidget("Item", CreateWindow:getChildById('recipe'))
  35.     recipeItem:setId(slot)
  36.     recipeItem:setVirtual(true)
  37.   end
  38.  
  39.   SpeedUpWindow = g_ui.createWidget('SpeedUpWindow', modules.game_interface.getRootPanel())
  40.  
  41.   CreateWindow:hide()
  42.   SpeedUpWindow:hide()
  43.  
  44.    g_keyboard.bindKeyDown(KEY, toggle)
  45.   ProtocolGame.registerExtendedOpcode(opcode, getCraft)
  46. end
  47.  
  48. function terminate()
  49.   disconnect(g_game, {
  50.     onGameStart = offline,
  51.     onGameEnd = offline,
  52.     onWalk = check,
  53.     onAutoWalk = check
  54.   })
  55.    g_keyboard.unbindKeyDown(KEY, toggle)
  56.   ProtocolGame.unregisterExtendedOpcode(opcode)
  57.   MainWindow:destroy()
  58.   CreateWindow:destroy()
  59.   SpeedUpWindow:destroy()
  60. end
  61.  
  62. function offline()
  63.   removeCraftEvents()
  64.   CraftListPanel:destroyChildren()
  65.   hide()
  66. end
  67.  
  68. function toggle()
  69.   if MainWindow:isVisible() then
  70.     MainWindow:hide()
  71.   else
  72.     MainWindow:show()
  73.   end
  74. end
  75.  
  76. function hide()
  77.   if MainWindow then
  78.     MainWindow:setVisible(false)
  79.   end
  80.   CreateWindow:hide()
  81.   SpeedUpWindow:hide()
  82. end
  83.  
  84. function removeCraftEvents()
  85.   for id, child in pairs(CraftListPanel:getChildren()) do
  86.     removeEvent(child.event)
  87.     child.event = nil
  88.   end
  89. end
  90.  
  91. function getServerItems(rank)
  92.   g_game.getProtocolGame():sendExtendedOpcode(opcode, '###RANK###'..rank)
  93. end
  94.  
  95. function showCreateWindow()
  96.   if not CraftListPanel:getFocusedChild() then return end
  97.   local INFO = CraftListPanel:getFocusedChild().INFO
  98.   CreateWindow:getChildById('item'):setItemId(INFO.itemid)
  99.   CreateWindow:getChildById('name'):setText(INFO.name..(INFO.qnt > 1 and " ("..INFO.qnt.."x)" or ""))
  100.   CreateWindow:getChildById('qntScrollBar'):setValue(1)
  101.   CreateWindow:getChildById('labelTotal'):setText(tr('Total units')..': '..(INFO.qnt > 1 and "1 ("..INFO.qnt.."x)" or "1")..'\n'..tr('Total time')..': '..INFO.timeByUnit..'s')
  102.   for slot=1, 16 do
  103.     local recipeItem = CreateWindow:getChildById('recipe'):getChildById(slot)
  104.     recipeItem:setItemId(INFO.recipe[slot] and INFO.recipe[slot][1] or 0)
  105.     recipeItem:setItemCount(INFO.recipe[slot] and INFO.recipe[slot][2] or 0)
  106.     recipeItem:setTooltip(INFO.recipe[slot] and INFO.recipe[slot][3])
  107.   end
  108.   CreateWindow:show()
  109.   CreateWindow:focus()
  110.   CreateWindow:raise()
  111. end
  112.  
  113. function refreshCreateWindow()
  114.   if not CraftListPanel:getFocusedChild() then return end
  115.   local INFO = CraftListPanel:getFocusedChild().INFO
  116.   local qntValue = CreateWindow:getChildById('qntScrollBar'):getValue()
  117.   -- CreateWindow:getChildById('labelTotal'):setText(tr('Total units')..': '..(INFO.qnt > 1 and qntValue.." ("..INFO.qnt.."x)" or qntValue)..'\n'..tr('Total time')..': '..convertSecondsToString((qntValue * INFO.timeByUnit)))
  118.   CreateWindow:getChildById('labelTotal'):setText(tr('Total units')..': '..(INFO.qnt * qntValue)..'\n'..tr('Total time')..': '..convertSecondsToString((qntValue * INFO.timeByUnit)))
  119.   for _, recipe in ipairs(INFO.recipe) do
  120.     local recipeItem = CreateWindow:getChildById('recipe'):getChildById(_)
  121.     recipeItem:setItemCount(qntValue * recipe[2])
  122.     recipeItem:setTooltip(recipe[3])
  123.   end
  124. end
  125.  
  126. function doCreateItem()
  127.   g_game.getProtocolGame():sendExtendedOpcode(opcode, '###CRAFT###,RANK'..RankPanel:getFocusedChild():getId()..',ID'..tonumber(CraftListPanel:getFocusedChild():getId())..',QNT'..tonumber(CreateWindow:getChildById('qntScrollBar'):getValue()))
  128. end
  129.  
  130. function showSpeedUp()
  131.   local focusedChild = CraftListPanel:getFocusedChild()
  132.   if not focusedChild then return end
  133.   if not focusedChild.INFO then return end
  134.   local dustCost =  math.ceil(focusedChild.INFO.storage_time/(5*60))
  135.   SpeedUpWindow:getChildById('text'):setText(tr('Isso irá custar ')..dustCost..(dustCost > 1 and 's' or '')..' Você tem certeza que deseja acelerar o craft do '..focusedChild.INFO.name..'?')
  136.   SpeedUpWindow:show()
  137.   SpeedUpWindow:focus()
  138.   SpeedUpWindow:raise()
  139. end
  140.  
  141. function speedUpItem()
  142.   g_game.getProtocolGame():sendExtendedOpcode(opcode, RankPanel:getFocusedChild():getId()..'###SPEEDUP###'..tonumber(CraftListPanel:getFocusedChild():getId()))
  143.   SpeedUpWindow:hide()
  144. end
  145.  
  146. function collectItemCraft()
  147.   g_game.getProtocolGame():sendExtendedOpcode(opcode, RankPanel:getFocusedChild():getId()..'###COLLECT###'..tonumber(CraftListPanel:getFocusedChild():getId()))
  148. end
  149.  
  150. function updateCraftPanel(self, focusedChild, unfocusedChild, reason)
  151.   local focusedId = tonumber(focusedChild:getId())
  152.   focusedChild:setImageSource('images/interface/window_craft')
  153.   if focusedId then focusedChild:setImageSource('images/interface/'..(focusedId % 2 == 0 and "window_craftLight" or "window_craftLight")) end
  154.   if unfocusedChild then
  155.     local unfocusedId = tonumber(unfocusedChild:getId())
  156.     if unfocusedId then unfocusedChild:setImageSource('images/interface/'..(unfocusedId % 2 == 0 and "window_craft" or "window_craft")) end
  157.   end
  158.   if focusedChild.INFO then
  159.     if focusedChild.INFO.collectable > 0 then
  160.       CraftItemCollect:show()
  161.       SpeedUpCraft:addAnchor(AnchorRight, 'craftItemCollect', AnchorLeft)
  162.     else
  163.       CraftItemCollect:hide()
  164.       SpeedUpCraft:addAnchor(AnchorRight, 'craftItemCreate', AnchorLeft)
  165.     end
  166.     if focusedChild.INFO.storage_time > 0 then
  167.       SpeedUpCraft:show()
  168.     else
  169.       SpeedUpCraft:hide()
  170.     end
  171.   end
  172. end
  173.  
  174. function check()
  175.   local player = g_game.getLocalPlayer()
  176.   if not player then return end
  177.   local currentPosition = player:getPosition()
  178.  
  179.   if lastPlayerPosition then
  180.     if currentPosition ~= lastPlayerPosition then
  181.       hide()
  182.     end
  183.   end
  184. end
  185.  
  186. local RANKS = {["E"] = 5, ["D"] = 4, ["C"] = 3, ["B"] = 2, ["A"] = 1}
  187.  
  188. function getCraft(protocol, opcode, buffer)
  189.   local player = g_game.getLocalPlayer()
  190.   if not player then return end
  191.   local receive = loadstring("return ".. buffer)()
  192.   if receive[3] == 'work' then
  193.     local workName = Protocol_read(receive)
  194.     WorkNivel = tonumber(Protocol_read(receive))
  195.     local percent = Protocol_read(receive)
  196.     local open_window = Protocol_read(receive)
  197.     MainWindow:getChildById('workName'):setImageSource('images/topName/'..workName)
  198.     MainWindow:getChildById('workImagem'):setImageSource('images/works/'..workName)
  199.     MainWindow:getChildById('workNivel'):setText(WorkNivel)
  200.     MainWindow:getChildById('expBar'):setPercent(percent)
  201.     MainWindow:getChildById('expBar'):setText(percent.."%")
  202.     if open_window then
  203.       lastPlayerPosition = player:getPosition()
  204.       MainWindow:show()
  205.       MainWindow:focus()
  206.       MainWindow:raise()
  207.     end
  208.     print("aqui")
  209.   elseif receive[3] == 'refreshItem' then
  210.    print("aqui 2")
  211.     local rank = Protocol_read(receive)
  212.     local id = Protocol_read(receive)
  213.     local INFO = Protocol_read(receive)
  214.     CreateWindow:hide()
  215.     if (RankPanel:getFocusedChild():getId() == rank) and (tonumber(CraftListPanel:getFocusedChild():getId()) == id) then
  216.       local craftItemWidget = CraftListPanel:getChildById(id)
  217.       removeEvent(craftItemWidget.event)
  218.       craftItemWidget.event = nil
  219.       craftItemWidget.INFO.storage_qnt = INFO.storage_qnt
  220.       craftItemWidget.INFO.storage_collected = INFO.storage_collected
  221.       craftItemWidget.INFO.storage_time = INFO.storage_time
  222.       DrawItemInList(craftItemWidget, id, craftItemWidget.INFO, true)
  223.     end
  224.   elseif receive[3] == 'items' then
  225.    print("aqui 3")
  226.     local first = Protocol_read(receive)
  227.     local rank = Protocol_read(receive)
  228.     local max_board = Protocol_read(receive)
  229.     local items = Protocol_read(receive)
  230.     if first then
  231.       removeCraftEvents()
  232.       CraftListPanel:destroyChildren()
  233.       CreateWindow:hide()
  234.       if max_board then
  235.         local currentRank = RANKS[rank]
  236.         for id, child in pairs(RankPanel:getChildren()) do
  237.           if RANKS[child:getId()] < currentRank then
  238.             child:disable()
  239.           else
  240.             child:enable()
  241.           end
  242.         end
  243.       end
  244.       RankPanel:getChildById(rank):focus()
  245.     end
  246.     for id, INFO in ipairs(items) do
  247.       DrawItemInList(g_ui.createWidget("CraftItemWidget", CraftListPanel), INFO.id, INFO)
  248.     end
  249.   end
  250. end
  251.  
  252. function DrawItemInList(craftItemWidget, id, INFO, refresh)
  253.   INFO.collectable = 0
  254.   if not refresh then
  255.     craftItemWidget:setId(id)
  256.     craftItemWidget:getChildById('item'):setItemId(INFO.itemid)
  257.     -- craftItemWidget:getChildById('item'):setItemCount(INFO.qnt)
  258.     craftItemWidget:getChildById('name'):setText(INFO.name..(INFO.qnt > 1 and " ("..INFO.qnt.."x)" or ""))
  259.     craftItemWidget:getChildById('desc'):setText(INFO.desc)
  260.     craftItemWidget:getChildById('level'):setText(INFO.level)
  261.     craftItemWidget:getChildById("level").advancedTooltip = "level de profissão necessário para\nconstruir o item."
  262.     craftItemWidget:getChildById('time'):setText(convertSecondsToString(INFO.timeByUnit))
  263.     craftItemWidget:getChildById("time").advancedTooltip = "Tempo que durará para\nconstruir o item."
  264.     craftItemWidget:setImageSource('images/interface/'..(id == 1 and "window_craftLight" or (id % 2 == 0 and "window_craft" or "window_craft")))
  265.     for _, recipe in ipairs(INFO.recipe) do
  266.       local recipeItem = g_ui.createWidget("Item", craftItemWidget:getChildById('recipe'))
  267.       recipeItem:setItemId(recipe[1])
  268.       recipeItem:setItemCount(recipe[2])
  269.       recipeItem:setVirtual(true)
  270.       recipeItem:setTooltip(recipe[3])
  271.     end
  272.   end
  273.   craftItemWidget:getChildById('level'):setColor(WorkNivel < INFO.level and "#c30000" or "white")
  274.   local V1 = (INFO.storage_qnt * INFO.timeByUnit) -- VALOR TOTAL
  275.   local progressLabel = craftItemWidget:getChildById('progressLabel')
  276.   local progressBar = craftItemWidget:getChildById('progressBar')
  277.   local progressIcon = craftItemWidget:getChildById('progressIcon')
  278.   if INFO.storage_qnt > 0 then
  279.     local created = tonumber(INFO.storage_time == 0 and (INFO.storage_qnt > 0 and INFO.storage_qnt or "") or math.floor((V1 - INFO.storage_time) / INFO.timeByUnit))
  280.     INFO.collectable = created - (INFO.storage_collected == -1 and 0 or INFO.storage_collected)
  281.     progressLabel:setText("("..created.."("..INFO.collectable..")".."/"..INFO.storage_qnt..")")
  282.     progressLabel:show()
  283.     if INFO.storage_time <= 0 then
  284.       progressBar:setValue(100, 0, 100)
  285.       progressBar:setText("100%")
  286.       progressIcon:setImageSource("images/progressIcon")
  287.       progressIcon:show()
  288.     end
  289.     progressBar:show()
  290.     progressIcon:show()
  291.   else
  292.     progressBar:hide()
  293.     progressIcon:hide()
  294.     progressLabel:hide()
  295.   end
  296.   if CraftListPanel:getFocusedChild() == craftItemWidget then
  297.     if INFO.collectable > 0 then
  298.       CraftItemCollect:show()
  299.       SpeedUpCraft:addAnchor(AnchorRight, 'craftItemCollect', AnchorLeft)
  300.     else
  301.       CraftItemCollect:hide()
  302.       SpeedUpCraft:addAnchor(AnchorRight, 'craftItemCreate', AnchorLeft)
  303.     end
  304.     if INFO.storage_time > 0 then
  305.       SpeedUpCraft:show()
  306.     else
  307.       SpeedUpCraft:hide()
  308.     end
  309.   end
  310.   if INFO.storage_time > 0 then
  311.     local function reloadPercent()
  312.       INFO.storage_time = INFO.storage_time - 1
  313.       local V2 = INFO.storage_time -- VALOR NOVO OU ATUAL
  314.       local percent = -math.floor(((V2 - V1) / V1) * 100)
  315.       progressBar:setValue(percent, 0, 100)
  316.       progressBar:setText(convertSecondsToString(INFO.storage_time))
  317.       local created = tonumber(INFO.storage_time == 0 and (INFO.storage_qnt > 0 and INFO.storage_qnt or "") or math.floor((V1 - INFO.storage_time) / INFO.timeByUnit))
  318.       INFO.collectable = created - (INFO.storage_collected == -1 and 0 or INFO.storage_collected)
  319.       progressLabel:setText("("..created.."("..INFO.collectable..")".."/"..INFO.storage_qnt..")")
  320.       if INFO.storage_time <= 0 then
  321.         progressBar:setText("100%")
  322.         progressIcon:setImageSource("images/progressIcon")
  323.         progressIcon:show()
  324.         removeEvent(craftItemWidget.event)
  325.         craftItemWidget.event = nil
  326.       end
  327.       if CraftListPanel:getFocusedChild() == craftItemWidget then
  328.         if INFO.collectable > 0 then
  329.           CraftItemCollect:show()
  330.           SpeedUpCraft:addAnchor(AnchorRight, 'craftItemCollect', AnchorLeft)
  331.         else
  332.           CraftItemCollect:hide()
  333.           SpeedUpCraft:addAnchor(AnchorRight, 'craftItemCreate', AnchorLeft)
  334.         end
  335.         if INFO.storage_time > 0 then
  336.           SpeedUpCraft:show()
  337.         else
  338.           SpeedUpCraft:hide()
  339.         end
  340.       end
  341.       craftItemWidget.INFO = INFO
  342.     end
  343.     local V2 = INFO.storage_time -- VALOR ATUAL
  344.     local V1 = (INFO.storage_qnt * INFO.timeByUnit) -- VALOR TOTAL
  345.     local percent = -math.floor(((V2 - V1) / V1) * 100)
  346.     progressBar:setValue(percent, 0, 100)
  347.     progressBar:setText(convertSecondsToString(INFO.storage_time))
  348.     progressIcon:setImageSource("images/progressIconAnimated")
  349.     craftItemWidget.event = cycleEvent(reloadPercent, 1000)
  350.   else
  351.     removeEvent(craftItemWidget.event)
  352.     craftItemWidget.event = nil
  353.   end
  354.   craftItemWidget.INFO = INFO
  355. end
  356.  
  357. function convertSeconds(seconds)
  358.   local hours = 0
  359.   local minutes = 0
  360.   repeat
  361.     if seconds >= 60 then
  362.       minutes = minutes + 1; seconds = seconds - 60
  363.     elseif minutes >= 60 then
  364.       hours = hours + 1; minutes = minutes - 60
  365.     end
  366.   until seconds < 60 and minutes < 60
  367.   return {hours = hours, seconds = seconds, minutes = minutes}
  368. end
  369.  
  370. function convertSecondsToString(seconds)
  371.   local time = convertSeconds(tonumber(seconds))
  372.   return (time.hours > 0 and time.hours.."h " or "") .. (time.minutes > 0 and time.minutes.."m " or "") .. time.seconds.."s"
  373. end
Add Comment
Please, Sign In to add comment