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 ConfirmButton
class_name UndoButton

export(NodePath) onready var status = get_node(status) as StatusLabel
const undo_request_message = "%s requested a undo"
const undo_declined_message = "undo declined"


func _ready() -> void:
	PacketHandler.connect("undo", self, "undo_signal_recieved")


func _pressed() -> void:
	if Globals.spectating:
		return
	if waiting_on_answer:
		_confirmed(true)
	else:
		var two_undos = true if Globals.grid.chess.turn == Globals.grid.team else false
		var completed_moves = Globals.grid.chess.history().size()
		if completed_moves == 0 or (two_undos && completed_moves == 1):
			status.set_text("No moves to undo!")
			return
		var msg = undo_request_message % Utils.expand_color(Globals.grid.team)
		var pckt = {gamecode = PacketHandler.game_code, question = msg, two = two_undos}
		status.set_text("")
		PacketHandler.send_packet(pckt, PacketHandler.HEADERS.undo)
		Globals.chat.server(msg)
		set_disabled(true)


func undo_signal_recieved(sig: Dictionary) -> void:
	if "question" in sig:
		Globals.chat.server(sig.question)
		confirm()
	else:
		set_disabled(false)
		if sig.accepted:
			undo(sig.two)
		else:
			# declined signal reception
			Globals.chat.server(undo_declined_message)


func _confirmed(what: bool) -> void:
	._confirmed(what)
	var two_undos = not Globals.grid.is_my_turn()  # not my turn
	var pckt = {gamecode = PacketHandler.game_code, accepted = what, two = two_undos}
	PacketHandler.send_packet(pckt, PacketHandler.HEADERS.undo)
	if what:
		undo(two_undos)
	else:
		# pressed no reception
		Globals.chat.server(undo_declined_message)


func undo(two_undos := false):
	Globals.grid.undo(two_undos)
	status.set_text("")