Advertisement
Guest User

comments.lua

a guest
Mar 24th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.05 KB | None | 0 0
  1.     local c = require('component')
  2.     local term = require('term')
  3.     local ev = require('event')
  4.     local ser = require('serialization')
  5.     local fs = require('filesystem')
  6.     local comp = require('computer')
  7.     local u = require('unicode')
  8.  
  9.     local len = u.len
  10.     local av = c.isAvailable
  11.     local rep = string.rep
  12.     local uA, rU = comp.addUser, comp.removeUser
  13.     local g = c.gpu
  14.     local color = g.setForeground
  15.  
  16.     local owner = 'Sayonara71'
  17.     local relogTime = 30
  18.  
  19.     local configPath = '/warp.cfg'
  20.     local nick = ''
  21.     local maxW,maxH = 90,26
  22.     local logTimer = 0
  23.     local colors = { general = 0x2BBBeB, gray = 0x404040, text = 0xDADADA, white = 0xFFFFFF, red = 0xFF0000, black = 0x000000 }
  24.     local login = 0
  25.     local n = 1
  26.  
  27.  
  28.        
  29.     local advertising
  30.     local positions = {}
  31.     local commen = {}
  32.     local colorsTable = {
  33.                 ['&0'] = function() color(0x000000) end,
  34.                 ['&1'] = function() color(0x0000AA) end,
  35.                 ['&2'] = function() color(0x00AA00) end,
  36.                 ['&3'] = function() color(0x00AAAA) end,
  37.                 ['&4'] = function() color(0xAA0000) end,
  38.                 ['&5'] = function() color(0xAA00AA) end,
  39.                 ['&6'] = function() color(0xFFAA00) end,
  40.                 ['&7'] = function() color(0xAAAAAA) end,
  41.                 ['&8'] = function() color(0x555555) end,
  42.                 ['&9'] = function() color(0x5555FF) end,
  43.                 ['&a'] = function() color(0x55FF55) end,
  44.                 ['&b'] = function() color(0x55FFFF) end,
  45.                 ['&c'] = function() color(0xFF5555) end,
  46.                 ['&d'] = function() color(0xFF55FF) end,
  47.                 ['&e'] = function() color(0xFFFF55) end,
  48.                 ['&f'] = function() color(0xFFFFFF) end,
  49.                 ['&r'] = function() color(0xFFFFFF) end
  50.                              }
  51.            
  52.            
  53.     function conf_S()
  54.         local file = io.open(configPath,'w')
  55.         file:write(ser.serialize(positions,false)):flush():close()
  56.     end
  57.  
  58.     function conf_R()
  59.         local file = io.open(configPath,'r')
  60.         positions = ser.unserialize(file:read(fs.size(configPath)))
  61.         file:close()
  62.     end    
  63.  
  64.     if not fs.exists("/"..configPath) then
  65.         conf_S()
  66.     end
  67.    
  68.     function drawMenu()
  69.         term.clear()
  70.         g.setResolution(maxW,maxH)
  71.         drawBox(1,1,maxW,maxH,colors.general)
  72.         drawBox(35,24,21,3,colors.general,'Добавить отзыв',4)
  73.        
  74.         conf_R()
  75.         local i = 0
  76.        
  77.         for key, val in pairs(positions) do
  78.             i=i+1
  79.         end
  80.         local C=i
  81.         i=0
  82.         local non = -1
  83.         if C>15 then
  84.             non=C-15
  85.             non=math.random(0, non)
  86.         end
  87.         local l = 1
  88.         for key, val in pairs(positions) do
  89.             if non > 0 then
  90.                 non = non - 1
  91.             else
  92.                 i=i+1
  93.                 printFormatText(3,(20-C)/2+i+4,84,val,colors.white)
  94.                 l = l + 1
  95.                 if l == 16 then
  96.                     break
  97.                 end
  98.             end
  99.         end
  100.         if i > 0 then
  101.             drawBox(2,(20-C)/2+4,maxW-4,2+i,colors.gray)
  102.         end
  103.     end
  104.  
  105.     function drawMenu2()
  106.         drawBox(1,1,maxW,maxH,colors.general)
  107.         drawBox(27,24,15,3,colors.general,'Добавить',3)
  108.         drawBox(47,24,15,3,colors.general,'Отменить',3)
  109.         setText(20,14,"Закройте окно и напишите ваш отзыв в локальный чат!",colors.text)
  110.         drawBox(2,16,maxW-3,3,colors.gray)
  111.         if nick == owner then
  112.             drawBox(27,21,35,3,colors.general,'Админ панель',5)
  113.         end
  114.     end
  115.  
  116.     function drawMenu3()
  117.         if n == 0 then
  118.             n=1
  119.         end
  120.         term.clear()
  121.         drawBox(1,1,maxW,maxH,colors.general)
  122.         drawBox(35,24,15,3,colors.general,'Закрыть',4)  
  123.         drawBox(35,17,15,3,colors.red,'Удалить',4)  
  124.         drawBox(51,17,10,3,colors.white,'----->',1)
  125.         drawBox(24,17,10,3,colors.white,'<-----',1)
  126.        
  127.         commen={}
  128.         drawBox(2,13,maxW-3,3,colors.gray)
  129.         conf_R()
  130.         local i = 0
  131.        
  132.         for key, val in pairs(positions) do
  133.             i=i+1
  134.             commen[i]=key
  135.         end
  136.         if #commen>0 then
  137.             drawBox((90-u.len(commen[n]))/2-3,10,u.len(commen[n])+2,3,colors.text,commen[n],1)
  138.             printFormatText(3,14,84,positions[commen[n]],colors.white)
  139.         end
  140.     end
  141.  
  142.     function printLogTimer()
  143.         logTime = logTime - 1
  144.         if login ~= 0 then setText(44,3,logTime..'с. ',colors.general) end
  145.         if logTime <= 0 then
  146.             exit()
  147.         end
  148.     end
  149.  
  150.  
  151.     function setText(x,y,text,col)
  152.         color(col)
  153.         g.set(x,y,text)
  154.     end
  155.  
  156.     function drawBox(x,y,width,height,col,txt,align)
  157.         color(col)
  158.         g.fill(x,y,1,height,'│')
  159.         g.fill(x+width-1,y,1,height,'│')
  160.         g.set(x,y,'┌'..rep('─',width-2)..'┐')
  161.         g.set(x,y+height-1,'└'..rep('─',width-2)..'┘')
  162.         if txt then
  163.             g.set(x+align,y+1,txt)
  164.         end
  165.     end
  166.  
  167.     function clearArea(x,y,width,height)
  168.         g.fill(x,y,width,height,' ')
  169.     end
  170.  
  171.     function printFormatText(x,y,width,text,defColor,clearHeight)
  172.         color(defColor)
  173.         clearArea(x,y,width,clearHeight or 1)
  174.         local crit = '&'
  175.         local corr = 0
  176.         local totalLen = 0
  177.         for i = 1, u.len(text) do
  178.             local letter = u.sub(text,i,i)
  179.             if letter == crit then
  180.                 pcall(colorsTable[u.sub(text,i,i+1)])
  181.                 corr = corr + 2
  182.                 skip = true
  183.             else
  184.                 if not skip then
  185.                     totalLen = i-corr-1
  186.                     g.set(totalLen+x,y,letter)
  187.                 end
  188.                 skip = false
  189.             end
  190.             if totalLen == width-1 then
  191.                 break
  192.             end
  193.         end
  194.     end
  195.  
  196.     function clipboarder(_,_,chatNick,data)
  197.         if login==1 and chatNick == nick then
  198.             advertising = data:lower():gsub('\\',' ')
  199.             printFormatText(3,17,84, advertising,colors.white)
  200.         end
  201.     end
  202.  
  203.     drawMenu()
  204.  
  205.     function exit()
  206.         login = 0
  207.         local users={comp.users()}
  208.         for i=1, #users do
  209.             comp.removeUser(users[i])
  210.         end
  211.         ev.ignore('chat_message',clipboarder)
  212.         ev.cancel(logTimer)
  213.         drawMenu()
  214.     end
  215.  
  216.     function addComent()
  217.         conf_R()
  218.         positions[nick]=nick .. " - " .. advertising
  219.         conf_S()
  220.         exit()
  221.     end
  222.  
  223.     while true do
  224.         local e,_,w,h,_,name = ev.pull(1, "touch")
  225.         if e == "touch" then
  226.             logTime=relogTime
  227.             nick=name
  228.            
  229.             if login==1 then
  230.                 if w>=27 and w<=42 and h>=24 and h<=27 then
  231.                     addComent()
  232.                 elseif w>=47 and w<=62 and h>=24 and h<=27 then
  233.                     exit()
  234.                 elseif w>=27 and w<=62 and h>=21 and h<=24 and nick==owner then
  235.                     login = 2
  236.                     drawMenu3()
  237.                 end    
  238.             elseif login==2 then
  239.                 if w>=35 and w<=50 and h>=24 and h<=27 then
  240.                     exit()
  241.                 elseif w>=24 and w<=34 and h>=17 and h<=20 then
  242.                     if n>1 then
  243.                         n=n-1
  244.                         drawMenu3()
  245.                     end
  246.                 elseif w>=51 and w<=61 and h>=17 and h<=20 then
  247.                     if n<#commen then
  248.                         n=n+1
  249.                         drawMenu3()
  250.                     end
  251.                 elseif w>=35 and w<=50 and h>=17 and h<=20 then
  252.                     positions[commen[n]]=nil
  253.                     conf_S()
  254.                     n=n-1
  255.                     drawMenu3()
  256.                 end
  257.             else
  258.                 if w>=35 and w<=56 and h>=24 and h<=27 then
  259.                     uA(nick)
  260.                     logTimer = ev.timer(1,printLogTimer,math.huge)
  261.                     login = 1
  262.                     ev.listen('chat_message',clipboarder)
  263.                     term.clear()
  264.                     drawMenu2()
  265.                 end
  266.             end
  267.         end
  268.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement