TwiXon4ik

warp отзыв

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