Advertisement
syabro

GlassesChat

Jul 7th, 2014
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.30 KB | None | 0 0
  1. --local command_list = {"set text_coordX", "set text_coordY", "set text_color", "set bg_show", "set bg_color", "set bg_opacity", "set max_height", "set adm_nick", "reboot", "del msg", "ban", "unban"}
  2. local command_list = {}
  3. local admin = "Syabro"
  4. local settings = {20, 52, 0xFFFFFF, true, 0x0101FF, 0.5, 10}
  5. local banned_player = {}
  6. local text = {}
  7. local player_banned = false
  8. local this_command = false
  9.  
  10.  
  11. function newCMD(name, func)
  12.   local s = {name = name, func = func}
  13.   return s
  14. end
  15. function addCMD(cmd)
  16.   table.insert(command_list, cmd)
  17. end
  18. function findCMD(name)
  19.   for i=1, #command_list do
  20.     if (command_list[i].name == name) then
  21.       table.save(settings,"settings.ch")
  22.       table.save(banned_player,"banned_palyer.ch")
  23.       this_command = true
  24.       return command_list[i]
  25.     end
  26.   end
  27. end
  28. function table.save(tbl,filename)
  29.   local fileHandle = fs.open (filename, 'w')
  30.   fileHandle.write (textutils.serialize (tbl))
  31.   fileHandle.close()
  32. end
  33. function table.read(filename)
  34.   local fileHandle   = fs.open (filename, 'r')
  35.   tbl        = textutils.unserialize (fileHandle.readAll())
  36.   fileHandle.close()
  37.   return tbl
  38. end
  39.  
  40. local function init()
  41.   local side={"left","right","back","front","top","bottom"}
  42.   for s=1,6 do
  43.     if peripheral.isPresent(side[s]) then
  44.       if peripheral.getType(side[s]) == "openperipheral_glassesbridge" then
  45.         glass=peripheral.wrap(side[s])
  46.         glass.clear()
  47.         break
  48.       end
  49.     end
  50.   end
  51.   if fs.exists("settings.ch") then
  52.     term.clear()
  53.     term.setCursorPos(1,1)
  54.     print("Read settings file...")
  55.     settings = table.read("settings.ch")
  56.   else
  57.     table.save(settings,"settings.ch")
  58.   end
  59.   if fs.exists("banned_palyer.ch") then
  60.     term.clear()
  61.     term.setCursorPos(1,1)
  62.     print("Read banned player file...")
  63.     banned_player = table.read("banned_palyer.ch")
  64.   else
  65.     table.save(banned_player,"banned_palyer.ch")
  66.   end
  67.   if fs.exists("chCommand") then
  68.     term.clear()
  69.     term.setCursorPos(1,1)
  70.     print("Read command file...")
  71.     os.loadAPI("chCommand")
  72.   else
  73.     term.clear()
  74.     term.setCursorPos(1,1)
  75.     print("Load command file...")
  76.     shell.run("pastebin get 83Sy431N chCommand")
  77.     term.clear()
  78.     term.setCursorPos(1,1)
  79.     print("Read command file...")
  80.     os.loadAPI("chCommand")
  81.   end
  82.   if fs.exists("glass.lua") then
  83.     term.clear()
  84.     term.setCursorPos(1,1)
  85.     print("Read api file...")
  86.     dofile("glass.lua")
  87.   else
  88.     term.clear()
  89.     term.setCursorPos(1,1)
  90.     print("Load api file...")
  91.     shell.run("pastebin get DiYrqfti glass.lua")
  92.     term.clear()
  93.     term.setCursorPos(1,1)
  94.     print("Read api file...")
  95.     dofile("glass.lua")
  96.   end
  97.   addCMD(newCMD("ban",chCommand.ban))
  98.   addCMD(newCMD("unban",chCommand.unban))
  99. end
  100.  
  101. local function commands()
  102.   term.clear()
  103.   term.setCursorPos(1,1)
  104.   print("Setting service started")
  105.   while true do
  106.     _, s_msg, s_nick = os.pullEvent("chat_command")
  107.     --for g_nick, k in pairs(users) do
  108.       if s_nick == admin and string.find(s_msg, " ") ~= nil then
  109.         local msg_arr={}
  110.         for k, v in string.gmatch(s_msg, " ") do
  111.             msg_arr[k] = v
  112.          end
  113.         command_name = msg_arr[1]
  114.         table.remove(msg_arr, 1)
  115.         findCMD(command_name):func(msg_arr)
  116.       end
  117.     --end
  118.   end
  119. end
  120.  
  121. local function chat()
  122.   print("Chat started")
  123.   while true do
  124.     _, c_msg, c_nick = os.pullEvent("chat_command")
  125.     if not this_command then
  126.       for get_ban=1,#banned_player do
  127.         if c_nick==banned_player[get_ban] then
  128.           player_banned=true
  129.           break
  130.         else
  131.           player_banned=false
  132.         end
  133.       end
  134.       if not player_banned then
  135.         glass.clear()
  136.         table.insert(text,textutils.formatTime(os.time(), true).." "..c_nick..": "..tostring(c_msg))
  137.         print(textutils.formatTime(os.time(), true).." "..c_nick..": "..tostring(c_msg))
  138.         if #text>settings[7] then
  139.           table.remove(text, 1)
  140.         end
  141.         printGlassText(glass, text, settings[1], settings[2],settings[3],settings[4],settings[5],settings[6])
  142.       else
  143.         printGlassText(glass, {"You are baned!"}, settings[1], settings[2],settings[3],settings[4],0xFF0000,1,c_nick)
  144.         player_banned=false
  145.        end
  146.      end
  147.   end
  148. end
  149.  
  150. init()
  151. parallel.waitForAll(commands, chat)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement