Added addon to send player connect and disconnect

This commit is contained in:
Sven Heidemann 2023-12-11 20:48:33 +01:00
commit a22c226c97
4 changed files with 108 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
server/

13
install_server.sh Executable file
View File

@ -0,0 +1,13 @@
mkdir -p server/steam
cd server/steam
wget https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz
tar xfvz steamcmd_linux.tar.gz
rm steamcmd_linux.tar.gz
cd ..
./steam/steamcmd.sh +force_install_dir ../ +login anonymous +app_update 4020 validate +quit
cd ..
echo "done"

View File

@ -0,0 +1,85 @@
local config = {}
function error(message, err)
print("Sh_ONTIME_HANDLER", "ERROR", message, err)
end
function info(message)
print("Sh_ONTIME_HANDLER", "INFO", message)
end
function readConfig()
local JSONData = file.Read("config.json", "DATA")
if JSONData == nil then
error("Config not found!")
end
config = util.JSONToTable(JSONData)
if config == nil || config.apiURL == nil || config.apiKey == nil then
error("Invalid config!")
exit()
end
info("Config loaded")
end
local function getUserJoinedMutation(id)
return util.TableToJSON({
query = "mutation { userJoinedGameServer { userJoined(input: { ident: \"" .. id .. "\" }) { id } } }"
})
end
local function getUserLeftMutation(id)
return util.TableToJSON({
['query'] = "mutation { userJoinedGameServer { userLeft(input: { ident: \"" .. id .. "\" }) { id } } }"
})
end
function sendJoinedPlayer(id)
HTTP({
url=config.apiURL,
method="POST",
headers={
['Authorization']="API-Key " .. config.apiKey
},
type="application/json",
success=function( code, body, headers )
end,
failed=function( err )
error("Cannot send player joined", err)
end,
body=getUserJoinedMutation(id)
})
end
function sendLeftPlayer(id)
HTTP({
url=config.apiURL,
method="POST",
headers={
['Authorization']="API-Key " .. config.apiKey
},
type="application/json",
success=function( code, body, headers )
end,
failed=function( err )
error("Cannot send player joined", err)
end,
body=getUserLeftMutation(id)
})
end
info("Loading")
readConfig()
info("Listening")
gameevent.Listen( "player_connect" )
hook.Add("player_connect", "HandlePlayerConnect", function( data )
info(string.format("Player %s connected", data.name))
sendJoinedPlayer(data.networkid)
end)
gameevent.Listen( "player_disconnect" )
hook.Add("player_disconnect", "HandlePlayerDisconnect", function( data )
info(string.format("Player %s disconnected", data.name))
sendLeftPlayer(data.networkid)
end)

9
start_server.sh Executable file
View File

@ -0,0 +1,9 @@
mkdir -p server/garrysmod/lua/autorun/server/sh_ontime_handler_gmod
cp src/* server/garrysmod/lua/autorun/server/
cd server
./srcds_run -game garrysmod -game garrysmod +map gm_construct +ip 0.0.0.0 -allowlocalhttp
cd ..