Advertisement
MeXaN1cK

Табло в кабинке

Apr 18th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.10 KB | None | 0 0
  1. --cab
  2. local term=require('term')
  3. local event=require('event')
  4. local shell=require('shell')
  5. local com = require("component")
  6. local card=com.card_reader
  7. local fs=require('filesystem')
  8. local tun=com.tunnel
  9. local serialization=require('serialization')
  10. local function saveTbl(tbl, fl) file = io.open(fl, 'w') file:write(serialization.serialize(tbl)) file:close() end
  11. local function loadTbl(fl) file = io.open(fl, 'r') if not file then file = io.open(fl, 'w') file:write('{}') return {} else return serialization.unserialize(file:read('*a')) end file:close() end
  12. local gpu=com.gpu
  13. local m=com.modem
  14. local loc1
  15. local owner='vah'
  16. page=1
  17. local nextcab='82b0b45e-d89e-a686-off15c9aee7a' --2 кабинка
  18. local addressRobot='e51089c7-42c7-465b-9791-ccd8758480ea'
  19. m.open(1)
  20. local x1,y1,w1,h1=38,20,4,1 local x2,y2,w2,h2=46,20,4,1 local x3,y3,w3,h3=45,3,5,3 local x4,y4,w4,h4=51,3,4,3
  21. local x5,y5,w5,h5=67,3,4,3  local x6,y6,w6,h6=72,3,5,3 local x7,y7,w7,h7=45,7,32,3 local x8,y8,w8,h8=45,11,32,3
  22. local x9,y9,w9,h9=45,15,32,3
  23. strlk='|< |' strlk1='| >|'
  24. items1={} price1={}
  25. local thread=require('thread')
  26. thread.init()
  27. gpu.setResolution(87,20)
  28. A,B=gpu.getResolution()
  29. col={blue=0x0000FF,blue1=0x0000AF,grey=0x898989,grey1=0xACACAC}
  30. bp={'╬═══════════[Ticket]═══════════╬','╬══════════════════════════════╬','║'}
  31. function getxy() _,_,ps1,ps2,_=event.pull('touch') return ps1,ps2 end
  32. function au() card.auth() _,_,_,_,nick=event.pull('card_event') return nick end
  33. function cpay(sko) card.pay(owner,sko) _,_,_,tf,pri=event.pull('card_event') return tf,pri end
  34. function top()
  35. gpu.setBackground(col.grey) term.clear()
  36. gpu.setBackground(col.grey1) gpu.fill(51,2,15,28,' ')
  37. gpu.setBackground(col.blue1) gpu.fill(1,1,A,1,' ')
  38. term.setCursor(5,1) io.write('Предмет:')
  39. gpu.setBackground(col.blue) term.setCursor(51,1) io.write('   В наличии:  ') gpu.setBackground(col.blue1)
  40. term.setCursor(A-12,1) io.write('Цена за шт:')
  41. gpu.setBackground(0x6495ED) gpu.fill(1,20,87,1,' ') gpu.setBackground(0xFFA500) gpu.set(x1,y1,strlk) gpu.set(x2,y2,strlk1) gpu.setBackground(col.grey)
  42. end
  43.  
  44. function adch()
  45. while true do
  46. c=nil price=nil amount=nil b=nil
  47. key,_,address,port,_,message = event.pull('modem_message')
  48. c=string.sub(message,1,3) price=string.sub(message,4,9) amount=string.sub(message,10,13) b=string.sub(message,14)
  49. amount=tonumber(amount) price=tonumber(price)
  50. if c=='add' then
  51. for i=1,#items1 do if items1[i]==b then per=1 per2=i break else per=0 end end
  52. if per==0 then items1[#items1+1]=b saveTbl(items1,'items.txt') f1=io.open(b..'.txt','w') f1:write('{'..amount..','..price..'}') f1:close()
  53. elseif per==1 then f2=loadTbl(items1[per2]..'.txt') f2[1]=amount saveTbl(f2,items1[per2]..'.txt') end  
  54. elseif c=='del' then
  55. for i=1,#items do if items[i]==b then per=1 per2=i break else per=0 end end
  56. if per==1 then
  57. shell.execute('del '..items[per2]..'.txt')
  58. items1={}
  59. for sp=1,#items do if items[sp]~=b then items1[#items1+1]=items[sp] end end
  60. items=loadTbl('items.txt') saveTbl(items1,'items.txt') end  
  61. end end end
  62.  
  63. function tb(co,str)
  64. gpu.setBackground(0x898989) term.clear()
  65. gpu.setBackground(co) gpu.fill(23,7,36,5,' ') term.setCursor(25,9) io.write(str)
  66. term.setCursor(23,7) io.write('╬══════════════════════════════════╬') for i=8,11 do term.setCursor(23,i) io.write(bp[3]) end
  67. term.setCursor(23,12) io.write('╬══════════════════════════════════╬') for i=8,11 do term.setCursor(58,i) io.write(bp[3]) end
  68. end
  69.  
  70. function wrt()
  71. gpu.setBackground(col.grey) term.clear()
  72. gpu.setBackground(col.blue1) gpu.fill(1,1,A,1,' ')
  73. gpu.set(39,1,'[Покупка]') gpu.setBackground(col.grey)
  74. gpu.setBackground(col.grey1) gpu.fill(3,3,30,16,' ')
  75. term.setCursor(2,3) io.write(bp[1]) term.setCursor(2,18) io.write(bp[2])
  76. for i=4,17 do term.setCursor(2,i) io.write(bp[3]) end
  77. for i=4,17 do term.setCursor(33,i) io.write(bp[3]) end
  78. gpu.setBackground(0xFFA500)
  79. gpu.fill(x3,y3,w3,h3,' ') term.setCursor(x3+1,y3+1) io.write('+10') gpu.fill(x4,y4,w4,h4,' ') term.setCursor(x4+1,y4+1) io.write('+1')
  80. gpu.fill(x5,y5,w5,h5,' ') term.setCursor(x5+1,y5+1) io.write('-1') gpu.fill(x6,y6,w6,h6,' ') term.setCursor(x6+1,y6+1) io.write('-10')
  81. gpu.setBackground(0x00AA00) gpu.fill(x7,y7,w7,h7,' ') term.setCursor(x7+13,y7+1) io.write('Купить')
  82. gpu.setBackground(0x6495ED) gpu.fill(x8,y8,w8,h8,' ') term.setCursor(x8+6,y8+1) io.write('Использовать скидку')
  83. gpu.setBackground(0xD00000) gpu.fill(x9,y9,w9,h9,' ') term.setCursor(x9+13,y9+1) io.write('Выход')
  84. end
  85.  
  86. function buy(pos,it)
  87. skdk=0
  88. et=true
  89. items=loadTbl('items.txt') tps=pos*it-1
  90. if items1[tps] ~=nil then
  91. tps1=loadTbl(items1[tps]..'.txt')
  92. locc=tps1[1] locc1=tps1[2]
  93. m.send(nextcab,1,'add'..'000000'..'0000'..items1[tps])
  94. if locc ~=0 then
  95. mlt=math.floor(999998/locc1)
  96. lct=1
  97. wrt()
  98. while et do
  99. mtp=lct*tps1[2]
  100. sys=math.floor(mtp-mtp*skdk)
  101. ss=mtp-sys
  102. if skdk==0 then mlt1=math.floor(999998/locc1) else
  103. ml=locc1*skdk mlt1=math.floor(999998/(locc1-ml)) end
  104. gpu.setBackground(col.grey1) gpu.fill(4,4,29,14,' ')
  105. term.setCursor(5,4) io.write('Предмет:') term.setCursor(5,5) io.write(items[tps])
  106. term.setCursor(5,6) io.write('Цена за штуку: ',tps1[2])
  107. term.setCursor(5,7) io.write('Предметов на складе: ',tps1[1])
  108. term.setCursor(5,8) io.write('Предметов в чеке: ',lct)
  109. term.setCursor(5,11) io.write('Итого: ',mtp)
  110. term.setCursor(5,12) io.write('Скидка: ',skdk*100,'%')
  111. term.setCursor(5,13) io.write('С учетом скидки: ',sys)
  112. term.setCursor(5,14) io.write('Сэкономленно средств: ',ss)
  113. term.setCursor(5,16) io.write('К оплате: ',sys)
  114. gpu.setBackground(0) gpu.fill(57,3,8,3,' ') term.setCursor(59,4) io.write(lct)
  115. gpu.setBackground(col.grey)
  116. sis,sis2=getxy()
  117. if sis > x3-1 and sis < x3+w3+1 and sis2 > y3-1 and sis2 < h3+y3 then
  118. if locc>1 then lct=lct+10 end if lct>locc then lct=locc end if lct>mlt1 then lct=mlt1 end
  119. elseif sis > x4-1 and sis < x4+w4+1 and sis2 > y4-1 and sis2 < h4+y4 then
  120. if locc>1 then lct=lct+1 end if lct>locc then lct=locc end if lct>mlt1 then lct=mlt1 end
  121. elseif sis > x5-1 and sis < x5+w5+1 and sis2 > y5-1 and sis2 < h5+y5 then
  122. if locc>1 then lct=lct-1 end if lct<1 then lct=1 end
  123. elseif sis > x6-1 and sis < x6+w6+1 and sis2 > y6-1 and sis2 < h6+y6 then
  124. if locc>1 then lct=lct-10 end if lct<1 then lct=1 end
  125. elseif sis > x7-1 and sis < x7+w7+1 and sis2 > y7-1 and sis2 < h7+y7 then
  126. tb(0x6495ED,'Приложите карту к картоприёмнику!')
  127. nickname=au()
  128. tb(0xFFA500,'Приложите карту для оплаты чека !')
  129. usp,prich=cpay(sys)
  130. if usp==true and prich=='ok' then
  131. if fs.exists(nickname..'.sale')==false then fi=io.open(nickname..'.sale','w') fi:write('{'..sys..'}') fi:close()
  132. tb(0x00AA00,'       Спасибо за покупку!      ')
  133. tps1[1]=locc1-lct saveTbl(tps1,items1[tps]..'.txt')
  134. if tps1[1]<9 then tps1[1]='000'..tps1[1] elseif tps1[1]<99 and tps1[1] > 9 then loc1='00'..tps1[1] elseif tps1[1]<999 and tps1[1]>99 then loc1='0'..tps1[1]  elseif tps1[1]<9999 and tps1[1]>999 then loc1=tps1[1] end
  135. m.send(nextcab,1,'add'..'000000'..loc1..items1[tps])
  136. if lct<9 then lct1=tostring('000'..lct) elseif lct<99 and lct > 9 then lct1=tostring('00'..lct) elseif lct<999 and lct>99 then lct1=tostring('0'..lct) elseif lct<9999 and lct>999 then lct1=tostring(lct) end
  137. tun.send(lct1..items1[tps]) os.sleep(2) et=false
  138. elseif fs.exists(nickname..'.sale')==true then sal=loadTbl(nickname..'.sale') sal[1]=sal[1]+sys saveTbl(sal,nickname..'.sale') tb(0x00AA00,'       Спасибо за покупку!      ')
  139. tps1[1]=locc-lct saveTbl(tps1,items1[tps]..'.txt')
  140. if tps1[1]<9 then tps1[1]='000'..tps1[1] elseif tps1[1]<99 and tps1[1] > 9 then loc1='00'..tps1[1] elseif tps1[1]<999 and tps1[1]>99 then loc1='0'..tps1[1]  elseif tps1[1]<9999 and tps1[1]>999 then loc1=tps1[1] end
  141. m.send(nextcab,1,'add'..'000000'..loc1..items1[tps])
  142. if lct<9 then lct1=tostring('000'..lct) elseif lct<99 and lct > 9 then lct1=tostring('00'..lct) elseif lct<999 and lct>99 then lct1=tostring('0'..lct) elseif lct<9999 and lct>999 then lct1=tostring(lct) end
  143. tun.send(lct1..items1[tps]) os.sleep(2) et=false end
  144. elseif usp==false and prich=='rejected' then  
  145. tb(0xD00000,'  Платеж отменен пользователем!  ') os.sleep(2) et=false
  146. end
  147. term.clear() wr(page)
  148. elseif sis > x8-1 and sis < x8+w8+1 and sis2 > y8-1 and sis2 < h8+y8 then
  149. tb(0x6495ED,'Приложите карту к картоприёмнику!') nickname=au()
  150. if fs.exists(nickname..'.sale')==false then fi=io.open(nickname..'.sale','w') fi:write('{0}') fi:close() skdk=0.05
  151. elseif fs.exists(nickname..'.sale')==true then sal=loadTbl(nickname..'.sale') skdk=sal[1]/10000000 if skdk>0.20 then skdk=0.20 end saveTbl(sal,nickname..'.sale') end wrt()
  152. elseif sis > x9-1 and sis < x9+w9+1 and sis2 > y9-1 and sis2 < h9+y9 then et=false term.clear() wr(page) end end
  153. end end end
  154.  
  155. function wr(k)
  156. top()
  157. op=k*18-18
  158. op1=k*18
  159. for i=op,op1 do
  160. os.sleep(0.0001)
  161. if items1[i]~=nil then
  162. mas=loadTbl(items1[i]..'.txt')
  163. gpu.setBackground(col.grey) term.setCursor(2,i+1) io.write(items1[i]) term.setCursor(57,i+1) gpu.setBackground(col.grey1) io.write(mas[1]) term.setCursor(A-8,i+1) gpu.setBackground(col.grey) io.write(mas[2])
  164. end end end
  165.  
  166. function osn()
  167. while true do
  168. key,_,address,port,_,message = event.pull('touch')
  169. if address > x1-1 and address < x1+w1+1 and port > y1-1 and port < h1+y1 then
  170. if page>1 and page~=0 then page=page-1 end if page<1 then page=1 end
  171. wr(page)
  172. elseif address > x2-1 and address < x2+w2+1 and port > y2-1 and port < h2+y2 then
  173. ypa=math.floor(#items1/18) ypa2=#items/18 if ypa2>ypa then ypa3=math.floor(ypa+1) else ypa3=ypa end
  174. if page<ypa3 then page=page+1 end if page>ypa3 then page=ypa3 end
  175. wr(page)
  176. elseif port>1 and port<20 then
  177. buy(page,port)
  178. end end end
  179. items=loadTbl('items.txt') for i=1,#items do items1[i]=items[i] end
  180. wr(page)
  181. thread.create(osn)
  182. thread.create(adch)
  183. thread.waitForAll()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement