using Godot; using System; public partial class Bagelicious : Control { private LineEdit _textEdit; public String Code; private HttpRequest _httpEntry = new HttpRequest(); private HttpRequest _httpButton = new HttpRequest(); private HttpRequest _httpLocation = new HttpRequest(); private Control _delivery; private LineEdit _editX; private LineEdit _editY; private int _currentOrderId; private Control _delivering; private Timer TT = new Timer(); // private Random _rng = new Random(); // public char GenerateChar() // { // return (char) (_rng.Next('A', 'Z' + 1)); // } // public string GenerateString(int length) // { // char[] letters = new char[length]; // for (int i = 0; i < length; i++) // { // letters[i] = GenerateChar(); // } // return new string(letters); // } public override void _Ready() { AddChild(_httpButton); AddChild(_httpEntry); AddChild(_httpLocation); _httpButton.RequestCompleted += OnRequestCompletedButton; _httpEntry.RequestCompleted += OnRequestCompletedEntry; _httpLocation.RequestCompleted += OnRequestCompletedLoc; _httpButton.Timeout = 2.0f; _httpEntry.Timeout = 2.0f; _httpLocation.Timeout = 2.0f; _delivery = (Control)FindChild("Delivery"); _editX = (LineEdit)FindChild("LineEditE"); _editY = (LineEdit)FindChild("LineEditS"); _textEdit = (LineEdit)FindChild("EnterCode"); _delivering = GetNode("Delivering"); TT.WaitTime = 5.0; TT.OneShot = true; TT.Timeout += () => { _delivering.Hide(); }; AddChild(TT); Code = ""; _textEdit.TextChanged += (String s) => { if(s.Length == 4) { String req = "https://nairobi.ninja/bagel/verify/" + s; _httpEntry.Request(req); } }; foreach(Node bagelnode in GetTree().GetNodesInGroup("bagelbutton")) { Button bagelbutton = (Button)bagelnode; int id = bagelbutton.GetMeta("bagelid").As(); bagelbutton.Pressed += () => { if (Code.Length == 4) { _currentOrderId = id; _delivery.Show(); } }; } ((Button)FindChild("SendBB")).Pressed += () => { if(_editX.Text.Length > 0 && _editY.Text.Length > 0) { String req = "https://nairobi.ninja/bagel/order/" + Code + "/" + _currentOrderId + "/" + _editX.Text + "/" + _editY.Text; GD.Print(req); foreach(Node bagelnode in GetTree().GetNodesInGroup("bagelbutton")) { Button bagelbutton = (Button)bagelnode; bagelbutton.Disabled = true; } _httpButton.Request(req); _delivery.Hide(); } }; (GetNode("PosSpyTimer")).Timeout += () => { if (Code.Length == 4) { String req = "https://nairobi.ninja/bagel/getpos/" + Code; _httpLocation.Request(req); } }; } public override void _Input(InputEvent ev) { if(ev.IsActionReleased("esc")) { Code = ""; GetNode("OtherScreen").Show(); GetNode("MainScreen").Hide(); GetNode("PosSpy").Hide(); } } private void OnRequestCompletedLoc(long result, long responseCode, string[] headers, byte[] body){ Json j = new Json(); var e = j.Parse(System.Text.Encoding.UTF8.GetString(body)); if(e == Error.Ok) { Variant x; Variant y; j.Data.AsGodotDictionary().TryGetValue("x", out x); j.Data.AsGodotDictionary().TryGetValue("y", out y); var xx = x.As(); var yy = y.As(); if(xx >= 0.0f || yy >= 0.0f) { GetNode("PosSpy/RichTextLabel").Text = "[center]Your location: (" + yy.ToString("0.000") + "°S, " + xx.ToString("0.000") + "°E)[/center]"; } else { GetNode("PosSpy/RichTextLabel").Text = "[center]Your location: unreachable[/center]"; } } else { GetNode("PosSpy/RichTextLabel").Text = "[center]Connection lost[/center]"; } } private void OnRequestCompletedEntry(long result, long responseCode, string[] headers, byte[] body) { var res = System.Text.Encoding.UTF8.GetString(body); if(responseCode == 200 && res.Length == 4) { Code = res; _textEdit.Text = ""; GetNode("OtherScreen").Hide(); GetNode("MainScreen").Show(); GetNode("PosSpy").Show(); } else if(responseCode == 404) { _textEdit.Text = ""; _textEdit.PlaceholderText = "Invalid code"; } else { _textEdit.Text = ""; _textEdit.PlaceholderText = "Server unreachable"; } } private void OnRequestCompletedButton(long result, long responseCode, string[] headers, byte[] body) { if(responseCode == 200) { foreach(Node bagelnode in GetTree().GetNodesInGroup("bagelbutton")) { Button bagelbutton = (Button)bagelnode; bagelbutton.Disabled = false; } _delivering.Show(); TT.Start(); } else { GetNode("OtherScreen").Show(); GetNode("MainScreen").Hide(); GetNode("PosSpy").Hide(); Code = ""; _textEdit.PlaceholderText = "Server unreachable"; } } }