Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local serialization = require("serialization")
- local event = require("event")
- local component = require("component")
- local gpu = component.gpu
- local name_server = "bd"
- local name_client = "cli"
- local lname_server = "listbd"
- local internet = require("internet")
- item_list={}
- server_port = 4000
- srvlist_port = 5000
- local event = require("event")
- rd_from = 1.86
- rd_to = 2.77
- hr={5,55,105}
- vr={7,12,17,22,27,32,37,42,47}
- local radar = component.radar
- local wmodem = component.modem
- md = nil
- function is_russian_letter(char)
- local c = char:byte() * 256 + char:byte(-1)
- return c >= 0xD090 and c <= 0xD18F or c == 0xD081 or c == 0xD191
- end
- function text(xt,yt,str)
- if str~="" then
- size = #str
- k = 0
- i = 0
- while k<size and i<40 do
- k = k + 1
- i = i + 1
- if k<size and is_russian_letter(str:sub(k,k+1)) then
- gpu.fill(xt+i-1,yt,1,1,str:sub(k,k+1))
- k = k + 1
- elseif k<=size then
- gpu.fill(xt+i-1,yt,1,1,str:sub(k,k))
- end
- end
- end
- end
- function asort(list,num)
- gpu.setBackground(0x4becf2)--
- gpu.fill(2,7,48,3," ")
- gpu.setBackground(0x4aa4f1)
- gpu.fill(1,6,50,1," ")
- gpu.fill(1,10,50,1," ")
- gpu.fill(1,7,1,3," ")
- gpu.fill(50,7,1,3," ")
- gpu.copy(1,6,50,5,50,0)
- gpu.copy(1,6,50,5,100,0)
- gpu.copy(1,6,150,5,0,5)
- gpu.copy(1,6,150,5,0,10)
- gpu.copy(1,6,150,10,0,15)
- gpu.copy(1,6,150,10,0,25)
- gpu.copy(1,6,150,10,0,35)
- gpu.setBackground(0x4becf2)
- gpu.setForeground(0x000000)
- if num==nil or num<0 or num>#list then num = 1 end
- lt = #list
- if lt == nil then lt = 0 end
- if lt > 27 then lt = 27+num-1 end
- for i=num-1,lt-1,1 do
- j=i - num + 1
- if list[i+1]~=nil then
- text(hr[1+j%3],vr[1+(j-j%3)/3],list[i+1][2])
- text(hr[1+j%3],2+vr[1+(j-j%3)/3],list[i+1][3] .. " юнитов")
- end
- end
- end
- function drawgui(rec)
- os.execute("clear")
- if rec == nil or rec[1]==nil or rec[2]==nil or rec[3]==nil then rec = {"",0,0} end
- gpu.setBackground(0x504bf2)
- gpu.fill(1,1,160,5," ")
- gpu.setForeground(0xdef24b)
- text(1,1,"Приветствую!")
- text(50,1,"Баланс:")
- text(100,1,"Задолженность:")
- text(1,2,rec[1])
- text(50,2,"" .. rec[2])
- text(100,2,"" .. rec[3])
- gpu.setBackground(0x0d0282)
- gpu.setForeground(0xffffff)
- gpu.fill(151,6,10,10," ")
- gpu.fill(151,41,10,10," ")
- gpu.setBackground(0x4b6f70)
- gpu.fill(151,16,10,25," ")
- text(155,7,"/\\")
- text(154,8,"/ \\")
- text(155,49,"\\/")
- text(154,48,"\\ /")
- end
- function wait(txt)
- gpu.setBackground(0x504bf2)
- gpu.setForeground(0xdef24b)
- text(70,3,txt)
- end
- function clwait( ... )
- gpu.setBackground(0x504bf2)
- gpu.setForeground(0xdef24b)
- gpu.fill(1,3,150,1," ")
- end
- function auth_player()
- pl = radar.getPlayers()
- pr = 0
- name = ""
- for i=1,#pl,1 do
- if pl[i].distance < rd_to and pl[i].distance>rd_from then
- name = pl[i].name
- pr = pr + 1
- end
- end
- if pr == 1 then
- return name,true
- else
- return "",false
- end
- end
- function get_key()
- key = math.random(1000000)
- if key%2==0 then key = key + 1 end
- return key
- end
- function getfrombd(phpurl,post)
- local result, response = pcall(internet.request, phpurl, post .. "&auth=" .. authserverkey,{"Set-Cookie: beget=begetok"})
- local str = ""
- for chunk in response do
- str = str .. chunk
- end
- result,response = nil,nil
- return serialization.unserialize(str)
- end
- function getrec(name)
- return getfrombd(url,"query=getbd&nick=" .. name .. "")
- end
- function buy(name,sum)
- return getfrombd(url,"query=wrfmn&nick=" .. name .. "&money=" .. sum)
- end
- function addrec(name,sum)
- return getfrombd(url,"query=addmn&nick=" .. name .. "&money=" .. sum)
- end
- function getlist(name)
- return getfrombd(url,"query=getlst&uid=" .. name .. "")
- end
- function getfulllist()
- return getfrombd(url,"query=getfulllist")
- end
- function dellist(name,sum)
- end
- function addlist(name,label,sell,buy)
- return getfrombd(url,"query=addlst&uid=" .. name .. "&label=" .. label .. "&sell=" .. sell .. "&buy=" .. buy)
- end
- function robot_auth()
- print("listen")
- wmodem.open(wlisten_port)
- _,_,_,_,_,answ = event.pull(2,"modem")
- if answ~=nil then answ = serialization.unserialize(answ) end
- while (answ == nil or answ[3]~=robot_authkey) do
- _,_,_,_,_,answ = event.pull(2,"modem")
- if answ~=nil then answ = serialization.unserialize(answ) end
- end
- wmodem.broadcast(robot_port,serialization.serialize({name_client,"answer",robot_authkey,"ok"}))
- if answ~=nil then item_list = answ[4] end
- wmodem.close(wlisten_port)
- end
- function robot_drop(name,num)
- component.redstone.setOutput(0,15)
- wmodem.open(wlisten_port)
- ky = get_key()
- wmodem.broadcast(robot_port,serialization.serialize({name_client,"drop",ky,{name,num}}))
- _,_,_,_,_,answ = event.pull(20,"modem")
- if answ~=nil then answ = serialization.unserialize(answ) end
- _,ath = auth_player()
- while (answ == nil or answ[3]~=ky) and ath do
- wmodem.broadcast(robot_port,serialization.serialize({name_client,"drop",ky,{name,num}}))
- _,_,_,_,_,answ = event.pull(20,"modem")
- if answ~=nil then answ = serialization.unserialize(answ) end
- _,ath = auth_player()
- end
- wmodem.close(wlisten_port)
- component.redstone.setOutput(0,0)
- if answ[4] == "good" then return true else return false end
- end
- function setprice()
- lit = item_list
- item_list = {}
- for i = 1,#lit,1 do
- anw = getlist(lit[i][1])
- if anw[3]~=nil and anw[3]+0>0 then
- item_list[#item_list+1] = {}
- item_list[#item_list][1] = anw[1]
- item_list[#item_list][2] = anw[2]
- item_list[#item_list][3] = anw[3] + 0
- item_list[#item_list][4] = anw[4] + 0
- else
- print("\nНайден необъявленный предмет:" .. lit[i][1] .. "\nвведите чере Enter: label sell buy")
- item_list[#item_list+1] = {}
- item_list[#item_list][1] = lit[i][1]
- item_list[#item_list][2] = io.read()
- item_list[#item_list][3] = io.read() + 0
- item_list[#item_list][4] = io.read() + 0
- addlist(item_list[#item_list][1],item_list[#item_list][2],item_list[#item_list][3],item_list[#item_list][4])
- end
- end
- end
- function accept(usr,txt,sho,started)
- gpu.setBackground(0x000000)
- gpu.setForeground(0xffffff)
- gpu.fill(50,10,50,10," ")
- klt = started
- text(60,11,txt)
- if sho then
- text(60,10,"Вы хотите купить")
- text(60,12,"+")
- text(65,12,klt .. "")
- text(70,12,"-")
- text(60,13,"+10")
- text(70,13,"-10")
- else
- text(55,10,"Нехватает средств, взять кредит?")
- end
- text(60,15,"YES")
- text(70,15,"No")
- user = nil
- if user == nil then
- 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
- _,_,xe,ye,_,user = event.pull(3,"touch")
- player_name, authorize = auth_player()
- if xe~=nil and ye~=nil and usr==user and sho then
- if (59<=xe and xe<=64) and ( ye==12 ) and klt<64 then
- text(65,12," ")
- klt = klt + 1
- text(65,12,klt .. "")
- elseif (69<=xe and xe<=73) and ( ye==12 ) and klt>1 then
- text(65,12," ")
- klt = klt - 1
- text(65,12,klt .. "")
- elseif (59<=xe and xe<=64) and ( ye==13 ) and klt<55 then
- text(65,12," ")
- klt = klt + 10
- text(65,12,klt .. "")
- elseif (69<=xe and xe<=73) and ( ye==13 ) and klt>10 then
- text(65,12," ")
- klt = klt - 10
- text(65,12,klt .. "")
- end
- end
- end
- end
- if (not (xe==nil)) and (59<=xe and xe<=64) and ( 14<=ye and ye <= 16 ) and usr==user then
- return true,klt
- elseif (xe == nil) or (69<=xe and xe<=73) and ( 14<=ye and ye <= 16 ) then
- return false,klt
- else
- return nil,klt
- end
- end
- component.redstone.setOutput(0,15)
- robot_auth()
- component.redstone.setOutput(0,0)
- setprice()
- authorize = false
- page = 1
- drawgui({"",0,0})
- asort(item_list,page)
- while true do
- for k,v in component.list() do if v=="screen" and gpu.getScreen~=v then gpu.bind(k) end end
- if not authorize then page = 1 drawgui({"",0,0}) asort(item_list,page) end
- authorize = false
- while not authorize do
- player_name, authorize = auth_player()
- if player_name~="" then
- drawgui(rd)
- asort(item_list,page)
- else
- rd = nil
- end
- end
- if rd==nil or rd[1]~= player_name then
- wait("Ожиданеие ответа от сервера")
- rd = getrec(player_name)
- clwait()
- drawgui(rd)
- asort(item_list,page)
- end
- xx = nil
- yy = nil
- user = nil
- while xx==nil and authorize do
- _,_,xx,yy,_,user = event.pull(10,"touch")
- player_name, authorize = auth_player()
- end
- if user~=nil and rd~=nil and user==rd[1] then
- if xx>=151 and xx<=160 and yy>=6 and yy<=15 and page>6 then page = page - 6 asort(item_list,page) end
- 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
- if yy>5 and xx<151 then
- nx = (math.modf((xx-1)/50))+ 1
- ny = (math.modf((yy-1)/5)) - 1
- if ny*3+nx-1+page <= #item_list then
- itm = item_list[ny*3+nx-1+page]
- bq = true
- if rd[2] >= itm[3] then
- bq = false
- wait("формируется заказ")
- pr,kl = accept(rd[1],"" .. itm[2],true,1)
- asort(item_list,page)
- if rd[2] >= itm[3]*kl then
- if pr~= nil and pr then
- if robot_drop(itm[1],kl) then
- buy(rd[1],itm[3]*kl)
- rd[2] = rd[2]-itm[3]*kl
- clwait()
- else
- wait("Предмет отсутствует")
- end
- else
- wait("Отменено")
- end
- else
- bq = true
- end
- end
- if bq and rd[2]*2>=itm[3] then
- wait("формируется заказ")
- pr,kl = accept(rd[1],"" .. itm[2],false,kl)
- asort(item_list,page)
- if rd[2]*2>= itm[3]*kl then
- if pr~= nil and pr then
- if robot_drop(itm[1],kl) then
- buy(rd[1],itm[3]*kl)
- rd[3] = itm[3]*kl-rd[2]
- rd[2] = 0
- clwait()
- else
- wait("Предмет отсутствует")
- end
- else
- wait("canceled")
- end
- else wait("невозможно выдать кредит") end
- elseif bq then
- wait("нет денег")
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement