online multiplayer chess game (note server currently down)
add a undo button to sf
bendn 2022-10-06
parent 2593946 · commit e603bbb
-rw-r--r--Globals.gd4
-rw-r--r--project.godot6
-rw-r--r--ui/menus/local_multiplayer/LocalMultiplayer.gd36
-rw-r--r--ui/menus/sidebarright/SidebarRight.tscn2
-rw-r--r--ui/menus/sidebarright/UserPanel.gd5
-rw-r--r--ui/menus/sidebarright/undobutton.gd10
6 files changed, 43 insertions, 20 deletions
diff --git a/Globals.gd b/Globals.gd
index 232a243..235667a 100644
--- a/Globals.gd
+++ b/Globals.gd
@@ -5,7 +5,7 @@ var piece_set := "california"
var board_color1: Color = Color(0.870588, 0.890196, 0.901961)
var board_color2: Color = Color(0.54902, 0.635294, 0.678431)
var spectating := false
-var local := false
+var local: LocalMultiplayer = null
var playing := false setget , get_playing
var chat: Chat = null
var grid: Grid = null
@@ -15,7 +15,7 @@ func reset_vars() -> void:
team = "w"
grid = null
chat = null
- local = false
+ local = null
spectating = false
diff --git a/project.godot b/project.godot
index b296c59..615b210 100644
--- a/project.godot
+++ b/project.godot
@@ -124,6 +124,11 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://ui/menus/lobby/Lobby.gd"
}, {
+"base": "Control",
+"class": "LocalMultiplayer",
+"language": "GDScript",
+"path": "res://ui/menus/local_multiplayer/LocalMultiplayer.gd"
+}, {
"base": "Node",
"class": "Log",
"language": "GDScript",
@@ -278,6 +283,7 @@ _global_script_class_icons={
"GridMenuButton": "",
"HueSlider": "",
"Lobby": "",
+"LocalMultiplayer": "",
"Log": "",
"MaterialLabel": "",
"MaterialLabelManager": "",
diff --git a/ui/menus/local_multiplayer/LocalMultiplayer.gd b/ui/menus/local_multiplayer/LocalMultiplayer.gd
index 62ac0e8..b9a8f60 100644
--- a/ui/menus/local_multiplayer/LocalMultiplayer.gd
+++ b/ui/menus/local_multiplayer/LocalMultiplayer.gd
@@ -1,4 +1,5 @@
extends Control
+class_name LocalMultiplayer
onready var gameconfig := $"%GameConfig"
@@ -16,7 +17,7 @@ var ui: GameUI
func create(moves: PoolStringArray, player1_color: bool, players: PoolIntArray, engine_depth: int) -> void:
assign_mode(players)
- Globals.local = true
+ Globals.local = self
ui = load("res://ui/board/Game.tscn").instance()
var b: Grid = ui.get_board() as Grid
b.local = true
@@ -91,7 +92,7 @@ func _pressed():
func _input(_event):
- if Input.is_action_pressed("ui_cancel") and Globals.local == true and in_game:
+ if Input.is_action_pressed("ui_cancel") and Globals.local and in_game:
Events.emit_signal("go_back", "", true)
@@ -112,6 +113,10 @@ func go_back(_reason: String, _isok: bool) -> void:
get_parent().current_tab = get_parent().get_children().find(self)
+func undo(two: bool = false) -> void:
+ board_engine_bridge.undo(two)
+
+
class BoardEngineBridge:
extends Reference
signal nps(nps)
@@ -144,8 +149,14 @@ class BoardEngineBridge:
func connect_signals():
stockfish.connect("info", self, "_info")
stockfish.connect("bestmove", self, "clear_arrows")
+ stockfish.connect("bestmove", self, "play_bestmove")
Events.connect("turn_over", self, "turn_over")
+ func undo(two: bool = false) -> void:
+ stockfish.stop()
+ b.undo(two)
+ set_engine_position()
+
func _info(info: Dictionary):
if searching == false:
return
@@ -174,25 +185,22 @@ class BoardEngineBridge:
func turn_over():
set_engine_position()
if stockfish.game.turn in playing:
- play_bestmove()
+ find_bestmove()
- func play_bestmove():
+ # play_bestmove() will play the move when its found
+ func find_bestmove():
yield(tree, "idle_frame")
- var move: String = yield(bestmove(), "completed")
- emit_signal("nps", 0)
+ stockfish.go(depth)
+ searching = true
+
+ func play_bestmove(move: Dictionary) -> void:
emit_signal("depth", 0)
- b.move(move, false, false)
+ emit_signal("nps", 0)
+ b.move(move.san, false, false)
func set_engine_position():
stockfish._position()
- func bestmove() -> String:
- stockfish.go(depth)
- searching = true
- var bestmove = yield(stockfish, "bestmove")
- searching = false
- return bestmove.san
-
func kill() -> void:
stockfish.kill()
stockfish = null
diff --git a/ui/menus/sidebarright/SidebarRight.tscn b/ui/menus/sidebarright/SidebarRight.tscn
index c96de9a..48188a1 100644
--- a/ui/menus/sidebarright/SidebarRight.tscn
+++ b/ui/menus/sidebarright/SidebarRight.tscn
@@ -144,7 +144,7 @@ text = ""
script = ExtResource( 2 )
confirm_text = "Resign?"
-[node name="UndoButton" type="Button" parent="V/buttonbarholder/buttonbar" groups=["freeinlocalmultiplayer", "hideongameover"]]
+[node name="UndoButton" type="Button" parent="V/buttonbarholder/buttonbar" groups=["hideongameover"]]
margin_left = 1051.0
margin_right = 1402.0
margin_bottom = 50.0
diff --git a/ui/menus/sidebarright/UserPanel.gd b/ui/menus/sidebarright/UserPanel.gd
index d63f908..5b5f2af 100644
--- a/ui/menus/sidebarright/UserPanel.gd
+++ b/ui/menus/sidebarright/UserPanel.gd
@@ -29,9 +29,9 @@ func set_nps(new_nps: int) -> void:
nps_display.hide()
else:
nps_display.show()
- if OS.has_feature('JavaScript'):
+ if OS.has_feature("JavaScript"):
var localized = JavaScript.eval("(%d).toLocaleString(document.documentElement.lang || 'en')" % new_nps)
- nps_display.text = "%sn/s" % (localized if localized else format_n(new_nps)) # fallback
+ nps_display.text = "%sn/s" % (localized if localized else format_n(new_nps)) # fallback
else:
nps_display.text = "%sn/s" % format_n(new_nps)
nps = new_nps
@@ -48,6 +48,7 @@ func format_n(n: int) -> String:
strn = strn.insert(i, ",")
return strn
+
func set_thinking(new_thinking: int) -> void:
if new_thinking == thinking:
return
diff --git a/ui/menus/sidebarright/undobutton.gd b/ui/menus/sidebarright/undobutton.gd
index 5572135..dc44100 100644
--- a/ui/menus/sidebarright/undobutton.gd
+++ b/ui/menus/sidebarright/undobutton.gd
@@ -13,7 +13,15 @@ func _ready() -> void:
func _pressed() -> void:
if Globals.spectating:
return
- if waiting_on_answer:
+ if Globals.local:
+ 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
+ Globals.local.undo(two_undos)
+
+ elif waiting_on_answer:
_confirmed(true)
else:
var two_undos = true if Globals.grid.chess.turn == Globals.grid.team else false