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
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():
	Globals.network.connect("signal_recieved", self, "_on_signal")


func _on_signal(what: Dictionary):
	if what.type == Network.SIGNALHEADERS.draw:
		if "question" in what:
			confirmbar.confirm(self, "Your opponent requests a draw", 20)
			waiting_on_answer = true
		else:
			disabled = false
			if what.accepted:
				drawed()
			else:
				status.set_text("Your opponent rejected the draw")


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.signal("", Network.SIGNALHEADERS.draw, "question")
		status.set_text("Draw request sent")


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