Bagelicious.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using Godot;
  2. using System;
  3. public partial class Bagelicious : Control
  4. {
  5. private LineEdit _textEdit;
  6. public String Code;
  7. private HttpRequest _httpEntry = new HttpRequest();
  8. private HttpRequest _httpButton = new HttpRequest();
  9. private HttpRequest _httpLocation = new HttpRequest();
  10. private Control _delivery;
  11. private LineEdit _editX;
  12. private LineEdit _editY;
  13. private int _currentOrderId;
  14. private Control _delivering;
  15. private Timer TT = new Timer();
  16. // private Random _rng = new Random();
  17. // public char GenerateChar()
  18. // {
  19. // return (char) (_rng.Next('A', 'Z' + 1));
  20. // }
  21. // public string GenerateString(int length)
  22. // {
  23. // char[] letters = new char[length];
  24. // for (int i = 0; i < length; i++)
  25. // {
  26. // letters[i] = GenerateChar();
  27. // }
  28. // return new string(letters);
  29. // }
  30. public override void _Ready()
  31. {
  32. AddChild(_httpButton);
  33. AddChild(_httpEntry);
  34. AddChild(_httpLocation);
  35. _httpButton.RequestCompleted += OnRequestCompletedButton;
  36. _httpEntry.RequestCompleted += OnRequestCompletedEntry;
  37. _httpLocation.RequestCompleted += OnRequestCompletedLoc;
  38. _httpButton.Timeout = 2.0f;
  39. _httpEntry.Timeout = 2.0f;
  40. _httpLocation.Timeout = 2.0f;
  41. _delivery = (Control)FindChild("Delivery");
  42. _editX = (LineEdit)FindChild("LineEditE");
  43. _editY = (LineEdit)FindChild("LineEditS");
  44. _textEdit = (LineEdit)FindChild("EnterCode");
  45. _delivering = GetNode<Control>("Delivering");
  46. TT.WaitTime = 5.0;
  47. TT.OneShot = true;
  48. TT.Timeout += () => {
  49. _delivering.Hide();
  50. };
  51. AddChild(TT);
  52. Code = "";
  53. _textEdit.TextChanged += (String s) => {
  54. if(s.Length == 4) {
  55. String req = "https://nairobi.ninja/bagel/verify/" + s;
  56. _httpEntry.Request(req);
  57. }
  58. };
  59. foreach(Node bagelnode in GetTree().GetNodesInGroup("bagelbutton")) {
  60. Button bagelbutton = (Button)bagelnode;
  61. int id = bagelbutton.GetMeta("bagelid").As<int>();
  62. bagelbutton.Pressed += () => {
  63. if (Code.Length == 4) {
  64. _currentOrderId = id;
  65. _delivery.Show();
  66. }
  67. };
  68. }
  69. ((Button)FindChild("SendBB")).Pressed += () => {
  70. if(_editX.Text.Length > 0 && _editY.Text.Length > 0) {
  71. String req = "https://nairobi.ninja/bagel/order/" + Code + "/" + _currentOrderId + "/" + _editX.Text + "/" + _editY.Text;
  72. GD.Print(req);
  73. foreach(Node bagelnode in GetTree().GetNodesInGroup("bagelbutton")) {
  74. Button bagelbutton = (Button)bagelnode;
  75. bagelbutton.Disabled = true;
  76. }
  77. _httpButton.Request(req);
  78. _delivery.Hide();
  79. }
  80. };
  81. (GetNode<Timer>("PosSpyTimer")).Timeout += () => {
  82. if (Code.Length == 4) {
  83. String req = "https://nairobi.ninja/bagel/getpos/" + Code;
  84. _httpLocation.Request(req);
  85. }
  86. };
  87. }
  88. public override void _Input(InputEvent ev) {
  89. if(ev.IsActionReleased("esc")) {
  90. Code = "";
  91. GetNode<Control>("OtherScreen").Show();
  92. GetNode<Control>("MainScreen").Hide();
  93. GetNode<Control>("PosSpy").Hide();
  94. }
  95. }
  96. private void OnRequestCompletedLoc(long result, long responseCode, string[] headers, byte[] body){
  97. Json j = new Json();
  98. var e = j.Parse(System.Text.Encoding.UTF8.GetString(body));
  99. if(e == Error.Ok) {
  100. Variant x;
  101. Variant y;
  102. j.Data.AsGodotDictionary().TryGetValue("x", out x);
  103. j.Data.AsGodotDictionary().TryGetValue("y", out y);
  104. var xx = x.As<float>();
  105. var yy = y.As<float>();
  106. if(xx >= 0.0f || yy >= 0.0f) {
  107. GetNode<RichTextLabel>("PosSpy/RichTextLabel").Text = "[center]Your location: (" + yy.ToString("0.000") + "°S, " + xx.ToString("0.000") + "°E)[/center]";
  108. } else {
  109. GetNode<RichTextLabel>("PosSpy/RichTextLabel").Text = "[center]Your location: unreachable[/center]";
  110. }
  111. } else {
  112. GetNode<RichTextLabel>("PosSpy/RichTextLabel").Text = "[center]Connection lost[/center]";
  113. }
  114. }
  115. private void OnRequestCompletedEntry(long result, long responseCode, string[] headers, byte[] body)
  116. {
  117. var res = System.Text.Encoding.UTF8.GetString(body);
  118. if(responseCode == 200 && res.Length == 4) {
  119. Code = res;
  120. _textEdit.Text = "";
  121. GetNode<Control>("OtherScreen").Hide();
  122. GetNode<Control>("MainScreen").Show();
  123. GetNode<Control>("PosSpy").Show();
  124. } else if(responseCode == 404) {
  125. _textEdit.Text = "";
  126. _textEdit.PlaceholderText = "Invalid code";
  127. } else {
  128. _textEdit.Text = "";
  129. _textEdit.PlaceholderText = "Server unreachable";
  130. }
  131. }
  132. private void OnRequestCompletedButton(long result, long responseCode, string[] headers, byte[] body)
  133. {
  134. if(responseCode == 200) {
  135. foreach(Node bagelnode in GetTree().GetNodesInGroup("bagelbutton")) {
  136. Button bagelbutton = (Button)bagelnode;
  137. bagelbutton.Disabled = false;
  138. }
  139. _delivering.Show();
  140. TT.Start();
  141. } else {
  142. GetNode<Control>("OtherScreen").Show();
  143. GetNode<Control>("MainScreen").Hide();
  144. GetNode<Control>("PosSpy").Hide();
  145. Code = "";
  146. _textEdit.PlaceholderText = "Server unreachable";
  147. }
  148. }
  149. }