继续开始
This commit is contained in:
@@ -17,10 +17,10 @@ var client: TCPClient = TCPClient.new()
|
||||
|
||||
# 服务器配置 - 支持多个服务器地址
|
||||
var server_configs = [
|
||||
#{"host": "127.0.0.1", "port": 4040, "name": "本地"},
|
||||
{"host": "127.0.0.1", "port": 4040, "name": "本地"},
|
||||
#{"host": "192.168.1.110", "port": 4040, "name": "局域网"},
|
||||
#{"host": "47.108.90.0", "port": 4040, "name": "成都内网穿透"}#成都内网穿透
|
||||
{"host": "47.108.90.0", "port": 6060, "name": "成都公网"}#成都服务器
|
||||
#{"host": "47.108.90.0", "port": 6060, "name": "成都公网"}#成都服务器
|
||||
]
|
||||
|
||||
var current_server_index = 0
|
||||
@@ -39,6 +39,7 @@ var ping_interval = 3.0 # 每3秒ping一次
|
||||
var ping_timeout = 5.0 # ping超时时间
|
||||
var is_measuring_ping = false
|
||||
|
||||
#=====================================网络连接基本处理=========================================
|
||||
func _ready():
|
||||
# 创建TCP客户端实例
|
||||
self.add_child(client)
|
||||
@@ -70,16 +71,16 @@ func _process(delta):
|
||||
if is_trying_to_connect:
|
||||
var elapsed_time = Time.get_unix_time_from_system() - connection_start_time
|
||||
if elapsed_time > connection_timeout:
|
||||
print("连接超时,尝试下一个服务器")
|
||||
is_trying_to_connect = false
|
||||
client.disconnect_from_server()
|
||||
|
||||
if auto_retry and not has_tried_all_servers:
|
||||
try_next_server()
|
||||
else:
|
||||
status_label.text = "服务器连接失败"
|
||||
status_label.modulate = Color.RED
|
||||
has_tried_all_servers = false # 重置标志,允许下次重试
|
||||
status_label.text = "连接超时"
|
||||
status_label.modulate = Color.RED
|
||||
connection_button.text = "连接"
|
||||
|
||||
# 通知主游戏更新在线人数显示
|
||||
if main_game and main_game.has_method("_update_online_players_display"):
|
||||
main_game._update_online_players_display(0, false, false)
|
||||
|
||||
# 处理延迟测量
|
||||
if client.is_client_connected():
|
||||
@@ -92,7 +93,6 @@ func _process(delta):
|
||||
if is_measuring_ping:
|
||||
var ping_elapsed = Time.get_unix_time_from_system() - ping_start_time
|
||||
if ping_elapsed > ping_timeout:
|
||||
print("Ping超时,重置测量状态")
|
||||
is_measuring_ping = false
|
||||
current_ping = 999 # 显示为高延迟
|
||||
|
||||
@@ -109,7 +109,7 @@ func _process(delta):
|
||||
|
||||
func _on_connected():
|
||||
print("成功连接到服务器: ", server_configs[current_server_index]["name"])
|
||||
status_label.text = "已连接 测量中..."
|
||||
status_label.text = "已连接 ..."
|
||||
status_label.modulate = Color.GREEN
|
||||
connection_button.text = "断开"
|
||||
is_trying_to_connect = false
|
||||
@@ -127,11 +127,9 @@ func _on_connected():
|
||||
})
|
||||
|
||||
# 连接成功后立即请求作物数据
|
||||
print("连接成功,正在请求最新作物数据...")
|
||||
sendGetCropData()
|
||||
|
||||
# 连接成功后立即请求在线人数
|
||||
print("连接成功,正在请求在线人数...")
|
||||
sendGetOnlinePlayers()
|
||||
|
||||
# 立即开始第一次ping测量
|
||||
@@ -150,12 +148,8 @@ func _on_connection_failed():
|
||||
ping_timer = 0.0
|
||||
|
||||
# 通知主游戏更新在线人数显示
|
||||
if main_game and main_game.has_method("_update_online_players_display"):
|
||||
main_game._update_online_players_display(0, false, false)
|
||||
main_game._update_online_players_display(0, false, false)
|
||||
|
||||
# 自动尝试下一个服务器
|
||||
if auto_retry:
|
||||
try_next_server()
|
||||
|
||||
func _on_connection_closed():
|
||||
print("连接断开: ", server_configs[current_server_index]["name"])
|
||||
@@ -173,15 +167,11 @@ func _on_connection_closed():
|
||||
if main_game and main_game.has_method("_update_online_players_display"):
|
||||
main_game._update_online_players_display(0, false, false)
|
||||
|
||||
# 自动重连,优先尝试其他服务器
|
||||
if auto_retry:
|
||||
print("连接断开,准备重连...")
|
||||
var timer = get_tree().create_timer(retry_delay)
|
||||
await timer.timeout
|
||||
if not client.is_client_connected():
|
||||
# 先尝试下一个服务器,如果所有服务器都失败了再回到当前服务器
|
||||
try_next_server()
|
||||
|
||||
# 通知主游戏连接已断开,显示登录面板
|
||||
main_game._on_connection_lost()
|
||||
|
||||
|
||||
#=========================客户端与服务端通信核心=========================================
|
||||
func _on_data_received(data):
|
||||
# 根据数据类型处理数据
|
||||
response_label.text = "收到: %s" % JSON.stringify(data)
|
||||
@@ -194,9 +184,6 @@ func _on_data_received(data):
|
||||
match message_type:
|
||||
"ping": #ping是否在线
|
||||
return
|
||||
"pong": #服务器回复pong响应
|
||||
handle_pong_response()
|
||||
return
|
||||
"response": #服务器通用响应
|
||||
return
|
||||
"login_response": #登录响应
|
||||
@@ -243,9 +230,14 @@ func _on_data_received(data):
|
||||
main_game.level = updated_data["level"]
|
||||
if updated_data.has("体力值"):
|
||||
main_game.stamina = updated_data["体力值"]
|
||||
if updated_data.has("作物仓库"):
|
||||
main_game.crop_warehouse = updated_data["作物仓库"]
|
||||
|
||||
# 更新UI
|
||||
main_game._update_ui()
|
||||
# 更新作物仓库UI
|
||||
if main_game.crop_warehouse_panel:
|
||||
main_game.crop_warehouse_panel.update_crop_warehouse_ui()
|
||||
Toast.show(message, Color.GREEN)
|
||||
else:
|
||||
Toast.show(message, Color.RED)
|
||||
@@ -274,18 +266,75 @@ func _on_data_received(data):
|
||||
Toast.show(message, Color.GREEN)
|
||||
else:
|
||||
Toast.show(message, Color.RED)
|
||||
"buy_item":#处理购买道具响应
|
||||
if success:
|
||||
# 更新玩家数据
|
||||
if updated_data.has("money"):
|
||||
main_game.money = updated_data["money"]
|
||||
if updated_data.has("道具背包"):
|
||||
main_game.item_bag = updated_data["道具背包"]
|
||||
|
||||
# 更新UI
|
||||
main_game._update_ui()
|
||||
if main_game.item_bag_panel:
|
||||
main_game.item_bag_panel.update_item_bag_ui()
|
||||
Toast.show(message, Color.GREEN)
|
||||
else:
|
||||
Toast.show(message, Color.RED)
|
||||
"use_item":#处理使用道具响应
|
||||
print("调试:收到道具使用响应")
|
||||
print(" - success: ", success)
|
||||
print(" - message: ", message)
|
||||
print(" - updated_data: ", updated_data)
|
||||
|
||||
if success:
|
||||
print("调试:道具使用成功,开始更新数据")
|
||||
# 更新玩家数据
|
||||
if updated_data.has("道具背包"):
|
||||
main_game.item_bag = updated_data["道具背包"]
|
||||
print("调试:更新道具背包")
|
||||
if updated_data.has("farm_lots"):
|
||||
print("调试:更新地块数据")
|
||||
main_game.farm_lots = updated_data["farm_lots"]
|
||||
if updated_data.has("experience"):
|
||||
main_game.experience = updated_data["experience"]
|
||||
print("调试:更新经验值: ", main_game.experience)
|
||||
if updated_data.has("level"):
|
||||
main_game.level = updated_data["level"]
|
||||
print("调试:更新等级: ", main_game.level)
|
||||
|
||||
# 更新UI
|
||||
print("调试:开始更新UI")
|
||||
main_game._update_ui()
|
||||
main_game._update_farm_lots_state()
|
||||
if main_game.item_bag_panel:
|
||||
main_game.item_bag_panel.update_item_bag_ui()
|
||||
print("调试:UI更新完成")
|
||||
Toast.show(message, Color.GREEN)
|
||||
else:
|
||||
print("错误:道具使用失败: ", message)
|
||||
Toast.show(message, Color.RED)
|
||||
"dig_ground":#处理开垦土地
|
||||
if success:
|
||||
# 更新玩家数据
|
||||
if updated_data.has("money"):
|
||||
main_game.money = updated_data["money"]
|
||||
if updated_data.has("experience"):
|
||||
main_game.experience = updated_data["experience"]
|
||||
if updated_data.has("level"):
|
||||
main_game.level = updated_data["level"]
|
||||
if updated_data.has("farm_lots"):
|
||||
main_game.farm_lots = updated_data["farm_lots"]
|
||||
if updated_data.has("player_bag"):
|
||||
main_game.player_bag = updated_data["player_bag"]
|
||||
|
||||
# 更新UI
|
||||
main_game._update_ui()
|
||||
main_game._update_farm_lots_state()
|
||||
Toast.show(message, Color.GREEN)
|
||||
# 更新背包UI
|
||||
if main_game.player_bag_panel:
|
||||
main_game.player_bag_panel.update_player_bag_ui()
|
||||
Toast.show(message, Color.GREEN, 3.0, 1.0)
|
||||
else:
|
||||
Toast.show(message, Color.RED)
|
||||
"remove_crop":#处理铲除作物
|
||||
@@ -309,8 +358,10 @@ func _on_data_received(data):
|
||||
main_game.money = updated_data["money"]
|
||||
if updated_data.has("farm_lots"):
|
||||
main_game.farm_lots = updated_data["farm_lots"]
|
||||
if updated_data.has("体力值"):
|
||||
main_game.stamina = updated_data["体力值"]
|
||||
if updated_data.has("experience"):
|
||||
main_game.experience = updated_data["experience"]
|
||||
if updated_data.has("level"):
|
||||
main_game.level = updated_data["level"]
|
||||
|
||||
# 更新UI
|
||||
main_game._update_ui()
|
||||
@@ -325,8 +376,10 @@ func _on_data_received(data):
|
||||
main_game.money = updated_data["money"]
|
||||
if updated_data.has("farm_lots"):
|
||||
main_game.farm_lots = updated_data["farm_lots"]
|
||||
if updated_data.has("体力值"):
|
||||
main_game.stamina = updated_data["体力值"]
|
||||
if updated_data.has("experience"):
|
||||
main_game.experience = updated_data["experience"]
|
||||
if updated_data.has("level"):
|
||||
main_game.level = updated_data["level"]
|
||||
|
||||
# 更新UI
|
||||
main_game._update_ui()
|
||||
@@ -396,15 +449,38 @@ func _on_data_received(data):
|
||||
"new_player_gift_response": #新手大礼包响应
|
||||
if main_game and main_game.has_method("_handle_new_player_gift_response"):
|
||||
main_game._handle_new_player_gift_response(data)
|
||||
"online_gift_data_response": #在线礼包数据响应
|
||||
if main_game and main_game.has_method("_handle_online_gift_data_response"):
|
||||
main_game._handle_online_gift_data_response(data)
|
||||
"claim_online_gift_response": #领取在线礼包响应
|
||||
if main_game and main_game.has_method("_handle_claim_online_gift_response"):
|
||||
main_game._handle_claim_online_gift_response(data)
|
||||
"pong": #延迟检测响应
|
||||
handle_pong_response(data)
|
||||
"modify_account_info_response": #修改账号信息响应
|
||||
if main_game and main_game.has_method("_handle_account_setting_response"):
|
||||
main_game._handle_account_setting_response(data)
|
||||
"delete_account_response": #删除账号响应
|
||||
if main_game and main_game.has_method("_handle_account_setting_response"):
|
||||
main_game._handle_account_setting_response(data)
|
||||
"refresh_player_info_response": #刷新玩家信息响应
|
||||
if main_game and main_game.has_method("_handle_account_setting_response"):
|
||||
main_game._handle_account_setting_response(data)
|
||||
_:
|
||||
# 显示其他类型的消息
|
||||
return
|
||||
_:
|
||||
# 处理非JSON数据
|
||||
return
|
||||
#=========================客户端与服务端通信核心=========================================
|
||||
|
||||
#=====================================网络连接基本处理=========================================
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#=====================================网络操作处理=========================================
|
||||
func _on_connection_button_pressed():
|
||||
if client.is_client_connected():
|
||||
# 断开连接
|
||||
@@ -431,7 +507,7 @@ func connect_to_current_server():
|
||||
connection_start_time = Time.get_unix_time_from_system()
|
||||
|
||||
client.connect_to_server(config["host"], config["port"])
|
||||
|
||||
#手动发送消息处理
|
||||
func _on_send_button_pressed():
|
||||
if not client.is_client_connected():
|
||||
status_label.text = "未连接,无法发送"
|
||||
@@ -451,7 +527,10 @@ func _on_send_button_pressed():
|
||||
|
||||
# 清空输入
|
||||
message_input.text = ""
|
||||
#=====================================网络操作处理=========================================
|
||||
|
||||
|
||||
#=====================================客户端向服务端发送消息处理=========================================
|
||||
#发送登录信息
|
||||
func sendLoginInfo(username, password):
|
||||
client.send_data({
|
||||
@@ -535,6 +614,36 @@ func sendBuySeed(crop_name):
|
||||
})
|
||||
return true
|
||||
|
||||
#发送购买道具信息
|
||||
func sendBuyItem(item_name, item_cost):
|
||||
if not client.is_client_connected():
|
||||
return false
|
||||
|
||||
client.send_data({
|
||||
"type": "buy_item",
|
||||
"item_name": item_name,
|
||||
"item_cost": item_cost,
|
||||
"timestamp": Time.get_unix_time_from_system()
|
||||
})
|
||||
return true
|
||||
|
||||
#发送使用道具信息
|
||||
func sendUseItem(lot_index, item_name, use_type, target_username = ""):
|
||||
|
||||
if not client.is_client_connected():
|
||||
print("错误:客户端未连接")
|
||||
return false
|
||||
var message = {
|
||||
"type": "use_item",
|
||||
"lot_index": lot_index,
|
||||
"item_name": item_name,
|
||||
"use_type": use_type,
|
||||
"target_username": target_username,
|
||||
"timestamp": Time.get_unix_time_from_system()
|
||||
}
|
||||
client.send_data(message)
|
||||
return true
|
||||
|
||||
#发送获取游玩时间请求
|
||||
func sendGetPlayTime():
|
||||
if not client.is_client_connected():
|
||||
@@ -556,12 +665,16 @@ func sendUpdatePlayTime():
|
||||
return true
|
||||
|
||||
#发送获取玩家排行榜请求
|
||||
func sendGetPlayerRankings():
|
||||
func sendGetPlayerRankings(sort_by = "level", sort_order = "desc", filter_online = false, search_qq = ""):
|
||||
if not client.is_client_connected():
|
||||
return false
|
||||
|
||||
client.send_data({
|
||||
"type": "request_player_rankings"
|
||||
"type": "request_player_rankings",
|
||||
"sort_by": sort_by,
|
||||
"sort_order": sort_order,
|
||||
"filter_online": filter_online,
|
||||
"search_qq": search_qq
|
||||
})
|
||||
return true
|
||||
|
||||
@@ -740,41 +853,42 @@ func sendClaimNewPlayerGift():
|
||||
})
|
||||
return true
|
||||
|
||||
#发送获取在线礼包数据请求
|
||||
func sendGetOnlineGiftData():
|
||||
if not client.is_client_connected():
|
||||
return false
|
||||
|
||||
client.send_data({
|
||||
"type": "get_online_gift_data",
|
||||
"timestamp": Time.get_unix_time_from_system()
|
||||
})
|
||||
return true
|
||||
|
||||
#发送领取在线礼包请求
|
||||
func sendClaimOnlineGift(gift_name: String):
|
||||
if not client.is_client_connected():
|
||||
return false
|
||||
|
||||
client.send_data({
|
||||
"type": "claim_online_gift",
|
||||
"gift_name": gift_name,
|
||||
"timestamp": Time.get_unix_time_from_system()
|
||||
})
|
||||
return true
|
||||
|
||||
#检查是否连接到服务器
|
||||
func is_connected_to_server():
|
||||
return client.is_client_connected()
|
||||
|
||||
# 尝试连接下一个服务器
|
||||
func try_next_server():
|
||||
var original_index = current_server_index
|
||||
current_server_index = (current_server_index + 1) % server_configs.size()
|
||||
|
||||
# 如果回到了原始服务器,说明所有服务器都尝试过了
|
||||
if current_server_index == original_index:
|
||||
if has_tried_all_servers:
|
||||
print("=== 所有服务器连接失败 ===")
|
||||
print("已尝试所有 ", server_configs.size(), " 个服务器")
|
||||
for i in range(server_configs.size()):
|
||||
print("- ", server_configs[i]["name"], " (", server_configs[i]["host"], ":", server_configs[i]["port"], ")")
|
||||
status_label.text = "所有服务器连接失败"
|
||||
status_label.modulate = Color.RED
|
||||
is_trying_to_connect = false
|
||||
return
|
||||
else:
|
||||
has_tried_all_servers = true
|
||||
|
||||
var config = server_configs[current_server_index]
|
||||
|
||||
print("=== 切换到下一个服务器 ===")
|
||||
print("从 ", server_configs[original_index]["name"], " 切换到 ", config["name"])
|
||||
print("新服务器地址: ", config["host"], ":", config["port"])
|
||||
|
||||
var timer = get_tree().create_timer(1.0) # 稍微缩短等待时间
|
||||
await timer.timeout
|
||||
|
||||
#发送通用消息
|
||||
func send_message(message: Dictionary):
|
||||
if not client.is_client_connected():
|
||||
connect_to_current_server()
|
||||
|
||||
return false
|
||||
|
||||
client.send_data(message)
|
||||
return true
|
||||
|
||||
|
||||
# 手动切换到指定服务器
|
||||
func switch_to_server(server_index: int):
|
||||
if server_index >= 0 and server_index < server_configs.size():
|
||||
@@ -815,13 +929,16 @@ func send_ping():
|
||||
"timestamp": ping_start_time
|
||||
})
|
||||
|
||||
#=====================================客户端向服务端发送消息处理=========================================
|
||||
|
||||
|
||||
# 处理服务器返回的pong消息
|
||||
func handle_pong_response(data = null):
|
||||
if is_measuring_ping:
|
||||
var current_time = Time.get_unix_time_from_system()
|
||||
current_ping = int((current_time - ping_start_time) * 1000) # 转换为毫秒
|
||||
is_measuring_ping = false
|
||||
print("延迟: ", current_ping, "ms")
|
||||
#print("延迟: ", current_ping, "ms")
|
||||
|
||||
# 更新连接状态显示
|
||||
update_connection_status()
|
||||
@@ -834,13 +951,13 @@ func update_connection_status():
|
||||
var ping_text = str(current_ping) + "ms"
|
||||
var server_name = server_configs[current_server_index]["name"]
|
||||
|
||||
if current_ping < 30:
|
||||
if current_ping < 100:
|
||||
status_label.text = "✅ " + server_name + " " + ping_text
|
||||
status_label.modulate = Color.GREEN
|
||||
elif current_ping < 80:
|
||||
elif current_ping < 150:
|
||||
status_label.text = "🟡 " + server_name + " " + ping_text
|
||||
status_label.modulate = Color.YELLOW
|
||||
elif current_ping < 150:
|
||||
elif current_ping < 300:
|
||||
status_label.text = "🟠 " + server_name + " " + ping_text
|
||||
status_label.modulate = Color.ORANGE
|
||||
else:
|
||||
@@ -848,7 +965,7 @@ func update_connection_status():
|
||||
status_label.modulate = Color.RED
|
||||
else:
|
||||
var server_name = server_configs[current_server_index]["name"]
|
||||
status_label.text = "🔄 " + server_name + " 测量中..."
|
||||
status_label.text = "🔄 " + server_name + "..."
|
||||
status_label.modulate = Color.CYAN
|
||||
else:
|
||||
status_label.text = "❌ 未连接"
|
||||
|
||||
Reference in New Issue
Block a user