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