Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local createdTopics = {}
- local topicsCreated = 10
- local ids = 10000
- local mice = {}
- local POPUP = {
- TOPICTITLE = 1,
- TOPICMESSAGE = 2,
- TOPICREPLY = 3
- }
- local TEXTAREA = {
- GOUP = -9,
- GODOWN = -8,
- TOPICDOWN = -7,
- TOPICUP = -6,
- BACKGROUND = -5,
- NEWTOPIC = -4,
- CLOSETOPIC = -3,
- UPDATE = -2,
- MINIMIZE = -1
- }
- function drawUI(name)
- ui.addTextArea(TEXTAREA.BACKGROUND, "<b><p align='center'><font type='soopafresh' color='#ffff00'>Café</font></p></b>", name, 40, 40, 700, 350)
- --ui.addTextArea(TEXTAREA.NEWTOPIC, "<p align='center'><a href='event:createTopic'>Crear un nuevo tema</a></p>", nil, 150, 310, nil, nil, 0x1C3C41)
- ui.addTextArea(TEXTAREA.NEWTOPIC, "<p align='center'><a href='event:createTopic'>Crear un nuevo tema</a></p>", name, 60, 335, 300, nil, 0x1C3C41)
- --ui.addTextArea(TEXTAREA.UPDATE, "<a href='event:update'><font color='#FFFFFF'>⇔</color></a>", name, 700, 40, nil, nil, 0x00AA00)
- ui.addTextArea(TEXTAREA.UPDATE, "<a href='event:update'><font color='#FFFFFF'>✔</color></a>", name, 700, 40, nil, nil, 0x00AA00)
- ui.addTextArea(TEXTAREA.MINIMIZE, "<a href='event:minimize'><font color='#FFFFFF'>✘</color></a>", name, 725, 40, nil, nil, 0xFF0000)
- ui.addTextArea(TEXTAREA.GOUP, "<a href='event:goup'>↑</a>", name, 370, 70)
- ui.addTextArea(TEXTAREA.GODOWN, "<a href='event:godown'>↓</a>", name, 370, 280)
- end
- function eventNewPlayer(name)
- mice[name] = {
- id = ids,
- creatingTopic = false,
- topicList = 1,
- currentTopic = {
- pos = 0,
- page = 1
- },
- currentPage = 1,
- color = math.random(0x000000, 0xFFFFFF)
- }
- ids = ids + 1
- drawUI(name)
- update(name)
- end
- function createTopicUI(name, state)
- local id = mice[name].id
- if (state == POPUP.TOPICTITLE) then
- ui.addPopup(id+POPUP.TOPICTITLE, 2, "<font color='#ffff00'>Título:</font>", name, 400, 70, 300)
- ui.addTextArea(TEXTAREA.CLOSETOPIC, "<a href='event:createTopicClose'>[X]</a>", name, 666, 72, nil, 5, 0x324650, 0x324650)
- --ui.addTextArea(3, "", name, 405, 124, 290, 18, 0x324650, 0x324650)
- elseif (state == POPUP.TOPICMESSAGE) then
- ui.addPopup(id+POPUP.TOPICMESSAGE, 2, "<font color='#6a7495'>Mensaje:</font>", name, 400, 153, 300)
- --ui.addTextArea(2, "<a href='event:createTopicClose'>[X]</a>", name, 666, 155, nil, 5, 0x324650, 0x324650)
- --ui.addTextArea(4, "", name, 402, 150, 299, 10, 0x324650, 0x324650)
- end
- end
- function removeTopicUI(name, minimize)
- local id = mice[name].id
- if (minimize) then
- for _, n in pairs(TEXTAREA) do
- ui.removeTextArea(n, name)
- end
- for i in pairs(createdTopics) do
- ui.removeTextArea(i, name)
- end
- ui.addTextArea(id, "<a href='event:maximize'><font color='#ffff00'>Café</font></a>", name, 750, 370)
- end
- -- Popups can't be removed once they're created until the player answers them,
- -- so as a workaround move it to an unreachable zone
- ui.addPopup(id+POPUP.TOPICTITLE, 2, "", name, -5000, -5000)
- ui.removeTextArea(id+TEXTAREA.CLOSETOPIC, name)
- --ui.removeTextArea(3, name)
- ui.addPopup(id+POPUP.TOPICMESSAGE, 2, "", name, -5000, -5000)
- --ui.removeTextArea(4, name)
- end
- function showTopic(author, title, pos, name)
- local topicToShow = {}
- local id = mice[name].id
- for i, topic in ipairs(createdTopics) do
- --print(topic.author .. " ==? " .. author .. " | " .. topic.title .. " ==? " .. title .. " | " .. i .. " ==? " .. pos)
- print(tostring(topic.author == author) .. " " .. tostring(topic.title == title) .. " " .. tostring(i == pos))
- if (topic.author == author and topic.title == title and i == pos) then
- print("topic to show:")
- print(topic.author .. " -> " .. author .. " | " .. topic.title .. " -> " .. title .. " | " .. i .. " -> " .. pos)
- topicToShow = topic
- mice[name].currentTopic.pos = pos
- break
- end
- end
- --assert(topicToShow ~= nil)
- if (not topicToShow or not topicToShow.replies) then
- update(name)
- --showTopic(author, title, pos, name)
- return
- end
- -- ??????
- ui.addTextArea(mice[name].id+TEXTAREA.TOPICUP, "<a href='event:threadup'>↑</a>", name, 715, 70)
- ui.addTextArea(mice[name].id+TEXTAREA.TOPICDOWN, "<a href='event:threaddown'>↓</a>", name, 715, 280)
- local x, y = 400, 70
- for i, replies in ipairs(topicToShow.replies) do
- local page = mice[name].currentTopic.page
- if (i >= page and i <= page+3) then
- local _name = replies.author:gsub("#%d+", "")
- local tag = replies.author:match("#%d+")
- local txt = "<font color='#%x'>%s</font><font size='9' color='#60608F'>%s</font>\n%s"
- ui.addTextArea(mice[name].id+100+i, txt:format(mice[replies.author].color, _name, tag, replies.reply), name, x, y, 300, 50, 0x22464d)
- y = y + 60
- end
- end
- ui.addPopup(id+POPUP.TOPICREPLY, 2, "", name, 400, 320, 300)
- end
- function eventTextAreaCallback(id, name, event)
- print(name .. " " .. event)
- if (event == "minimize" and not mice[name].creatingTopic) then
- if (mice[name].currentTopic.pos ~= 0) then
- local p = mice[name].currentTopic.pos
- local r = createdTopics
- .replies
- local _id = mice[name].id
- for i in ipairs(r) do
- ui.removeTextArea(mice[name].id+100+i, name)
- end
- ui.removeTextArea(_id+TEXTAREA.TOPICUP, name)
- ui.removeTextArea(_id+TEXTAREA.TOPICDOWN, name)
- ui.addPopup(_id+POPUP.TOPICREPLY, 2, "", name, -5000, -5000)
- end
- removeTopicUI(name, true)
- elseif (event == "maximize") then
- ui.removeTextArea(mice[name].id, name)
- drawUI(name)
- update(name)
- end
- if (event == "threadup") then
- elseif (event == "threaddown") then
- local currentPos = mice[name].currentTopic.pos
- local currentPage = mice[name].currentTopic.page
- local n = #createdTopics[currentPos].replies
- end
- if (event:find("createTopic") and not mice[name].creatingTopic) then
- ui.removeTextArea(mice[name].id+TEXTAREA.TOPICUP, author)
- ui.removeTextArea(mice[name].id+TEXTAREA.TOPICDOWN, author)
- if (id == TEXTAREA.CLOSETOPIC) then
- removeTopicUI(name)
- else
- if (mice[name].currentTopic.pos ~= 0) then
- local p = mice[name].currentTopic.pos
- local r = createdTopics
- .replies
- local _id = mice[name].id
- for i in ipairs(r) do
- ui.removeTextArea(mice[name].id+100+i, name)
- end
- ui.addPopup(_id+POPUP.TOPICREPLY, 2, "", name, -5000, -5000)
- end
- createTopicUI(name, POPUP.TOPICTITLE)
- end
- elseif (event:find("openTopic") and not mice[name].creatingTopic) then
- removeTopicUI(name)
- if (mice[name].currentTopic.pos ~= 0) then
- local p = mice[name].currentTopic.pos
- local r = createdTopics
- .replies
- for i in ipairs(r) do
- ui.removeTextArea(mice[name].id+100+i, name)
- end
- end
- local e = event
- -- Get author, remove parenthesis
- local author = e:match("%(.+#%d+%)")
- if (not author) then return end
- author = author:gsub("%(", ""):gsub("%)", "")
- e = e:gsub("%(.+#%d+%)", "")
- -- Get position in "createdTopics" table
- local pos = e:match("%(%d+%)"):gsub("%(", ""):gsub("%)", "")
- e = e:gsub("%(%d+%)", "")
- -- Get title
- local title = e:match("%(.+%)"):gsub("%(", ""):gsub("%)", "")
- --print (author)
- --print(title)
- --print(pos)
- showTopic(author, title, tonumber(pos), name)
- elseif (id == TEXTAREA.GODOWN) then
- if (mice[name].topicList == 1) then
- mice[name].topicList = mice[name].topicList + 4
- for i=1, 4 do
- ui.removeTextArea(i, name)
- end
- update(name)
- print(mice[name].topicList)
- end
- elseif (id == TEXTAREA.GOUP) then
- if (mice[name].topicList == 5) then
- mice[name].topicList = mice[name].topicList - 4
- for i=1, 4 do
- ui.removeTextArea(i, name)
- end
- update(name)
- print(mice[name].topicList)
- end
- elseif (id == TEXTAREA.UPDATE and not mice[name].creatingTopic) then
- update(name)
- end
- end
- function update(name)
- local x, y = 60, 70
- for i, topic in ipairs(createdTopics) do
- if (i >= mice[name].topicList and i <= mice[name].topicList+3) then
- 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>"
- ui.addTextArea(i, txt:format(topic.author, topic.title, i, topic.author, topic.title, #topic.replies), name, x, y, 300, 50, 0x1C3C41)
- y = y + 60
- end
- end
- if (mice[name].currentTopic.pos ~= 0) then
- local p = mice[name].currentTopic.pos
- local t = createdTopics
- showTopic(t.author, t.title, p, name)
- end
- --[[for author, info in pairs(createdTopics) do
- for _, topic in ipairs(info) do
- print(topic.title)
- print(name)
- 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>"
- ui.addTextArea(topic.position, txt:format(author, topic.title, topic.position, author, topic.title, #topic.replies+1), name, x, y, 300, 50, 0x1C3C41)
- y = y + 60
- end
- end]]
- end
- function eventPopupAnswer(id, author, answer)
- if (id == mice[author].id + POPUP.TOPICTITLE) then
- --[[if (not createdTopics[author]) then
- createdTopics[author] = {}
- end]]
- mice[author].currentTopic.pos = 0
- mice[author].creatingTopic = true
- if (answer:len() > 21) then
- answer = answer:sub(1, 21)
- elseif (answer:len() == 0) then
- answer = " "
- end
- --answer = answer:gsub("<", "<"):gsub(">", ">"):gsub("'", "'")
- answer = answer:gsub("<", ""):gsub(">", ""):gsub("'", "")
- if (answer:match("%D") == nil) then
- answer = string.char(math.random(97, 122)) .. answer
- end
- if (#createdTopics == 0) then
- table.insert(createdTopics, {
- author = author,
- title = answer,
- message = nil,
- replies = {}
- })
- else
- table.insert(createdTopics, 1, {
- author = author,
- title = answer,
- message = nil,
- replies = {}
- })
- for name, info in pairs(mice) do
- if (info.currentTopic.pos ~= 0 and name ~= author) then
- info.currentTopic.pos = info.currentTopic.pos + 1
- --update(name)
- end
- end
- end
- --[[table.insert(createdTopics[author], {
- author = author,
- title = answer,
- message = nil,
- position = 0,
- replies = {}
- })]]
- ui.removeTextArea(TEXTAREA.CLOSETOPIC, author)
- createTopicUI(author, POPUP.TOPICMESSAGE)
- elseif (id == mice[author].id + POPUP.TOPICMESSAGE) then
- mice[author].creatingTopic = false
- if (answer:len() > 100) then
- answer = answer:sub(1, 100)
- end
- if (answer:len() == 0) then
- answer = " "
- end
- answer = answer:gsub("<", ""):gsub(">", ""):gsub("'", "")
- --for _, info in pairs(createdTopics[author]) do
- for _, topic in ipairs(createdTopics) do
- print(topic.message)
- print(topic.author)
- print(tostring(topic.message == nil) .. " " .. tostring(topic.author == author))
- if (topic.message == nil and topic.author == author) then
- topic.message = answer
- -- Insert first post in "replies" table
- table.insert(topic.replies, {
- author = author,
- reply = answer
- })
- if (topicsCreated == 8) then
- table.remove(createdTopics, #createdTopics)
- else
- topicsCreated = topicsCreated + 1
- end
- break
- end
- end
- --[[for name, info in pairs(createdTopics) do
- for _, topic in ipairs(info) do
- topic.position = topic.position + 1
- end
- end]]
- update(author)
- removeTopicUI(author)
- elseif (id == mice[author].id + POPUP.TOPICREPLY) then
- local p = mice[author].currentTopic.pos
- local t = createdTopics
- if (answer:len() == 0) then
- ui.addPopup(id+POPUP.TOPICREPLY, 2, "", name, 400, 320, 300)
- return
- end
- table.insert(t.replies, {
- author = author,
- reply = answer
- })
- -- take topic to pos 1
- -- Source: https://stackoverflow.com/a/32228160
- table.insert(createdTopics, 1, table.remove(createdTopics, p))
- for name, info in pairs(mice) do
- if (info.currentTopic.pos ~= 0) then
- if (info.currentTopic.pos < p) then
- info.currentTopic.pos = info.currentTopic.pos + 1
- update(name)
- elseif (info.currentTopic.pos == p) then
- info.currentTopic.pos = 1
- end
- end
- end
- mice[author].currentTopic.pos = 1
- showTopic(t.author, t.title, 1, author)
- update(author)
- end
- end
- function eventChatCommand(name, command)
- if (command == "color") then
- ui.showColorPicker(mice[name].id, name, mice[name].color, "Elige un color para tu nombre de usuario")
- end
- end
- function eventColorPicked(id, name, color)
- if (color ~= -1) then
- mice[name].color = color
- end
- end
- system.disableChatCommandDisplay("color")
- table.foreach(tfm.get.room.playerList, eventNewPlayer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement