online multiplayer chess game (note server currently down)
Diffstat (limited to 'Utils.gd')
-rw-r--r--Utils.gd85
1 files changed, 32 insertions, 53 deletions
diff --git a/Utils.gd b/Utils.gd
index d255488..99fafca 100644
--- a/Utils.gd
+++ b/Utils.gd
@@ -3,6 +3,7 @@ extends Node
var internet := false
signal newmove(move)
signal newfen(fen)
+signal pop_move(fen, was_num)
var moves_list: PoolStringArray = []
var fen := ""
@@ -13,56 +14,21 @@ func get_pgn():
func _on_turn_over() -> void:
- fen = get_fen()
+ fen = Fen.get_fen()
Log.info("fen: " + fen)
emit_signal("newfen", fen)
-func spotispiece(piece_type: int, spot: Piece) -> bool:
- return SanParse.from_str(spot.shortname.to_upper()) == piece_type if spot else false
+func pop_move() -> String:
+ emit_signal("pop_move")
+ moves_list.remove(moves_list.size() - 1)
+ var pgn = get_pgn()
+ moves_list.resize(0)
+ return pgn
-func get_fen() -> String:
- var pieces := ""
- for rank in range(8):
- var empty := 0
- for file in range(8):
- var spot: Piece = Globals.grid.matrix[rank][file]
- if spot == null:
- empty += 1
- if len(pieces) > 0 and str(empty - 1) == pieces[-1]:
- pieces[-1] = str(empty)
- else:
- pieces += str(empty)
- else:
- pieces += (spot.shortname[0].to_upper() if spot.white else spot.shortname[0].to_lower())
- empty = 0
- if rank != 7:
- pieces += "/"
- # handle castling checks
- var whitecastling := PoolStringArray(Globals.white_king.castleing(true)).join("")
- var blackcastling := PoolStringArray(Globals.black_king.castleing(true)).join("")
- var castlingrights := ""
- if blackcastling and whitecastling:
- castlingrights = whitecastling.to_upper() + blackcastling.to_lower()
- else:
- castlingrights = "-"
-
- var enpassants := ""
- for pawn in Globals.pawns:
- if pawn.just_double_stepped and pawn.just_set:
- enpassants += Utils.to_algebraic(pawn.real_position + (Vector2.DOWN * pawn.whiteint))
- return (
- "%s %s %s %s %s %s"
- % [
- pieces,
- "w" if Globals.turn else "b",
- castlingrights,
- enpassants if enpassants else "-",
- Globals.halfmove,
- Globals.fullmove,
- ]
- ) # pos # turn # castling # enpassant # halfmove # fullmove
+func spotispiece(piece_type: int, spot: Piece) -> bool:
+ return SanParse.from_str(spot.shortname.to_upper()) == piece_type if spot else false
static func str_bool(string: String) -> bool:
@@ -94,7 +60,6 @@ func get_args() -> Dictionary:
func _ready() -> void:
- internet_available()
Events.connect("turn_over", self, "_on_turn_over")
if "help" in get_args():
print("usage: ./chess%s [debug | help]" % exec_ext())
@@ -103,6 +68,16 @@ func _ready() -> void:
get_tree().quit() # dont wait
Debug.monitor(self, "fen")
Debug.monitor(self, "pgn", "get_pgn()")
+ var t = Timer.new()
+ add_child(t)
+ t.name = "t"
+ t.connect("timeout", self, "_on_timeout", [t])
+ _on_timeout(t)
+
+
+func _on_timeout(timer: Timer) -> void:
+ timer.start(600) # every 10m
+ request() # ping server so it doesnt go down
static func exec_ext() -> String:
@@ -146,14 +121,14 @@ static func get_node_name(node: Node) -> Array:
return ["", ""]
-func internet_available() -> bool:
+func request() -> int: # returns err
var http := HTTPRequest.new()
add_child(http)
- var httpurl := "https://1.1.1.1"
- var returnable := http.request(httpurl) == OK
- http.queue_free()
- internet = returnable
- return returnable
+ var httpurl := Network.url.replace("wss://", "http://")
+ var error := http.request(httpurl)
+ http.free()
+ internet = error == OK
+ return error
func walk_dir(path := "res://assets/pieces", only_dir := true, exclude := []) -> PoolStringArray: # walk the directory, finding the asset packs
@@ -182,7 +157,10 @@ func format_seconds(time: float, use_milliseconds: bool = false) -> String:
func _notification(what: int) -> void:
- if what == MainLoop.NOTIFICATION_WM_QUIT_REQUEST or what == MainLoop.NOTIFICATION_WM_GO_BACK_REQUEST:
+ if (
+ what == MainLoop.NOTIFICATION_WM_QUIT_REQUEST
+ or what == MainLoop.NOTIFICATION_WM_GO_BACK_REQUEST
+ ):
Log.debug("Bye!")
@@ -205,9 +183,10 @@ static func from_algebraic(pos: String) -> Vector2:
static func to_str(type: int) -> String:
- return " NBRQK"[type].strip_edges() # if its a pawn, return nothing
+ return "PNBRQK"[type]
+# cant wait for 4.0 dict.merge(dict) :C
static func append_dict(dict: Dictionary, newdict: Dictionary) -> Dictionary:
for key in newdict:
dict[key] = newdict[key]