Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 KB | None | 0 0
  1. local GC = {}
  2. GlobalChat = GC
  3.  
  4. GC.VERSION = 3
  5.  
  6. GC.Host = "162.248.88.21"
  7. GC.Port = 27015
  8. GC.User = "Fast Red Snail"
  9. GC.ServerID = 0xA2
  10. GC.Pass = "alphabets"
  11.  
  12. ///////Initialization/////////
  13.  
  14. util.AddNetworkString('GlobalChatText')
  15.  
  16. print(pcall(function() require("luasocket") end))
  17.  
  18. local GSck = assert(socket.tcp())
  19.  
  20.  
  21. ///Setup TCP Socket///
  22.  
  23.  
  24. local function msg(...)
  25. MsgC(Color(84,0,255),"[Global Chat 3] ")
  26. MsgC(Color(165,81,255), ..., "\n")
  27.  
  28. end
  29.  
  30. local LastReconnectAttempt = CurTime() + 4
  31.  
  32. function GC.TryConnect()
  33.  
  34. MsgC(Color(84,0,255),"[Global Chat 3] ")
  35. MsgC(Color(165,81,255),"Connecting... \n")
  36.  
  37. GSck:setoption('reuseaddr',true)
  38. GSck:settimeout(0,"b")
  39. GSck:connect(GC.Host,GC.Port)
  40.  
  41. timer.Simple(0.1,function()
  42. if type(GSck)=="tcp{client}" then
  43. local dta, stat = GSck:send(GC.Pass)
  44. if stat!="closed" then
  45. msg('Connected')
  46. else
  47. msg("CONNECTION FAILED.")
  48. end
  49.  
  50. else
  51. msg('should never happen -- connection failed.')
  52. end
  53. end)
  54. end
  55.  
  56. GC.TryConnect()
  57.  
  58. function GC.TryReconnect()
  59. if LastReconnectAttempt < CurTime() then
  60. MsgC(Color(84,0,255),"[Global Chat 3] ")
  61. MsgC(Color(165,81,255),"Attempting reconnect... \n")
  62. GC.TryConnect()
  63. LastReconnectAttempt = CurTime() + 4
  64. return true
  65. end
  66. end
  67.  
  68.  
  69. local function PlayerInfoJson(ply,msgstr,mergtable)
  70. if !IsValid(ply) then return false end
  71. local pinfo = {}
  72. pinfo["name"] = ply:Nick()
  73. pinfo["steamid"] = ply:SteamID()
  74. pinfo["steamid64"] = ply:SteamID64()
  75. pinfo["team"] = team.GetName(ply:Team())
  76. pinfo["teamcolor"] = team.GetColor(ply:Team())
  77. pinfo["message"] = msgstr
  78. pinfo["ugrp"] = ply:GetUserGroup();
  79. pinfo["gdisabled"] = ply["DisableGlobalChat"]==true // spooky
  80. if !mergtable then mergtable = {} end
  81. for k,v in pairs(mergtable) do
  82. pinfo[k]=v
  83. end
  84. return pinfo
  85. end
  86.  
  87.  
  88.  
  89. function GC.OnData(data)
  90. if #data < 10 then
  91. local bt = string.byte(data)
  92.  
  93. if bt == 0x20 then
  94. msg('authentication requested')
  95. elseif bt==0x4 then
  96. msg('auth ok')
  97. elseif bt==0x5 then
  98. msg('authentication failed')
  99. end
  100.  
  101. else
  102.  
  103. if data[1]=="{" then
  104. local buftab = util.JSONToTable(data)
  105.  
  106.  
  107.  
  108. if buftab["Type"]=="GlobalChat" then
  109.  
  110. local server = buftab["ServerID"]
  111. local buff = buftab["BufferData"]
  112.  
  113. local msg = {Color(255,255,255),"[",server,"]"," ",buff["teamcolor"],buff["name"],Color(255,255,255),": ",buff["message"]}
  114.  
  115.  
  116.  
  117.  
  118. for k,v in pairs(player.GetAll()) do
  119. if v["DisableGlobalChat"]~=true then
  120.  
  121. net.Start("GlobalChatText")
  122. net.WriteTable(msg)
  123. net.Send(v)
  124. end
  125. end
  126.  
  127.  
  128.  
  129. end
  130.  
  131. if buftab["Type"]=="ServerMessage" then
  132.  
  133. local server = buftab["ServerID"]
  134. local buff = buftab["BufferData"]
  135.  
  136. local msg = {Color(255,0,255),"[",server,"]",Color(255,255,0)," : ", buff["message"]}
  137.  
  138.  
  139.  
  140. for k,v in pairs(player.GetAll()) do
  141. net.Start("GlobalChatText")
  142. net.WriteTable(msg)
  143. net.Send(v)
  144. end
  145.  
  146. end
  147.  
  148.  
  149.  
  150. if buftab["Type"]=="CServMessage" then
  151.  
  152. local buff = buftab["BufferData"]
  153.  
  154. local msg = {Color(150,150,150),"[ChatServer]",Color(255,255,255)," : ", buff["message"]}
  155.  
  156.  
  157.  
  158. for k,v in pairs(player.GetAll()) do
  159.  
  160. net.Start("GlobalChatText")
  161. net.WriteTable(msg)
  162. net.Send(v)
  163.  
  164. end
  165.  
  166.  
  167.  
  168. end
  169.  
  170. end
  171.  
  172. end
  173. end
  174.  
  175.  
  176.  
  177. function GC.Chat(plr,t)
  178.  
  179. local tbrk = string.Explode(" ",t)
  180.  
  181.  
  182. if tbrk[1]=="!global" then
  183. if tbrk[2]=="on" then
  184.  
  185.  
  186. local msg = {Color(150,150,150),"[GlobalChat]: ",Color(255,255,255), "Global chat is now ON."}
  187.  
  188. net.Start("GlobalChatText")
  189. net.WriteTable(msg)
  190. net.Send(plr)
  191.  
  192. plr["DisableGlobalChat"] = false
  193. end
  194. if tbrk[2]=="off" then
  195.  
  196. local msg = {Color(150,150,150),"[GlobalChat]: ",Color(255,255,255), "Global chat is now OFF."}
  197.  
  198. net.Start("GlobalChatText")
  199. net.WriteTable(msg)
  200. net.Send(plr)
  201.  
  202.  
  203.  
  204. plr["DisableGlobalChat"] = true
  205. end
  206. return ""
  207. end
  208.  
  209. if tbrk[1]=="/global" then
  210. table.remove(tbrk,1)
  211. local bad = table.concat(tbrk," ")
  212. local msg = {Color(255,255,255),"[",GC.User,"]"," ",team.GetColor(plr:Team()),plr:Nick(),Color(255,255,255),": ",bad}
  213.  
  214.  
  215.  
  216.  
  217. for k,v in pairs(player.GetAll()) do
  218. if v["DisableGlobalChat"]~=true then
  219.  
  220. net.Start("GlobalChatText")
  221. net.WriteTable(msg)
  222. net.Send(v)
  223. end
  224. end
  225.  
  226.  
  227.  
  228. GC.Send(PlayerInfoJson(plr,bad),"GlobalChat")
  229. return ""
  230. end
  231.  
  232. end
  233. hook.Add("PlayerSay","Global Chat 3",GC.Chat)
  234.  
  235. function GC.SendServerMessage(msg)
  236. GC.Send({message = msg},"ServerMessage")
  237. end
  238.  
  239. function GC.Send(tab,identity)
  240. local buff = {}
  241. buff["ServerID"] = GC.User
  242. buff["IDN"] = GC.ServerID
  243. buff["Type"] = identity
  244. buff["BufferData"] = tab
  245.  
  246. local bufftext = util.TableToJSON(buff)
  247.  
  248. GSck:send(bufftext)
  249.  
  250. end
  251.  
  252.  
  253. function GC.ThinkNetwork()
  254. if type(GSck)=="tcp{client}" then
  255.  
  256. local data, status, partial = GSck:receive()
  257. local data_real = data or partial
  258.  
  259. if status=="closed" or status=="Transport endpoint is not connected" then
  260. if GC.TryReconnect()==true then
  261. msg('lost connection -- reconnecting.')
  262. end
  263.  
  264. elseif data_real and #data_real > 0 then
  265. GC.OnData(data_real)
  266. end
  267. else
  268. GC.TryReconnect()
  269. end
  270.  
  271. end
  272.  
  273. hook.Add("Think","GCReceiveThink",GC.ThinkNetwork)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement