• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[TFS 1.4+] Async HttpClient system

danilopucci

Well-Known Member
Joined
Nov 22, 2019
Messages
117
Solutions
4
Reaction score
98
GitHub
danilopucci
Hello community,

I would like to introduce a module that I see some guys requesting it here in otland, I started to work some time ago and now it is ready: HttpClient module
You can find full code details on this pull-request.

How does it works?
It is based on http client lib. There is a dedicated thread, very similar to DatabaseTasks which will handle requests both comming from C++ or Lua context.

How to use it in lua?
The syntax is pretty much simple, and it is very similar(and inspired) to Requests lib, from Python.
Lua:
-- define a callback
local function requestCallback(response)
 --[[response is a table with current fields:
    "requestId", "version", "statusCode", "location", "contentType", "responseTimeMs", "headerData", "bodySize", "bodyData", "success", "errorMessage"
    where you can use "contentType" to get if it is a json and parse "bodyData" accordinly
 ]]
end


-- dummy example to get something
local httpClientRequest = HttpClientRequest()
local url = "https://myapi.mydomain.com"
httpClientRequest:get(url, requestCallback)


-- dummy example to post something
local url = "https://myapi.mydomain.com"
local token = "abcdef123456789"
local data = "I want to post something"
local fields = {
    accept = "application/json",
    authorization = "Bearer " .. token
    }
httpClientRequest:post(url, requestCallback, fields, data)

For usage in C++, check the pull-request

For what is this used for?
It can be used to multiple things, the main usage is to make integration to API services, like:
  • chatgpt api, to generate and make things dinamically in game.
  • monitoring services: you can post data to a monitoring service
  • discord API: send things that is happening on realtime to your discord server


Hope you enjoy :)
 
I tried this aswell a few months back, not in a dedicated thread, but this made every request hung the server for a millisecond. Will try this solution, thanks!
 
Yeah, this is indeed very valuable contribution. Best of luck merging it to TFS!
Thank you! Looking forward to see the usages from the community

I tried this aswell a few months back, not in a dedicated thread, but this made every request hung the server for a millisecond. Will try this solution, thanks!
Yeah, I followed this approach to avoid any blocking to game loop, so it is safe in this point of view.
The callback context to parse response is on the dedicated thread too, so you can safely parse and dispatch any other action to the game loop
 
chatgpt api, to generate and make things dinamically in game. what does this means, could you elaborate a bit more please?

nice release, thank you
 
chatgpt api, to generate and make things dinamically in game. what does this means, could you elaborate a bit more please?

nice release, thank you

The most easy example that comes to my mind is on NPC System.
ChatGPT has an API, which you can redirect player message to it, and its response you can send back to the player. If you fine tune it, add the context of your server/game, it can generate content dinamically to your game.

Also, it will generate one different result for each player...

Anyway, it is up to the game creator imagination
 
The most easy example that comes to my mind is on NPC System.
ChatGPT has an API, which you can redirect player message to it, and its response you can send back to the player. If you fine tune it, add the context of your server/game, it can generate content dinamically to your game.

Also, it will generate one different result for each player...

Anyway, it is up to the game creator imagination
It takes time for the response to be generated so if anything, its gonna add lag to NPCs conversation. Also its gonna cost you fuck ton of money (ChatGPT API) if you were to run this on a populated server.
 
It takes time for the response to be generated so if anything, its gonna add lag to NPCs conversation. Also its gonna cost you fuck ton of money (ChatGPT API) if you were to run this on a populated server.

Yeah, indeed... But it still can be done. Of course, add the ChatGPT to all npcs of the server, or add it to the Thais depot is not a good idea.

Also this problems can be solved by inserting it to the game context.
e.g: your NPC can request you a mission, you go back after some minutes and later you give the player reply. Also you can add a cost as game coins to the player, or only premium account players..

Again, it is up to the game creator imagination.
 
Back
Top