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
extends BarTextureButton
class_name DrawButton, "res://assets/ui/draw.png"

export(NodePath) onready var status = get_node(status) as StatusLabel
export(NodePath) onready var confirmbar = get_node(confirmbar) as Confirm

var waiting_on_answer := false


func _ready() -> void:
	Globals.network.connect("request", self, "draw_request")
	Globals.network.connect("request_result", self, "_on_draw_result")


func draw_request(what: Dictionary):
	if what.type == Network.REQUESTHEADERS.draw:
		if "questions" in what:
			Log.err("Draw request without prompt")
			return
		confirmbar.confirm(self, what.question, 20)
		waiting_on_answer = true


func drawed() -> GDScriptFunctionState:
	return Globals.grid.drawed("mutual agreement")


func stoplooking() -> void:
	_handle_confirm(false)


func _pressed() -> void:
	if waiting_on_answer:
		_handle_confirm(true)
		confirmbar.stop_looking()
	else:
		disabled = true
		Globals.network.send_request_packet(
			Network.REQUESTHEADERS.draw, "Your opponent wishes to draw, do you accept?"
		)
		status.set_text("Draw request sent")


func _on_draw_result(accepted: Dictionary) -> void:  # called from _handle_confirm
	disabled = false
	if accepted.type == Network.REQUESTHEADERS.draw:
		if accepted.answer:
			drawed()
		else:
			status.set_text("Your opponent has rejected the draw request.")


func _handle_confirm(yes: bool) -> void:  # called from confirmbar.confirmed
	if waiting_on_answer:
		disabled = false
		waiting_on_answer = false
		Globals.network.send_request_answer_packet(Network.REQUESTHEADERS.draw, yes)
		if yes:
			drawed()