summaryrefslogtreecommitdiff
path: root/scripts/Entities/Actions/EscapeAction.cs
blob: 1b3aae24d5a55856841e9299e688c7207c64e380 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using Godot;
using Godot.Collections;
using Microsoft.VisualBasic;
using TheLegendOfGustav.Entities.Actors;
using TheLegendOfGustav.Utils;

namespace TheLegendOfGustav.Entities.Actions;

public partial class EscapeAction(Actor actor, bool should_save = false) : Action(actor)
{
	private bool should_save = should_save;
	public override bool Perform()
	{
		if (should_save) {
			Actor.MapData.SaveGame();
		} else {
			// game over
			bool hasLeaderboardFile = FileAccess.FileExists("user://placar.json");
			if (hasLeaderboardFile) {
				using var leaderboardFile = FileAccess.Open("user://placar.json", FileAccess.ModeFlags.ReadWrite);
				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)
				{
					leaderboardFile.Resize(0);
					leaderboardFile.Seek(0);

					leaderBoardData = new()
					{
						{"placar", new Array<Dictionary<string, Variant>>() {Stats.Instance.Serialize()}}
					};
					boardString = Json.Stringify(leaderBoardData);
				
					leaderboardFile.StoreLine(boardString);

					SignalBus.Instance.EmitSignal(SignalBus.SignalName.EscapeRequested);
					return false;
				}

				Array<Dictionary<string, Variant>> players = (Array<Dictionary<string, Variant>>)leaderBoardData["placar"];

				players.Add(Stats.Instance.Serialize());

				for (int i = 0; i < players.Count; i++) {
					for (int j = 0; j < players.Count - 1 - i; j++) {
						if ((int)players[j]["andar_mais_fundo"] < (int)players[j + 1]["andar_mais_fundo"]) {
							Dictionary<string, Variant> tmp = players[j];
							players[j] = players[j + 1];
							players[j + 1] = tmp;
						}
					}
				}

				if (players.Count > 10) {
					players = players.GetSliceRange(0, 10);
				}

				leaderBoardData["placar"] = players;

				leaderboardFile.Resize(0);
				leaderboardFile.Seek(0);
				leaderboardFile.StoreLine(Json.Stringify(leaderBoardData));
			} else {
				using var leaderboardFile = FileAccess.Open("user://placar.json", FileAccess.ModeFlags.Write);
				Dictionary<string, Variant> leaderBoardData = new()
				{
					{"placar", new Array<Dictionary<string, Variant>>() {Stats.Instance.Serialize()}}
				};
				string boardString = Json.Stringify(leaderBoardData);
				
				leaderboardFile.StoreLine(boardString);
			}
		}
		SignalBus.Instance.EmitSignal(SignalBus.SignalName.EscapeRequested);
		return false;
	}
}