123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- 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<Control>("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<int>();
- 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<Timer>("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<Control>("OtherScreen").Show();
- GetNode<Control>("MainScreen").Hide();
- GetNode<Control>("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<float>();
- var yy = y.As<float>();
- if(xx >= 0.0f || yy >= 0.0f) {
- GetNode<RichTextLabel>("PosSpy/RichTextLabel").Text = "[center]Your location: (" + yy.ToString("0.000") + "°S, " + xx.ToString("0.000") + "°E)[/center]";
- } else {
- GetNode<RichTextLabel>("PosSpy/RichTextLabel").Text = "[center]Your location: unreachable[/center]";
- }
- } else {
- GetNode<RichTextLabel>("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<Control>("OtherScreen").Hide();
- GetNode<Control>("MainScreen").Show();
- GetNode<Control>("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<Control>("OtherScreen").Show();
- GetNode<Control>("MainScreen").Hide();
- GetNode<Control>("PosSpy").Hide();
- Code = "";
- _textEdit.PlaceholderText = "Server unreachable";
- }
- }
- }
|