diff --git a/mod.json b/mod.json index e17e5aa..4edbea5 100644 --- a/mod.json +++ b/mod.json @@ -9,9 +9,23 @@ "Name": "killstat_version", "DefaultValue": "ks_3.0.0" }, + { + "Name": "Tone_protocol", + "DefaultValue": "http" + }, { "Name": "Tone_URI", - "DefaultValue": "http://127.0.0.1:3001/v1" + "DefaultValue": "127.0.0.1:3001/v1" + }, + { + "Name": "Tone_ID", + "DefaultValue": "default", + "Flags": 128 + }, + { + "Name": "Tone_token", + "DefaultValue": "default", + "Flags": 4256 }, { "Name": "killstat_custom_parameters", diff --git a/mod/scripts/vscripts/killstat.nut b/mod/scripts/vscripts/killstat.nut index fa94fd0..0bab647 100644 --- a/mod/scripts/vscripts/killstat.nut +++ b/mod/scripts/vscripts/killstat.nut @@ -46,7 +46,9 @@ array HEADERS = [ //unused struct { string killstatVersion string Tone_URI - int serverId + string Tone_protocol + string Tone_ID + string Tone_token array customParameters int matchId @@ -57,8 +59,13 @@ struct { void function killstat_Init() { file.killstatVersion = GetConVarString("killstat_version") file.Tone_URI = GetConVarString("Tone_URI") - //TODO : request ServerID from API ------------------------------------------------------------------------------------------------ - file.serverId = 1 + file.Tone_protocol = GetConVarString("Tone_protocol") + file.Tone_ID = GetConVarString("Tone_ID") + file.Tone_token = GetConVarString("Tone_token") + + //register to Tone API if default or invalid token + Tone_Test_Auth() + // custom parameters string customParameterString = GetConVarString("killstat_custom_parameters") array customParameterEntries = split(customParameterString, ",") @@ -98,7 +105,7 @@ void function killstat_Begin() { file.map = StringReplace(GetMapName(), "mp_", "") Log("-----BEGIN KILLSTAT-----") - Log("Sending kill data to " + file.Tone_URI + "/servers/"+file.serverId+"/kill") + Log("Sending kill data to " + file.Tone_URI + "/servers/"+file.Tone_ID+"/kill") } void function killstat_Record(entity victim, entity attacker, var damageInfo) { @@ -176,7 +183,7 @@ void function killstat_Record(entity victim, entity attacker, var damageInfo) { string json = EncodeJSON(values) print("sending here... I realise") - NSHttpPostBody(file.Tone_URI + "/servers/"+file.serverId+"/kill", json) + NSHttpPostBody(GetToneURIWithAuth()+ "/servers/"+file.Tone_ID+"/kill", json) } void function killstat_End() { @@ -316,4 +323,69 @@ string function join(array list, string separator) { } return s +} + + +void function Tone_Test_Auth(){ + HttpRequest request + request.method = HttpRequestMethod.POST + request.url = GetToneURIWithAuth() + "/servers/"+file.Tone_ID + void functionref( HttpRequestResponse ) onSuccess = void function ( HttpRequestResponse response ) + { + print("[Tone API] Tone API Online !") + } + + void functionref( HttpRequestFailure ) onFailure = void function ( HttpRequestFailure failure ) + { + print("[Tone API] Tone API registration failed") + print("[Tone API] " + failure.errorMessage ) + thread Tone_Register_Threaded() + } + + NSHttpRequest(request, onSuccess, onFailure) +} + + +void function Tone_Register_Threaded(){ + table body = {} + body["name"] <- GetConVarString("ns_server_name") + body["description"] <- GetConVarString("ns_server_desc") + body["auth_port"] <- GetConVarString("ns_player_auth_port") + HttpRequest request + request.method = HttpRequestMethod.POST + request.url = file.Tone_protocol + "://" + file.Tone_URI+ "/servers/register" + + string json = EncodeJSON( body ) + request.body = json + int i = 0 + void functionref( HttpRequestResponse ) onSuccess = void function ( HttpRequestResponse response ) + { + table answer = DecodeJSON(response.body) + file.Tone_ID = expect string(answer["id"]) + file.Tone_token = expect string(answer["token"]) + SetConVarString("Tone_ID", expect string(answer["id"])) + SetConVarString("Tone_token", expect string(answer["token"])) + i = 5 + } + + void functionref( HttpRequestFailure ) onFailure = void function ( HttpRequestFailure failure ) + { + print("[Tone API] Tone API registration failed") + print("[Tone API] "+ failure.errorMessage ) + } + + + while(i < 5){ + print("[Tone API] requesting Tone API for registration... Time " + i + " out of 5" ) + NSHttpRequest( request, onSuccess, onFailure ) + i = i + 1 + wait 300 + } + + return +} + + +string function GetToneURIWithAuth(){ + return file.Tone_protocol + "://" +file.Tone_ID+":"+file.Tone_protocol+"@"+ file.Tone_URI } \ No newline at end of file