Jump to content

API Per-City Info?


Recommended Posts

add to ^; iirc. the source data for traffic (I don't know for certain, since I'm not involved with his side projects :P) is ets2map, so "worst" case, you can build it out yourself.

Link to comment
Share on other sites

19 minutes ago, Tuxy Fluffyclaws said:

add to ^; iirc. the source data for traffic (I don't know for certain, since I'm not involved with his side projects :P) is ets2map, so "worst" case, you can build it out yourself.

 

Yeah, on his traffic site, it does say it's built using ets2map. He gave me a link to the json output of all the traffic info so luckily I don't have to build it myself, but it'd be interesting to find out how to do that anyway.

Link to comment
Share on other sites

4 hours ago, SDCore said:

Yeah, on his traffic site, it does say it's built using ets2map. He gave me a link to the json output of all the traffic info so luckily I don't have to build it myself, but it'd be interesting to find out how to do that anyway.

 

Enough to have a list of cities and their coordinates.

Then, getting the full json for all servers from ets2map, compare the positions of the players with the coordinates of the cities, adding some radius.

This is just one possible example of such implementation.

  • Upvote 1
Link to comment
Share on other sites

There are two ways to achieve your goal if you want to do it yourself. Both will require ETS2MAP API.
http://tracker.ets2map.com/v2/fullmap

The above link provides you with JSON response of each and every truck that is online in game.


Both methods will require you to find out X and Y coordinates of the city as well as its radius. You will need to create a table where you will store it.
For example:
city_id    city_name    x    y    radius
1             london          2    5      10            (assume)

2             ..........            ....  ....    .....           (and so on)
$city_data will fetch data from above table. SELECT * FROM table_name;


1. First Way   (NOT RECOMMENDED) (Your Server IP may get banned from using ets2map API)(It will make too many requests on ets2map API and we do not want this to happen)

https://tracker.ets2map.com/v2/server_id/x/y/radius

It will return JSON reponse containing location and truckersmp_id and other information of each truck that is online in that circle in the game on a particular server.

The ets2map API provides us to find the location of each and every truck in a particular circle, if you know the center of circle coordinate and its radius.
When you will request this URL for each of the cities that your table has, it will generate tremendous amount of load on the server itself

<?php
foreach($servers as $server){
	foreach($city_data as $circle){
		$response = json_decode(file_get_contents(https://tracker.ets2map.com/v2/$server->server_id/$circle->x/$circle->y/$circle->radius));
        //CACHE HERE
	}
}
?>

 

The variable $response will pull in data of each online truck in every iteration. So you will need to cache it. Put this script in a cronjob that will run after a specific period of time and then you can fetch the data from the locally stored cache rather than requesting for data every time a user requests it.

 

I am also assuming that you have TruckersMP id of your Drivers in your VTC Management system.
So once the data of each city as per server is cached, You can only find the drivers that belong to your VTC.
I will not be discussing this part bcoz I am assuming you already know this.

 

$server has mapping of server_id to server names.
REMEMBER: server id on truckersmp API is different from ets2map API. Make sure you manually check it and create the required table accordingly.
I have messaged 'Tuxy Fluffyclaws' some time ago regarding this above issue. Got no response.

 

2. Second Way  (RECOMMENDED) (Your server will be the one who is doing work here)(Only 1 request will be sent to ETS2MAP API)

 

I am assuming you have created the city_data table which has all the coordinates and radius of city that you want to check for as shown above.

 

<?php
$response = json_decode(file_get_contents(http://tracker.ets2map.com/v2/fullmap));
//FILTER THIS RESPONSE SO that it has only your own VTC drivers.
foreach($city_data as $circle){
	//TRAVERSE THE RESPONSE HERE
  	foreach($response->trucks AS $truck){
    	//WE NEED TO FIND WHETHER THAT TRUCK LIES INSIDE THAT CIRCLE OR NOT
      	if(sqrt(pow(($circle->x - $truck->x),2)+pow(($circle->y - $truck->y),2)) <= $circle->radius){
          //TRUCK is in this city
          //push the data of the truck in an array.
          array_push(data fetched from $truck)
        }
    }
  	//array will have all the location of trucks in a particular city.
  	//CACHE this array and mark it as trucks in $circle->city_name.
  	//EMPTY ARRAY for next iteration of city.
}
?>

Instead of caching the above result, you can show the output to a user whenever they request the page.

 

Once again i would suggest putting this script in a cronjob.

When a user fetches for information.. you would only need to fetch data from cached files.

 

In the above script, you will get data from every server.. not one particular server.. if you want to find server also it can done using $truck->server

 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

I never really liked this response.. they never made it public. Someone (allegedly) already had access to the documentations/or verbally told, while the rest of the community did not.

 

It is not limited to cities.. and it does not have to be a circle.. It can be of any shape and size.

I hope the above written stuff helps the community in creating their desired applications. There are endless things you can do with the ETS2MAP API (thank you devs for this) <3

  • Upvote 1
Link to comment
Share on other sites

On 9/2/2017 at 5:18 PM, Orangee said:

There are two ways to achieve your goal if you want to do it yourself. Both will require ETS2MAP API.

 

 

Damn, dude! That's a lot of work and honestly is pretty cool.

 

Krashnz did provide a way that just returns the amount of traffic in each city, but I do have a few ideas for things using the work you did. I really appreciate the effort!

Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.