Advertisement
Makcu73

Online

Jun 15th, 2021
1,075
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local c = require('component')
  2. local ev = require('event')
  3. local ser = require('serialization')
  4. local fs = require('filesystem')
  5. local comp = require('computer')
  6. local term = require('term')
  7. local len = require('unicode').len
  8.  
  9. local workDir = '/checkPlayer/'
  10. local clear = term.clear
  11. local uA = comp.addUser
  12. local rU = comp.removeUser
  13. local g = c.gpu
  14. local xOld, yOld = g.getResolution()
  15. local x, y = 50, 25
  16. local startX = 2
  17. local startY = 3
  18. local statusX = 39
  19. local maxPlayers = 20
  20. local work = true
  21. local buttons = { '[Добавить]', 'x', 'online', 'offline' }
  22.  
  23. local config = { colors  = {
  24.                
  25.                 0xFFFFFF, -- белый 1
  26.                 0xff0066, -- розовый 2
  27.                 0xff0000, -- красный 3
  28.                 0x474747, -- серый 4
  29.                 0x00ff00, -- зелёный 5
  30.                 0xfff200, -- жёлтый 6
  31.                 0xff00ff, -- пурпурный 7
  32.                 0x0000ff, -- синий 8
  33.                 0xff8800, -- оранжевый 9
  34.                 0x00bbff, -- голубой 10
  35.                 0x00ffff, -- бирюзовый 11
  36.                 0x773d00, -- коричневый 12
  37.                 0x770000, -- бордовый 13
  38.                 0x007200, -- тёмно зелёный 14
  39.                 0x00575b, -- тёмно бирюзовый 15
  40.                 0x11007f, -- тёмно синий 16
  41.                 0xb77979, -- тусклый красный 17
  42.                 0xbc9ace, -- тусклый фиолетовый 18
  43.                 0x9aa0ce, -- тусклый синий 19
  44.                 0xcfd396  -- тусклый жёлтый 20
  45.  
  46. }, players = {  } }
  47.  
  48. function check()
  49.     g.setResolution(x,y)
  50.     g.setBackground(0x000000)
  51.     clear()
  52.     g.setForeground(config.colors[4])
  53.     g.set(18,1,'Список игроков:')
  54.     g.set(1,y,buttons[1])
  55.     g.set(x,y,buttons[2])
  56.     for i = 1, #config.players do
  57.         local nick = config.players[i].nick
  58.         local prefix = config.players[i].prefix
  59.         local nickColor = config.players[i].nickColor
  60.         local prefixColor = config.players[i].prefixColor
  61.         local isActiveScript = config.players[i].activeScript
  62.         local LenP = len(prefix)
  63.         local height = startY - 1 + i
  64.         local test = uA(nick) rU(nick)
  65.         g.setForeground(config.colors[1])
  66.         g.set(startX, height, '[')
  67.         g.set(startX + LenP + 1, height, '] - ')
  68.         g.setForeground(prefixColor)
  69.         g.set(startX + 1, height, prefix)
  70.         g.setForeground(nickColor)
  71.         g.set(startX + LenP + 5, height, nick)
  72.         if test then
  73.             if isActiveScript then pcall(loadfile(workDir..nick..'.lua')) config.players[i].activeScript = false end
  74.             g.setForeground(config.colors[5])
  75.             g.set(statusX,height,'   '..buttons[3]..' ')
  76.             g.setForeground(config.colors[4])
  77.             g.set(x-1,height,buttons[2]..' ')
  78.         else
  79.             config.players[i].activeScript = true
  80.             g.setForeground(config.colors[4])
  81.             g.set(statusX,height,'  '..buttons[4]..' ')
  82.             g.set(x-1,height,buttons[2]..' ')
  83.         end
  84.     end
  85. end
  86.  
  87. function link(bNum,bX,bY)
  88.     comp.beep(1500)
  89.     g.setForeground(config.colors[9])
  90.     g.set(bX,bY,buttons[bNum])
  91.     os.sleep(0.3)
  92. end
  93.  
  94. function clicker(_,_,curX,curY)
  95.     if curX == x  and curY == y then link(2,x,y) work = false
  96.     elseif curX == x-1 and curY >= startY and curY <= startY + #config.players - 1 then link(2,x-1,curY) local index = curY-startY+1 remover(index)
  97.     elseif curX >= 1 and curX <= 10 and curY == y then link(1,1,y) if #config.players < maxPlayers then addPlayer() else playersLimitter() end
  98.     end
  99. end
  100.  
  101. function remover(index)
  102.     fs.remove(workDir..config.players[index].nick..'.lua')
  103.     table.remove(config.players,index)
  104.     conf_S()
  105. end
  106.  
  107. function playersLimitter()
  108.     comp.beep(1700)
  109.     g.set(1,y-1,'> Вы достигли лимита в '..maxPlayers..' игроков!')
  110.     os.sleep(2)
  111. end
  112.  
  113. function makeUserScript(name)
  114.     local file = io.open(workDir..name..'.lua','w')
  115.     file:flush()
  116.     file:close()
  117. end
  118.  
  119. function addPlayer()
  120.     ev.ignore('touch',clicker)
  121.     g.fill(1,10,100,100,' ')
  122.     for col = 1, #config.colors do
  123.         g.setForeground(config.colors[col])
  124.         if col <= 10 then
  125.             g.set(col*5-3, 24, '>'..tostring(col))
  126.         else
  127.             g.set(col*5-3-x, 25, '>'..tostring(col))
  128.         end
  129.     end
  130.     g.setForeground(config.colors[9])
  131.     g.fill(1,10,100,1,'─')
  132.     g.fill(1,23,100,1,'─')
  133.     g.set(23,22,'Цвета:')
  134.     term.setCursor(1,11)
  135.     io.write('> Новый ник: ')
  136.     n = io.read()
  137.     io.write('> Префикс: ')
  138.     p = io.read()
  139.     io.write('> Цвет ника (по номеру ниже): ')
  140.     local input = tonumber(io.read())
  141.     if input == nil or input > 20 or input < 1 then nC = 1
  142.     else nC = math.floor(input) end
  143.     io.write('> Цвет префикса (по номеру ниже): ')
  144.     local input = tonumber(io.read())
  145.     if input == nil or input > 20 or input < 1 then pC = 1
  146.     else pC = math.floor(input) end
  147.     io.write('> Создать скрипт '..workDir..'*ник игрока*.lua,\nвыполняющийся при заходе игрока? Y/N: ')
  148.     local input = io.read()
  149.     if input == 'y' or input == 'Y' then makeUserScript(n) end
  150.     table.insert(config.players,{ nick = n, prefix = p, prefixColor = config.colors[pC], nickColor = config.colors[nC], activeScript = true }) conf_S()
  151.     ev.listen('touch',clicker)
  152. end
  153.  
  154. function conf_S()
  155.     local file = io.open(workDir..'config.cfg','w')
  156.     file:write(ser.serialize(config,false))
  157.     file:flush()
  158.     file:close()
  159. end
  160.  
  161. function conf_R()
  162.     local file = io.open(workDir..'config.cfg','r')
  163.     config = ser.unserialize(file:read(fs.size(workDir..'config.cfg')))
  164.     file:close()
  165. end
  166.  
  167. function run()
  168.     if not fs.exists(workDir) then fs.makeDirectory(workDir) conf_S() else conf_R() end
  169.     ev.listen('touch',clicker)
  170.     while work do
  171.         check()
  172.         os.sleep(1)
  173.     end
  174.     exit()
  175. end
  176.  
  177. function exit()
  178.     conf_S()
  179.     ev.ignore('touch',clicker)
  180.     clear()
  181.     g.setForeground(config.colors[1])
  182.     g.setResolution(xOld,yOld)
  183. end
  184.  
  185. function start()
  186.     if g.maxDepth() <= 4 then
  187.         comp.beep(1500)
  188.         print('CheckPlayer: Ошибка! Глубина цвета 4 и ниже не поддерживается! Наобходима видеокарта и монитор 3 уровня.')
  189.     else
  190.         run()
  191.     end
  192. end
  193.  
  194. function ev.shouldInterrupt()
  195.     return false
  196. end
  197.  
  198. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement