Could you show a simple example on how to securely connect to a database, retrieve data from it, and write into it? I use blueprints and I don’t know much about the specifics of database communication.
There are no “simple examples”. This is a complex task.
You need a web server in which to develop and deploy a REST API. The API itself would have connectivity with the DB. You’d develop specific api queries the game server (GS) would request with an argument and parameters. The API would process the request and return a json array.
The GS would then parse the json array into useful data.
I recommend Php/MySQL as there’s tons of documentation/support/tutorials. Php is also easier to learn and 99% of all webhosting services offer it. I will note that once you get a lot of servers/users hitting the api (# requests per minute) you’ll need to transition to a pure C++ service (C++ instead of php).
All Web based API’s (uses WAN) are structured/developed like a typical website. You enter a url and the page executes the request and responds with data a browser would display.
e.g. https://mydomain.com/myapi/
Private API’s are typically token/key based. Meaning the user must have permissions to access the data/action requested. Keys are provided and managed by the api. Each token represents a specific user (e.g. GS1, GS2, GS3 etc).
All requests to the API for data, say player stats, requires the key/token as an argument parameter.
https://mydomain.com/myapi/players/?token=###&player=###&arg=###
Argument Params:
Token = GH!fV45949gjcm349itg9 // 256bit hashed token
Player = 123123534 // players id
Arg = stats
The Argument (arg) would tell the api what specific data you want.
e.g. Stats, Cosmetics, GameSave, GameSettings, Keyboard binds etc
Most API’s utilize CuRL for communication between user and service.
Here’s an example from an old project.
// ARGS: TRN_API_KEY, PLATFORM, SERVERID
$url = "https://battlefieldtracker.com/bf1/api/quick-server-info?platform=".$args['platform']."&id=".$args['serverid'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('TRN-Api-Key:'. TRN_API_KEY));
$response = curl_exec($ch);
$status = curl_getinfo($ch);
curl_close($ch);
The response would be reformatted to specific needs.
$r = json_decode($response, true);
$result['server'] = array(
'name' => $r['serverInfo']['name'],
'slots' => $r['slots']['Soldier']['current']."/".$r['slots']['Soldier']['max'],
'mode' => $r['activityInfo']['currentMode'],
'map_name' => $r['activityInfo']['currentMap'],
'mapimg' => NoLeech('map', $r['activityInfo']['mapImageUrl']),
'description' => $r['serverInfo']['description'],
'que' => $r['slots']['Queue']['current']."/".$r['slots']['Queue']['max'],
'spectator' => $r['slots']['Spectator']['current']."/".$r['slots']['Spectator']['max']
);
A good start would be to look into PHP REST API Frameworks like Comet, Laravel, Leaf, Lumen (lightweight laravel) etc. Definitely hit up StackOverflow.