Advertisement
Guest User

Untitled

a guest
Sep 13th, 2014
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.25 KB | None | 0 0
  1. -- AntiCrash coded by Flapadar
  2. -- Fixed by Leystryku
  3. -- Modified by Agentmass - 30/11/12(remove)
  4. -- Modified by Eccid
  5.  
  6. AntiCrash = {}
  7.  
  8. CreateConVar("anticrash_enabled", "1", FCVAR_ARCHIVE)
  9.  
  10. if ( not GetConVar("anticrash_enabled"):GetBool() ) then
  11.  
  12.     return
  13.  
  14. end
  15.  
  16.  
  17. if SERVER then 
  18.     util.AddNetworkString("AntiCrash.Pong")
  19.     AddCSLuaFile("acrash.lua")
  20.  
  21.     function AntiCrash.Ping( ply, cmd, args )
  22.    
  23.         if ( not ply.LastPing or ply.LastPing + 5 < CurTime() ) then
  24.        
  25.             ply.LastPing = CurTime()
  26.            
  27.             net.Start("AntiCrash.Pong")
  28.             net.Send( ply )
  29.  
  30.             --MsgN("Ping !")
  31.         end
  32.        
  33.     end
  34.    
  35.     concommand.Add("_anticrash_ping", AntiCrash.Ping)
  36.  
  37.     return
  38. end
  39.  
  40. AntiCrash.LastMoveTime = CurTime() + 10
  41. AntiCrash.ShouldRetry = true
  42. AntiCrash.Crashed = false
  43. AntiCrash.Spawned = false
  44. AntiCrash.Pending = false
  45. AntiCrash.SpawnTime = 0
  46.  
  47.  
  48. function AntiCrash.IsCrashed()
  49.  
  50.     if ( not AntiCrash.Spawned or not LocalPlayer or AntiCrash.Crashed ) then return end
  51.        
  52.     if ( AntiCrash.SpawnTime > CurTime() ) then return end
  53.  
  54.     if ( AntiCrash.LastMoveTime > CurTime() ) then return end
  55.  
  56.     if ( not IsValid(LocalPlayer()) ) then return end
  57.  
  58.     if ( not LocalPlayer():IsFrozen() and not LocalPlayer():InVehicle() ) then
  59.  
  60.         return true
  61.  
  62.     end
  63.  
  64. end
  65.  
  66. function AntiCrash.Pong( um )
  67.  
  68.     AntiCrash.LastMoveTime = CurTime() + 10
  69.  
  70. end
  71.  
  72. function AntiCrash.Move()
  73.  
  74.     AntiCrash.LastMoveTime = CurTime() + 1
  75.    
  76. end
  77.  
  78. function AntiCrash.InitPostEntity()
  79.  
  80.     AntiCrash.Spawned = true
  81.     AntiCrash.SpawnTime = CurTime() + 5
  82.  
  83. end
  84.  
  85. function AntiCrash.ServerCrash()
  86.     local menucrashtime = CurTime()
  87.     local retrytime = menucrashtime + 25
  88.    
  89.     for k , v  in ipairs(player.GetAll()) do
  90.         v.CrashedPing = v:Ping()
  91.     end
  92.  
  93.     local dframe = vgui.Create("DFrame")
  94.     dframe:SetSize(200 , 120)
  95.     dframe:SetTitle("AntiCrash")
  96.     dframe:ShowCloseButton( false )
  97.     dframe:SetDraggable( false )
  98.     dframe:SetBackgroundBlur( true )
  99.     dframe:Center()
  100.    
  101.  
  102.     function dframe:Close(...)
  103.         AntiCrash.ShouldRetry = false
  104.         return DFrame.Close(self , ...)
  105.     end
  106.  
  107.     local dlabel = vgui.Create("DLabel")
  108.     dlabel:SetParent(dframe)
  109.     dlabel:SetPos(27 , 30)
  110.     dlabel:SetSize(195 , 25)
  111.     function dlabel:Paint( ... )   
  112.         self:SetText(string.format("Auto-reconnect in %d seconds!" , retrytime - CurTime()))
  113.         if(retrytime - CurTime() == 20) then
  114.             dframe:MakePopup()
  115.         end
  116.     end
  117.  
  118.     local dlabel = vgui.Create("DLabel")
  119.     dlabel:SetParent(dframe)
  120.     dlabel:SetPos(20 , 60)
  121.     dlabel:SetSize(195 , 50)
  122.     dlabel:SetText( "The server seems to have crashed.\nWhen the timer above hits zero, \nyou will automatically reconnect \n               to the server." )
  123.  
  124.     -- local dbutton = vgui.Create("DButton")
  125.     -- dbutton:SetParent(dframe)
  126.     -- dbutton:SetPos(5 , 120)
  127.     -- dbutton:SetSize(190 , 22)
  128.     -- dbutton:SetText("Disconnect")
  129.     -- dbutton.DoClick = function()
  130.         -- RunConsoleCommand("disconnect")
  131.         -- dframe:SetVisible(false)
  132.     -- end
  133.    
  134.     hook.Add("Think" , "Crashed" , function()
  135.         for k , v in ipairs(player.GetAll()) do
  136.             if v.CrashedPing != v:Ping() then
  137.                 hook.Remove("Think" , "Crashed")
  138.                 AntiCrash.Crashed = false
  139.                 AntiCrash.LastMoveTime = CurTime() + 5
  140.             end
  141.         end
  142.        
  143.         if AntiCrash.Crashed and (retrytime - CurTime() - 0.5) < 0 and AntiCrash.LastMoveTime + 5 < CurTime() then
  144.             if AntiCrash.ShouldRetry then
  145.                 RunConsoleCommand("retry")
  146.             end
  147.         elseif AntiCrash.LastMoveTime > CurTime() then
  148.             hook.Remove("Think" , "Crashed")
  149.             AntiCrash.Crashed = false
  150.             if dframe and dframe:IsValid() then
  151.                 dframe:Remove()
  152.             end
  153.         end
  154.     end )
  155.    
  156. end
  157.  
  158. function AntiCrash.Think()
  159.     if not AntiCrash.Crashed and AntiCrash.IsCrashed() then
  160.         RunConsoleCommand("_anticrash_ping")
  161.         if AntiCrash.LastMoveTime < CurTime() then
  162.             AntiCrash.Crashed = true
  163.             AntiCrash.ShouldRetry = true -- This is a seperate crash from the previous, the user might want to reconnect this time.
  164.  
  165.             AntiCrash.ServerCrash()
  166.             hook.Call( "ServerCrash" , nil ) -- Incase anyone else wants to hook into server crashes.
  167.         else
  168.             AntiCrash.Crashed = false
  169.         end
  170.  
  171.     end
  172.    
  173. end
  174.  
  175.  
  176. hook.Add("InitPostEntity" , "AntiCrash.InitPostEntity", AntiCrash.InitPostEntity)
  177. hook.Add("Move" , "AntiCrash.Move", AntiCrash.Move)
  178. hook.Add("Think" , "AntiCrash.Think", AntiCrash.Think)
  179.  
  180. net.Receive("AntiCrash.Pong", AntiCrash.Pong)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement