feat: pulling dragons from server working

This commit is contained in:
Gerard Gascón 2025-04-12 00:21:07 +02:00
parent 50bc814367
commit b080ab7bf4
11 changed files with 46 additions and 193 deletions

View file

@ -1,147 +0,0 @@
class_name CodeGenerator
var hat_outfits: DragonOutfit
var shirt_outfits: DragonOutfit
var shoes_outfits: DragonOutfit
var key: int = 123
var alphabet: Dictionary = {
'A': '00',
'B': '01',
'C': '02',
'D': '03',
'E': '04',
'F': '05',
'G': '06',
'H': '07',
'I': '08',
'J': '09',
'K': '10',
'L': '11',
'M': '12',
'N': '13',
'O': '14',
'P': '15',
'Q': '16',
'R': '17',
'S': '18',
'T': '19',
'U': '20',
'V': '21',
'W': '22',
'X': '23',
'Y': '24',
'Z': '25',
' ': '26'
}
var alphabet_inverse: Dictionary = {
'00': 'A',
'01': 'B',
'02': 'C',
'03': 'D',
'04': 'E',
'05': 'F',
'06': 'G',
'07': 'H',
'08': 'I',
'09': 'J',
'10': 'K',
'11': 'L',
'12': 'M',
'13': 'N',
'14': 'O',
'15': 'P',
'16': 'Q',
'17': 'R',
'18': 'S',
'19': 'T',
'20': 'U',
'21': 'V',
'22': 'W',
'23': 'X',
'24': 'Y',
'25': 'Z',
'26': ' '
}
func _init(hat: DragonOutfit, shirt: DragonOutfit, shoes: DragonOutfit) -> void:
hat_outfits = hat
shirt_outfits = shirt
shoes_outfits = shoes
func encrypt(hat: Texture2D, shirt: Texture2D, shoes: Texture2D, name: String) -> String:
var hat_index: int = hat_outfits.get_index(hat)
var shirt_index: int = shirt_outfits.get_index(shirt)
var shoes_index: int = shoes_outfits.get_index(shoes)
var code: String = generate_code(hat_index, shirt_index, shoes_index, name)
return xor_encrypt_decrypt(code)
func descrypt(input: String) -> Dictionary:
var decrypted_code: String = xor_encrypt_decrypt(input)
if len(decrypted_code) % 2 != 0 or len(decrypted_code) <= 6:
return {'hat': null, 'shirt': null, 'shoes': null, 'name': null}
return read_code(decrypted_code)
func generate_code(int1: int, int2: int, int3: int, input_string: String) -> String:
input_string = input_string.substr(0, 8)
var binary_data: String = ""
binary_data += _int_to_bin(int1)
binary_data += _int_to_bin(int2)
binary_data += _int_to_bin(int3)
for i in range(input_string.length()):
binary_data += _char_to_bin(input_string[i])
return binary_data
func _int_to_bin(value: int) -> String:
return "%02X" % value
func _bin_to_int(value: String) -> int:
return value.hex_to_int()
func _char_to_bin(character: String) -> String:
return alphabet[character]
func _bin_to_char(character: String) -> String:
if not alphabet_inverse.has(character):
return ""
return alphabet_inverse[character]
func xor_encrypt_decrypt(data: String) -> String:
var result: String = ""
for i in range(data.length()):
var c: int = data[i].unicode_at(0)
var encrypted_char: int = c ^ key
result += char(encrypted_char)
return result
func read_code(code: String) -> Dictionary:
var hat: Texture2D = hat_outfits.get_texture(_bin_to_int(code.substr(0, 2)))
var shirt: Texture2D = shirt_outfits.get_texture(_bin_to_int(code.substr(2, 2)))
var shoes: Texture2D = shoes_outfits.get_texture(_bin_to_int(code.substr(4, 2)))
var name: String = ""
for i in range(code.substr(6).length()):
if i % 2 != 0:
continue
if _bin_to_char(code.substr(6 + i, 2)) == "":
return {'hat': null, 'shirt': null, 'shoes': null, 'name': null}
name += _bin_to_char(code.substr(6 + i, 2))
return {'hat': hat, 'shirt': shirt, 'shoes': shoes, 'name': name}

View file

@ -1 +0,0 @@
uid://bhnm8ysguu4u4

View file

@ -5,6 +5,13 @@ class_name DragonLibrary
var dragons: Array[DragonProperties] = []
func has(properties: DragonProperties) -> bool:
for dragon in dragons:
if properties.equals(dragon):
return true
return false
func add_dragon(properties: DragonProperties) -> int:
if properties.id == 0:
properties.id = randi()
@ -12,6 +19,6 @@ func add_dragon(properties: DragonProperties) -> int:
return properties.id
func add_dragons(dragons: Array[DragonProperties]) -> void:
for d in dragons:
func add_dragons(new_dragons: Array[DragonProperties]) -> void:
for d in new_dragons:
dragons.push_back(d)

View file

@ -1,6 +1,6 @@
extends RefCounted
class_name DragonProperties
var name: String
var origin: String
var color: int
@ -22,3 +22,7 @@ func _init(name: String, origin: String, color: int, hat: int, shirt: int, decor
func _to_string() -> String:
return '%s: [origin: %s, color: %d, hat: %d, shirt: %d, decor: %d]' % [self.name, self.origin, self.color, self.hat, self.shirt, self.decor]
func equals(other: DragonProperties):
return name == other.name and origin == other.origin and color == other.color and hat == other.hat and shirt == other.shirt and decor == other.decor

View file

@ -1,11 +1,10 @@
class_name SaveLoad
const save_location = "user://save.data"
var contents_to_save: Dictionary = {
}
}
func save() -> void:
@ -20,7 +19,7 @@ func load() -> void:
var file: FileAccess = FileAccess.open(save_location, FileAccess.READ)
var data: Dictionary = file.get_var()
file.close()
contents_to_save = data.duplicate(true)
else:
contents_to_save.get_or_add('coins', 0)
@ -40,19 +39,19 @@ func clear_dragons() -> void:
contents_to_save['dragons'] = []
func add_dragon(id: int, name: String, hat: int, shirt: int, shoes: int, pos: Vector2i) -> void:
contents_to_save['dragons'].push_back({'id': id, 'name': name, 'hat': hat, 'shirt': shirt, 'shoes': shoes, 'pos': pos})
func add_dragon(dragon: DragonProperties) -> void:
contents_to_save['dragons'].push_back({'name': dragon.name, 'origin': dragon.origin, 'color': dragon.color, 'hat': dragon.hat, 'shirt': dragon.shirt, 'decor': dragon.decor, 'pos': dragon.position})
func get_dragon(dragon: Dictionary) -> DragonProperties:
var name: String = dragon['name']
var origin: String = dragon['origin']
var hat: int = dragon['hat']
var shirt: int = dragon['shirt']
var decor: int = dragon['decor']
var color: int = dragon['color']
var name: String = dragon['name']
var origin: String = dragon['origin']
var hat: int = dragon['hat']
var shirt: int = dragon['shirt']
var decor: int = dragon['decor']
var color: int = dragon['color']
var position: Vector2i = dragon['pos']
var properties = DragonProperties.new(name, origin, color, hat, shirt, decor)
var properties = DragonProperties.new(name, origin, color, hat, shirt, decor)
properties.position = position
properties.id = randi()
return properties