Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local gpu = component.gpu
- local GUI = require("GUI")
- local application = GUI.application()
- local unicode = require("unicode")
- local computer = require("computer")
- local io = require("io")
- local maxWidth, maxHeight = gpu.getViewport()
- local event = require("event")
- gpu.setResolution((maxWidth/3) * 2 , (maxHeight/3) * 2)
- local crystal = component.isAvailable("crystal") and component.crystal or error("Нет буферного сундука")
- local interface = component.me_interface or error("Не найден МЭ интерфейс")
- local db = component.isAvailable("database") and component.database or error("Нет улучшения базы данных")
- local screenWidth, screenHeight = gpu.getResolution()
- local notChargedDraconicBlockInfo = {displayName = "Дракониевый блок", id = "DraconicEvolution:draconium", dmg = 0}
- local chargedDraconicBlockInfo = {displayName = "Заряженный дракониевый блок", id = "DraconicEvolution:draconium", dmg = 2}
- local pricePerBlock = {
- money = {displayName= "Деньги(Банкомат на /warp shop)", id = "customnpcs:npcMoney", dmg = 0, count = 8},
- }
- local constants = {
- log_file_name = "/home/LogDraconicBlocs.txt"
- }
- local localize = {
- shop_title = "Магазин для зарядки ДК блоков",
- shop_details_1 = "Это магазин в котором вы можете зарядить ваши Дракониевые блоки блоки id: (0967/0)",
- shop_details_2 = "Поместите денежную оплату в размере "..pricePerBlock.money.count.." эмов за блок (можно снять на /warp shop)",
- shop_details_3 = "А так же поместите обычные Дракониевые блоки id: (0967/0) в сундук.",
- shop_details_4 = "Нажми кнопку \"Сделать заряженные блоки\" и ожидай зарядки (очень быстро)",
- buy_button_text = "Сделать заряженные дракониевые блоки",
- check_price_text = "Проверить стоимость",
- need_draconic_blocks_text = "Дракониевых блоков не найдено",
- need_money_text_mask = "Для оплаты за %d ДК блока нужно %d Денег",
- need_more_money = "Для оплаты за %d ДК блоков нужно еще %d %s. Сейчас %d/%d",
- item_was_taken_mask = "Предмет %s в нужном количестве был взят из сундука",
- sorry_not_enought_price_mask = "Извините, вы достали из сундука %d %s предметов. Положите обратно",
- wait_for_craft = "Ожидайте, в процессе создания...",
- thanks_for_buying = "Спасибо за ожидание! Ваши %d %s были выданы в сундук",
- last_buyer_mask = "Посл. покупатель: %s",
- count_buyers_mask = "Всего покупателей: %d"
- }
- local constraints = {
- leftBorder = 5
- }
- local function showInfo(infoText, durationSeconds)
- infoTextField.text = infoText
- infoTextField.hidden = false
- application:draw()
- if durationSeconds then
- computer.pullSignal(durationSeconds)
- infoTextField.hidden = true
- application:draw()
- end
- end
- local function pushItem(itemObject, count)
- local convertedObject = {name = itemObject.id, damage = itemObject.dmg}
- db.clear(1)
- interface.store(convertedObject, db.address, 1)
- interface.setInterfaceConfiguration(1, db.address, 1, 64)
- local drop = 0
- while true do
- if drop == count then
- interface.setInterfaceConfiguration(1, db.address, 1, 0)
- break
- else
- local dropcount = interface.pushItem("UP", 1, count-drop)
- drop = drop + dropcount
- end
- end
- end
- local function takeItem(itemObject, count)
- local takeItemsLeft = count
- local data = crystal.getAllStacks(0)
- local size = crystal.getInventorySize()
- for slot = 1, size do
- if data[slot] and data[slot].id == itemObject.id and data[slot].dmg == itemObject.dmg then
- if takeItemsLeft > 0 then
- if takeItemsLeft > 64 then
- local item_count = crystal.pushItem("DOWN",slot,64)
- takeItemsLeft = takeItemsLeft - item_count
- elseif takeItemsLeft > 0 and takeItemsLeft <= 64 then
- local item_count = crystal.pushItem("DOWN",slot,takeItemsLeft)
- takeItemsLeft = takeItemsLeft - item_count
- end
- else
- break
- end
- end
- end
- if takeItemsLeft > 0 then
- infoTextField.text = string.format(localize.sorry_not_enought_price_mask, takeItemsLeft, itemObject.displayName)
- infoTextField.hidden = false
- application:draw()
- computer.pullSignal(1)
- takeItem(itemObject, takeItemsLeft)
- else
- showInfo(string.format(localize.item_was_taken_mask, itemObject.displayName), 1)
- end
- end
- local function lookForItemInChest(id, damage)
- local data = crystal.getAllStacks(0)
- local size = crystal.getInventorySize()
- local count = 0
- for slot = 1, size do
- if data[slot] and data[slot].id == id and data[slot].dmg == damage then
- count = count + data[slot].qty
- end
- end
- return count
- end
- local function checkCurrentPrice()
- local draconicBlocksCount = lookForItemInChest(notChargedDraconicBlockInfo.id, notChargedDraconicBlockInfo.dmg)
- local moneyCountNeeded = draconicBlocksCount * pricePerBlock.money.count
- if draconicBlocksCount == 0 then
- showInfo(string.format(localize.need_draconic_blocks_text), 5)
- else
- showInfo(string.format(localize.need_money_text_mask, draconicBlocksCount, moneyCountNeeded), 5)
- end
- end
- local function requestCraft(itemObject, count)
- local craftable = interface.getCraftables({name = itemObject.id, damage = itemObject.dmg})
- if craftable then
- if craftable[1] then
- local request = craftable[1].request(count)
- repeat
- showInfo(localize.wait_for_craft)
- computer.pullSignal(3)
- until request.isDone() or request.isCanceled()
- end
- end
- end
- local function giveDraconicBlocksBack(count)
- pushItem(chargedDraconicBlockInfo, count)
- logUserBuy(userNickName, pricePerBlock.money.displayName, moneyCountNeeded)
- loadBuyersInfo()
- showInfo(string.format(localize.thanks_for_buying, count, chargedDraconicBlockInfo.displayName), 10)
- end
- local function startChargingDcBlocks()
- local draconicBlocksCount = lookForItemInChest(notChargedDraconicBlockInfo.id, notChargedDraconicBlockInfo.dmg)
- if draconicBlocksCount == 0 then
- showInfo(string.format(localize.need_draconic_blocks_text), 5)
- else
- local moneyCountNeeded = draconicBlocksCount * pricePerBlock.money.count
- local moneyInChest = lookForItemInChest(pricePerBlock.money.id, pricePerBlock.money.dmg)
- if moneyInChest < moneyCountNeeded then
- showInfo(string.format(localize.need_more_money, draconicBlocksCount, moneyCountNeeded - moneyInChest, pricePerBlock.money.displayName, moneyInChest, moneyCountNeeded), 5)
- else
- takeItem(notChargedDraconicBlockInfo, draconicBlocksCount)
- takeItem(pricePerBlock.money, moneyCountNeeded)
- requestCraft(chargedDraconicBlockInfo, draconicBlocksCount)
- giveDraconicBlocksBack(draconicBlocksCount)
- end
- end
- end
- application:addChild(GUI.panel(1, 1, application.width, application.height, 0xCCCCCC))
- application:addChild(GUI.text(constraints.leftBorder, 1, 0x000000, localize.shop_title))
- application:addChild(GUI.text(constraints.leftBorder, 5, 0x000000, localize.shop_details_1))
- application:addChild(GUI.text(constraints.leftBorder, 6, 0x000000, localize.shop_details_2))
- application:addChild(GUI.text(constraints.leftBorder, 7, 0x000000, localize.shop_details_3))
- application:addChild(GUI.text(constraints.leftBorder, 8, 0x000000, localize.shop_details_4))
- infoTextField = application:addChild(GUI.text(constraints.leftBorder, 11, 0x000000, " "))
- local widthStartChargingButton = unicode.len(localize.buy_button_text) + 2
- local startChargingDcBlocksButton = application:addChild(GUI.button(5, 14, widthStartChargingButton, 3, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, localize.buy_button_text))
- startChargingDcBlocksButton.animated = false
- startChargingDcBlocksButton.onTouch = function()
- startChargingDcBlocks()
- end
- local widthPriceButton = unicode.len(localize.check_price_text) + 2
- local checkPriceButton = application:addChild(GUI.button(5 + widthStartChargingButton + 1, 14, widthPriceButton, 3, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, localize.check_price_text))
- checkPriceButton.animated = false
- checkPriceButton.onTouch = function()
- checkCurrentPrice()
- end
- local userNameText = application:addChild(GUI.text(constraints.leftBorder + widthStartChargingButton + widthPriceButton + 2, 14, 0x000000, " "))
- local countOfBuyersText = application:addChild(GUI.text(constraints.leftBorder + widthStartChargingButton + widthPriceButton + 2, 15, 0x000000, " "))
- function readTable(path)
- local file = io.open(path, "r")
- if file then
- local result, reason = file:read("*all")
- if result then
- return require("serialization").unserialize(result) or {}
- end
- end
- return result, reason
- end
- local function writeTable(path, ...)
- local file = io.open(path, "w")
- file:write(require("serialization").serialize(...))
- file:close()
- end
- function getAllUsersBought()
- return readTable(constants.log_file_name) or {}
- end
- function logUserBuy(userName, itemPrice, count)
- local usersTable = readTable(constants.log_file_name)
- usersTable = usersTable and usersTable or {}
- --local humanTime = os.date("%d %b %Y %H:%M:%S", system.getTime())
- local userBuyItemLog = {user = userName, itemBought = itemPrice, count = count}
- table.insert(usersTable, userBuyItemLog)
- writeTable(constants.log_file_name, usersTable)
- countBuyersTmp = #usersTable
- end
- function loadBuyersInfo()
- local usersBuyers = getAllUsersBought()
- if #usersBuyers > 0 then
- userNameText.text = string.format(localize.last_buyer_mask, usersBuyers[#usersBuyers].user)
- userNameText.hidden = false
- countOfBuyersText.text = string.format(localize.count_buyers_mask, #usersBuyers)
- countOfBuyersText.hidden = false
- else
- userNameText.hidden = true
- countOfBuyersText.hidden = true
- end
- application:draw()
- end
- application.eventHandler = function(workspace, object, ...)
- local args = table.pack(...)
- userNickName = args[6]--get nickname
- return false
- end
- function event.shouldInterrupt()
- return false
- end
- loadBuyersInfo()
- application:draw()
- application:start()
Add Comment
Please, Sign In to add comment