Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Задается при установке
- local shell = require("shell")
- local args = shell.parse(...)
- local authServerKey = args[1]
- local url = args[2]
- local port = tonumber(args[3])
- local craftNum = tonumber(args[4])
- --добавлять отсутствующие в прайсе, но присутствующие в бд предметы в прайс
- local smart = false
- local serialization = require("serialization")
- local component = require("component")
- local event = require("event")
- local term = require("term")
- local unicode = require("unicode")
- local gpu = component.gpu
- local bg = gpu.getBackground()
- local fg = gpu.getForeground()
- --Интциализация переменных
- local start = true
- --Здесь храниться инфа о человеке, который залогинился
- local account = {nick="",balance=0,credit=0}
- local posScroll = 1
- --расстояние авторизации
- local rd_from = 1.86
- local rd_to = 2.77
- --размер улучшения базы данных
- local sizeDatabase = 81
- --Проверка и инициализация компонентов
- local function log(text)
- gpu.fill(1,49,160,2," ")
- gpu.set(2,49,text)
- end
- function _G.package.loaded.event.shouldInterrupt(...)
- return false
- end
- function _G.package.loaded.event.shouldSoftInterrupt(...)
- return false
- end
- function _G.package.loaded.computer.addUser(...)
- return false
- end
- do
- if component.getPrimary("internet").address~= nil then print("Check inrternet card is ok") else print("Check inrternet card is absent") start = false end
- if component.getPrimary("gpu").address~= nil then print("Check gpu card is ok") else print("Check gpu card is absent") start = false end
- if component.getPrimary("me_interface").address~= nil then print("Check me interface is ok") else print("Check me interface is absent") start = false end
- if component.getPrimary("radar").address~= nil then print("Check radar is ok") else print("Check radar is absent") start = false end
- if component.getPrimary("redstone").address~= nil then print("Check redstone is ok") else print("Check redstone is absent") start = false end
- if component.getPrimary("database").address~= nil then print("Check database is ok") else print("Check database is absent") start = false end
- end
- --Работа со скриптом
- local base = {}
- if start then
- local function getFromBD(post)
- log("Ожидание ответа от базы данных")
- local result, response
- local j = 0
- local str =""
- local i,k
- while not result or str == "" do
- if j>10 then os.sleep(60) end
- j = j + 1
- log("Попытка №" .. j .. " соединиться с бд")
- result, response = pcall(component.internet.connect, url, port)
- response.write()
- response.write(post .. '\n')
- i = 0
- k = nil
- while i<10 and k == nil do
- i = i + 1
- k = response.read(1024)
- end
- i = 0
- while i<100 and k ~= nil do
- i = i + 1
- str = str .. tostring(k)
- k = response.read(1024)
- end
- end
- return serialization.unserialize(str)
- end
- function base.getAccount(nick)
- return getFromBD("authkey=".. authServerKey .."&bdquery=getac&nick=" .. nick)
- end
- function base.getAccounts()
- return getFromBD("authkey=".. authServerKey .."&bdquery=getacs")
- end
- function base.updAccount(nick,money)
- return getFromBD("authkey=".. authServerKey .."&bdquery=updac&nick=".. nick .."&money=" .. money)
- end
- function base.getPrice(uid)
- return getFromBD("authkey=".. authServerKey .."&bdquery=getpr&uid=" .. uid)
- end
- function base.getPrices()
- return getFromBD("authkey=".. authServerKey .."&bdquery=getprs")
- end
- function base.updPrice(uid,label,buy,sell)
- return getFromBD("authkey=".. authServerKey .."&bdquery=updpr&uid=".. uid .."&label=".. label .."&buy=".. buy .."&sell=".. sell)
- end
- end
- --Проверяем me_interface, если находит предметы - добавляет в бд
- if start then
- log("Проверка мэ интерфейса")
- for i=1,9,1 do
- local record = {}
- slot = component.me_interface.getInterfaceConfiguration(i)
- if slot~=nil then
- print("Add new item in list(" .. slot.label .. ", " .. slot.name .. ").\n Enter label: ")
- record.label = io.read()
- print("Enter buy: ")
- record.buy = io.read()
- print("Enter sell: ")
- record.sell = io.read()
- print("Added: " .. tostring(base.updPrice(slot.name .. slot.label, record.label, record.buy, record.sell)))
- component.me_interface.setInterfaceConfiguration(i)
- end
- slot = nil
- end
- end
- --Авторизация игрока на радаре
- function authPlayer()
- local pl = component.radar.getPlayers()
- local pr = 0
- local name = ""
- for i=1,#pl,1 do
- if pl~=nil and pl[i]~=nil and 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
- if account.nick~=nil and account.nick == "" then account = base.getAccount(name) end
- return name,true
- else
- posScroll = 1
- account = {nick="",balance=0,credit=0}
- return "",false
- end
- end
- --fix io.read()
- local ioread=false
- function ioreadFix( )
- if ioread and authPlayer()=="" then
- event.push("key_down",component.keyboard.address,13,28)
- ioread=false
- end
- end
- event.timer(10,ioreadFix,math.huge)
- --Подгрузка price происходит автоматически раз 6 мин
- local priceList = {}
- local craftList = {}
- function loadPrice( )
- if authPlayer()=="" or start then
- log("Загрузка прайслиста")
- priceList = base.getPrices()
- local slot = nil
- for i=1,sizeDatabase,1 do
- slot = component.database.get(i)
- if slot~=nil and priceList[slot.name .. slot.label]~=nil then
- priceList[slot.name .. slot.label].db = i
- elseif smart then --автодобавление, для отключения smart = false
- local record = {}
- if slot~=nil then
- term.clear()
- print("Add new item in list(" .. slot.label .. ", " .. slot.name .. ").\n Enter label: ")
- record.label = io.read()
- print("Enter buy: ")
- record.buy = io.read()
- print("Enter sell: ")
- record.sell = io.read()
- base.updPrice(slot.name .. slot.label, record.label, record.buy, record.sell)
- priceList[slot.name .. slot.label] = {}
- priceList[slot.name .. slot.label].label = record.label
- priceList[slot.name .. slot.label].buy = record.buy
- priceList[slot.name .. slot.label].sell = record.sell
- priceList[slot.name .. slot.label].db = i
- end
- slot = nil
- end
- end
- local me = component.me_interface.getCraftables()
- local k = {}
- craftList = {}
- for i=1,#me do
- k = me[i].getItemStack()
- if priceList[k.name .. k.label]~=nil then
- craftList[#craftList+1] = {}
- craftList[#craftList].is = k
- craftList[#craftList].is.size = nil
- craftList[#craftList].request = me[i].request
- end
- end
- for k,v in pairs(priceList) do
- if v.db==nil then
- priceList[k] = nil
- end
- end
- end
- end
- loadPrice()
- --if _G.package.loaded.loadPrice==nil then
- -- _G.package.loaded.loadPrice=true
- -- event.timer(360, loadPrice, math.huge)
- --end
- --Автокрафт
- function getSize(name,label)
- local msize = 0
- for k,v in pairs(component.me_interface.getItemsInNetwork({["name"]=name,["label"]=label})) do
- if tonumber(k)~= nil then
- if v.size > msize then msize = v.size end
- end
- end
- return msize
- end
- local numCraft = 0
- local queue = nil
- function autocraft( )
- if authPlayer()=="" then
- if numCraft ==nil or numCraft==#craftList then numCraft=0 end
- log("Проверка списка предметов " .. #craftList)
- if queue~=nil and ( queue.isDone() or queue.isCanceled() ) then log("очередь очищена") queue = nil end
- if queue==nil then
- numCraft = numCraft + 1
- local pr = getSize(craftList[numCraft].is.name, craftList[numCraft].is.label)
- if pr~=nil and pr<craftNum then
- local crnum = craftNum
- queue = craftList[numCraft].request(crnum)
- while queue.isCanceled() and crnum>1 do
- crnum = math.floor(crnum/2)
- queue = craftList[numCraft].request(crnum)
- end
- end
- end
- log("поставлен предмет под номером " .. i .. ", " .. craftList[i].is.name .. ", " .. craftList[i].is.label)
- end
- end
- if _G.package.loaded.autocraft==nil then
- _G.package.loaded.autocraft=true
- event.timer(10, autocraft, math.huge)
- end
- --Фикс монитора
- function ScreenFix()
- component.gpu.bind(component.screen.address,true)
- end
- event.timer(10,ScreenFix,math.huge)
- --Окно соглашения
- function drawBorder(x,y,width,height)
- width = width - 1
- height = height - 1
- gpu.fill(x+1 ,y ,width ,1 ,unicode.char(10249))
- gpu.fill(x ,y ,1 ,1 ,unicode.char(10319))
- gpu.fill(x+1+width ,y ,1 ,1 ,unicode.char(10425))
- gpu.fill(x ,y+1 ,1 ,height ,unicode.char(10311))
- gpu.fill(x+1+width ,y+1 ,1 ,height ,unicode.char(10424))
- gpu.fill(x+1 ,y+1+height ,width ,1 ,unicode.char(10432))
- gpu.fill(x ,y+1+height ,1 ,1 ,unicode.char(10439))
- gpu.fill(x+1+width ,y+1+height ,1 ,1 ,unicode.char(10488))
- end
- function interfaceIsEmpty()
- for i = 1,9,1 do
- if component.me_interface.getInterfaceConfiguration(i) ~= nil then return false end
- end
- return true
- end
- function redstone(bool)
- local output = 0
- if bool then output = 15 end
- for i=0,5,1 do component.redstone.setOutput(i,output) end
- end
- function acceptable(kPrice,vPrice)
- log("Формирование заказа")
- gpu.setBackground(0x301945)
- term.clear()
- gpu.setForeground(0xb3bd57)
- gpu.set(70,2,"Формирование заказа ...")
- drawBorder(69,1,24,2)
- gpu.set(5,10,"Ваш заказ: " .. vPrice.label)
- gpu.set(10,12,"Цена за 1 штуку: " .. vPrice.buy)
- local maxSize = 0
- local inME = component.database.get(vPrice.db)
- maxSize = inME.maxSize
- inME = getSize(inME.namem, inME.label)
- --if inME~=nil then inME = component.me_interface.getItemsInNetwork(inME)[1] end
- --if inME~=nil then maxSize = inME.maxSize inME = inME.size end
- --if inME==nil then inME = 0 end
- gpu.set(10,13,"В МЭ храниться: " .. inME)
- gpu.set(10,14,"Выбранное количетсво(нажмите, чтобы ввести): 0")
- gpu.set(20,46,"Купить выбранное")
- drawBorder(19,44,17,4)
- gpu.set(120,46,"Отменить")
- drawBorder(119,44,9,4)
- local amount = 0
- local cancel = false
- while not cancel do
- _,_,x,y,_,name = event.pull("touch")
- if authPlayer() == name then
- if account.credit~=0 then
- cancel = true
- end
- if not cancel and 119<=x and x<=130 and 44<=y and y<=48 then cancel = true log("отмена") end
- if not cancel and 55<=x and x<=130 and 13<=y and y<=15 then
- log("Выбор количетсва")
- term.setCursor(55,14)
- ioread=true
- local _,txt = pcall(io.read)
- if txt==nil then txt = "" end
- amount = tonumber(txt)
- gpu.setBackground(0x301945)
- gpu.setForeground(0xb3bd57)
- term.clear()
- gpu.set(70,2,"Формирование заказа ...")
- drawBorder(69,1,24,2)
- gpu.set(5,10,"Ваш заказ: " .. vPrice.label)
- gpu.set(10,12,"Цена за 1 штуку: " .. vPrice.buy)
- maxSize = 0
- inME = component.database.get(vPrice.db)
- maxSize = inME.maxSize
- inME = getSize(inME.namem, inME.label)
- --if inME~=nil then inME = component.me_interface.getItemsInNetwork(inME)[1] end
- --if inME~=nil then maxSize = inME.maxSize inME = inME.size end
- --if inME==nil then inME = 0 end
- gpu.set(10,13,"В МЭ храниться: " .. inME)
- gpu.set(10,14,"Выбранное количетсво(нажмите, чтобы ввести): 0")
- gpu.set(20,46,"Купить выбранное")
- drawBorder(19,44,17,4)
- gpu.set(120,46,"Отменить")
- drawBorder(119,44,9,4)
- if amount == nil then amount = 0 end
- if amount<0 then amount = 0 end
- if account.balance*2 < vPrice.buy*amount then
- amount = math.floor(account.balance*2 / vPrice.buy)
- end
- if amount>inME then amount = inME end
- if amount>maxSize*9 then amount = maxSize*9 end
- gpu.fill(55,14,106,1," ")
- gpu.set(55,14,"" .. amount)
- end
- if not cancel and 19<=x and x<=36 and 44<=y and y<=48 then
- local i = 0
- local amt = 0
- base.updAccount(account.nick,-vPrice.buy*amount)
- account = {nick="",balance=0,credit=0}
- authPlayer()
- while i<9 and amount>0 do
- i = i+1
- if amount<maxSize then
- amt = amount
- amount = 0
- else
- amt = maxSize
- amount = amount - amt
- end
- component.me_interface.setInterfaceConfiguration(i,component.database.address,vPrice.db,amt)
- end
- redstone(true)
- for i=1,9,1 do component.me_interface.setInterfaceConfiguration(i) end
- os.sleep(1)
- redstone(false)
- cancel = true
- end
- else cancel = true
- end
- end
- gpu.setBackground(bg)
- gpu.setForeground(fg)
- end
- function drawHeader()
- if account==nil then account = {} end
- if account.nick==nil or account.balance==nil or account.credit == nil then account.nick="" account.balance=0 account.credit =0 end
- gpu.setBackground(0x301945)
- gpu.setForeground(0xb3bd57)
- gpu.fill(1,2,160,1," ")
- drawBorder(0,1,161,2)
- gpu.set(1,2,"Аккаунт:")
- gpu.set(9,2,account.nick)
- gpu.set(30,2,"Баланс:")
- gpu.set(38,2,account.balance .. "")
- gpu.set(55,2,"Задолженность:")
- gpu.set(70,2,account.credit .. "")
- gpu.set(85,2,"Поиск:")
- gpu.fill(92,2,69,1,"_")
- gpu.setBackground(bg)
- gpu.setForeground(fg)
- end
- function drawScroll()
- gpu.setBackground(0x6a0d73)
- gpu.setForeground(0xe5df25)
- gpu.fill(158,4,3,22," ")
- gpu.fill(159,5,1,1,unicode.char(9650))
- gpu.setBackground(0x730d37)
- gpu.fill(158,26,3,22," ")
- gpu.fill(159,46,1,1,unicode.char(9660))
- gpu.setBackground(bg)
- gpu.setForeground(fg)
- end
- function drawBody()
- gpu.setBackground(0x301945)
- gpu.fill(1,8,157,40," ")
- gpu.setBackground(0x421f63)
- gpu.setForeground(0xe5df25)
- gpu.fill(1,4,157,4," ")
- gpu.fill(1,12,157,4," ")
- gpu.fill(1,20,157,4," ")
- gpu.fill(1,28,157,4," ")
- gpu.fill(1,36,157,4," ")
- gpu.fill(1,44,157,4," ")
- gpu.setBackground(bg)
- gpu.setForeground(fg)
- end
- function drawItemList(price)
- local i = 0
- gpu.setForeground(0xe5df25)
- for k,v in pairs(price) do
- i = i + 1
- if posScroll<=i and i<=posScroll+10 then
- if ((posScroll+1)%2+i)%2==1 then
- gpu.setBackground(0x421f63)
- else
- gpu.setBackground(0x301945)
- end
- gpu.set(3, 4 + 4 * (i - posScroll), "Предмет: " .. v.label)
- gpu.set(3, 6 + 4 * (i - posScroll), "Цена продажи: " .. v.buy)
- end
- end
- gpu.setBackground(bg)
- gpu.setForeground(fg)
- end
- function starting( )
- start = false
- local fprice = {}
- local filtered = false
- local prices = {}
- while true do
- log("Стартуем!")
- filtered = false
- local p,a = authPlayer()
- drawHeader()
- if a then
- term.clear()
- drawHeader()
- drawScroll()
- drawBody()
- drawItemList(priceList)
- while authPlayer()~="" do
- log("Ожидание дейcтвий пользователя")
- local _,_,x,y,_,name = event.pull(10,"touch")
- if x~=nil and y~=nil and name~=nil then
- if filtered then
- prices = fprice
- else
- prices = priceList
- end
- --скрол
- if 157<=x and x<=160 and 4<=y and y<=25 then posScroll = posScroll - 1 if posScroll <1 then posScroll = 1 end drawBody() drawItemList(prices) end
- if 157<=x and x<=160 and 26<=y and y<=47 then posScroll = posScroll + 1 if posScroll >78 then posScroll = 1 end drawBody() drawItemList(prices) end
- --Поиск
- if 92<=x and x<=160 and 1<=y and y<=3 then
- drawHeader()
- posScroll = 1
- term.setCursor(92,2)
- ioread=true
- local _,filter = pcall(io.read)
- if filter==nil then filter = "" end
- if #filter>200 then
- drawHeader()
- drawScroll()
- end
- fprice = {}
- local pr = true
- if filter == "" then pr = false end
- if pr then
- pr = false
- for k,v in pairs(priceList) do
- if v.label:lower():find(filter:lower())~=nil then
- pr = true
- fprice[k] = v
- end
- end
- end
- if pr then
- drawBody()
- posScroll = 1
- drawItemList(fprice)
- filtered = true
- else
- drawBody()
- posScroll = 1
- drawItemList(priceList)
- filtered = false
- end
- end
- --Выбор
- if 1<=x and x<=156 and 4<=y and y<=47 then
- local i = 0
- for k,v in pairs(prices) do
- i = i + 1
- if i == (posScroll + math.floor((y-4)/4)) then
- pcall(acceptable,k,v)
- term.clear()
- drawHeader()
- drawScroll()
- drawBody()
- posScroll = 1
- drawItemList(priceList)
- filtered = false
- if account.credit~=0 then
- log("Погаси задолженность " .. account.nick .. "!")
- os.sleep(2)
- end
- end
- end
- end
- end
- end
- else
- os.sleep(1)
- end
- end
- end
- while true do pcall(starting) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement