Advertisement
Guest User

Untitled

a guest
Jun 16th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.74 KB | None | 0 0
  1.  
  2. -------------------------
  3. -- Game Server Library --
  4. -------------------------
  5.  
  6. require_once( "events" )
  7. require_once( "handles" )
  8. require_once( "concommand" )
  9.  
  10. gameserver = {}
  11.  
  12. local clientVersion = ""
  13.  
  14. local PlayerList = {}
  15.  
  16. local metaPly = {}
  17. metaPly.__index = metaPly
  18.  
  19. local asciiMode = true
  20.  
  21. function metaPly:New()
  22.     self.Valid = true
  23.     local Disconnect, Data
  24.     function Disconnect( conn )
  25.         if not self.Valid then return end
  26.         local hdl = Handle( conn, "connection" )
  27.         if not (hdl:GetID() == self._hdl:GetID()) then return end
  28.         for id, ply in pairs( PlayerList ) do
  29.             if (ply == self) then
  30.                 table.remove( PlayerList, id )
  31.                 break
  32.             end
  33.         end
  34.         events.Unhook( "OnDisconnect", Disconnect )
  35.         events.Unhook( "OnConnectionData", Data )
  36.         events.Raise( "PlayerDisconnect", self )
  37.         --print( "Player object deleted!" )
  38.         self.Valid = false
  39.     end
  40.     function Data( conn, pckt )
  41.         if not self.Valid then return print( "Received data for invalid player object" ) end
  42.         local hdl = Handle( conn, "connection" )
  43.         if not (hdl:GetID() == self._hdl:GetID()) then return print( "Handle mismatch" ) end
  44.         --print( "Handling data..." )
  45.         local data = Handle( pckt, "netpacket" )
  46.         self:HandleData( data )
  47.  
  48.     end
  49.     events.Hook( "OnDisconnect", Disconnect )
  50.     events.Hook( "OnConnectionData", Data )
  51.     print( "Player object created!" )
  52.     self:SetAuthLevel( 0 )
  53.     self:SetNick( "Unconnected" )
  54.     self.lastactivity = CurTime()
  55.     self.ping = 0
  56.     self:Ping()
  57. end
  58.  
  59. function metaPly:HandleData( pckt ) -- This handles CLIENT -> SERVER data
  60.     self.lastactivity = CurTime()
  61.     local id = pckt:ReadByte()
  62.     if (asciiMode) then id = tonumber( string.char( id ) ) end
  63.     if (id == 0) then -- Ping
  64.         local pk = self:StartPacket()
  65.         self:WriteByte( 1 ) -- Pong
  66.         pk:WriteString( "PONG" )
  67.         self:Send()
  68.     end
  69.     if (id == 1) then -- Pong
  70.         if self.pingstart then
  71.             self.ping = math.ceil( (CurTime()-self.pingstart)*1000 )
  72.             self.pingstart = nil
  73.             --print( "[" .. self:GetID() .. "] Ping: " .. self.ping )
  74.         else
  75.             self:Error( "PONG WITHOUT PING" )
  76.         end
  77.     end
  78.     if (id == 2) then -- Version auth
  79.         local str = pckt:ReadString()
  80.         if (str == clientVersion) then
  81.             local pk = self:StartPacket()
  82.             self:WriteByte( 2 ) -- Version auth reply
  83.             self:WriteByte( 1 ) -- Success
  84.             pk:WriteString( clientVersion )
  85.             self:Send()
  86.             self:IncAuth( 1 )
  87.         else
  88.             self:Error( "BAD CLIENT VERSION (" .. str .. ")" )
  89.             local pk = self:StartPacket()
  90.             self:WriteByte( 2 ) -- Version auth reply
  91.             self:WriteByte( 0 ) -- Failed
  92.             pk:WriteString( clientVersion )
  93.             self:Send()
  94.         end
  95.     end
  96.     if (id == 3) then -- Disconnect
  97.         self:Drop( "Disconnect by user." )
  98.         return -- No leftover packet for you
  99.     end
  100.     if (id == 4) then -- User auth
  101.         if (self:GetAuthLevel() == 1) then
  102.             local user = pckt:ReadString()
  103.             local pass = pckt:ReadString()
  104.             -- TODO: Proper auth
  105.             if (string.lower( user ) == "guest") and (pass == "guest") then
  106.                 self:IncAuth( 2 )
  107.                 self:SetNick( user )
  108.                 local pk = self:StartPacket()
  109.                 self:WriteByte( 4 ) -- User auth reply
  110.                 self:WriteByte( 1 ) -- Success
  111.                 self:Send()
  112.                 gameserver.BroadcastChat( 0, self:GetNick() .. " has connected!" )
  113.             else
  114.                 local pk = self:StartPacket()
  115.                 self:WriteByte( 4 ) -- User auth reply
  116.                 self:WriteByte( 0 ) -- Failed
  117.                 self:Send()
  118.                 self.FailedUserAuth = (self.FailedUserAuth or 0) + 1
  119.                 if (self.FailedUserAuth == 3) then self:Drop( "Too many failed user auth attempts." ) end
  120.             end
  121.         else
  122.             self:Error( "TRIED TO USER AUTH BEFORE VERSION AUTH" )
  123.         end
  124.     end
  125.     if (id == 5) then -- Chat message
  126.         if (self:IsAuthLevel( 2 )) then
  127.             gameserver.BroadcastChat( 1, self:GetNick() .. ": " .. pckt:ReadString() )
  128.         else
  129.             self:Error( "TRIED TO CHAT BEFORE USER AUTH" )
  130.         end
  131.     end
  132.  
  133.     local leftover = pckt:GetSize() - pckt:GetCursorPos()
  134.     if (leftover > 0) then
  135.         self:HandleData( pckt )
  136.     end
  137. end
  138.  
  139. function metaPly:Error( msg )
  140.     Msg( self:GetInfo() .. " --- " .. msg )
  141. end
  142.  
  143. function metaPly:Drop( reason )
  144.     local pk = self:StartPacket()
  145.     self:WriteByte( 3 ) -- Dropped with reason
  146.     pk:WriteString( reason )
  147.     self:Send()
  148.     self:Error( "Dropped ('" .. reason .. "')" )
  149.     self._hdl:Close()
  150.     self.Dropped = true
  151. end
  152.  
  153. function metaPly:StartPacket()
  154.     local raw = CreatePacket()
  155.     local hdl = Handle( raw, "netpacket" )
  156.     self._outgoing = hdl
  157.     return hdl
  158. end
  159.  
  160. function metaPly:Send( packet )
  161.     if not self.Valid then return end
  162.     if self.Dropped then return end
  163.     if not packet then packet = self._outgoing end
  164.     self._hdl:SendPacket( packet._o )
  165. end
  166.  
  167. function metaPly:WriteByte( val )
  168.     if (asciiMode) then
  169.         self._outgoing:WriteByte( string.byte( tostring( val ) ) )
  170.     else
  171.         self._outgoing:WriteByte( val )
  172.     end
  173. end
  174.  
  175. function metaPly:SetNick( name )
  176.     self.nick = name
  177. end
  178.  
  179. function metaPly:GetNick()
  180.     return self.nick
  181. end
  182.  
  183. function metaPly:GetPing()
  184.     return self.ping
  185. end
  186.  
  187. function metaPly:GetIP()
  188.     return self._hdl:GetRemoteIP()
  189. end
  190.  
  191. function metaPly:GetID()
  192.     return self._hdl:GetID()
  193. end
  194.  
  195. function metaPly:Ping()
  196.     if (self.pingstart) then return end
  197.     self.pingstart = CurTime()
  198.     local pk = self:StartPacket()
  199.     self:WriteByte( 0 )
  200.     pk:WriteString( "PING" )
  201.     self:Send()
  202. end
  203.  
  204. function metaPly:SetAuthLevel( lvl )
  205.     self.authlvl = lvl
  206. end
  207.  
  208. function metaPly:IncAuth( min )
  209.     self.authlvl = ((self.authlvl < min) and min) or self.authlvl
  210. end
  211.  
  212. function metaPly:IsAuthLevel( lvl )
  213.     return self.authlvl >= lvl
  214. end
  215.  
  216. function metaPly:GetAuthLevel()
  217.     return self.authlvl
  218. end
  219.  
  220. function metaPly:GetInfo()
  221.     return self:GetID() .. ": IP=" .. self:GetIP() .. ", PING=" .. self:GetPing()
  222. end
  223.  
  224. function metaPly:SendChat( channel, msg )
  225.     local pk = self:StartPacket()
  226.     self:WriteByte( 5 ) -- Chat message
  227.     self:WriteByte( channel ) -- Channel ID
  228.     pk:WriteString( msg )
  229.     self:Send()
  230. end
  231.  
  232. function gameserver.SetClientVersion( str )
  233.     clientVersion = str
  234. end
  235.  
  236. function gameserver.PrintInfo()
  237.     for i=1, #PlayerList do
  238.         local ply = PlayerList[i]
  239.         print( tostring( ply ) )
  240.         Msg( ply:GetInfo() )
  241.     end
  242. end
  243.  
  244. function gameserver.GetPlayers()
  245.     return PlayerList
  246. end
  247.  
  248. function gameserver.SendToAll( pckt )
  249.     for i=1, #PlayerList do
  250.         local ply = PlayerList[i]
  251.         ply:Send( pckt )
  252.     end
  253. end
  254.  
  255. function gameserver.BroadcastChat( channel, msg )
  256.     print( "[" .. channel .. "] " .. msg )
  257.     for i=1, #PlayerList do
  258.         local ply = PlayerList[i]
  259.         ply:SendChat( channel, msg )
  260.     end
  261. end
  262.  
  263. local function cmdPrintInfo()
  264.     gameserver.PrintInfo()
  265. end
  266. command.Register( "printinfo", cmdPrintInfo )
  267.  
  268. local function evConnect( port, rawconn )
  269.     local hdl = Handle( rawconn, "connection" )
  270.     local o = {}
  271.     setmetatable( o, metaPly )
  272.     o._hdl = hdl
  273.     o:New()
  274.     table.insert( PlayerList, o )
  275.     events.Raise( "PlayerConnect", o )
  276. end
  277. events.Hook( "OnConnection", evConnect )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement