Author Topic: Track information  (Read 741 times)

Cas

  • Moderator
  • Straight Specialist
  • *****
  • Posts: 1177
  • Think different. That's OK... as long as you think
    • View Profile
    • Dimioca Labs
Re: Track information
« Reply #15 on: September 24, 2021, 11:57:18 PM »
That sounds very reasonable. With the current method, an inter-site read has to be made every time somebody loads stunts.hu, while with what you suggest, the slowdown would only occur twice a month, when a race is started or ends in Race For Kicks.

I have thoughts of ideas similar to this before, but I haven't yet done it. So far, I'm familiar with sending a POST when the user presses a Submit button, but I don't know how to trigger a POST with a PHP function. I would have to read more about that. Then, I suppose I'd be creating an HTTP form with the necessary information to be sent via POST, only instead of it being triggered by a button, it would be unconditionally sent somehow.

Another thing I don't know is how to have website A send a POST to website B without it resulting in website B being loaded on the browser, instead keeping the same page being browsed.

If you can more or less hint me on these things, I can start to make a picture of what it would be like and I'm sure I can do it.
Earth is my country. Science is my religion.

dreadnaut

  • Corner Controller
  • ***
  • Posts: 1260
    • View Profile
    • chezDreadnaut
Re: Track information
« Reply #16 on: September 25, 2021, 12:52:22 AM »
[...] the slowdown would only occur twice a month, when a race is started or ends in Race For Kicks.

If we add the start date of the race next to the end date, then it should be enough to only send information once, when you add the race to your website.

If you can more or less hint me on these things, I can start to make a picture of what it would be like and I'm sure I can do it.

There's a few ways in PHP. One is with the "curl" functions, but they are quite complex and have tons of options to keep track of. An easier one is to use file_get_contents().

A non-obvious feature of file_get_contents is that is can do HTTP calls, for example to retrieve the content of a page:
Code: [Select]
$page = file_get_contents('http://raceforkicks.com');

That would be a GET request, but with some options you can convince it to do a POST as well:

Code: [Select]
$race_for_kicks_data = [
  'website' => ...
  'races' => ...
];

$context = stream_context_create([ 'http' => [
  'method' => 'POST',
  'header' => 'Content-Type: application/x-www-form-urlencoded',
  'content' => http_build_query($race_for_kicks_data),
]]);

$response = file_get_contents('http://stunts.hu/register-race', false, $context);

The above would send a POST request to the stunts.hu address, with the form data in $race_for_kicks_data. The receiving script could reply OK!, and that would end up in $response.
« Last Edit: September 25, 2021, 12:55:02 AM by dreadnaut »

Cas

  • Moderator
  • Straight Specialist
  • *****
  • Posts: 1177
  • Think different. That's OK... as long as you think
    • View Profile
    • Dimioca Labs
Re: Track information
« Reply #17 on: September 26, 2021, 03:54:50 AM »
Wow!  That looks a lot simpler than I expected!
I don't have much time today, but I'll certainly be updating that soon
Earth is my country. Science is my religion.

dreadnaut

  • Corner Controller
  • ***
  • Posts: 1260
    • View Profile
    • chezDreadnaut
Re: Track information
« Reply #18 on: September 26, 2021, 09:32:40 AM »
I don't have much time today, but I'll certainly be updating that soon

No rush! I can prepare things on the ZakStunts side, so you have something to test against.

dreadnaut

  • Corner Controller
  • ***
  • Posts: 1260
    • View Profile
    • chezDreadnaut
Re: Track information
« Reply #19 on: September 27, 2021, 05:05:15 PM »
Here I am! The ZakStunts side is ready: you can now register "events" that will eventually appear on the portal, be they races or competitions, etc.

I've attached a minimal client for the API, so you don't have to write much code. It works like this:
Code: [Select]
require 'ZakStuntsApi.php';

$api = new ZakStuntsApi('<your API client id>', '<your API client secret>');

$response = $api->updateEvent([
  "id"           => "r4k-2021-09",
  "name"         => "2021-09 - Tropical Springs Stunt Park",
  "url"          => "http://www.raceforkicks.com/index.php?page=race&race=2021-09",
  "opens_on"     => "2021-09-01T15:59:00+02:00",
  "closes_on"    => "2021-09-27T01:59:00+02:00",
// these fields are optional, all three or none
  "track_title"  => "Tropical Springs Stunt Park",
  "track_author" => "KyLiE",
  "track_url"    => "http://www.raceforkicks.com/tracks/r4k14.trk"
]);

echo var_export($response, true);

The id is whatever you want to use to identify the race. As long as you use the same one, you can update the event information as many times as you want.

If the update goes well, it will return "status" => 200. Otherwise, you should get a useful error message.

I'll send you credentials through a different channel ;)
« Last Edit: September 27, 2021, 06:55:01 PM by dreadnaut »

Cas

  • Moderator
  • Straight Specialist
  • *****
  • Posts: 1177
  • Think different. That's OK... as long as you think
    • View Profile
    • Dimioca Labs
Re: Track information
« Reply #20 on: September 28, 2021, 01:16:04 AM »
Uhm... now I'm a little lost. I had understood the previous post, but now I downloaded the API php file and I'm not sure how the thing works. My confusion may have to do with the OOP structure, as I always write procedure oriented code, but I think there's probably something more I haven't understood.

Anyway, is stunts.hu already waiting for a message?  If I get this working, will it reflect the changes?  This is, I would like to begin testing and want to make sure when the thing starts working. I already got the information you sent me via e-mail.
Earth is my country. Science is my religion.