summaryrefslogtreecommitdiff
path: root/scenes/GUI/Leaderboard.cs
blob: cf126c9fbf0de2e563fe4c4756a91c9275344526 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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);
			}

		}
	}
}