summaryrefslogtreecommitdiff
path: root/scenes/GUI/Leaderboard.cs
diff options
context:
space:
mode:
authorGustavoeklund01 <eklundgu@gmail.com>2025-11-10 17:25:59 -0300
committerGustavoeklund01 <eklundgu@gmail.com>2025-11-10 17:25:59 -0300
commit1a042d7303081e8bc72e9e3c341db3e528c8d998 (patch)
treecb56eafbd73ab8c7f8ba231fd512ae246b4dd64b /scenes/GUI/Leaderboard.cs
parentde99779d19b77d174c561cb3795538412f53bcbc (diff)
parentf46d058571d453ccf7ddf884ccb5d8694f970d7c (diff)
Merge branch 'master' of https://github.com/Simplesmente-O-Grupo/projeto-fantasiaHEADmaster
Diffstat (limited to 'scenes/GUI/Leaderboard.cs')
-rw-r--r--scenes/GUI/Leaderboard.cs62
1 files changed, 62 insertions, 0 deletions
diff --git a/scenes/GUI/Leaderboard.cs b/scenes/GUI/Leaderboard.cs
new file mode 100644
index 0000000..cf126c9
--- /dev/null
+++ b/scenes/GUI/Leaderboard.cs
@@ -0,0 +1,62 @@
+using System;
+using Godot;
+using Godot.Collections;
+using TheLegendOfGustav.Utils;
+
+namespace TheLegendOfGustav.GUI;
+public partial class Leaderboard : Control
+{
+ [Signal]
+ public delegate void MenuRequestedEventHandler();
+ private static readonly PackedScene LeaderboardItemScene = GD.Load<PackedScene>("res://scenes/GUI/leaderboard_item.tscn");
+
+ private VBoxContainer leaderboard;
+ private Button BackButton;
+ // Called when the node enters the scene tree for the first time.
+ public override void _Ready()
+ {
+ leaderboard = GetNode<VBoxContainer>("VBoxContainer/VBoxContainer");
+ BackButton = GetNode<Button>("VBoxContainer/exit");
+
+ BackButton.Pressed += () => EmitSignal(SignalName.MenuRequested);
+
+ bool hasLeaderboardFile = FileAccess.FileExists("user://placar.json");
+ if (hasLeaderboardFile)
+ {
+ using var leaderboardFile = FileAccess.Open("user://placar.json", FileAccess.ModeFlags.Read);
+ string boardString = leaderboardFile.GetLine();
+
+ Dictionary<string, Variant> leaderBoardData;
+
+ try
+ {
+ var parseResult = Json.ParseString(boardString);
+ if (parseResult.VariantType == Variant.Type.Nil)
+ {
+ throw new Exception();
+ }
+ leaderBoardData = (Dictionary<string, Variant>)parseResult;
+ }
+ catch (Exception)
+ {
+ // Arquivo inválido.
+ return;
+ }
+
+ Array<Dictionary<string, Variant>> players = (Array<Dictionary<string, Variant>>)leaderBoardData["placar"];
+
+ foreach (Dictionary<string, Variant> player in players)
+ {
+ LeaderboardItem item = LeaderboardItemScene.Instantiate<LeaderboardItem>();
+
+ item.PlayerName = (string)player["jogador"];
+ item.Floor = (string)player["andar_mais_fundo"];
+ item.Kills = (string)player["inimigos_mortos"];
+ item.Damage = (string)player["dano_tomado"];
+
+ leaderboard.AddChild(item);
+ }
+
+ }
+ }
+}