Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local GC = {}
- GlobalChat = GC
- GC.VERSION = 3
- GC.Host = "162.248.88.21"
- GC.Port = 27015
- GC.User = "Fast Red Snail"
- GC.ServerID = 0xA2
- GC.Pass = "alphabets"
- ///////Initialization/////////
- util.AddNetworkString('GlobalChatText')
- print(pcall(function() require("luasocket") end))
- local GSck = assert(socket.tcp())
- ///Setup TCP Socket///
- local function msg(...)
- MsgC(Color(84,0,255),"[Global Chat 3] ")
- MsgC(Color(165,81,255), ..., "\n")
- end
- local LastReconnectAttempt = CurTime() + 4
- function GC.TryConnect()
- MsgC(Color(84,0,255),"[Global Chat 3] ")
- MsgC(Color(165,81,255),"Connecting... \n")
- GSck:setoption('reuseaddr',true)
- GSck:settimeout(0,"b")
- GSck:connect(GC.Host,GC.Port)
- timer.Simple(0.1,function()
- if type(GSck)=="tcp{client}" then
- local dta, stat = GSck:send(GC.Pass)
- if stat!="closed" then
- msg('Connected')
- else
- msg("CONNECTION FAILED.")
- end
- else
- msg('should never happen -- connection failed.')
- end
- end)
- end
- GC.TryConnect()
- function GC.TryReconnect()
- if LastReconnectAttempt < CurTime() then
- MsgC(Color(84,0,255),"[Global Chat 3] ")
- MsgC(Color(165,81,255),"Attempting reconnect... \n")
- GC.TryConnect()
- LastReconnectAttempt = CurTime() + 4
- return true
- end
- end
- local function PlayerInfoJson(ply,msgstr,mergtable)
- if !IsValid(ply) then return false end
- local pinfo = {}
- pinfo["name"] = ply:Nick()
- pinfo["steamid"] = ply:SteamID()
- pinfo["steamid64"] = ply:SteamID64()
- pinfo["team"] = team.GetName(ply:Team())
- pinfo["teamcolor"] = team.GetColor(ply:Team())
- pinfo["message"] = msgstr
- pinfo["ugrp"] = ply:GetUserGroup();
- pinfo["gdisabled"] = ply["DisableGlobalChat"]==true // spooky
- if !mergtable then mergtable = {} end
- for k,v in pairs(mergtable) do
- pinfo[k]=v
- end
- return pinfo
- end
- function GC.OnData(data)
- if #data < 10 then
- local bt = string.byte(data)
- if bt == 0x20 then
- msg('authentication requested')
- elseif bt==0x4 then
- msg('auth ok')
- elseif bt==0x5 then
- msg('authentication failed')
- end
- else
- if data[1]=="{" then
- local buftab = util.JSONToTable(data)
- if buftab["Type"]=="GlobalChat" then
- local server = buftab["ServerID"]
- local buff = buftab["BufferData"]
- local msg = {Color(255,255,255),"[",server,"]"," ",buff["teamcolor"],buff["name"],Color(255,255,255),": ",buff["message"]}
- for k,v in pairs(player.GetAll()) do
- if v["DisableGlobalChat"]~=true then
- net.Start("GlobalChatText")
- net.WriteTable(msg)
- net.Send(v)
- end
- end
- end
- if buftab["Type"]=="ServerMessage" then
- local server = buftab["ServerID"]
- local buff = buftab["BufferData"]
- local msg = {Color(255,0,255),"[",server,"]",Color(255,255,0)," : ", buff["message"]}
- for k,v in pairs(player.GetAll()) do
- net.Start("GlobalChatText")
- net.WriteTable(msg)
- net.Send(v)
- end
- end
- if buftab["Type"]=="CServMessage" then
- local buff = buftab["BufferData"]
- local msg = {Color(150,150,150),"[ChatServer]",Color(255,255,255)," : ", buff["message"]}
- for k,v in pairs(player.GetAll()) do
- net.Start("GlobalChatText")
- net.WriteTable(msg)
- net.Send(v)
- end
- end
- end
- end
- end
- function GC.Chat(plr,t)
- local tbrk = string.Explode(" ",t)
- if tbrk[1]=="!global" then
- if tbrk[2]=="on" then
- local msg = {Color(150,150,150),"[GlobalChat]: ",Color(255,255,255), "Global chat is now ON."}
- net.Start("GlobalChatText")
- net.WriteTable(msg)
- net.Send(plr)
- plr["DisableGlobalChat"] = false
- end
- if tbrk[2]=="off" then
- local msg = {Color(150,150,150),"[GlobalChat]: ",Color(255,255,255), "Global chat is now OFF."}
- net.Start("GlobalChatText")
- net.WriteTable(msg)
- net.Send(plr)
- plr["DisableGlobalChat"] = true
- end
- return ""
- end
- if tbrk[1]=="/global" then
- table.remove(tbrk,1)
- local bad = table.concat(tbrk," ")
- local msg = {Color(255,255,255),"[",GC.User,"]"," ",team.GetColor(plr:Team()),plr:Nick(),Color(255,255,255),": ",bad}
- for k,v in pairs(player.GetAll()) do
- if v["DisableGlobalChat"]~=true then
- net.Start("GlobalChatText")
- net.WriteTable(msg)
- net.Send(v)
- end
- end
- GC.Send(PlayerInfoJson(plr,bad),"GlobalChat")
- return ""
- end
- end
- hook.Add("PlayerSay","Global Chat 3",GC.Chat)
- function GC.SendServerMessage(msg)
- GC.Send({message = msg},"ServerMessage")
- end
- function GC.Send(tab,identity)
- local buff = {}
- buff["ServerID"] = GC.User
- buff["IDN"] = GC.ServerID
- buff["Type"] = identity
- buff["BufferData"] = tab
- local bufftext = util.TableToJSON(buff)
- GSck:send(bufftext)
- end
- function GC.ThinkNetwork()
- if type(GSck)=="tcp{client}" then
- local data, status, partial = GSck:receive()
- local data_real = data or partial
- if status=="closed" or status=="Transport endpoint is not connected" then
- if GC.TryReconnect()==true then
- msg('lost connection -- reconnecting.')
- end
- elseif data_real and #data_real > 0 then
- GC.OnData(data_real)
- end
- else
- GC.TryReconnect()
- end
- end
- hook.Add("Think","GCReceiveThink",GC.ThinkNetwork)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement