132 lines
4.1 KiB
C#
132 lines
4.1 KiB
C#
using System.Collections.Generic;
|
|
using System.Text;
|
|
using Godot;
|
|
|
|
namespace openF1Manager.scenes.Main;
|
|
|
|
public partial class Main : Node
|
|
{
|
|
public enum APIEndpoints
|
|
{
|
|
CarData,
|
|
Drivers,
|
|
Intervals,
|
|
Laps,
|
|
Location,
|
|
Meetings,
|
|
Overtakes,
|
|
Pit,
|
|
Position,
|
|
RaceControl,
|
|
Sessions,
|
|
SessionResult,
|
|
StartingGrid,
|
|
Stints,
|
|
TeamRadio,
|
|
Weather
|
|
}
|
|
|
|
private static readonly Dictionary<APIEndpoints, string> enpointUrl = new()
|
|
{
|
|
{ APIEndpoints.CarData, "car_data" },
|
|
{ APIEndpoints.Drivers, "drivers" },
|
|
{ APIEndpoints.Intervals, "intervals" },
|
|
{ APIEndpoints.Laps, "laps" },
|
|
{ APIEndpoints.Location, "location" },
|
|
{ APIEndpoints.Meetings, "meetings" },
|
|
{ APIEndpoints.Overtakes, "overtakes" },
|
|
{ APIEndpoints.Pit, "pit" },
|
|
{ APIEndpoints.Position, "position" },
|
|
{ APIEndpoints.RaceControl, "race_control" },
|
|
{ APIEndpoints.Sessions, "sessions" },
|
|
{ APIEndpoints.SessionResult, "session_result" },
|
|
{ APIEndpoints.StartingGrid, "starting_grid" },
|
|
{ APIEndpoints.Stints, "stints" },
|
|
{ APIEndpoints.TeamRadio, "team_radio" },
|
|
{ APIEndpoints.Weather, "weather" },
|
|
};
|
|
|
|
[Export] public APIEndpoints Enpoint = APIEndpoints.Sessions;
|
|
private string openf1Url = "https://api.openf1.org/v1/";
|
|
|
|
[Export] public string options = "date_start>=2023-09-01&date_end<=2023-09-30";
|
|
|
|
public override async void _Ready()
|
|
{
|
|
HttpRequest httpRequest = GetNode<HttpRequest>("HTTPRequest");
|
|
// httpRequest.RequestCompleted += OnRequestCompleted;
|
|
|
|
// Created address
|
|
GD.Print("Trying created address");
|
|
string requestUrl = $"{openf1Url}{enpointUrl[Enpoint]}?{options}";
|
|
GD.Print(requestUrl);
|
|
//var error = httpRequest.Request(requestUrl);
|
|
|
|
|
|
// await ToSignal(httpRequest, "request_completed");
|
|
|
|
// // Correct address
|
|
// GD.Print("Trying hardcoded address");
|
|
// requestUrl = "https://api.openf1.org/v1/sessions?date_start%3E%3D2023-09-01&date_end%3C%3D2023-09-30";
|
|
// GD.Print("https://api.openf1.org/v1/sessions?date_start%3E%3D2023-09-01&date_end%3C%3D2023-09-30");
|
|
// error = httpRequest.Request(requestUrl);
|
|
// if (error != Error.Ok)
|
|
// {
|
|
// GD.Print(error);
|
|
// return;
|
|
// }
|
|
|
|
var children = GetChildren();
|
|
|
|
foreach (var child in children)
|
|
{
|
|
if (child is Control control)
|
|
{
|
|
GD.Print(control.Name);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnRequestCompleted(long result, long responseCode, string[] headers, byte[] body)
|
|
{
|
|
var text = Encoding.UTF8.GetString(body);
|
|
var parsed = Json.ParseString(text);
|
|
|
|
if (parsed.VariantType == Variant.Type.Array)
|
|
{
|
|
var arr = parsed.AsGodotArray();
|
|
GD.Print($"Array items: {arr.Count}");
|
|
foreach (var item in arr)
|
|
{
|
|
// Each item is typically an object/dictionary; print a few fields or all key-values
|
|
if (item.VariantType == Variant.Type.Dictionary)
|
|
{
|
|
var dict = item.AsGodotDictionary();
|
|
foreach (var (k, v) in dict)
|
|
{
|
|
GD.Print($"{k}: {v}");
|
|
}
|
|
GD.Print("----");
|
|
}
|
|
else
|
|
{
|
|
GD.Print(item);
|
|
}
|
|
}
|
|
}
|
|
else if (parsed.VariantType == Variant.Type.Dictionary)
|
|
{
|
|
var json = parsed.AsGodotDictionary();
|
|
GD.Print($"Object keys: {json.Count}");
|
|
foreach (var (key, value) in json)
|
|
{
|
|
GD.Print($"{key}: {value}");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
GD.PrintErr($"Unexpected JSON root type: {parsed.VariantType}");
|
|
GD.Print(text); // Optional: inspect raw payload
|
|
}
|
|
}
|
|
} |