Advertisement
PandaDoddo72Rus

Auto

May 8th, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.09 KB | None | 0 0
  1. -- Payment system
  2.  
  3. local unicode = require('unicode')
  4. local event = require("event")
  5. local component = require('component')
  6. local serialization = require("serialization")
  7. local tunnel = require('component').tunnel
  8. local shell = require('shell')
  9. local object = require('object')
  10. local gpu = component.gpu
  11.  
  12. local args = shell.parse(...)
  13.  
  14. if args[1] ~= 'table' and args[1] == 'string' then
  15.   args = serialization.unserialize(args[1])
  16. else
  17.   args = args[1]
  18. end
  19.  
  20. local rX,rY = gpu.getResolution()
  21. local settings = {}
  22. local w = rX/11*5
  23. local h = rY/13*9
  24. object.init()
  25.  
  26.  
  27. local function getEvent(...)
  28.   local args = {...}
  29.   evnt = nil
  30.   if type(args[1]) == 'number' then
  31.     time = args[1]
  32.   end  
  33.   evtrue = true
  34.   local function getRawEvent()
  35.     return {event.pull()}
  36.   end
  37.   while evtrue do
  38.    -- s , evnt = pcall(getRawEvent)
  39.   evnt = {event.pull(time)}
  40.   --  if s ~= false then
  41.     for i=1,#args do
  42.       if evnt[1] == args[i] and time == nil then
  43.         evtrue = false
  44.         break
  45.     elseif time ~= nil then  
  46.       evtrue = false
  47.         break
  48.       end
  49.     end
  50.   --end
  51.   end
  52.   return evnt
  53. end
  54.  
  55. function payment(arr)
  56.   local request = {
  57.     operation = 'transfer',
  58.     user = arr.buyer,
  59.     key = arr.key,
  60.     target = arr.seller,
  61.     amount = arr.amount,
  62.     description = arr.comment,
  63.     answearKey = arr.answearKey
  64.   }
  65.   tunnel.send(serialization.serialize(request))
  66.   result = {}
  67.   while true do
  68.     local e = {event.pull('modem_message')}
  69.     local answear = serialization.unserialize(e[6])
  70.     if answear.operation == 'transactionResult' and answear.status == true then
  71.       result.status = true
  72.       result.data = answear
  73.       break
  74.     elseif answear.operation == 'transactionResult' and answear.status == false and answear.error == 'timeout' then
  75.       result.status = false
  76.       result.data = answear
  77.       break
  78.     elseif answear.operation == 'transactionResult' and answear.status == false and answear.error == 'deny_command' then
  79.       result.status = false
  80.       result.data = answear
  81.       break
  82.     end
  83.     if answear.operation == 'transfer' and answear.status == true then
  84.      -- print('Payment register!')
  85.     elseif answear.operation == 'transfer' and answear.status == false and answear.status == 'Not enough money' then
  86.      result.status = false
  87.      data = answear
  88.      break
  89.     end
  90.   end
  91.   return result
  92. end
  93.  
  94. function drawLMenu()
  95.   object.square(math.floor((rX-w)/2),math.ceil((rY-h)/2),w,h,' ',0xffffff,0x898989)
  96.   object.frame(math.floor((rX-w)/2)+2,math.ceil((rY-h)/2)+1,w-4,h-5,1)
  97. end
  98.  
  99. function drawMain(arr)
  100.   gpu.setBackground(0x898989)
  101.   drawLMenu()
  102.   object.square(math.floor((rX-w)/2),math.ceil((rY-h)/2),w,h,' ',0xffffff,0x898989)
  103.   object.frame(math.floor((rX-w)/2)+2,math.ceil((rY-h)/2)+1,w-4,h-5,1)
  104.   object.add('button',math.floor((rX-w)/2)+6,math.ceil((rY-h)/2)+h-4,w-12,3,'Оплатить',2,0xffffff,0x009900,'pay')
  105.   -------
  106.   object.text(math.floor((rX-w)/2)+4,math.ceil((rY-h)/2)+2,w-6,1,'Получатель: '..arr.seller,'First','')
  107.   object.text(math.floor((rX-w)/2)+4,math.ceil((rY-h)/2)+4,w-6,1,'Плательщик: '..arr.buyer,'First','')
  108.   object.text(math.floor((rX-w)/2)+4,math.ceil((rY-h)/2)+6,w-6,1,'Сумма оплаты: '..arr.amount,'First','')
  109.   -------
  110.   object.drawAllObject()
  111.   while true do
  112.     e = getEvent('touch')
  113.     if e[1] == 'touch' then
  114.       arr=object.getPressedObject(e[3],e[4])
  115.       if arr ~= false then
  116.         if arr[1] == 'button' then
  117.           if arr[2] == 'pay' then
  118.             local result = payment(args)
  119.             if result.status == true then
  120.               shell.execute(args.trueFile)
  121.             elseif result.status == false and result.data.error == 'timeout' then
  122.               --
  123.             elseif result.status == false and result.data.error == 'Not enough money' then
  124.               --
  125.             end
  126.           end
  127.         end
  128.       end
  129.     end
  130.   end  
  131. end
  132.  
  133. args = {
  134.   operation = 'transfer',
  135.   buyer = 'PandaDoddo',
  136.   key = 'shop',
  137.   seller = 'PandaDoddo',
  138.   amount = 100,
  139.   comment = 'test',
  140.   answearKey = 'KEY',
  141.   trueFile = 'yes'
  142. }
  143.  
  144. gpu.setBackground(0x898989)
  145.  
  146. drawMain(args)
  147.  
  148. shell.execute("z")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement