demonkingswarn revisó este gist . Ir a la revisión
1 file changed, 105 insertions
Leaderboard.cs(archivo creado)
| @@ -0,0 +1,105 @@ | |||
| 1 | + | using Godot; | |
| 2 | + | using Godot.Collections; | |
| 3 | + | public partial class Leaderboard : Control | |
| 4 | + | { | |
| 5 | + | [Export] ScrollContainer _scrollContainer; | |
| 6 | + | ||
| 7 | + | [Export] PackedScene userPanel; | |
| 8 | + | ||
| 9 | + | [Export] MarginContainer marginContainer; | |
| 10 | + | ||
| 11 | + | [Export] TextEdit nameText; | |
| 12 | + | [Export] TextEdit pointsText; | |
| 13 | + | ||
| 14 | + | [Export] string googleFormsID; | |
| 15 | + | [Export] string googleSheetsID; | |
| 16 | + | ||
| 17 | + | string formsUrl = "https://docs.google.com/forms/u/0/d/e"; | |
| 18 | + | string sheetsUrl = "https://opensheet.elk.sh"; | |
| 19 | + | ||
| 20 | + | private string urlSubmit; | |
| 21 | + | private string urlData; | |
| 22 | + | string[] headers = { | |
| 23 | + | "User-Agent: Demonic-Leaderboard/0.0.1", | |
| 24 | + | "Content-Type: application/x-www-form-urlencoded" | |
| 25 | + | }; | |
| 26 | + | ||
| 27 | + | HttpClient client = new HttpClient(); | |
| 28 | + | ||
| 29 | + | public override void _Ready() | |
| 30 | + | { | |
| 31 | + | urlSubmit = $"{formsUrl}/{googleFormsID}/formResponse"; | |
| 32 | + | urlData = $"{sheetsUrl}/{googleSheetsID}/Data"; | |
| 33 | + | } | |
| 34 | + | ||
| 35 | + | void Update() | |
| 36 | + | { | |
| 37 | + | var http = new HttpRequest(); | |
| 38 | + | AddChild(http); | |
| 39 | + | http.RequestCompleted += (long result, long responseCode, string[] headers, byte[] body) => | |
| 40 | + | { | |
| 41 | + | HttpDone(http, result, responseCode, headers, body); | |
| 42 | + | }; | |
| 43 | + | ||
| 44 | + | var err = http.Request(urlData, headers, HttpClient.Method.Get); | |
| 45 | + | if (err != 0) | |
| 46 | + | { | |
| 47 | + | http.QueueFree(); | |
| 48 | + | } | |
| 49 | + | } | |
| 50 | + | ||
| 51 | + | void Add() | |
| 52 | + | { | |
| 53 | + | var http = new HttpRequest(); | |
| 54 | + | AddChild(http); | |
| 55 | + | http.RequestCompleted += (long result, long responseCode, string[] headers, byte[] body) => | |
| 56 | + | { | |
| 57 | + | HttpSubmit(http, result, responseCode, headers, body); | |
| 58 | + | }; | |
| 59 | + | var userData = new Dictionary() | |
| 60 | + | { | |
| 61 | + | { "entry.1772479419", nameText.Text }, | |
| 62 | + | { "entry.680244927", pointsText.Text } | |
| 63 | + | }; | |
| 64 | + | var data = client.QueryStringFromDict(userData); | |
| 65 | + | var err = http.Request(urlSubmit, headers, HttpClient.Method.Post, data); | |
| 66 | + | if (err != 0) | |
| 67 | + | { | |
| 68 | + | http.QueueFree(); | |
| 69 | + | } | |
| 70 | + | else | |
| 71 | + | { | |
| 72 | + | nameText.Text = ""; | |
| 73 | + | pointsText.Text = ""; | |
| 74 | + | } | |
| 75 | + | } | |
| 76 | + | ||
| 77 | + | void HttpSubmit(HttpRequest http, long result, long respCode, string[] headers, byte[] body) | |
| 78 | + | { | |
| 79 | + | http.QueueFree(); | |
| 80 | + | } | |
| 81 | + | ||
| 82 | + | void HttpDone(HttpRequest http, long result, long respCode, string[] headers, byte[] body) | |
| 83 | + | { | |
| 84 | + | http.QueueFree(); | |
| 85 | + | if (result != null) | |
| 86 | + | { | |
| 87 | + | var data = Json.ParseString(body.GetStringFromUtf8()); | |
| 88 | + | Array array = data.AsGodotArray(); | |
| 89 | + | foreach (Dictionary entry in array) | |
| 90 | + | { | |
| 91 | + | string name = (string)entry["Name"]; | |
| 92 | + | string points = (string)entry["Points"]; | |
| 93 | + | ||
| 94 | + | GD.Print($"Name: {name}, Points: {points}"); | |
| 95 | + | ||
| 96 | + | var users = userPanel.Instantiate(); | |
| 97 | + | var userName = users.GetNode<RichTextLabel>("Name"); | |
| 98 | + | var userPoints = users.GetNode<RichTextLabel>("Points"); | |
| 99 | + | userName.Text = name; | |
| 100 | + | userPoints.Text = points; | |
| 101 | + | marginContainer.AddChild(users); | |
| 102 | + | } | |
| 103 | + | } | |
| 104 | + | } | |
| 105 | + | } | |
Siguiente
Anterior