Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.58 KB | None | 0 0
  1. local createdTopics = {}
  2. local topicsCreated = 10
  3.  
  4. local ids = 10000
  5.  
  6. local mice = {}
  7.  
  8. local POPUP = {
  9. TOPICTITLE = 1,
  10. TOPICMESSAGE = 2,
  11. TOPICREPLY = 3
  12. }
  13.  
  14. local TEXTAREA = {
  15. GOUP = -9,
  16. GODOWN = -8,
  17. TOPICDOWN = -7,
  18. TOPICUP = -6,
  19. BACKGROUND = -5,
  20. NEWTOPIC = -4,
  21. CLOSETOPIC = -3,
  22. UPDATE = -2,
  23. MINIMIZE = -1
  24. }
  25.  
  26. function drawUI(name)
  27. ui.addTextArea(TEXTAREA.BACKGROUND, "<b><p align='center'><font type='soopafresh' color='#ffff00'>Café</font></p></b>", name, 40, 40, 700, 350)
  28. --ui.addTextArea(TEXTAREA.NEWTOPIC, "<p align='center'><a href='event:createTopic'>Crear un nuevo tema</a></p>", nil, 150, 310, nil, nil, 0x1C3C41)
  29. ui.addTextArea(TEXTAREA.NEWTOPIC, "<p align='center'><a href='event:createTopic'>Crear un nuevo tema</a></p>", name, 60, 335, 300, nil, 0x1C3C41)
  30. --ui.addTextArea(TEXTAREA.UPDATE, "<a href='event:update'><font color='#FFFFFF'>⇔</color></a>", name, 700, 40, nil, nil, 0x00AA00)
  31. ui.addTextArea(TEXTAREA.UPDATE, "<a href='event:update'><font color='#FFFFFF'>✔</color></a>", name, 700, 40, nil, nil, 0x00AA00)
  32. ui.addTextArea(TEXTAREA.MINIMIZE, "<a href='event:minimize'><font color='#FFFFFF'>✘</color></a>", name, 725, 40, nil, nil, 0xFF0000)
  33. ui.addTextArea(TEXTAREA.GOUP, "<a href='event:goup'>↑</a>", name, 370, 70)
  34. ui.addTextArea(TEXTAREA.GODOWN, "<a href='event:godown'>↓</a>", name, 370, 280)
  35. end
  36.  
  37.  
  38. function eventNewPlayer(name)
  39. mice[name] = {
  40. id = ids,
  41. creatingTopic = false,
  42. topicList = 1,
  43. currentTopic = {
  44. pos = 0,
  45. page = 1
  46. },
  47. currentPage = 1,
  48. color = math.random(0x000000, 0xFFFFFF)
  49. }
  50. ids = ids + 1
  51. drawUI(name)
  52. update(name)
  53. end
  54.  
  55.  
  56. function createTopicUI(name, state)
  57. local id = mice[name].id
  58. if (state == POPUP.TOPICTITLE) then
  59. ui.addPopup(id+POPUP.TOPICTITLE, 2, "<font color='#ffff00'>Título:</font>", name, 400, 70, 300)
  60. ui.addTextArea(TEXTAREA.CLOSETOPIC, "<a href='event:createTopicClose'>[X]</a>", name, 666, 72, nil, 5, 0x324650, 0x324650)
  61. --ui.addTextArea(3, "", name, 405, 124, 290, 18, 0x324650, 0x324650)
  62. elseif (state == POPUP.TOPICMESSAGE) then
  63. ui.addPopup(id+POPUP.TOPICMESSAGE, 2, "<font color='#6a7495'>Mensaje:</font>", name, 400, 153, 300)
  64. --ui.addTextArea(2, "<a href='event:createTopicClose'>[X]</a>", name, 666, 155, nil, 5, 0x324650, 0x324650)
  65. --ui.addTextArea(4, "", name, 402, 150, 299, 10, 0x324650, 0x324650)
  66. end
  67. end
  68.  
  69. function removeTopicUI(name, minimize)
  70. local id = mice[name].id
  71. if (minimize) then
  72. for _, n in pairs(TEXTAREA) do
  73. ui.removeTextArea(n, name)
  74. end
  75. for i in pairs(createdTopics) do
  76. ui.removeTextArea(i, name)
  77. end
  78. ui.addTextArea(id, "<a href='event:maximize'><font color='#ffff00'>Café</font></a>", name, 750, 370)
  79. end
  80. -- Popups can't be removed once they're created until the player answers them,
  81. -- so as a workaround move it to an unreachable zone
  82. ui.addPopup(id+POPUP.TOPICTITLE, 2, "", name, -5000, -5000)
  83. ui.removeTextArea(id+TEXTAREA.CLOSETOPIC, name)
  84. --ui.removeTextArea(3, name)
  85.  
  86. ui.addPopup(id+POPUP.TOPICMESSAGE, 2, "", name, -5000, -5000)
  87. --ui.removeTextArea(4, name)
  88. end
  89.  
  90. function showTopic(author, title, pos, name)
  91. local topicToShow = {}
  92. local id = mice[name].id
  93. for i, topic in ipairs(createdTopics) do
  94. --print(topic.author .. " ==? " .. author .. " | " .. topic.title .. " ==? " .. title .. " | " .. i .. " ==? " .. pos)
  95. print(tostring(topic.author == author) .. " " .. tostring(topic.title == title) .. " " .. tostring(i == pos))
  96. if (topic.author == author and topic.title == title and i == pos) then
  97. print("topic to show:")
  98. print(topic.author .. " -> " .. author .. " | " .. topic.title .. " -> " .. title .. " | " .. i .. " -> " .. pos)
  99. topicToShow = topic
  100. mice[name].currentTopic.pos = pos
  101. break
  102. end
  103. end
  104. --assert(topicToShow ~= nil)
  105. if (not topicToShow or not topicToShow.replies) then
  106. update(name)
  107. --showTopic(author, title, pos, name)
  108. return
  109. end
  110.  
  111. -- ??????
  112. ui.addTextArea(mice[name].id+TEXTAREA.TOPICUP, "<a href='event:threadup'>↑</a>", name, 715, 70)
  113. ui.addTextArea(mice[name].id+TEXTAREA.TOPICDOWN, "<a href='event:threaddown'>↓</a>", name, 715, 280)
  114.  
  115. local x, y = 400, 70
  116. for i, replies in ipairs(topicToShow.replies) do
  117. local page = mice[name].currentTopic.page
  118.  
  119. if (i >= page and i <= page+3) then
  120. local _name = replies.author:gsub("#%d+", "")
  121. local tag = replies.author:match("#%d+")
  122. local txt = "<font color='#%x'>%s</font><font size='9' color='#60608F'>%s</font>\n%s"
  123. ui.addTextArea(mice[name].id+100+i, txt:format(mice[replies.author].color, _name, tag, replies.reply), name, x, y, 300, 50, 0x22464d)
  124. y = y + 60
  125. end
  126. end
  127. ui.addPopup(id+POPUP.TOPICREPLY, 2, "", name, 400, 320, 300)
  128. end
  129.  
  130. function eventTextAreaCallback(id, name, event)
  131. print(name .. " " .. event)
  132.  
  133. if (event == "minimize" and not mice[name].creatingTopic) then
  134. if (mice[name].currentTopic.pos ~= 0) then
  135. local p = mice[name].currentTopic.pos
  136. local r = createdTopics
  137. .replies
  138. local _id = mice[name].id
  139. for i in ipairs(r) do
  140. ui.removeTextArea(mice[name].id+100+i, name)
  141. end
  142. ui.removeTextArea(_id+TEXTAREA.TOPICUP, name)
  143. ui.removeTextArea(_id+TEXTAREA.TOPICDOWN, name)
  144. ui.addPopup(_id+POPUP.TOPICREPLY, 2, "", name, -5000, -5000)
  145. end
  146. removeTopicUI(name, true)
  147. elseif (event == "maximize") then
  148. ui.removeTextArea(mice[name].id, name)
  149. drawUI(name)
  150. update(name)
  151. end
  152.  
  153. if (event == "threadup") then
  154.  
  155. elseif (event == "threaddown") then
  156. local currentPos = mice[name].currentTopic.pos
  157. local currentPage = mice[name].currentTopic.page
  158. local n = #createdTopics[currentPos].replies
  159.  
  160.  
  161. end
  162.  
  163. if (event:find("createTopic") and not mice[name].creatingTopic) then
  164. ui.removeTextArea(mice[name].id+TEXTAREA.TOPICUP, author)
  165. ui.removeTextArea(mice[name].id+TEXTAREA.TOPICDOWN, author)
  166. if (id == TEXTAREA.CLOSETOPIC) then
  167. removeTopicUI(name)
  168. else
  169. if (mice[name].currentTopic.pos ~= 0) then
  170. local p = mice[name].currentTopic.pos
  171. local r = createdTopics
  172.  
  173. .replies
  174. local _id = mice[name].id
  175. for i in ipairs(r) do
  176. ui.removeTextArea(mice[name].id+100+i, name)
  177. end
  178. ui.addPopup(_id+POPUP.TOPICREPLY, 2, "", name, -5000, -5000)
  179. end
  180.  
  181. createTopicUI(name, POPUP.TOPICTITLE)
  182. end
  183. elseif (event:find("openTopic") and not mice[name].creatingTopic) then
  184. removeTopicUI(name)
  185. if (mice[name].currentTopic.pos ~= 0) then
  186. local p = mice[name].currentTopic.pos
  187. local r = createdTopics
  188.  
  189. .replies
  190. for i in ipairs(r) do
  191. ui.removeTextArea(mice[name].id+100+i, name)
  192. end
  193. end
  194. local e = event
  195. -- Get author, remove parenthesis
  196. local author = e:match("%(.+#%d+%)")
  197. if (not author) then return end
  198. author = author:gsub("%(", ""):gsub("%)", "")
  199. e = e:gsub("%(.+#%d+%)", "")
  200. -- Get position in "createdTopics" table
  201. local pos = e:match("%(%d+%)"):gsub("%(", ""):gsub("%)", "")
  202. e = e:gsub("%(%d+%)", "")
  203. -- Get title
  204. local title = e:match("%(.+%)"):gsub("%(", ""):gsub("%)", "")
  205. --print (author)
  206. --print(title)
  207. --print(pos)
  208. showTopic(author, title, tonumber(pos), name)
  209. elseif (id == TEXTAREA.GODOWN) then
  210. if (mice[name].topicList == 1) then
  211. mice[name].topicList = mice[name].topicList + 4
  212. for i=1, 4 do
  213. ui.removeTextArea(i, name)
  214. end
  215. update(name)
  216. print(mice[name].topicList)
  217. end
  218. elseif (id == TEXTAREA.GOUP) then
  219. if (mice[name].topicList == 5) then
  220. mice[name].topicList = mice[name].topicList - 4
  221. for i=1, 4 do
  222. ui.removeTextArea(i, name)
  223. end
  224. update(name)
  225. print(mice[name].topicList)
  226. end
  227. elseif (id == TEXTAREA.UPDATE and not mice[name].creatingTopic) then
  228. update(name)
  229. end
  230. end
  231.  
  232. function update(name)
  233. local x, y = 60, 70
  234. for i, topic in ipairs(createdTopics) do
  235. if (i >= mice[name].topicList and i <= mice[name].topicList+3) then
  236. local txt = "<br><a href='event:openTopic(%s)(%s)(%d)'><font size='10'><p align='center'>[%s] %s</p></font><p align='right'>%d</p></a>"
  237. ui.addTextArea(i, txt:format(topic.author, topic.title, i, topic.author, topic.title, #topic.replies), name, x, y, 300, 50, 0x1C3C41)
  238. y = y + 60
  239. end
  240. end
  241. if (mice[name].currentTopic.pos ~= 0) then
  242. local p = mice[name].currentTopic.pos
  243. local t = createdTopics
  244.  
  245.  
  246. showTopic(t.author, t.title, p, name)
  247. end
  248. --[[for author, info in pairs(createdTopics) do
  249. for _, topic in ipairs(info) do
  250. print(topic.title)
  251. print(name)
  252. local txt = "<br><a href='event:openTopic(%s)(%s)(%d)'><font size='10'><p align='center'>[%s] %s</p></font><p align='right'>%d</p></a>"
  253. ui.addTextArea(topic.position, txt:format(author, topic.title, topic.position, author, topic.title, #topic.replies+1), name, x, y, 300, 50, 0x1C3C41)
  254. y = y + 60
  255. end
  256. end]]
  257. end
  258.  
  259. function eventPopupAnswer(id, author, answer)
  260. if (id == mice[author].id + POPUP.TOPICTITLE) then
  261. --[[if (not createdTopics[author]) then
  262. createdTopics[author] = {}
  263. end]]
  264. mice[author].currentTopic.pos = 0
  265. mice[author].creatingTopic = true
  266. if (answer:len() > 21) then
  267. answer = answer:sub(1, 21)
  268. elseif (answer:len() == 0) then
  269. answer = " "
  270. end
  271. --answer = answer:gsub("<", "&lt;"):gsub(">", "&gt;"):gsub("'", "&#39;")
  272. answer = answer:gsub("<", ""):gsub(">", ""):gsub("'", "")
  273. if (answer:match("%D") == nil) then
  274. answer = string.char(math.random(97, 122)) .. answer
  275. end
  276. if (#createdTopics == 0) then
  277. table.insert(createdTopics, {
  278. author = author,
  279. title = answer,
  280. message = nil,
  281. replies = {}
  282. })
  283. else
  284. table.insert(createdTopics, 1, {
  285. author = author,
  286. title = answer,
  287. message = nil,
  288. replies = {}
  289. })
  290. for name, info in pairs(mice) do
  291. if (info.currentTopic.pos ~= 0 and name ~= author) then
  292. info.currentTopic.pos = info.currentTopic.pos + 1
  293. --update(name)
  294. end
  295. end
  296. end
  297. --[[table.insert(createdTopics[author], {
  298. author = author,
  299. title = answer,
  300. message = nil,
  301. position = 0,
  302. replies = {}
  303. })]]
  304. ui.removeTextArea(TEXTAREA.CLOSETOPIC, author)
  305. createTopicUI(author, POPUP.TOPICMESSAGE)
  306. elseif (id == mice[author].id + POPUP.TOPICMESSAGE) then
  307. mice[author].creatingTopic = false
  308. if (answer:len() > 100) then
  309. answer = answer:sub(1, 100)
  310. end
  311. if (answer:len() == 0) then
  312. answer = " "
  313. end
  314. answer = answer:gsub("<", ""):gsub(">", ""):gsub("'", "")
  315. --for _, info in pairs(createdTopics[author]) do
  316. for _, topic in ipairs(createdTopics) do
  317. print(topic.message)
  318. print(topic.author)
  319. print(tostring(topic.message == nil) .. " " .. tostring(topic.author == author))
  320. if (topic.message == nil and topic.author == author) then
  321. topic.message = answer
  322. -- Insert first post in "replies" table
  323. table.insert(topic.replies, {
  324. author = author,
  325. reply = answer
  326. })
  327. if (topicsCreated == 8) then
  328. table.remove(createdTopics, #createdTopics)
  329. else
  330. topicsCreated = topicsCreated + 1
  331. end
  332. break
  333. end
  334. end
  335. --[[for name, info in pairs(createdTopics) do
  336. for _, topic in ipairs(info) do
  337. topic.position = topic.position + 1
  338. end
  339. end]]
  340. update(author)
  341. removeTopicUI(author)
  342. elseif (id == mice[author].id + POPUP.TOPICREPLY) then
  343. local p = mice[author].currentTopic.pos
  344. local t = createdTopics
  345.  
  346.  
  347.  
  348. if (answer:len() == 0) then
  349. ui.addPopup(id+POPUP.TOPICREPLY, 2, "", name, 400, 320, 300)
  350. return
  351. end
  352.  
  353. table.insert(t.replies, {
  354.  
  355. author = author,
  356. reply = answer
  357. })
  358.  
  359. -- take topic to pos 1
  360. -- Source: https://stackoverflow.com/a/32228160
  361. table.insert(createdTopics, 1, table.remove(createdTopics, p))
  362. for name, info in pairs(mice) do
  363. if (info.currentTopic.pos ~= 0) then
  364. if (info.currentTopic.pos < p) then
  365. info.currentTopic.pos = info.currentTopic.pos + 1
  366. update(name)
  367. elseif (info.currentTopic.pos == p) then
  368. info.currentTopic.pos = 1
  369. end
  370. end
  371. end
  372. mice[author].currentTopic.pos = 1
  373. showTopic(t.author, t.title, 1, author)
  374. update(author)
  375. end
  376. end
  377.  
  378. function eventChatCommand(name, command)
  379. if (command == "color") then
  380. ui.showColorPicker(mice[name].id, name, mice[name].color, "Elige un color para tu nombre de usuario")
  381. end
  382. end
  383.  
  384. function eventColorPicked(id, name, color)
  385. if (color ~= -1) then
  386. mice[name].color = color
  387. end
  388. end
  389.  
  390. system.disableChatCommandDisplay("color")
  391. table.foreach(tfm.get.room.playerList, eventNewPlayer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement