|
| 1 | +AZLINK_VERSION = "0.1.0" -- TODO |
| 2 | +AzLink = AzLink or { } |
| 3 | +AzLink.lastSent = 0 |
| 4 | +AzLink.lastFullSent = 0 |
| 5 | +AzLink.config = { } |
| 6 | + |
| 7 | +function AzLink:Fetch( force ) |
| 8 | + local siteKey = AzLink.config.site_key |
| 9 | + local baseUrl = AzLink.config.url |
| 10 | + if siteKey == nil or baseUrl == nil or not force and RealTime( ) - AzLink.lastSent < 15 then return end |
| 11 | + local sendFull = os.date( "*t" ).min % 15 == 0 and RealTime( ) - AzLink.lastFullSent > 60 |
| 12 | + AzLink.lastSent = RealTime( ) |
| 13 | + |
| 14 | + if sendFull then |
| 15 | + AzLink.lastFullSent = RealTime( ) |
| 16 | + end |
| 17 | + |
| 18 | + return AzLink.Fetcher:Run( sendFull ) |
| 19 | +end |
| 20 | + |
| 21 | +function AzLink:Ping( ) |
| 22 | + local siteKey = AzLink.config.site_key |
| 23 | + local baseUrl = AzLink.config.url |
| 24 | + if siteKey == nil or baseUrl == nil then return nil end |
| 25 | + |
| 26 | + return AzLink.HttpClient:Request( "GET", "", data ):Catch( function( error, status ) |
| 27 | + if status == nil then |
| 28 | + AzLink.Logger:Error( "Unable to ping: " .. error ) |
| 29 | + else |
| 30 | + local errorMessage = ( error.message or error ) .. " (" .. status .. ")" |
| 31 | + AzLink.Logger:Error( "An HTTP error occured during ping: " .. errorMessage ) |
| 32 | + end |
| 33 | + end ) |
| 34 | +end |
| 35 | + |
| 36 | +function AzLink:GetServerData( full ) |
| 37 | + local players = { } |
| 38 | + |
| 39 | + for _, player in ipairs( player.GetHumans( ) ) do |
| 40 | + table.insert( players, { |
| 41 | + ["name"] = player:Nick( ), |
| 42 | + ["uid"] = player:SteamID64( ), |
| 43 | + } ) |
| 44 | + end |
| 45 | + |
| 46 | + local baseData = { |
| 47 | + ["platform"] = { |
| 48 | + ["type"] = "GMOD", |
| 49 | + ["name"] = "Garry's Mod", |
| 50 | + ["version"] = VERSIONSTR, |
| 51 | + }, |
| 52 | + ["version"] = AZLINK_VERSION, |
| 53 | + ["players"] = players, |
| 54 | + ["maxPlayers"] = game.MaxPlayers( ), |
| 55 | + ["full"] = full, |
| 56 | + } |
| 57 | + |
| 58 | + if not full then return baseData end |
| 59 | + |
| 60 | + baseData.worlds = { |
| 61 | + ["entities"] = ents.GetCount( ), |
| 62 | + } |
| 63 | + |
| 64 | + if serverstat ~= nil then |
| 65 | + baseData.system = { |
| 66 | + ["cpu"] = serverstat.ProcessCPUUsage( ) * 100.0, |
| 67 | + ["ram"] = serverstat.ProcessMemoryUsage( ), |
| 68 | + } |
| 69 | + end |
| 70 | + |
| 71 | + return baseData |
| 72 | +end |
| 73 | + |
| 74 | +function AzLink:SaveConfig( ) |
| 75 | + file.Write( "azlink/config.json", util.TableToJSON( AzLink.config ) ) |
| 76 | +end |
| 77 | + |
| 78 | +MsgN( "[AzLink] Starting AzLink v" .. AZLINK_VERSION .. "..." ) |
| 79 | + |
| 80 | +hook.Add( "Initialize", "azlink-init", function( ) |
| 81 | + timer.Create( "azlink-fetcher-task", 60, 0, function( ) |
| 82 | + AzLink:Fetch( ) |
| 83 | + end ) |
| 84 | + |
| 85 | + timer.Start( "azlink-fetcher-task" ) |
| 86 | +end ) |
| 87 | + |
| 88 | +file.CreateDir( "azlink" ) |
| 89 | +local rawConfig = file.Read( "azlink/config.json" ) |
| 90 | + |
| 91 | +if rawConfig ~= nil then |
| 92 | + AzLink.config = util.JSONToTable( rawConfig ) |
| 93 | +end |
| 94 | + |
| 95 | +if #file.Find( "lua/bin/gmsv_serverstat*", "GAME" ) == 0 or not pcall( require, "serverstat" ) then |
| 96 | + MsgN( "[AzLink] Unable to load serverstat, please install the module by following" ) |
| 97 | + MsgN( "these instructions: https://github.com/WilliamVenner/gmsv_serverstat#installation" ) |
| 98 | +end |
| 99 | + |
| 100 | +MsgN( "[AzLink] AzLink successfully enabled." ) |
0 commit comments