Advertisement
Guest User

chatty

a guest
May 13th, 2015
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.48 KB | None | 0 0
  1. --[[
  2. Copyright 2015 Max Thor <thormax5@gmail.com>
  3.  
  4.    Licensed under the Apache License, Version 2.0 (the "License");
  5.    you may not use this file except in compliance with the License.
  6.    You may obtain a copy of the License at
  7.  
  8.        http://www.apache.org/licenses/LICENSE-2.0
  9.  
  10.    Unless required by applicable law or agreed to in writing, software
  11.    distributed under the License is distributed on an "AS IS" BASIS,
  12.    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.    See the License for the specific language governing permissions and
  14.    limitations under the License.
  15.    ]]--
  16.  
  17. for k,v in ipairs(rs.getSides()) do
  18.     if(peripheral.getType(v) == "modem") then
  19.       modem = peripheral.wrap(v)
  20.     end
  21. end
  22.  
  23. local msg = ""
  24. local name = ""
  25. local cmd = ""
  26. local args
  27. local w,h = term.getSize()
  28. local debug = true
  29. local channel = 1
  30. local chans = {"1", "2", "3"}
  31. local cmds = {
  32.   ["me"] = {
  33.     ["does"] = function(args)
  34.       send('* '..name..' '..args)
  35.     end
  36.   },
  37.   ["nick"] = {
  38.     ["does"] = function(nc)
  39.       send(name..' is now known as '..nc)
  40.       name=nc
  41.     end
  42.   },
  43.   ["server"] = {
  44.     ["does"] = function(args)
  45.       term.setCursorPos(1, 5)
  46.       print(args)
  47.       channel = tonumber(args[1])
  48.       for k, v in pairs(chans) do
  49.         if v == args[1] then
  50.        
  51.         else
  52.           table.insert(chans, args[1])
  53.         end
  54.       end
  55.       --term.clear()
  56.       --line()
  57.     end
  58.   },
  59.   ["debug"] = {
  60.     ["does"] = function(args)
  61.       if debug then
  62.         print(loadstring(args))
  63.       end
  64.     end
  65.   }
  66. }
  67.  
  68. local history = {}
  69.  
  70. function gui()
  71.   term.clear()
  72.   term.setCursorPos(1,1)
  73.   paintutils.drawLine(1, 1, w, 1, colors.red)
  74.   term.setCursorPos(1,1)
  75.   print(name)
  76.   term.setBackgroundColor(colors.black)
  77.   term.setCursorPos(1, h);
  78. end
  79.  
  80. function line()
  81.   x = name:len()+1
  82.   term.setCursorPos(1,1)
  83.   paintutils.drawLine(1, 1, w, 1, colors.red)
  84.   term.setCursorPos(1,1)
  85.   print(name)
  86.   term.setBackgroundColor(colors.red)
  87.   term.setCursorPos(1, h)
  88.   for k, v in pairs(chans) do
  89.     x = x+2
  90.     term.setCursorPos(x, 1)
  91.     if(tonumber(v) == channel) then
  92.       term.setBackgroundColor(colors.cyan)
  93.       write(v)
  94.       term.setBackgroundColor(colors.red)
  95.     else
  96.       write(v)
  97.     end
  98.   end
  99.     term.setBackgroundColor(colors.black)
  100. end
  101.  
  102. function initConf()
  103.   modem.open(channel)
  104.   gui()
  105. end
  106.  
  107. function send(msg)
  108.    modem.transmit(channel,channel,msg)
  109. end
  110.  
  111. function input()
  112.   while true do  
  113.     _, p1 = os.pullEvent("key")
  114.     if(p1 == 28) then
  115.       local inp = read()
  116.       line()
  117.       term.setCursorPos(1, h-1)
  118.       term.clearLine()
  119.       local cmd,args = inp:match("^/(%w+)%s*(.-)$")
  120.       if cmd then
  121.         if(cmds[cmd]) then
  122.           cmds[cmd]['does'](args)
  123.         else
  124.                print("Unknown command: "..cmd)
  125.         end
  126.       else
  127.         send("<"..name.."> "..inp)
  128.         term.clearLine()
  129.           print("<"..name.."> "..inp)
  130.       end
  131.     end
  132.   end
  133. end
  134.  
  135. function chat()
  136.   while true do
  137.     e, p1, p2, p3, p4 = os.pullEventRaw()
  138.     if(e == "modem_message") then
  139.       if(p2 == channel) then
  140.         --line()
  141.         print(p4)
  142.         line()
  143.       end
  144.     elseif(e == "terminate") then
  145.       send(name.." has left")
  146.     end
  147.   end
  148. end
  149.  
  150. function init()
  151.   term.clear()
  152.   term.setCursorPos(1,1)
  153.   print("Chatty")
  154.   write("Name > ")
  155.   name = read()
  156.   initConf()
  157.   send(name.." has joined!")
  158.   parallel.waitForAny(chat,input)
  159. end
  160.  
  161. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement