From 3ec9992707c441de2ad8f1b00abaccfbf4e46cbb Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Thu, 13 Oct 2022 14:40:19 -0500 Subject: [PATCH] Chat features --- Assets/star.png | Bin 0 -> 4178 bytes Assets/star.png.import | 35 +++++++++++++++++++++++++ Scenes/Lobby.tscn | 44 ++++++++++++++++++++++++++----- Scripts/Lobby.gd | 30 ++++++++++++++++++--- Scripts/Message.gd | 29 +++++++++++++++++++++ Scripts/WebsocketController.gd | 46 +++++++++++++++++++++++---------- project.godot | 8 +++++- 7 files changed, 166 insertions(+), 26 deletions(-) create mode 100644 Assets/star.png create mode 100644 Assets/star.png.import create mode 100644 Scripts/Message.gd diff --git a/Assets/star.png b/Assets/star.png new file mode 100644 index 0000000000000000000000000000000000000000..7d7ffd4c04ee0e34330542478f790e22005ff661 GIT binary patch literal 4178 zcmai1dpMKt8-JY|#j=ocXju-$s5R1-RIHHW#ujrv=bUNIGEt3cMbYF`LN?}*L-|HT z%wZq5=Th2dprsoMQ85KZe|J=aWGTo9KkuW9H&w z3aPejhOZUhpLA38m45r#J2W-!Xl<=r`IYkEVyAV7VkUlQJ6=I(4uvw&cvTr;rjIzL z$ym>QK25w(#g_D5*Ffw9C=29uGgWlymDIjx?OHpua zADcV3mo|0>5a!pf@?O$W1cDJ|X#~%~{z4`$vjE)bj1gaMY0Vlagsiz7c1QtDB0rUN>GYPyzbfozIuz#dFk*d?&HPwjxVO zIBMGXqLtBr3n$U#UrSD;3yBwEM8UGR6Nr3H)}u3HtH*b(9~T_!3h9@g>8~2}83>1W z zic$e<@Gr&N!coDWS_D#U7Qx|AK$)Q*r6zKS>Vy-r0w!%CACMBxB&T^lVB}Tk>zvw= zj;u#M+@=Dc%%{-`li*c~pMaxu&tU$Z&gYoS<)^z`*0N}@ldNgUxXP+$7HU~s$&WGy zjy}w2{2zP*uP82=cJ1WMGi%nlA||*p%=n~ib>|?wh~dtaO1J23jaXujtMbU4rmp*0ilWk>dqjoO8o( zJp-L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==*JQQu#AA=@81HF1IwiLi_u~6@@uNm$ zlYj@ysX|X{bsOb1X(aUbm>?G|k5mg7<8o?a?i+7IEb?1C=4TmS5A`*w^dCLg`M;s~ hzmxI*6OWcpl#d&|Z3{~%7R~>F)oD9Sm6_M&{{Y3$=@$S1 literal 0 HcmV?d00001 diff --git a/Assets/star.png.import b/Assets/star.png.import new file mode 100644 index 0000000..f64ce82 --- /dev/null +++ b/Assets/star.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/star.png-59a17f0ff6ffca9363606061d3940469.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://Assets/star.png" +dest_files=[ "res://.import/star.png-59a17f0ff6ffca9363606061d3940469.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +process/normal_map_invert_y=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/Scenes/Lobby.tscn b/Scenes/Lobby.tscn index f2d59cf..b56a28c 100644 --- a/Scenes/Lobby.tscn +++ b/Scenes/Lobby.tscn @@ -10,13 +10,43 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="ItemList" type="ItemList" parent="."] -anchor_left = 0.0449219 -anchor_top = 0.125 -anchor_right = 0.254477 -anchor_bottom = 0.955 -margin_right = -27.584 -margin_bottom = -62.0 +[node name="Chat" type="Control" parent="."] +anchor_left = 0.80957 +anchor_top = 0.265 +anchor_right = 0.80957 +anchor_bottom = 0.265 +margin_right = 40.0 +margin_bottom = 40.0 __meta__ = { "_edit_use_anchors_": true } + +[node name="TextEdit" type="TextEdit" parent="Chat"] +margin_left = -59.0 +margin_top = -98.0 +margin_right = 143.0 +margin_bottom = 280.0 +readonly = true +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="LineEdit" type="LineEdit" parent="Chat"] +margin_left = -59.0 +margin_top = 287.0 +margin_right = 142.0 +margin_bottom = 316.0 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="CopyBtn" type="Button" parent="."] +margin_left = 48.0 +margin_top = 33.0 +margin_right = 168.0 +margin_bottom = 53.0 +disabled = true +text = "Copy Room Code" + +[connection signal="text_entered" from="Chat/LineEdit" to="." method="_on_LineEdit_text_entered"] +[connection signal="pressed" from="CopyBtn" to="." method="_on_CopyBtn_pressed"] diff --git a/Scripts/Lobby.gd b/Scripts/Lobby.gd index d62d449..38a7951 100644 --- a/Scripts/Lobby.gd +++ b/Scripts/Lobby.gd @@ -1,14 +1,36 @@ extends Control +onready var location = JavaScript.get_interface("location") + func _init(): - WebsocketController.connect("hostjoin", self, "_on_hostjoin") + WebsocketController.connect("connected", self, "_on_connected") + WebsocketController.connect("join_ok", self, "_on_join_ok") + WebsocketController.connect("room_code", self, "_on_room_code") + WebsocketController.connect("chat", self, "_on_chat") func _ready(): WebsocketController.connect_websocket() -func _on_hostjoin(): - var location = JavaScript.get_interface("location") +func _on_connected(): if location and location.hash != "": - WebsocketController.join_game(location.hash) + WebsocketController.join_game(location.hash.trim_prefix("#")) else: WebsocketController.host_game() + +func _on_room_code(code): + location.hash = code + $CopyBtn.disabled = false + +func _on_join_ok(): + $CopyBtn.disabled = false + +func _on_chat(msg): + $Chat/TextEdit.text += msg + $Chat/TextEdit.text += "\n" + +func _on_CopyBtn_pressed(): + OS.clipboard = location.href + +func _on_LineEdit_text_entered(text): + WebsocketController.chat(text) + $Chat/LineEdit.clear() diff --git a/Scripts/Message.gd b/Scripts/Message.gd new file mode 100644 index 0000000..cec7e74 --- /dev/null +++ b/Scripts/Message.gd @@ -0,0 +1,29 @@ +extends Object + +class_name Message + +var command: String +var args: Array + +func _init(text: String = ""): + if text == "": + command = "" + args = [] + return + var match_re = RegEx.new(); + # r"((\\,|[^,])+)" + match_re.compile("(\\\\,|[^,])+") + + var radix = text.find(':') + command = text.substr(0, radix) + var args_raw = text.substr(radix+1) + args = [] + for match_ in match_re.search_all(args_raw): + var arg = match_.strings[0].strip_edges().replace("\\,", ",") + args.push_back(arg) + +func _to_string(): + var text = "%s:" % self.command + for arg in self.args: + text += arg.replace(",", "\\,") + return text diff --git a/Scripts/WebsocketController.gd b/Scripts/WebsocketController.gd index 857e0b9..e46834a 100644 --- a/Scripts/WebsocketController.gd +++ b/Scripts/WebsocketController.gd @@ -1,38 +1,56 @@ extends Node var client = WebSocketClient.new() -var peer +var ws -signal hostjoin +signal connected +signal chat +signal room_code +signal join_ok func _init(): client.connect("connection_established", self, "_on_connect") client.connect("data_received", self, "_on_data") func _on_connect(_proto): - peer = client.get_peer(1) - peer.set_write_mode(WebSocketPeer.WRITE_MODE_TEXT) + ws = client.get_peer(1) + ws.set_write_mode(WebSocketPeer.WRITE_MODE_TEXT) + emit_signal("connected") func _on_data(): - var msg = peer.get_packet().get_string_from_utf8() - var regex = RegEx.new() - regex.compile("([\\w_]+):") - var res = regex.search(msg) - match res.strings[1]: - "HOSTJOIN": - emit_signal("hostjoin") + var msg = Message.new(ws.get_packet().get_string_from_utf8()) + match msg.command: + "CHAT": + emit_signal("chat", msg.args[0]) + "ROOM_CODE": + emit_signal("room_code", msg.args[0]) + "JOIN_OK": + emit_signal("join_ok") + _: + print("Unknown command => ", msg) func _process(_delta): client.poll() func send_message(message): - peer.put_packet(message.to_utf8()) + ws.put_packet(str(message).to_utf8()) func connect_websocket(): client.connect_to_url(ProjectSettings["global/server_url"]); func host_game(): - send_message("HOST:") + var msg = Message.new() + msg.command = "HOST" + send_message(msg) func join_game(hash_): - send_message("JOIN: %s" % hash_) + var msg = Message.new() + msg.command = "JOIN" + msg.args.push_back(hash_) + send_message(msg) + +func chat(text): + var msg = Message.new() + msg.command = "CHAT" + msg.args.push_back(text) + send_message(msg) diff --git a/project.godot b/project.godot index 88406f4..5e64016 100644 --- a/project.godot +++ b/project.godot @@ -23,11 +23,17 @@ _global_script_classes=[ { "class": "Hex", "language": "GDScript", "path": "res://Scripts/Hex.gd" +}, { +"base": "Object", +"class": "Message", +"language": "GDScript", +"path": "res://Scripts/Message.gd" } ] _global_script_class_icons={ "Board": "", "Chit": "", -"Hex": "" +"Hex": "", +"Message": "" } [application]