Skip to content

Commit

Permalink
feat: add new API configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Alystrasz committed Jul 24, 2023
1 parent 0eefdf6 commit 0ffc0b1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
4 changes: 4 additions & 0 deletions mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
{
"Name": "parkour_api_secret",
"DefaultValue": "my_little_secret"
},
{
"Name": "parkour_api_event_id",
"DefaultValue": "f20a4c02-501a-405d-98bc-2544143455fe"
}
],
"Scripts": [
Expand Down
56 changes: 29 additions & 27 deletions mod/scripts/vscripts/utils/world_leaderboard.nut
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,43 @@ global function WorldLeaderboard_Init
global function SendWorldLeaderboardEntryToAPI

struct {
var event_id
var mapId
string endpoint
string secret
} file;

/**
* Entrypoint of the package, this method will load the API authentication token
* from the dedicated configuration variable; it will be used in all future HTTP
* requests to Parkour API.
* Entrypoint of the package, this method will load the API endpoint and
* authentication token from dedicated configuration variables; they will
* be used in all future HTTP requests to Parkour API.
*
* Once the token has been saved, this starts fetching events.
* Once information have been saved, this starts fetching maps.
**/
void function WorldLeaderboard_Init() {
file.secret = GetConVarString("parkour_api_secret")
file.endpoint = GetConVarString("parkour_api_endpoint")
thread WorldLeaderboard_FetchEvents()
thread WorldLeaderboard_FetchMaps()
}


/**
* This method fetches the `events` resource of the Parkour API to find information
* This method fetches the `maps` resource of the Parkour API to find information
* about the current match: where to save new scores, which settings (weapons/ability
* set) to apply to all players...
*
* Once corresponding event has been found, this will register said event identifier
* Once corresponding map has been found, this will register said map identifier
* locally, for it to be used in future HTTP requests, apply required changes to
* current match, and start fetching scores from distant API every few seconds.
*
* If no corresponding event is found, no further HTTP request will occur during the
* If no corresponding map is found, no further HTTP request will occur during the
* current match.
**/
void function WorldLeaderboard_FetchEvents() {
void function WorldLeaderboard_FetchMaps() {
string eventId = GetConVarString("parkour_api_event_id")

HttpRequest request
request.method = HttpRequestMethod.GET
request.url = format("%s/v1/events", file.endpoint)
request.url = format("%s/v1/events/%s/maps", file.endpoint, eventId)
table<string, array<string> > headers
headers[ "authentication" ] <- [file.secret]
request.headers = headers
Expand All @@ -58,33 +60,33 @@ void function WorldLeaderboard_FetchEvents() {

string inputStr = "{\"data\":" + response.body + "}"
table data = DecodeJSON(inputStr)
array events = expect array(data["data"])
array maps = expect array(data["data"])

// Currently, corresponding event is found by checking if its name contains the
// name of the current map, which might be improved.
string mapName = GetMapName()
foreach (value in events) {
table event = expect table(value)
string event_name = expect string(event["name"])
if ( event_name.find( mapName) != null ) {
file.event_id = expect string(event["id"])
thread WorldLeaderboard_FetchScores( expect string(event["id"]) )
foreach (value in maps) {
table map = expect table(value)
string map_name = expect string(map["map_name"])
if ( map_name.find( mapName) != null ) {
file.mapId = expect string(map["id"])
thread WorldLeaderboard_FetchScores()
has_api_access = true

table perks = expect table(event["perks"]);
table perks = expect table(map["perks"]);
ApplyPerks( perks )

return;
}
}

print("No event matches the current map.")
print("No map matches the event id and current map.")
has_api_access = false
}

void functionref( HttpRequestFailure ) onFailure = void function ( HttpRequestFailure failure )
{
print("Something went wrong while fetching events from parkour API.")
print("Something went wrong while fetching maps from parkour API.")
print("=> " + failure.errorCode)
print("=> " + failure.errorMessage)
has_api_access = false
Expand All @@ -95,19 +97,19 @@ void function WorldLeaderboard_FetchEvents() {


/**
* This fetches scores for the event linked to the current match, using the previously
* stored event id.
* This fetches scores for the map linked to the current match, using the previously
* stored map id.
* Scores fetching happens every few seconds.
*
* On scores reception, those are sent to connected game clients, to update the in-game
* "world" leaderboard.
**/
void function WorldLeaderboard_FetchScores(string event_id)
void function WorldLeaderboard_FetchScores()
{
print("Fetching scores for event" + event_id)
print("Fetching scores for map" + file.mapId)
HttpRequest request
request.method = HttpRequestMethod.GET
request.url = format("%s/v1/events/%s/scores", file.endpoint, event_id)
request.url = format("%s/v1/maps/%s/scores", file.endpoint, file.mapId)
table<string, array<string> > headers
headers[ "authentication" ] <- [file.secret]
request.headers = headers
Expand Down Expand Up @@ -170,7 +172,7 @@ void function SendWorldLeaderboardEntryToAPI( LeaderboardEntry entry )
{
HttpRequest request
request.method = HttpRequestMethod.POST
request.url = format("%s/v1/events/%s/scores", file.endpoint, file.event_id )
request.url = format("%s/v1/maps/%s/scores", file.endpoint, file.mapId )
table<string, array<string> > headers
headers[ "authentication" ] <- [file.secret]
request.headers = headers
Expand Down

0 comments on commit 0ffc0b1

Please sign in to comment.