SanyaRamzik

CloudMarket

Jul 18th, 2020
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.02 KB | None | 0 0
  1. -- *//
  2. -- edit dbMarket_slot
  3. -- hts4 SanyaRamzik dwcity:Materia 3 minecraft:iron_ore 60
  4. -- hts4 FeyMen minecraft:iron_ingot 60 minecraft:iron_ore 25
  5. -- hts1 EnergyHuman minecraft:iron_ingot 56 minecraft:iron_block 7
  6. -- hts2 NoName_ dwcity:Powder11 1 dwcity:Materia 4
  7. -- //*
  8.  
  9. local component = require('component')
  10. local gpu=component.gpu
  11. local computer = require('computer')
  12. local event=require('event')
  13. local pull_e = event.pull
  14.  
  15. local me_inv=component.proxy('')
  16. local chest_bank=component.proxy('be5e1869-67ed-4193-afd5-01f2826ea47b')
  17. local me_bank=component.proxy('237662c0-722f-4aac-96da-c30f9d98e014')
  18. local rs_bank=component.proxy('1fe0abb4-ab8f-485e-bff0-2f26c878fae4')
  19.  
  20. me_sclad=component.proxy('0ea38a30-6426-455b-ab22-a812909cc4e1')
  21. rs_sclad=component.proxy('fcd7dc67-5ef5-4126-8dca-758b1b079fd5')
  22.  
  23. local W, H = gpu.getResolution()
  24. b_color, f_color = gpu.getBackground(), gpu.getForeground()
  25. local timeout=30
  26. local background_color=0x000040
  27.  
  28. log_nickname="TEST"
  29. local status='.'
  30. local active_slot='.'
  31.  
  32. Slots={
  33. {X=36,
  34. Y=5,
  35. W=110,
  36. H=1,
  37. color=background_color,
  38. textColor=0xFFFFFF,
  39. Server='htc4',
  40. Seller='user',
  41. Slot='mc:iron_block',
  42. Slot_count='1',
  43. Price='em',
  44. Price_count='1',
  45. action = function()
  46.    computer.beep();
  47. end
  48. }
  49. }
  50.  
  51. function event.shouldInterrupt()
  52.     return false
  53. end
  54.  
  55. function check_timeout()
  56.     if timeout<=0 then
  57.       logOut()
  58.       return false
  59.     end
  60.     timeout=timeout-1;
  61.     set_str_timeout()
  62.   end
  63.  
  64.   function add_bank_str(server,seller,reward_item,reward_count)
  65.     local file=io.open("\\dbMarket_bank","a")
  66.     file:write(server.." "..seller.." "..reward_item.." "..reward_count.."\n")
  67.     file:close()
  68. end
  69.  
  70.   function clear_plane()
  71.   gpu.setBackground(background_color); gpu.fill(3,4,32,7,' ')
  72.   end
  73.  
  74.   function clear_log()
  75.     gpu.setBackground(background_color)
  76.     gpu.setForeground(background_color)
  77.     gpu.fill(10,19,25,3,' ')
  78.   end
  79.  
  80.   function set_str_timeout()
  81.     gpu.fill(30,45,5,1,' ')
  82.     gpu.setForeground(0xFFFFFF)
  83.     gpu.setBackground(0x3324FF)
  84.     gpu.set(30,45,tostring(timeout))
  85.   end
  86.  
  87. function split( str, del )
  88.  
  89.     local t = {}
  90.  
  91.     repeat
  92.       local b, e = str:find( del, 1, true )
  93.  
  94.       if b ~= nil then
  95.          t[ #t + 1 ] = str:sub( 1, b - 1 )
  96.          str = str:sub( e + 1 )
  97.       end
  98.     until b == nil
  99.  
  100.     t[ #t + 1 ] = str
  101.  
  102.     return t
  103. end
  104.  
  105. function show_list()
  106.  for i=1,#Slots-1 do table.remove(Slots,#Slots) end
  107. gpu.setBackground(0x3324FF)
  108. gpu.fill(36,4,W,H,' ')
  109.  gpu.setForeground(0xFFFFFF)
  110.  gpu.set(40,3,'Server')
  111.  gpu.set(48,3,'|')
  112.  gpu.set(50,3,'Seller')
  113.  gpu.set(65,3,'Slot')
  114.  gpu.set(90,3,'Count')
  115.  gpu.set(100,3,'Price')
  116.  gpu.set(130,3,'Count')
  117.  fill_slots()
  118. end
  119.  
  120. function gift(slot,slot_count)
  121. me_sclad.exportItem({id=slot},"UP",slot_count)
  122. end
  123.  
  124. function check_valide(server,seller,slot,slot_count,price,price_count)
  125.   local count=0
  126.   local t=0
  127.   for i=1,chest_bank.getInventorySize() do
  128.     local slot_item=chest_bank.getStackInSlot(i)
  129.     if slot_item~=nil then t=t+1
  130.       --print(slot_item.id.." "..slot_item.qty.." | "..price.." "..price_count)
  131.       if slot_item.id==tostring(price) then count=count+slot_item.qty end
  132.     end
  133.   end
  134. if count>=tonumber(price_count) then
  135.    for i=1,t do rs_bank.setOutput(1,15) os.sleep(1) rs_bank.setOutput(1,0) end
  136.     count=0
  137.     t=0
  138.    gift(slot,slot_count)
  139.    check_valide_gpu(true)
  140.     add_bank_str(server,seller,price,price_count)
  141.     remove_slot(active_slot)
  142. else check_valide_gpu(false)
  143. end
  144. end
  145.  
  146. function check_valide_gpu(bool)
  147.   clear_plane()
  148.   gpu.setForeground(0xFFFFFF)
  149.   if bool then
  150.     gpu.setBackground(0x33B600)
  151.     gpu.set(8,7,"Покупка оформлена!")
  152.   else
  153.     gpu.setBackground(0xCC0040)
  154.     gpu.set(8,7,"Недостаточно средств!")
  155.   end
  156.     gpu.set(3,4,"┌"); gpu.set(32,4,"┐")
  157.     gpu.set(3,10,"└"); gpu.set(32,10,"┘")
  158.     gpu.fill(4,4,28,1,"─"); gpu.fill(4,10,28,1,"─")
  159.     gpu.fill(3,5,1,5,"│"); gpu.fill(32,5,1,5,"│")
  160. end
  161.  
  162. function fill_slots()
  163. local t={}
  164.   for line in io.lines("\\dbMarket_slot") do
  165.    local slot=split(line,' ')
  166. Slots[#Slots+1]={
  167. Server=slot[1],
  168. Seller=slot[2],
  169. Slot=slot[3],
  170. Slot_count=slot[4],
  171. Price=slot[5],
  172. Price_count=slot[6],
  173. W=110,
  174. H=1,
  175. X=36,
  176. Y=Slots[#Slots].Y+2,
  177. color=background_color,
  178. textColor=0xFFFFFF,
  179. action = function()
  180.   computer.beep()
  181.  end
  182. }
  183.   end
  184.  for i=1,#Slots do drawSlot(i) end
  185. end
  186.  
  187. function remove_slot(active_slot)
  188.     local line=Slots[active_slot].Server.." "..Slots[active_slot].Seller.." "..Slots[active_slot].Slot.." "..Slots[active_slot].Slot_count.." "..Slots[active_slot].Price.." "..Slots[active_slot].Price_count
  189.     table.remove(Slots,active_slot)
  190.  local f = io.open("\\dbMarket_slot", "r")
  191.  local content = f:read("*all")
  192.  f:close()
  193.  content = content:gsub(line.."\n", ""); content=content:gsub(line,"")
  194.  local f = io.open("\\dbMarket_slot", "w")
  195.  f:write(content)
  196.  f:close()
  197. show_list()
  198. end
  199.  
  200. function drawSlot(n)
  201.     gpu.setBackground(Slots[n].color) -- задаем цвет кнопки
  202.     gpu.setForeground(Slots[n].textColor) -- задаем цвет текста
  203.   gpu.fill(Slots[n].X, Slots[n].Y, Slots[n].W, Slots[n].H, ' ') -- заливаем область
  204.   gpu.set(40,Slots[n].Y,Slots[n].Server)
  205.   gpu.set(48,Slots[n].Y,'|') gpu.set(48,Slots[n].Y-1,'|')
  206.   gpu.set(50,Slots[n].Y,Slots[n].Seller)
  207.   gpu.set(65,Slots[n].Y,Slots[n].Slot)
  208.   gpu.set(90,Slots[n].Y,Slots[n].Slot_count)
  209.   gpu.set(100,Slots[n].Y,Slots[n].Price)
  210.   gpu.set(130,Slots[n].Y,Slots[n].Price_count)
  211. end
  212.  
  213.   tButtons = {
  214.     {
  215.       visible = false,
  216.       X = W-5,
  217.       Y = 2,
  218.       W = 4,
  219.       H = 1,
  220.       color = 0xff0000,
  221.       textColor = 0xffffff,
  222.       text = 'X',
  223.       action = function()
  224.         event.cancel(1)
  225.         gpu.setBackground(b_color)
  226.         gpu.setForeground(f_color)
  227.         gpu.fill(1, 1, W, H, ' ')
  228.         os.exit()
  229.     end
  230.     },
  231.     {
  232.       visible = true,
  233.       X = 12.5,
  234.       Y = 35,
  235.       W = 15,
  236.       H = 3,
  237.       color = 0x3324FF,
  238.       textColor = 0xFFFFFF,
  239.       text = 'Expose',
  240.       action = function()
  241.         computer.beep()
  242.         event.cancel(1)
  243.         timeout=30
  244.         set_str_timeout()
  245.         clear_plane()
  246.         event.timer(1, function() check_timeout() end, 31)
  247.       end
  248.     },
  249.     {
  250.       visible=true,
  251.       X=12.5,
  252.       Y=40,
  253.       W=15,
  254.       H=3,
  255.       color= 0x3324FF,
  256.       textColor=0xFFFFFF,
  257.       text = 'Login',
  258.       action = function()
  259.       computer.beep()
  260.        clear_log()
  261.    
  262.        gpu.setForeground(0xFFFFFF)
  263.        gpu.set(12.5,20,log_nickname)
  264.    
  265.        toggleVisible(3)
  266.        toggleVisible(2)
  267.        toggleVisible(4)
  268.        toggleVisible(5)
  269.    
  270.        status = computer.addUser(log_nickname)
  271.         event.timer(1,function() check_timeout() end, 31)
  272.       end
  273.     },
  274.     {
  275.       visible=true,
  276.       X=12.5,
  277.       Y=40,
  278.       W=15,
  279.       H=3,
  280.       color=0x3324FF,
  281.       textColor=0xFFFFFF,
  282.       text='Unlogin',
  283.       action = function()
  284.         computer.beep()
  285.         clear_plane()
  286.         logOut()
  287.       end
  288.     },
  289. {
  290.       visible = true,
  291.       X = 12.5,
  292.       Y = 30,
  293.       W = 15,
  294.       H = 3,
  295.       color = 0x3324FF,
  296.       textColor = 0xFFFFFF,
  297.       text = 'Auction',
  298.       action = function()
  299.         computer.beep()
  300.         event.cancel(1)
  301.         timeout=30
  302.         set_str_timeout()
  303.         show_list()
  304.         event.timer(1, function() check_timeout() end, 31)
  305.       end
  306.     }
  307.   }
  308.  
  309.   function logOut()
  310.     event.cancel(1)
  311.     timeout=30
  312.     clear_log()
  313.     status=computer.removeUser(log_nickname)
  314.     log_nickname=" "    
  315.     toggleVisible(4)
  316.     toggleVisible(5)
  317.     toggleVisible(3)
  318.     toggleVisible(2)
  319.   end
  320.  
  321.   function drawButton(n) -- функция рисования кнопки
  322.     gpu.setBackground(tButtons[n].color) -- задаем цвет кнопки
  323.     gpu.setForeground(tButtons[n].textColor) -- задаем цвет текста
  324.     gpu.fill(tButtons[n].X, tButtons[n].Y, tButtons[n].W, tButtons[n].H, ' ') -- заливаем область
  325.     gpu.set(tButtons[n].X+(tButtons[n].W/2)-(#tButtons[n].text/2), tButtons[n].Y+(tButtons[n].H/2), tButtons[n].text) -- пишем текст по центру
  326.   end
  327.  
  328.   function toggleVisible(n) -- переключение видимости кнопки
  329.     if tButtons[n].visible then -- если кнопка видима
  330.       tButtons[n].visible = false -- отключаем
  331.       gpu.setBackground(background_color) -- берем цвет фона, полученный при старте программы
  332.       gpu.fill(tButtons[n].X, tButtons[n].Y, tButtons[n].W, tButtons[n].H, ' ') -- стираем кнопку
  333.     else -- если кнопка не активна
  334.       tButtons[n].visible = true -- активируем
  335.       drawButton(n) -- запускаем отрисовку
  336.     end
  337.   end
  338.    
  339. function blink(type,n) -- мигание кнопки
  340.     if type==1 then
  341.     tButtons[n].color, tButtons[n].textColor = tButtons[n].textColor, tButtons[n].color -- меняем местами цвета фона и текста
  342.     drawButton(n) -- отрисовываем кнопку
  343.     os.sleep(0.09) -- делаем задержку
  344.     tButtons[n].color, tButtons[n].textColor = tButtons[n].textColor, tButtons[n].color -- меняем цвета обратно
  345.     drawButton(n) -- перерисовываем кнопку
  346.     else
  347.       Slots[n].color, Slots[n].textColor = Slots[n].textColor, Slots[n].color -- меняем местами цвета фона и текста
  348.       drawSlot(n) -- отрисовываем кнопку
  349.       os.sleep(0.09) -- делаем задержку
  350.       Slots[n].color, Slots[n].textColor = Slots[n].textColor, Slots[n].color -- меняем цвета обратно
  351.       drawSlot(n) -- перерисовываем кнопку
  352.     end
  353. end
  354.  
  355.   gpu.setBackground(background_color)
  356.   gpu.setForeground(background_color)
  357.   gpu.fill(1, 1, W, H, ' ') -- очищаем экран
  358.    
  359.   for i = 1, #tButtons do
  360.     toggleVisible(i) -- активируем каждую кнопку
  361.   end
  362.   toggleVisible(3)
  363.  
  364.   gpu.setBackground(background_color)
  365. gpu.setForeground(0xFFFFFF)
  366. gpu.fill(1,1,W,1,'─') --горизонтальные черты
  367. gpu.fill(1,H,W,1,'─')
  368. gpu.set(W-30,H,"|-edited by SanyaRamzik-|") --подпись
  369. gpu.set(13,1,'|Warp LEGEND|')
  370.  
  371. gpu.setForeground(0x66B6FF)
  372. gpu.set(5,45,'Таймаут, выход через: ')
  373. set_str_timeout()
  374. gpu.setBackground(background_color)
  375. for i=1,H  do
  376.     gpu.set(35,i,"│")  --вертикальная черта
  377.     end
  378.  
  379.     while true do
  380.         local tEvent = {pull_e('touch')} -- ждем клика
  381.         for i = 1, #tButtons do -- перебираем все кнопки
  382.           if tButtons[i].visible then -- если кнопка активна
  383.             if tEvent[3] >= tButtons[i].X and tEvent[3] <= tButtons[i].X+tButtons[i].W and tEvent[4] >= tButtons[i].Y and tEvent[4] <= tButtons[i].Y+tButtons[i].H then -- если клик произведен в пределах кнопки
  384.                local usr={computer.users()}
  385.                log_nickname=tostring(tEvent[6])
  386.               if log_nickname==tostring(usr[1]) and tButtons[3].visible==false then
  387.                  blink(1,i) -- мигнуть кнопкой
  388.                  tButtons[i].action() -- выполнить назначенный код
  389.                  break
  390.               elseif tButtons[3].visible==true then
  391.                  blink(1,i)
  392.                  tButtons[i].action()
  393.                  break
  394.               end
  395.             end
  396.           end
  397.         end
  398.         for i = 1, #Slots do
  399.           if tEvent[3] >= Slots[i].X and tEvent[3] <= Slots[i].X+Slots[i].W and tEvent[4] >= Slots[i].Y and tEvent[4] <= Slots[i].Y+Slots[i].H then
  400.             local usr={computer.users()}
  401.             log_nickname=tostring(tEvent[6])
  402.             if log_nickname==tostring(usr[1]) then
  403.                 active_slot=i
  404.               blink(2,i) -- мигнуть кнопкой
  405.               Slots[i].action() -- выполнить назначенный код
  406.                 check_valide(Slots[i].Server,Slots[i].Seller,Slots[i].Slot,Slots[i].Slot_count,Slots[i].Price,Slots[i].Price_count)
  407.               break
  408.             end
  409.           end
  410.         end
  411.       end
Add Comment
Please, Sign In to add comment