This commit is contained in:
gdz
2025-09-07 14:57:10 +02:00
commit 7770ce1ae3
27 changed files with 1046 additions and 0 deletions

132
scenes/Main/Main.cs Normal file
View File

@@ -0,0 +1,132 @@
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
}
}
}

1
scenes/Main/Main.cs.uid Normal file
View File

@@ -0,0 +1 @@
uid://cj1o6v4tq5tiq

55
scenes/Main/main.tscn Normal file
View File

@@ -0,0 +1,55 @@
[gd_scene load_steps=3 format=3 uid="uid://cx2vkr0hbfe4c"]
[ext_resource type="Script" uid="uid://cj1o6v4tq5tiq" path="res://scenes/Main/Main.cs" id="1_gol0p"]
[ext_resource type="PackedScene" uid="uid://icuiqfoy5pkr" path="res://scenes/Tools/session_tool.tscn" id="2_br8qb"]
[node name="Main" type="Node"]
script = ExtResource("1_gol0p")
[node name="HTTPRequest" type="HTTPRequest" parent="."]
[node name="HUD" type="MarginContainer" parent="."]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 4
size_flags_vertical = 4
[node name="SessionTool" parent="HUD" instance=ExtResource("2_br8qb")]
layout_mode = 2
[node name="GlobalTool" type="MarginContainer" parent="HUD"]
clip_contents = true
layout_mode = 2
size_flags_horizontal = 4
size_flags_vertical = 4
[node name="BoxContainer" type="BoxContainer" parent="HUD/GlobalTool"]
layout_mode = 2
[node name="VBoxContainer" type="VBoxContainer" parent="HUD/GlobalTool/BoxContainer"]
layout_mode = 2
[node name="HBoxContainer" type="HBoxContainer" parent="HUD/GlobalTool/BoxContainer/VBoxContainer"]
layout_mode = 2
[node name="Label" type="Label" parent="HUD/GlobalTool/BoxContainer/VBoxContainer/HBoxContainer"]
layout_mode = 2
text = "openF1 Manager"
[node name="Button" type="Button" parent="HUD/GlobalTool/BoxContainer/VBoxContainer/HBoxContainer"]
layout_mode = 2
text = "Send Request"
[node name="ItemList" type="ItemList" parent="HUD/GlobalTool/BoxContainer/VBoxContainer"]
clip_contents = false
custom_minimum_size = Vector2(150, 20)
layout_mode = 2
size_flags_horizontal = 3
max_text_lines = 3
auto_width = true
text_overrun_behavior = 2
item_count = 1
item_0/text = "None"

154
scenes/Tools/SessionTool.cs Normal file
View File

@@ -0,0 +1,154 @@
using System.Text;
using Godot;
using openF1Manager.scripts;
namespace openF1Manager.scenes.Main;
public partial class SessionTool : MarginContainer
{
private LineEdit _meetingKey;
private LineEdit _sessionKey;
private LineEdit _location;
private LineEdit _dateStart;
private LineEdit _dateEnd;
private string _baseAddress = "https://api.openf1.org/v1/sessions?";
// MVVM-lite
private SessionFilter _vm;
private FormBinder<SessionFilter> _binder;
public override void _Ready()
{
_meetingKey =
GetNode<LineEdit>(
"ElementsVBoxContainer/PropertiesVBoxContainer/MeetingKeyHBoxContainer/MeetingKeyLineEdit");
_sessionKey =
GetNode<LineEdit>(
"ElementsVBoxContainer/PropertiesVBoxContainer/SessionKeyHBoxContainer/SessionKeyLineEdit");
_location = GetNode<LineEdit>(
"ElementsVBoxContainer/PropertiesVBoxContainer/LocationHBoxContainer/LocationLineEdit");
_dateStart =
GetNode<LineEdit>("ElementsVBoxContainer/PropertiesVBoxContainer/DateStartHBoxContainer/DateStartLineEdit");
_dateEnd = GetNode<LineEdit>(
"ElementsVBoxContainer/PropertiesVBoxContainer/DateEndHBoxContainer/DateEndLineEdit");
GD.Print(_meetingKey.PlaceholderText);
// Create VM and bind all LineEdits under the form root
_vm = new SessionFilter();
var formRoot = GetNode<Control>("ElementsVBoxContainer/PropertiesVBoxContainer");
_binder = new FormBinder<SessionFilter>(_vm, this).Bind();
}
public string BuildQueryOld()
{
GD.Print("Building query");
var queryString = System.Web.HttpUtility.ParseQueryString(string.Empty);
foreach (var pair in new (string Key, string Value)[]
{
("meeting_key", _meetingKey.Text),
("session_key", _sessionKey.Text),
("location", _location.Text),
("date_start", _dateStart.Text),
("date_end", _dateEnd.Text),
})
{
if (!string.IsNullOrWhiteSpace(pair.Value))
queryString[pair.Key] = pair.Value;
}
return _baseAddress + queryString.ToString();
}
public string BuildQuery()
{
GD.Print("Building query");
return ApiModelSerializer.ToQueryString(_vm, _baseAddress);
}
public async void SendRequest()
{
string requestAddress = BuildQuery();
GD.Print(requestAddress);
var httpRequest = GetParent().GetParent().GetNode<HttpRequest>("HTTPRequest");
httpRequest.RequestCompleted += OnSessionRequestCompleted;
GD.Print("Sending request");
var error = httpRequest.Request(requestAddress);
if (error != Error.Ok)
{
GD.Print(error);
return;
}
}
private void OnSessionRequestCompleted(long result, long responseCode, string[] headers, byte[] body)
{
GD.Print("Request completed");
var text = Encoding.UTF8.GetString(body);
var parsed = Json.ParseString(text);
if (parsed.VariantType != Variant.Type.Array)
{
GD.PrintErr("Unexpected JSON format. Expected array.");
return;
}
var arr = parsed.AsGodotArray();
if (arr.Count == 0)
{
GD.Print("No sessions returned.");
return;
}
// Populate List of sessions
ItemList itemList = GetNode<ItemList>("ElementsVBoxContainer/ControlsVBoxContainer/ItemList");
itemList.Clear();
int count = 0;
foreach (var item in arr)
{
count++;
itemList.AddItem(count.ToString());
}
// Populate UI fields from first item in array
var first = arr[0];
if (first.VariantType != Variant.Type.Dictionary)
{
GD.PrintErr("Unexpected item format. Expected dictionary.");
return;
}
var dict = first.AsGodotDictionary();
// Populate VM; binder reflects it to UI fields
ApiModelSerializer.PopulateFromDictionary(_vm, dict);
}
private void PopulateFieldsFromDictionary(Godot.Collections.Dictionary dict)
{
// Helper to read a value with multiple possible key casings
string GetString(params string[] keys)
{
foreach (var k in keys)
{
if (dict.ContainsKey(k) && dict[k].VariantType != Variant.Type.Nil)
return dict[k].ToString();
}
return string.Empty;
}
// Map likely API keys; try snake_case first, then camelCase fallbacks
_meetingKey.Text = GetString("meeting_key", "meetingKey");
_sessionKey.Text = GetString("session_key", "sessionKey");
_location.Text = GetString("location", "Location");
_dateStart.Text = GetString("date_start", "dateStart", "DateStart");
_dateEnd.Text = GetString("date_end", "dateEnd", "DateEnd");
}
}

View File

@@ -0,0 +1 @@
uid://ccc1qonkjw7k8

View File

@@ -0,0 +1,222 @@
[gd_scene load_steps=4 format=3 uid="uid://icuiqfoy5pkr"]
[ext_resource type="Script" uid="uid://ccc1qonkjw7k8" path="res://scenes/Tools/SessionTool.cs" id="1_icata"]
[sub_resource type="SystemFont" id="SystemFont_br8qb"]
[sub_resource type="Theme" id="Theme_px18m"]
default_font = SubResource("SystemFont_br8qb")
[node name="SessionTool" type="MarginContainer"]
clip_contents = true
size_flags_horizontal = 0
size_flags_vertical = 4
theme = SubResource("Theme_px18m")
script = ExtResource("1_icata")
[node name="ElementsBoxContainer" type="BoxContainer" parent="."]
layout_mode = 2
vertical = true
[node name="PropertiesVBoxContainer" type="VBoxContainer" parent="ElementsBoxContainer"]
layout_mode = 2
theme_override_constants/separation = 1
[node name="MeetingKeyHBoxContainer" type="HBoxContainer" parent="ElementsBoxContainer/PropertiesVBoxContainer"]
layout_mode = 2
[node name="MeetingKeyLabel" type="Label" parent="ElementsBoxContainer/PropertiesVBoxContainer/MeetingKeyHBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "Meeting Key:"
[node name="MeetingKeyLineEdit" type="LineEdit" parent="ElementsBoxContainer/PropertiesVBoxContainer/MeetingKeyHBoxContainer"]
layout_mode = 2
placeholder_text = "latest"
expand_to_text_length = true
clear_button_enabled = true
[node name="SessionKeyHBoxContainer" type="HBoxContainer" parent="ElementsBoxContainer/PropertiesVBoxContainer"]
layout_mode = 2
[node name="SessionKeyLabel" type="Label" parent="ElementsBoxContainer/PropertiesVBoxContainer/SessionKeyHBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "Session Key: "
[node name="SessionKeyLineEdit" type="LineEdit" parent="ElementsBoxContainer/PropertiesVBoxContainer/SessionKeyHBoxContainer"]
layout_mode = 2
placeholder_text = "latest"
expand_to_text_length = true
clear_button_enabled = true
[node name="LocationHBoxContainer" type="HBoxContainer" parent="ElementsBoxContainer/PropertiesVBoxContainer"]
layout_mode = 2
[node name="LocationLabel" type="Label" parent="ElementsBoxContainer/PropertiesVBoxContainer/LocationHBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "Location:"
[node name="LocationLineEdit" type="LineEdit" parent="ElementsBoxContainer/PropertiesVBoxContainer/LocationHBoxContainer"]
layout_mode = 2
placeholder_text = "Monza"
expand_to_text_length = true
clear_button_enabled = true
[node name="DateStartHBoxContainer" type="HBoxContainer" parent="ElementsBoxContainer/PropertiesVBoxContainer"]
layout_mode = 2
[node name="DateStartLabel" type="Label" parent="ElementsBoxContainer/PropertiesVBoxContainer/DateStartHBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "Date Start:"
[node name="DateStartLineEdit" type="LineEdit" parent="ElementsBoxContainer/PropertiesVBoxContainer/DateStartHBoxContainer"]
layout_mode = 2
placeholder_text = "2023-09-01T11:30:00+00:00"
expand_to_text_length = true
clear_button_enabled = true
[node name="DateEndHBoxContainer" type="HBoxContainer" parent="ElementsBoxContainer/PropertiesVBoxContainer"]
layout_mode = 2
[node name="DateEndLabel" type="Label" parent="ElementsBoxContainer/PropertiesVBoxContainer/DateEndHBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "Date End:"
[node name="DateEndLineEdit" type="LineEdit" parent="ElementsBoxContainer/PropertiesVBoxContainer/DateEndHBoxContainer"]
layout_mode = 2
placeholder_text = "2023-09-01T11:30:00+00:00"
expand_to_text_length = true
clear_button_enabled = true
[node name="SessionTypeHBoxContainer" type="HBoxContainer" parent="ElementsBoxContainer/PropertiesVBoxContainer"]
layout_mode = 2
[node name="SessionTypeLabel" type="Label" parent="ElementsBoxContainer/PropertiesVBoxContainer/SessionTypeHBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "Session Type:"
[node name="SessionTypeLineEdit" type="LineEdit" parent="ElementsBoxContainer/PropertiesVBoxContainer/SessionTypeHBoxContainer"]
layout_mode = 2
placeholder_text = "Practice"
expand_to_text_length = true
clear_button_enabled = true
[node name="SessionNameHBoxContainer" type="HBoxContainer" parent="ElementsBoxContainer/PropertiesVBoxContainer"]
layout_mode = 2
[node name="SessionNameLabel" type="Label" parent="ElementsBoxContainer/PropertiesVBoxContainer/SessionNameHBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "Session Name:"
[node name="SessionNameLineEdit" type="LineEdit" parent="ElementsBoxContainer/PropertiesVBoxContainer/SessionNameHBoxContainer"]
layout_mode = 2
placeholder_text = "Practice 1"
expand_to_text_length = true
clear_button_enabled = true
[node name="CountryKeyHBoxContainer" type="HBoxContainer" parent="ElementsBoxContainer/PropertiesVBoxContainer"]
layout_mode = 2
[node name="CountryKeyLabel" type="Label" parent="ElementsBoxContainer/PropertiesVBoxContainer/CountryKeyHBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "Country Key:"
[node name="CountryKeyLineEdit" type="LineEdit" parent="ElementsBoxContainer/PropertiesVBoxContainer/CountryKeyHBoxContainer"]
layout_mode = 2
placeholder_text = "13.0"
expand_to_text_length = true
clear_button_enabled = true
[node name="CountryCodeHBoxContainer" type="HBoxContainer" parent="ElementsBoxContainer/PropertiesVBoxContainer"]
layout_mode = 2
[node name="CountryCodeLabel" type="Label" parent="ElementsBoxContainer/PropertiesVBoxContainer/CountryCodeHBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "Country Code:"
[node name="CountryCodeLineEdit" type="LineEdit" parent="ElementsBoxContainer/PropertiesVBoxContainer/CountryCodeHBoxContainer"]
layout_mode = 2
placeholder_text = "ITA"
expand_to_text_length = true
clear_button_enabled = true
[node name="CountryNameHBoxContainer" type="HBoxContainer" parent="ElementsBoxContainer/PropertiesVBoxContainer"]
layout_mode = 2
[node name="CountryNameLabel" type="Label" parent="ElementsBoxContainer/PropertiesVBoxContainer/CountryNameHBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "Country Name:"
[node name="CountryNameLineEdit" type="LineEdit" parent="ElementsBoxContainer/PropertiesVBoxContainer/CountryNameHBoxContainer"]
layout_mode = 2
placeholder_text = "Italy"
expand_to_text_length = true
clear_button_enabled = true
[node name="CircuitKeyHBoxContainer" type="HBoxContainer" parent="ElementsBoxContainer/PropertiesVBoxContainer"]
layout_mode = 2
[node name="CircuitKeyLabel" type="Label" parent="ElementsBoxContainer/PropertiesVBoxContainer/CircuitKeyHBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "Circuit Key:"
[node name="CircuitKeyLineEdit" type="LineEdit" parent="ElementsBoxContainer/PropertiesVBoxContainer/CircuitKeyHBoxContainer"]
layout_mode = 2
placeholder_text = "39.0"
expand_to_text_length = true
clear_button_enabled = true
[node name="CircuitShortNameHBoxContainer" type="HBoxContainer" parent="ElementsBoxContainer/PropertiesVBoxContainer"]
layout_mode = 2
[node name="CircuitShortNameLabel" type="Label" parent="ElementsBoxContainer/PropertiesVBoxContainer/CircuitShortNameHBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "Circuit Short Name:"
[node name="CircuitShortNameLineEdit" type="LineEdit" parent="ElementsBoxContainer/PropertiesVBoxContainer/CircuitShortNameHBoxContainer"]
layout_mode = 2
placeholder_text = "Monza"
expand_to_text_length = true
clear_button_enabled = true
[node name="GmtOffsetHBoxContainer" type="HBoxContainer" parent="ElementsBoxContainer/PropertiesVBoxContainer"]
layout_mode = 2
[node name="GmtOffsetLabel" type="Label" parent="ElementsBoxContainer/PropertiesVBoxContainer/GmtOffsetHBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "GMT Offset:"
[node name="GmtOffsetLineEdit" type="LineEdit" parent="ElementsBoxContainer/PropertiesVBoxContainer/GmtOffsetHBoxContainer"]
layout_mode = 2
placeholder_text = "02:00:00"
expand_to_text_length = true
clear_button_enabled = true
[node name="YearBoxContainer" type="HBoxContainer" parent="ElementsBoxContainer/PropertiesVBoxContainer"]
layout_mode = 2
[node name="YearLabel" type="Label" parent="ElementsBoxContainer/PropertiesVBoxContainer/YearBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "Year:"
[node name="YearLineEdit" type="LineEdit" parent="ElementsBoxContainer/PropertiesVBoxContainer/YearBoxContainer"]
layout_mode = 2
placeholder_text = "2023.0"
expand_to_text_length = true
clear_button_enabled = true
[node name="ControlsVBoxContainer" type="HBoxContainer" parent="ElementsBoxContainer"]
layout_mode = 2