Leaderboard.cs
· 2.9 KiB · C#
Orginalformat
using Godot;
using Godot.Collections;
public partial class Leaderboard : Control
{
[Export] ScrollContainer _scrollContainer;
[Export] PackedScene userPanel;
[Export] MarginContainer marginContainer;
[Export] TextEdit nameText;
[Export] TextEdit pointsText;
[Export] string googleFormsID;
[Export] string googleSheetsID;
string formsUrl = "https://docs.google.com/forms/u/0/d/e";
string sheetsUrl = "https://opensheet.elk.sh";
private string urlSubmit;
private string urlData;
string[] headers = {
"User-Agent: Demonic-Leaderboard/0.0.1",
"Content-Type: application/x-www-form-urlencoded"
};
HttpClient client = new HttpClient();
public override void _Ready()
{
urlSubmit = $"{formsUrl}/{googleFormsID}/formResponse";
urlData = $"{sheetsUrl}/{googleSheetsID}/Data";
}
void Update()
{
var http = new HttpRequest();
AddChild(http);
http.RequestCompleted += (long result, long responseCode, string[] headers, byte[] body) =>
{
HttpDone(http, result, responseCode, headers, body);
};
var err = http.Request(urlData, headers, HttpClient.Method.Get);
if (err != 0)
{
http.QueueFree();
}
}
void Add()
{
var http = new HttpRequest();
AddChild(http);
http.RequestCompleted += (long result, long responseCode, string[] headers, byte[] body) =>
{
HttpSubmit(http, result, responseCode, headers, body);
};
var userData = new Dictionary()
{
{ "entry.1772479419", nameText.Text },
{ "entry.680244927", pointsText.Text }
};
var data = client.QueryStringFromDict(userData);
var err = http.Request(urlSubmit, headers, HttpClient.Method.Post, data);
if (err != 0)
{
http.QueueFree();
}
else
{
nameText.Text = "";
pointsText.Text = "";
}
}
void HttpSubmit(HttpRequest http, long result, long respCode, string[] headers, byte[] body)
{
http.QueueFree();
}
void HttpDone(HttpRequest http, long result, long respCode, string[] headers, byte[] body)
{
http.QueueFree();
if (result != null)
{
var data = Json.ParseString(body.GetStringFromUtf8());
Array array = data.AsGodotArray();
foreach (Dictionary entry in array)
{
string name = (string)entry["Name"];
string points = (string)entry["Points"];
GD.Print($"Name: {name}, Points: {points}");
var users = userPanel.Instantiate();
var userName = users.GetNode<RichTextLabel>("Name");
var userPoints = users.GetNode<RichTextLabel>("Points");
userName.Text = name;
userPoints.Text = points;
marginContainer.AddChild(users);
}
}
}
}
| 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 | } |
| 106 |