extends Node

var secret_key = "123"
var add_score_url = "http://billybro.erewego.ru/addscore.php"
var high_score_url = "http://billybro.erewego.ru/display.php"
var highscorelist

func post_scores(_name, score):
	var _hash = (_name + str(score) + secret_key).md5_text()
	var http_request = HTTPRequest.new()
	var headers = ["Content-Type: application/x-www-form-urlencoded"]
	http_request.connect("request_completed", self, "_on_post_scores_request_comleted")
	add_child(http_request)
	var body =  HTTPClient.new().query_string_from_dict({"name_post": _name, "score_post": score, "hash_post":_hash})
	var error = http_request.request(add_score_url, headers, true, HTTPClient.METHOD_POST, body)
	if error != OK:
		push_error("An error occurred in the HTTP request.")
	
func get_scores():
	var http_request = HTTPRequest.new()
	add_child(http_request)
	http_request.connect("request_completed", self, "_on_get_scores_request_comleted")
	var error = http_request.request(high_score_url)
	if error != OK:
		push_error("An error occurred in the HTTP request.")
		
func _on_post_scores_request_comleted(result, response, headers, body):
	print(body.get_string_from_utf8())
		
func _on_get_scores_request_comleted(result, response, headers, body):
	var data = body.get_string_from_utf8()
	var splited = data.rsplit(",", false, 0)
	var scores = []
	for i in splited.size():
		scores.append(splited[i].rsplit(":", false, 0))	
	get_parent().load_leaderboards_table(scores)
