Advertisement
Noki1111111

client_prod

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