Advertisement
Guest User

NC102.lua

a guest
Jul 21st, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.26 KB | None | 0 0
  1. -- Neptunium Chat (build2 | July 21st, 2019) | Built on FlexChat
  2.  
  3. term = require("term")
  4. component = require("component")
  5. computer = require("computer")
  6. event = require("event")
  7. gpu = component.gpu
  8. os = require("os")
  9. w, h = gpu.getResolution()
  10. serialization = require("serialization")
  11. messages = {}
  12. buffer = ""
  13. invalidating = false
  14.  
  15. term.clear()
  16.  
  17. computer.beep(100, 0.25)
  18. computer.beep(150, 0.25)
  19. computer.beep(200, 0.5)
  20.  
  21. print("Welcome to Neptunium Chat (Build 2), built upon (and compatible with) FlexChat! \n")
  22. print("Please select the port you'd like to communicate through:")
  23. port = tonumber(term.read())
  24.  
  25. while port == nil do
  26.     print("Unknown port number.")
  27.     computer.beep(1500, 0.5)
  28.     port = tonumber(io.stdin:read())
  29. end
  30.  
  31. print("Please choose a theme for your client:")
  32. print("[D]efault, [I]nverted [L]ight, [S]ummer, [F]all")
  33. usertheme = io.read()
  34.  
  35. if usertheme == "D" or usertheme == "d" then -- Select the default theme
  36.     gpu.setForeground(0xFFFFFF)
  37.     gpu.setBackground(0x000000)
  38.     gpu.fill(1, 1, w, h, " ")
  39.  
  40. elseif usertheme == "I" or usertheme == "i" then -- Select the inverted theme (better than light *I* guess :P)
  41.     gpu.setForeground(0x000000)
  42.     gpu.setBackground(0xFFFFFF)
  43.     gpu.fill(1, 1, w, h, " ")
  44.  
  45. elseif usertheme == "L" or usertheme == "l" then -- Select the light theme (freak)
  46.     gpu.setForeground(0x000000)
  47.     gpu.setBackground(0xC0C0C0)
  48.     gpu.fill(1, 1, w, h, " ")
  49.  
  50. elseif usertheme == "S" or usertheme == "s" then -- Select the bright summer theme
  51.     gpu.setForeground(0x008000)
  52.     gpu.setBackground(0x0000FF)
  53.     gpu.fill(1, 1, w, h, " ")
  54.  
  55. elseif usertheme == "F" or usertheme == "f" then -- Select the warm fall theme
  56.     gpu.setForeground(0x808080)
  57.     gpu.setBackground(0x654321)
  58.     gpu.fill(1, 1, w, h, " ")
  59.  
  60. elseif usertheme == "DE" or usetheme == "de" then -- Select the debug theme (easter egg!)
  61.     gpu.setForeground(0x000000)
  62.     gpu.setBackground(0x008000)
  63.     gpu.fill(1, 1, w, h, " ")
  64.  
  65. else
  66.    end
  67.  
  68. print("Please insert an alias:")
  69. alias = term.read()
  70. alias = alias:gsub("\n", "")
  71.  
  72. term.clear()
  73.  
  74. width, height = term.getViewport()
  75. height = height + 1
  76.  
  77. component.modem.open(port)
  78.  
  79. function getLines(str)
  80.         local lines = math.ceil(str:len()/width)
  81.         if lines == 0 then lines = 1 end
  82.         return lines
  83. end
  84.  
  85. function invalidate()
  86.     invalidating = true
  87.     term.clear()
  88.     local bufLines = getLines(buffer)
  89.     local lineBudget = height - bufLines
  90.     for i, v in ipairs(messages) do
  91.         local message = messages[i]
  92.         local lines = getLines(message.content)
  93.         lineBudget = lineBudget - lines
  94.         if lineBudget <= 0 then break end
  95.            
  96.         term.setCursor(1, lineBudget)
  97.         term.write(("[" .. message.name .. "]> " .. message.content), true)
  98.     end
  99.  
  100.     term.setCursor(1, height-bufLines)
  101.     term.write(buffer, true)
  102.     invalidating = false
  103. end
  104.  
  105. function spin()
  106.     while invalidating do
  107.         -- Spin until done invalidating
  108.     end
  109. end
  110.  
  111. function moveCursorBack()
  112.     local x, y = term.getCursor()
  113.     if x ~= 1 then
  114.         term.setCursor(x - 2, y)
  115.     end
  116.   invalidate()
  117. end
  118.  
  119. function unknownEvent() end -- If event is not relevant, do nothing
  120. eventHandlers = setmetatable({}, {__index = function() return unknownEvent end })
  121.  
  122. function eventHandlers.modem_message(_, _, _, _, message)
  123.     spin()
  124.     local msg = serialization.unserialize(message)
  125.     msg.name = msg.name:gsub("\n", "")
  126.     table.insert(messages, 1, msg) -- The first message is the latest
  127.   invalidate()
  128. end
  129.  
  130. function eventHandlers.key_down(kbdAddr, char, code, pName)
  131.     spin()
  132.     if char == 13 then -- Enter
  133.         local serializedMsg = serialization.serialize({name=alias,content=buffer})
  134.         buffer=""
  135.         eventHandlers["modem_message"](nil, nil, nil, nil, serializedMsg)
  136.         component.modem.broadcast(port, serializedMsg)
  137.       return
  138.     end
  139.    
  140.     if char == 127 then -- Backspace
  141.         moveCursorBack()
  142.         buffer = buffer:sub(1, -2)
  143.         invalidate()
  144.         return
  145.     end
  146.  
  147.     if char >= 63232 and char <= 63235 then -- Arrow keys
  148.         return -- They will crash otherwise
  149.     end
  150.    
  151.     buffer = buffer .. string.char(char)
  152.     invalidate()
  153. end
  154.  
  155. function handleEvent(eventId, ...)
  156.     if (eventId) then
  157.         eventHandlers[eventId](...)
  158.     end
  159. end
  160.  
  161. while true do
  162.     term.setCursorBlink(true)
  163.     handleEvent(event.pull())
  164. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement