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
extends Label
class_name OpeningLabel

var http_request := HTTPRequest.new()

var url := "https://explorer.lichess.ovh/masters?topGames=0&moves=2&fen=%s"
var current_req := ""


func _ready():
	add_child(http_request)
	Events.connect("turn_over", self, "update_opening")
	Globals.grid.connect("load_pgn", self, "update_opening")
	Globals.grid.connect("clear_pgn", self, "update_opening")
	Globals.grid.connect("remove_last", self, "update_opening")
	http_request.connect("request_completed", self, "_request_completed")


func update_opening(_var := null) -> void:
	if Utils.internet:
		var fen: String = Globals.grid.chess.fen()
		if fen != Globals.grid.chess.DEFAULT_POSITION && fen != current_req:
			if current_req:
				http_request.cancel_request()
			current_req = fen
			var u = url % fen.replace(" ", "_").http_escape()
			Log.net(["REQUEST: get opening with url:", u])
			http_request.request(u)
	else:
		set_text("")


func _request_completed(result, _response_code, _headers, byte_body):
	set_text("")  # empty text and hide self
	current_req = ""
	if result != OK:  # technically REQUEST_SUCCESS but i cant find it
		return
	var body = byte_body.get_string_from_utf8()
	Log.net("RECIEVED:" + body)
	var response = parse_json(body)

	if response.opening != null and "name" in response.opening:
		set_text(" %s" % response.opening.name)


func set_text(_text := ""):
	visible = _text != ""
	text = _text.strip_edges()