theCPU

Not my

Mar 17th, 2018
107
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require("component")
  2. local term = require("term")
  3. local event = require("event")
  4. local unicode=require("unicode")
  5. local computer = require("computer")
  6.  
  7. local g = component.gpu
  8. local COLOR_CMD = 0xffff00 --Цвет консоли
  9. local COLOR1 = 0xfff000 --Жёлт.
  10. local COLOR2 = 0x00ff00 --Зел.
  11. local label = "&6Какая миникарта лучше?"
  12. computer.removeUser("krovyaka")
  13. event.shouldInterrupt = function () return false end
  14.  
  15. function StrToTable(str)
  16.   local t,values,st = {},{},str
  17.   while true do
  18.     if string.find(st,":")==nil then break end
  19.     t[string.sub(st,1,string.find(st,":")-1)]=string.sub(st,string.find(st,":")+1,string.find(st,":")+1)
  20.     table.insert(values,tonumber(string.sub(st,string.find(st,":")+1,string.find(st,":")+1)))
  21.     st = string.sub(st,string.find(st,":")+2,string.len(str))
  22.   end
  23.   return t,values
  24. end
  25.  
  26. function TableToStr(t)
  27.   local str=""
  28.   for key, value in pairs(t) do str=str .. key .. ":" .. value end
  29.   return str
  30. end
  31.  
  32.   local filer = io.open("/home/votes.lua", "r")
  33.   if filer:read()==nil then
  34.     filer:close()
  35.     local filew = io.open("/home/votes.lua", "w")
  36.     filew:write("t1:1t2:2")
  37.     filew:close()
  38.   else
  39.     filer:close()
  40.   end
  41.  
  42.   function draw()
  43.     g.setResolution(81,25)
  44.     term.clear()
  45.     g.setForeground(COLOR_CMD)
  46.     g.set(1,1,"||============================================================================================================================================================||")
  47.     g.set(1,25,"||============================================================================================================================================================||")
  48.     g.fill(1,1,2,25,"|")
  49.     g.fill(80,1,2,25,"|")
  50.     g.setForeground(0xffffff)
  51.     Button(15,12,18,3,COLOR1,COLOR2,"JourneyMap")
  52.     Button(49,12,18,3,COLOR1,COLOR2,"VoxelMap")
  53.     if unicode.len(label)>=120 then
  54.       Text(12,5,unicode.sub(label,1,60))
  55.       Text(12,6,unicode.sub(label,61,120))
  56.       Text(12+math.floor((60-unicode.len(unicode.sub(label,121)))/2),7,unicode.sub(label,121))
  57.     elseif unicode.len(label)>=60 then
  58.       Text(12,5,unicode.sub(label,1,60))
  59.       Text(12+math.floor((60-unicode.len(unicode.sub(label,61)))/2),6,unicode.sub(label,61))
  60.     else
  61.       Text(12+math.floor((60-unicode.len(label))/2),6,label)
  62.     end
  63. end
  64.  
  65. function Button(x,y,w,h,col1,col2,text) -- Кнопка
  66.     g.setForeground(col1)
  67.     g.set(x + w/2 - unicode.len(text)/2, y+h/2, text)
  68.     g.setForeground(col2)
  69.     for i = 1, w-2 do
  70.         g.set(x+i,y,"━")
  71.         g.set(x+i,y+h-1,"━")
  72.     end
  73.     for i = 1, h-2 do
  74.         g.set(x,y+i,"┃")
  75.         g.set(x+w-1,y+i,"┃")
  76.     end
  77.     g.set(x,y,"┏")
  78.     g.set(x+w-1,y,"┓")
  79.     g.set(x,y+h-1,"┗")
  80.     g.set(x+w-1,y+h-1,"┛")
  81. end
  82.  
  83. function Text(x,y,text) --Цветной текст
  84.     local n = 1
  85.     for i = 1, unicode.len(text) do
  86.         if unicode.sub(text, i,i) == "&" then
  87.             SetColor(unicode.sub(text, i + 1, i + 1))
  88.             n = n + 2
  89.         elseif unicode.sub(text, i - 1, i - 1) ~= "&" then
  90.             g.set(x+i-n,y, unicode.sub(text, i,i))
  91.         end
  92.     end
  93. end
  94.  
  95. function SetColor(index) --Список цветов
  96.     if (index ~= "r") then back = g.getForeground() end
  97.     if (index == "0") then g.setForeground(0x333333) end
  98.     if (index == "1") then g.setForeground(0x0000ff) end
  99.     if (index == "2") then g.setForeground(0x008000) end
  100.     if (index == "3") then g.setForeground(0x30d5c8) end
  101.     if (index == "4") then g.setForeground(0xff0000) end
  102.     if (index == "5") then g.setForeground(0x8b00ff) end
  103.     if (index == "6") then g.setForeground(0xffa500) end
  104.     if (index == "7") then g.setForeground(0xbbbbbb) end
  105.     if (index == "8") then g.setForeground(0x808080) end
  106.     if (index == "9") then g.setForeground(0x79afe8) end
  107.     if (index == "a") then g.setForeground(0x66ff66) end
  108.     if (index == "b") then g.setForeground(0x00ffff) end
  109.     if (index == "c") then g.setForeground(0xff6347) end
  110.     if (index == "d") then g.setForeground(0xff00ff) end
  111.     if (index == "e") then g.setForeground(0xffff00) end
  112.     if (index == "f") then g.setForeground(0xffffff) end
  113.     if (index == "r") then g.setForeground(back) end
  114. end
  115.  
  116. function lines(votelist)
  117.   local counter1,counter2 = 0,0
  118.  
  119. for i=1,#votelist do
  120.   if votelist[i]==1 then counter1=counter1+1 end
  121.   if votelist[i]==2 then counter2=counter2+1 end
  122. end
  123.   local length = unicode.len(string.rep("█",math.floor((counter1/#votelist)*60)))
  124.   Text(11,18,"&1" .. string.rep("█",math.floor((counter1/#votelist)*60)))
  125.   Text(11+length,18,"&4" .. string.rep("█",60-length))
  126.   Text(12,20,"&f" .. counter1 )
  127.   Text(70,20,"&f" .. counter2 )
  128. end
  129.  
  130.  
  131. function vote(nick,vo)
  132.   local filer = io.open("/home/votes.lua", "r")
  133.   local votes,_ = StrToTable(filer:read())
  134.   filer:close()
  135.  
  136.     if votes[nick]==nil then
  137.       Text(12+math.floor((60-unicode.len("&cСпасибо за голос, " .. nick))/2),10,"Спасибо за голос, " .. nick)
  138.     else
  139.       Text(12+math.floor((60-unicode.len("&cВы изменили свой выбор, " .. nick))/2),10,"Вы изменили свой выбор, " .. nick)
  140.     end
  141.    
  142.     os.sleep(1.3)
  143.     Text(4,10,"                                                            ")
  144.     votes[nick]=tostring(vo)
  145.     local filew = io.open("/home/votes.lua", "w")
  146.     filew:write(TableToStr(votes))
  147.     filew:flush() filew:close()
  148.  
  149.     local filer = io.open("/home/votes.lua", "r")
  150.     local _,votelist = StrToTable(filer:read())
  151.     filer:close()
  152.     lines(votelist)
  153. end
  154.  
  155. function Buttons(w,h,nick)
  156.     if w>=15 and w<=32 and h>=12 and h<= 14 then
  157.         vote(nick,"1")
  158.     elseif w>=50 and w<=66 and h>=12 and h<= 14 then
  159.         vote(nick,"2")
  160.     end
  161. end
  162.  
  163. --while true do print(event.pull("touch")) os.sleep() end
  164.  
  165.  
  166. draw()
  167.  
  168. while true do
  169.     local e,_,w,h,_,nick = event.pull(1, "touch")
  170.     if e == "touch" then
  171.     Buttons(w,h,nick)
  172.   end
  173. end
RAW Paste Data