Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.97 KB | None | 0 0
  1. local serialization = require("serialization")
  2. local event = require("event")
  3. local component = require("component")
  4. local gpu = component.gpu
  5. local name_server = "bd"
  6. local name_client = "cli"
  7. local lname_server = "listbd"
  8. item_list={}
  9. robot_authkey = 121212
  10. listen_port = 2000
  11. server_port = 4000
  12. srvlist_port = 5000
  13. wlisten_port = 22000
  14. robot_port = 12000
  15. local event = require("event")
  16. rd_from = 1.86
  17. rd_to = 2.77
  18. hr={5,55,105}
  19. vr={7,12,17,22,27,32,37,42,47}
  20. local radar = component.radar
  21. local nmodem = component.proxy(component.get("86592"))
  22. local wmodem = component.proxy(component.get("593c745"))
  23. --[[list = {
  24.     {name = "", price = 123},
  25. }
  26. --]]
  27. --dofile(list.lua)  -- список продаваемых товаров за юниты
  28.                     -- list[].name, list[].price
  29.                     --component.inventory_controller.get StackInInternalSlot()
  30. function is_russian_letter(char)
  31.    local c = char:byte() * 256 + char:byte(-1)
  32.    return c >= 0xD090 and c <= 0xD18F or c == 0xD081 or c == 0xD191
  33. end
  34.  
  35.  
  36. function text(xt,yt,str)
  37. if str~="" then
  38.     ada = 0
  39.     m=0
  40.     if is_russian_letter(str:sub(1,2)) then m=1 ada=1 end
  41.     size = #str
  42.     if size>40 then size = 40 end
  43.     for i=1,size,1 do
  44.         gpu.fill(xt+i-1,yt,1,1,str:sub(i+(i-1)*m,i+(i-1)*m+ada))
  45.     end
  46. end
  47. end
  48. function asort(list,num)
  49.     --Отрисовка асортимента
  50.     --рамка
  51.     gpu.setBackground(0x4becf2)--
  52.     gpu.fill(2,7,48,3," ")
  53.     gpu.setBackground(0x4aa4f1)
  54.     gpu.fill(1,6,50,1," ")
  55.     gpu.fill(1,10,50,1," ")
  56.     gpu.fill(1,7,1,3," ")
  57.     gpu.fill(50,7,1,3," ")
  58.     gpu.copy(1,6,50,5,50,0)
  59.     gpu.copy(1,6,50,5,100,0)
  60.     gpu.copy(1,6,150,5,0,5)
  61.     gpu.copy(1,6,150,5,0,10)
  62.     gpu.copy(1,6,150,10,0,15)
  63.     gpu.copy(1,6,150,10,0,25)
  64.     gpu.copy(1,6,150,10,0,35)
  65.     gpu.setBackground(0x4becf2)
  66.     gpu.setForeground(0x000000)
  67.     if num==nil or num<0 or num>#list then num = 1 end
  68.     lt = #list
  69.     if lt == nil then lt = 0 end
  70.     if lt > 27 then lt = 27+num-1 end
  71.     for i=num-1,lt-1,1 do
  72.         j=i - num + 1
  73.         text(hr[1+j%3],vr[1+(j-j%3)/3],list[i+1][2])
  74.         text(hr[1+j%3],2+vr[1+(j-j%3)/3],list[i+1][3] .. " юнитов")
  75.     end
  76. end
  77.  
  78. function drawgui(rec)
  79.     os.execute("clear")
  80.     --отрисовка шапки
  81. if rec == nil or rec[1]==nil or rec[2]==nil or rec[3]==nil then rec = {"",0,0} end
  82.     gpu.setBackground(0x504bf2)
  83.     gpu.fill(1,1,160,5," ")
  84.     gpu.setForeground(0xdef24b)
  85.     text(1,1,"Приветствую!")
  86.     text(50,1,"Баланс:")
  87.     text(100,1,"Задолженность:")
  88.     text(1,2,rec[1])
  89.     text(50,2,"" .. rec[2])
  90.     text(100,2,"" .. rec[3])
  91.  
  92.     --скролл
  93.     gpu.setBackground(0x0d0282)
  94.     gpu.setForeground(0xffffff)
  95.     gpu.fill(151,6,10,10," ")
  96.     gpu.fill(151,41,10,10," ")
  97.     gpu.setBackground(0x4b6f70)
  98.     gpu.fill(151,16,10,25," ")
  99.     text(155,7,"/\\")
  100.     text(154,8,"/  \\")
  101.     text(155,49,"\\/")
  102.     text(154,48,"\\  /")
  103. end
  104.  
  105. function wait(txt)
  106.     gpu.setBackground(0x504bf2)
  107.     gpu.setForeground(0xdef24b)
  108.     text(70,3,txt)
  109. end
  110.  
  111. function clwait( ... )
  112.     gpu.setBackground(0x504bf2)
  113.     gpu.setForeground(0xdef24b)
  114.     gpu.fill(1,3,150,1," ")
  115. end
  116.  
  117. function deauth()
  118.     gpu.setBackground(0x000000)
  119.     gpu.setForeground(0xffffff)
  120.     text(50,10,"***********")
  121.     text(50,11,"Досвидания!")
  122.     text(50,12,"***********")
  123. end
  124.  
  125. --авторизация
  126. function auth_player()
  127.     pl = radar.getPlayers()
  128.     pr = 0
  129.     name = ""
  130.     for i=1,#pl,1 do
  131.         if pl[i].distance < rd_to and pl[i].distance>rd_from then
  132.             name = pl[i].name
  133.             pr = pr + 1
  134.         end
  135.     end
  136.     if pr == 1 then
  137.         return name,true
  138.     else
  139.         return "",false
  140.     end
  141. end
  142.  
  143. function get_key()
  144.     key = math.random(1000000)
  145.     if key%2==0 then key = key + 1 end
  146.     return key
  147. end
  148.  
  149. function msg_to_server(request, k, rcd,port,lport,ns)
  150. --  rcd = nil
  151.     nmodem.open(lport)
  152.     nmodem.broadcast(port,serialization.serialize({ns,request,k,rcd}))
  153.     _,_,_,_,_,answ = event.pull(2,"modem")
  154.     if answ~=nil then answ = serialization.unserialize(answ) end
  155.     _,ath = auth_player()
  156.     while (answ == nil or answ[3]~=k) and ath do
  157.         nmodem.broadcast(port,serialization.serialize({ns,request,k,rcd}))
  158.         _,_,_,_,_,answ = event.pull(2,"modem")
  159.         if answ~=nil then answ = serialization.unserialize(answ) end
  160.         _,ath = auth_player()
  161.     end
  162.     nmodem.close(lport)
  163.     if answ~=nil then
  164.         return answ[4]
  165.     else
  166.         return nil
  167.     end
  168. end
  169.  
  170.  
  171.  
  172. function getrec(name)
  173.     return msg_to_server("get",get_key(),{name},server_port,listen_port,name_server)
  174. end
  175.  
  176. function buy(name,sum)
  177.     return msg_to_server("wrf",get_key(),{name,sum},server_port,listen_port,name_server)
  178. end
  179.  
  180. function addrec(name,sum)
  181.     return msg_to_server("add",get_key(),{name,sum,0},server_port,listen_port,name_server)
  182. end
  183.  
  184. function getlist(name)
  185.     return msg_to_server("get",get_key(),{name},srvlist_port,listen_port,lname_server)
  186. end
  187.  
  188. function getfulllist(name)
  189.     return msg_to_server("getlist",get_key(),{name},srvlist_port,listen_port,lname_server)
  190. end
  191.  
  192. function dellist(name,sum)
  193.     return msg_to_server("del",get_key(),{name,sum},srvlist_port,listen_port,lname_server)
  194. end
  195.  
  196. function addlist(name,label,buy,sell)
  197.     return msg_to_server("add",get_key(),{name,"",buy,sell},srvlist_port,listen_port,lname_server)
  198. end
  199.  
  200. function robot_auth()
  201.     print("listen")
  202.     wmodem.open(wlisten_port)
  203.     _,_,_,_,_,answ = event.pull(2,"modem")
  204.     if answ~=nil then answ = serialization.unserialize(answ) end
  205.     while (answ == nil or answ[3]~=robot_authkey) do
  206.         _,_,_,_,_,answ = event.pull(2,"modem")
  207.         if answ~=nil then answ = serialization.unserialize(answ) end
  208.     end
  209.     wmodem.broadcast(robot_port,serialization.serialize({name_client,"answer",robot_authkey,"ok"}))
  210.     if answ~=nil then item_list = answ[4] end
  211.     wmodem.close(wlisten_port)
  212. end
  213.  
  214. function robot_drop(name,num)
  215.     component.redstone.setOutput(0,15)
  216.     wmodem.open(wlisten_port)
  217.     ky = get_key()
  218.     wmodem.broadcast(robot_port,serialization.serialize({name_client,"drop",ky,{name,num}}))
  219.     _,_,_,_,_,answ = event.pull(20,"modem")
  220.     if answ~=nil then answ = serialization.unserialize(answ) end
  221.     _,ath = auth_player()
  222.     while (answ == nil or answ[3]~=ky) and ath do
  223.         wmodem.broadcast(robot_port,serialization.serialize({name_client,"drop",ky,{name,num}}))
  224.         _,_,_,_,_,answ = event.pull(20,"modem")
  225.         if answ~=nil then answ = serialization.unserialize(answ) end
  226.         _,ath = auth_player()
  227.     end
  228.     wmodem.close(wlisten_port)
  229.     component.redstone.setOutput(0,0)
  230.     if answ[4] == "good" then return true else return false end
  231. end
  232.  
  233. function setprice()
  234.     lit = item_list
  235.     item_list = {}
  236.     for i = 1,#lit,1 do
  237.         anw = getlist(lit[i][1])
  238.         if anw[3]+0>0 then
  239.             item_list[#item_list+1] = {}
  240.             item_list[#item_list][1] = anw[1]
  241.             item_list[#item_list][2] = anw[2]
  242.             item_list[#item_list][3] = anw[3] + 0
  243.             item_list[#item_list][4] = anw[4] + 0
  244.         end
  245.     end
  246. end
  247.  
  248. function accept(usr,txt)
  249.     gpu.setBackground(0x000000)
  250.     gpu.setForeground(0xffffff)
  251.     gpu.fill(50,10,50,10," ")
  252.     klt = 1
  253.     text(60,10,txt)
  254.     text(60,12,"+")
  255.     text(65,12,klt .. "")
  256.     text(70,12,"-")
  257.     text(60,15,"YES")
  258.     text(70,15,"No")
  259.     _,_,xe,ye,_,user = event.pull(3,"touch")
  260.     if user == nil then
  261.         while (user==nil or not ( (59<=xe and xe<=64 or 69<=xe and xe<=73) and ( 14<=ye and ye <= 16 ) )) and authorize do
  262.             _,_,xe,ye,_,user = event.pull(3,"touch")
  263.             player_name, authorize = auth_player()
  264.             if xe~=nil and ye~=nil and usr==user then
  265.                 if (59<=xe and xe<=64) and ( 11<=ye and ye <= 13 ) and klt<64 then
  266.                     text(65,12,"    ")
  267.                     klt = klt + 1
  268.                     text(65,12,klt .. "")
  269.                 elseif (69<=xe and xe<=73) and ( 11<=ye and ye <= 13 ) and klt>1 then
  270.                     text(65,12,"    ")
  271.                     klt = klt + 1
  272.                     text(65,12,klt .. "")
  273.                 end
  274.             end
  275.         end
  276.     end
  277.     if (59<=xe and xe<=64) and ( 14<=ye and ye <= 16 ) and usr==user then
  278.         return true,klt
  279.     elseif (69<=xe and xe<=73) and ( 14<=ye and ye <= 16 ) then
  280.         return false,klt
  281.     else
  282.         return nil,klt
  283.     end
  284. end
  285.  
  286. component.redstone.setOutput(0,15)
  287. robot_auth()
  288. component.redstone.setOutput(0,0)
  289.  
  290. setprice()
  291. authorize = false
  292. page = 1
  293. drawgui({"",0,0})
  294. asort(item_list,page)
  295. while true do
  296.     if not authorize then page = 1 drawgui({"",0,0}) asort(item_list,page) end
  297.     authorize = false
  298.     while not authorize do
  299.         player_name, authorize = auth_player()
  300.         if player_name~="" then
  301.             drawgui(rd)
  302.             asort(item_list,page)
  303.         else
  304.             rd = nil
  305.         end
  306.     end
  307.     if rd==nil or rd[1]~= player_name then
  308.         wait("Ожиданеие ответа от сервера")
  309.         rd = getrec(player_name)
  310.         clwait()
  311.         drawgui(rd)
  312.         asort(item_list,page)
  313.     end
  314.     xx = nil
  315.     yy = nil
  316.     user = nil
  317.     while xx==nil and authorize do
  318.         _,_,xx,yy,_,user = event.pull(10,"touch")
  319.         player_name, authorize = auth_player()
  320.     end
  321.     if user~=nil and user==rd[1] then
  322.         --скролл
  323.         if xx>=151 and xx<=160 and yy>=6 and yy<=15 and page>1 then page = page - 1 asort(item_list,page) end
  324.         if xx>=151 and xx<=160 and yy>=41 and yy<=50 and page<#item_list then page = page + 1 asort(item_list,page) end
  325.         --обработка покупки
  326.         if yy>5 and xx<151 then
  327.             nx = (math.modf((xx-1)/50))+ 1
  328.             ny = (math.modf((yy-1)/5)) - 1
  329.             if ny*3+nx-1+page <= #item_list then
  330.                 itm = item_list[ny*3+nx-1+page]
  331.                 bq = true
  332.                 if rd[2] >= itm[3] then
  333.                     bq = false
  334.                     wait("формируется заказ")
  335.                     pr,kl = accept(rd[1],"Вы хотите купить " .. itm[2])
  336.                     asort(item_list,page)
  337.                     if  rd[2] >= itm[3]*kl then
  338.                         if pr~= nil and pr then
  339.                             if robot_drop(itm[1],kl) then
  340.                                 buy(rd[1],itm[3]*kl)
  341.                                 rd[2] = rd[2]-itm[3]*kl
  342.                                 clwait()
  343.                             else
  344.                                 wait("Предмет отсутствует")
  345.                             end
  346.                         else
  347.                             wait("Отменено")
  348.                         end
  349.                     else
  350.                         bq = true
  351.                     end
  352.                 end
  353.                 if bq and rd[2]*2>=itm[3] then
  354.                     wait("формируется заказ")
  355.                     pr,kl = accept(rd[1],"У вас не хватает средств для кредита " .. itm[2])
  356.                     asort(item_list,page)
  357.                     if  rd[2]*2>= itm[3]*kl then
  358.                         if pr~= nil and pr then
  359.                             if robot_drop(itm[1],kl) then
  360.                                 buy(rd[1],itm[3]*kl)
  361.                                 rd[3] = itm[3]*kl-rd[2]
  362.                                 rd[2] = 0
  363.                                 clwait()
  364.                             else
  365.                                 wait("Предмет отсутствует")
  366.                             end
  367.                         else
  368.                             wait("canceled")
  369.                         end
  370.                     else wait("невозможно выдать кредит") end
  371.                 elseif bq then
  372.                     wait("нет денег")
  373.                 end
  374.             end
  375.         end
  376.     end
  377. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement