commit a22c226c9748659803c0fb15182cd117a116bfb3 Author: Sven Heidemann Date: Mon Dec 11 20:48:33 2023 +0100 Added addon to send player connect and disconnect diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1550f16 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +server/ \ No newline at end of file diff --git a/install_server.sh b/install_server.sh new file mode 100755 index 0000000..684571e --- /dev/null +++ b/install_server.sh @@ -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" \ No newline at end of file diff --git a/src/sh_ontime_handler_gmod.lua b/src/sh_ontime_handler_gmod.lua new file mode 100644 index 0000000..b49e0ce --- /dev/null +++ b/src/sh_ontime_handler_gmod.lua @@ -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) \ No newline at end of file diff --git a/start_server.sh b/start_server.sh new file mode 100755 index 0000000..a574cb3 --- /dev/null +++ b/start_server.sh @@ -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 .. \ No newline at end of file