debug receive

This commit is contained in:
2020-03-04 18:03:06 +08:00
parent a83855bec0
commit 8aa45d2827
3 changed files with 33 additions and 3 deletions
+17 -2
View File
@@ -8,6 +8,12 @@ extends Node
var url = "ws://127.0.0.1"
var port = "1114"
enum MsgType {GameMsg = 0, ChatMsg}
enum PlayerAction {Start, Deal, Chow, Pong, Kong, Win}
signal deal_tiles
# Our WebSocketClient instance
var _client = WebSocketClient.new()
@@ -42,13 +48,22 @@ func _connected(proto = ""):
print("Connected with protocol: ", proto)
# You MUST always use get_peer(1).put_packet to send data to server,
# and not put_packet directly when not using the MultiplayerAPI.
_client.get_peer(1).put_packet("Test packet".to_utf8())
# _client.get_peer(1).put_packet("Test packet".to_utf8())
func _on_data():
# Print the received packet, you MUST always use get_peer(1).get_packet
# to receive data from server, and not get_packet directly when not
# using the MultiplayerAPI.
print("Got data from server: ", _client.get_peer(1).get_packet().get_string_from_utf8())
var data_recv = _client.get_peer(1).get_packet().get_string_from_utf8()
print("Got data from server: ", data_recv)
var json_recv = JSON.parse(data_recv)
var json = json_recv.result
if json["msg_type"]==MsgType.GameMsg:
if json["available_actions"][0]==PlayerAction.Deal:
print_debug("current tiles: ", json["current_tile"])
print_debug("type: ", typeof(json["current_tile"]))
emit_signal("deal_tiles", json)
print_debug("msg_type: ", json_recv.result["msg_type"])
func _process(delta):
# Call this in _process or _physics_process. Data transfer, and signals
+15
View File
@@ -6,10 +6,12 @@ extends Node2D
# var b = "text"
var Tile = preload("res://Tile.tscn")
var is_my_turn = false
var my_table_order = 0
# Called when the node enters the scene tree for the first time.
func _ready():
ConnManager.connect("deal_tiles", self, "deal_tile")
# center the timer
pass # Replace with function body.
@@ -19,6 +21,19 @@ func tile_click_handler(tile):
return
$BottomHand.remove_tile_by_instance(tile)
# TODO:send websocket msg
func deal_tile(msg):
var tiles = msg["current_tile"]
for i in range(tiles.size()):
var t = Tile.instance()
t.set_tile_type(tiles[i]["suit"], tiles[i]["number"])
$BottomHand.add_tile_by_instance(t)
pass
var tiles_count = msg["tiles_count"]
$RightHand.show_tiles(tiles_count[(my_table_order+1)%4])
$OppositeHand.show_tiles(tiles_count[(my_table_order+2)%4])
$LeftHand.show_tiles(tiles_count[(my_table_order+3)%4])
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
+1 -1
View File
@@ -160,7 +160,7 @@ text = "Send to Server"
[node name="TextEdit" type="TextEdit" parent="HBoxContainer"]
margin_left = 980.0
margin_right = 1080.0
margin_right = 1030.0
margin_bottom = 40.0
rect_min_size = Vector2( 50, 0 )