online multiplayer chess game (note server currently down)
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
extends Node
class_name SaveLoader

const settings_file := "user://chess.settings"
const id := "user://.chess.id"

const default_settings_data = {
	vsync = OS.vsync_enabled,
	fullscreen = OS.window_fullscreen,
	borderless = OS.window_borderless,
	piece_set = "california",
	board_color1 = Color(0.870588, 0.890196, 0.901961),
	board_color2 = Color(0.54902, 0.635294, 0.678431),
	rainbow = true
}

const default_id_data = {id = "", name = "", country = "rainbow", password = ""}

var files := {
	settings = {file = settings_file, data = default_settings_data.duplicate(true)},
	id = {file = id, data = default_id_data.duplicate()}
}  # file types


func get_public_info():
	return {name = files.id.data.name, country = files.id.data.country, id = files.id.data.id}


func get_data(type: String) -> Dictionary:
	if !files.has(type):
		return {}
	return files[type].data


func _ready() -> void:
	# Debug.monitor(self, "id data", "files.id.data")
	SaveLoad.load_data("settings")
	SaveLoad.load_data("id")


func to_base64(variant) -> String:
	return Marshalls.variant_to_base64(variant)


func from_base64(base64: String):
	return Marshalls.base64_to_variant(base64)


func save(type: String) -> void:
	var file := File.new()
	file.open(files[type]["file"], File.WRITE)
	file.store_string(to_base64(files[type]["data"]))


func load_data(type: String) -> Dictionary:
	if check_file(type):
		var file := File.new()
		file.open(files[type]["file"], File.READ)
		var text = file.get_as_text()
		if len(text) > 0:
			var read_dictionary = from_base64(text)
			if typeof(read_dictionary) != TYPE_DICTIONARY:
				save(type)  # OVERWRITE
			elif files[type]["data"].keys() == read_dictionary.keys():
				files[type]["data"] = read_dictionary
		save(type)  # overwrite.
		file.close()
	else:
		save(type)
	return files[type]["data"]


func check_file(type: String) -> bool:
	var file := File.new()
	return file.file_exists(files[type]["file"])