提交测试代码

This commit is contained in:
Bob.Song
2026-03-06 12:15:56 +08:00
parent 209049354c
commit 7127857199
37 changed files with 1269 additions and 270 deletions

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6169cecd85b9472e989a7ef14fc83964
timeCreated: 1772694660

View File

@@ -0,0 +1,52 @@
using System;
namespace NBF
{
/// <summary>
/// 聊天频道类型
/// </summary>
[Flags]
public enum ChatChannelType
{
None = 0,
World = 1 << 1, // 世界频道
Private = 1 << 2, // 私聊频道
System = 1 << 3, // 系统频道
Broadcast = 1 << 4, // 广播频道
Notice = 1 << 5, // 公告频道
Team = 1 << 6, // 队伍频道
Near = 1 << 7, // 附近频道
CurrentMap = 1 << 8, // 当前地图频道
// 所有频道
All = World | Private | System | Broadcast | Notice | Team | Near,
// 其他聊天栏显示的频道
Display = World | Private | System | Broadcast | Notice | Team | Near | CurrentMap
}
/// <summary>
/// 聊天节点类型
/// </summary>
public enum ChatNodeType
{
None = 0,
Position = 1, // 位置节点
OpenUI = 2, // 打开UI节点
Link = 3, // 链接节点
Item = 4, // 物品节点
Text = 5, // 文本节点
Image = 6, // 图片节点
}
/// <summary>
/// 聊天节点事件类型
/// </summary>
public enum ChatNodeEvent
{
None = 0,
OpenUI = 1, // 打开UI节点
ClickLink = 2, // 点击链接节点
UseItem = 3, // 使用物品节点
Position = 4, // 位置节点
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4ec94d1a6ab744c4bdcff56d8da8e6ea
timeCreated: 1772694755

View File

@@ -0,0 +1,16 @@
using System.Runtime.Serialization;
using LightProto;
using MongoDB.Bson.Serialization.Attributes;
using Newtonsoft.Json;
namespace Fantasy
{
public partial class ChatInfoTree
{
[BsonIgnore]
[JsonIgnore]
[ProtoIgnore]
[IgnoreDataMember]
public Scene Scene { get; set; }
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 73e6060aebcd46eca52cd4086e193fb3
timeCreated: 1772694724

View File

@@ -0,0 +1,82 @@
using System.Collections.Generic;
using Fantasy;
using Fantasy.Serialize;
namespace NBF
{
public static class ChatNodeEventHelper
{
public static void Handler(Scene scene, ChatInfoNode node)
{
switch ((ChatNodeEvent)node.ChatNodeEvent)
{
case ChatNodeEvent.ClickLink:
{
ClickLinkHandler(scene, node);
return;
}
case ChatNodeEvent.OpenUI:
{
OpenUIHandler(scene, node);
return;
}
case ChatNodeEvent.Position:
{
PositionHandler(scene, node);
return;
}
case ChatNodeEvent.UseItem:
{
UseItemHandler(scene, node);
return;
}
}
}
private static void ClickLinkHandler(Scene scene, ChatInfoNode node)
{
if (node.Data == null || node.Data.Length == 0)
{
return;
}
var chatLinkNode = SerializerManager.ProtoBufHelper.Deserialize<ChatLinkNode>(node.Data);
// var chatLinkNode = scene.GetComponent<SerializerComponent>().Deserialize<ChatLinkNode>(node.Data);
// 拿到这个之后,就可以为所欲为了。
// 根据自己的逻辑和UI设计做出相应的处理。
Log.Debug($"ClickLinkHandler Link:{chatLinkNode.Link}");
}
private static void OpenUIHandler(Scene scene, ChatInfoNode node)
{
if (node.Data == null || node.Data.Length == 0)
{
return;
}
var chatOpenUINode = SerializerManager.ProtoBufHelper.Deserialize<ChatOpenUINode>(node.Data);
// 拿到这个之后,就可以为所欲为了。
// 根据自己的逻辑和UI设计做出相应的处理。
Log.Debug($"OpenUIHandler UIName:{chatOpenUINode.UIName}");
}
private static void PositionHandler(Scene scene, ChatInfoNode node)
{
if (node.Data == null || node.Data.Length == 0)
{
return;
}
var chatPositionNode = SerializerManager.ProtoBufHelper.Deserialize<ChatPositionNode>(node.Data);
// 拿到这个之后,就可以为所欲为了。
// 根据自己的逻辑和UI设计做出相应的处理。
Log.Debug(
$"PositionHandler MapName:{chatPositionNode.MapName} X:{chatPositionNode.PosX} Y:{chatPositionNode.PosY} Z:{chatPositionNode.PosZ}");
}
private static void UseItemHandler(Scene scene, ChatInfoNode node)
{
// TODO: Implement UseItemHandler
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7bcf5d11cea04d86a8a9c5c3fce31ed8
timeCreated: 1772694878

View File

@@ -0,0 +1,130 @@
using Fantasy;
using Fantasy.Network;
using Fantasy.Serialize;
namespace NBF
{
/// <summary>
/// 聊天信息节点
/// </summary>
public static class ChatNodeFactory
{
/// <summary>
/// 添加文本节点
/// </summary>
/// <param name="chatInfoTree"></param>
/// <param name="content"></param>
/// <returns></returns>
public static ChatInfoTree AddendTextNode(this ChatInfoTree chatInfoTree, string content)
{
var chatInfoNode = new ChatInfoNode()
{
ChatNodeType = (int)ChatNodeType.Text,
Content = content
};
chatInfoTree.Node.Add(chatInfoNode);
return chatInfoTree;
}
/// <summary>
/// 添加链接节点
/// </summary>
/// <param name="chatInfoTree"></param>
/// <param name="content"></param>
/// <param name="link"></param>
/// <returns></returns>
public static ChatInfoTree AddendLinkNode(this ChatInfoTree chatInfoTree, string content,string link)
{
var chatLinkNode = new ChatLinkNode()
{
Link = link
};
// var serializerComponent = chatInfoTree.Scene.GetComponent<SerializerComponent>();
var chatInfoNode = new ChatInfoNode()
{
ChatNodeType = (int)ChatNodeType.Link,
ChatNodeEvent = (int)ChatNodeEvent.ClickLink,
Content = content,
Data = SerializerManager.ProtoBufHelper.Serialize(chatLinkNode)
};
chatInfoTree.Node.Add(chatInfoNode);
return chatInfoTree;
}
/// <summary>
/// 添加图片节点
/// </summary>
/// <param name="chatInfoTree"></param>
/// <param name="content"></param>
/// <returns></returns>
public static ChatInfoTree AddendImageNode(this ChatInfoTree chatInfoTree, string content)
{
var chatInfoNode = new ChatInfoNode()
{
ChatNodeType = (int)ChatNodeType.Image,
Content = content
};
chatInfoTree.Node.Add(chatInfoNode);
return chatInfoTree;
}
/// <summary>
/// 添加打开UI节点
/// </summary>
/// <param name="chatInfoTree"></param>
/// <param name="content"></param>
/// <param name="uiName"></param>
/// <returns></returns>
public static ChatInfoTree AddendOpenUINode(this ChatInfoTree chatInfoTree, string content,string uiName)
{
var chatOpenUINode = new ChatOpenUINode()
{
UIName = uiName
};
var chatInfoNode = new ChatInfoNode()
{
ChatNodeType = (int)ChatNodeType.OpenUI,
ChatNodeEvent = (int)ChatNodeEvent.OpenUI,
Content = content,
Data = SerializerManager.ProtoBufHelper.Serialize(chatOpenUINode)
};
chatInfoTree.Node.Add(chatInfoNode);
return chatInfoTree;
}
/// <summary>
/// 添加位置节点
/// </summary>
/// <param name="chatInfoTree"></param>
/// <param name="content"></param>
/// <param name="mapName"></param>
/// <param name="mapX"></param>
/// <param name="mapY"></param>
/// <param name="mapZ"></param>
/// <returns></returns>
public static ChatInfoTree AddendPositionNode(this ChatInfoTree chatInfoTree, string content, string mapName,
float mapX, float mapY, float mapZ)
{
var chatPositionNode = new ChatPositionNode()
{
MapName = mapName,
PosX = mapX,
PosY = mapY,
PosZ = mapZ,
};
// var serializerComponent = chatInfoTree.Scene.GetComponent<SerializerComponent>();
var chatInfoNode = new ChatInfoNode()
{
ChatNodeType = (int)ChatNodeType.Position,
ChatNodeEvent = (int)ChatNodeEvent.Position,
Content = content,
Data = SerializerManager.ProtoBufHelper.Serialize(chatPositionNode)
};
chatInfoTree.Node.Add(chatInfoNode);
return chatInfoTree;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: bbf26a0aef9841f89db6182475f50e59
timeCreated: 1772694930

View File

@@ -16,14 +16,6 @@ namespace NBF
[AutoFind(Name = "back")]
public GImage back;
[AutoFind(Name = "TextLoginInfo")]
public GTextField TextLoginInfo;
[AutoFind(Name = "InputAccount")]
public GLabel InputAccount;
[AutoFind(Name = "BtnLogout")]
public GButton BtnLogout;
[AutoFind(Name = "BtnLogin")]
public GButton BtnLogin;
[AutoFind(Name = "InputMessage")]
public GLabel InputMessage;
[AutoFind(Name = "BtnSendMessage")]
@@ -34,6 +26,8 @@ namespace NBF
public GButton BtnSendWorld;
[AutoFind(Name = "ChatList")]
public GList ChatList;
[AutoFind(Name = "InputFriendId")]
public GLabel InputFriendId;
public override string[] GetDependPackages(){ return new string[] {"Common","CommonNew"}; }
public static void Show(object param = null){ UI.Inst.OpenUI<ChatTestPanel>(param); }

View File

@@ -0,0 +1,115 @@
// 本脚本只在不存在时会生成一次。已存在不会再次生成覆盖
using System;
using System.Collections.Generic;
using System.Text;
using FairyGUI;
using Fantasy;
using Fantasy.Async;
using Fantasy.Network;
using NBC;
using UnityEngine;
using Log = NBC.Log;
using UIPanel = NBC.UIPanel;
namespace NBF
{
public partial class ChatTestPanel : UIPanel
{
private List<string> _messages = new List<string>();
protected override void OnInit()
{
this.AutoAddClick(OnClick);
}
protected override void OnShow()
{
}
private void RefreshList()
{
ChatList.RemoveChildrenToPool();
foreach (var item in _messages)
{
if (ChatList.AddItemFromPool() is ChatItem chatItem)
{
chatItem.InitData(item);
}
}
}
private void OnClick(GComponent btn)
{
if (btn == BtnSendWorld)
{
OnSendMessage(InputMessage.title).Coroutine();
}
else if (btn == BtnSendFriend)
{
OnSendFriendMessage().Coroutine();
}
}
#region
private async FTask OnSendMessage(string message)
{
var tree = ChatTreeFactory.Broadcast(Game.Main);
tree = tree.AddendPositionNode(message, "勇者大陆", 121, 131, 111);
var response = (Chat2C_SendMessageResponse)await Net.Call(new C2Chat_SendMessageRequest()
{
ChatInfoTree = tree
});
if (response.ErrorCode != 0)
{
Log.Error($"发送聊天消息失败 ErrorCode:{response.ErrorCode}");
}
else
{
Log.Info("发送消息成功");
}
}
private async FTask OnSendFriendMessage()
{
var tree = ChatTreeFactory.Private();
tree.Target.Add(Convert.ToInt64(InputFriendId.text));
tree.AddendTextNode("你好欢迎来到Fantasy Chat")
.AddendLinkNode("点击这里http://www.fantasy.com.cn", "http://www.fantasy.com.cn");
var response = (Chat2C_SendMessageResponse)await Net.Call(new C2Chat_SendMessageRequest()
{
ChatInfoTree = tree
});
if (response.ErrorCode != 0)
{
Log.Error($"发送私聊消息失败 ErrorCode:{response.ErrorCode}");
}
}
public void Message(ChatInfoTree tree)
{
// _messages.Add(message);
var sb = new StringBuilder();
foreach (var chatInfoNode in tree.Node)
{
// 这里只是演示一下处理事件的效果,实际使用时,需要根据实际情况处理事件
// 明显我现在这样做的方式不是对的,应该是自己拼接一个聊天信息,然后调用这个接口来处理事件
// entryComponent.Entry.ChatNodeEventButton.onClick.RemoveAllListeners();
// entryComponent.Entry.ChatNodeEventButton.onClick.AddListener(() =>
// {
// ChatNodeEventHelper.Handler(scene, chatInfoNode);
// });
sb.Append(chatInfoNode.Content);
}
_messages.Add(sb.ToString());
RefreshList();
}
#endregion
}
}

View File

@@ -0,0 +1,121 @@
using Fantasy;
namespace NBF
{
/// <summary>
/// 创建聊天树的总入口
/// </summary>
public static class ChatTreeFactory
{
/// <summary>
/// 创建世界聊天树
/// </summary>
/// <param name="scene"></param>
/// <returns></returns>
public static ChatInfoTree World()
{
return new ChatInfoTree()
{
Scene = Game.Main,
ChatChannelType = (int)ChatChannelType.World,
};
}
/// <summary>
/// 创建私聊聊天树
/// </summary>
/// <returns></returns>
public static ChatInfoTree Private()
{
return new ChatInfoTree()
{
Scene = Game.Main,
ChatChannelType = (int)ChatChannelType.Private,
};
}
/// <summary>
/// 创建系统聊天树
/// </summary>
/// <param name="scene"></param>
/// <returns></returns>
public static ChatInfoTree System(Scene scene)
{
return new ChatInfoTree()
{
Scene = scene,
ChatChannelType = (int)ChatChannelType.System,
};
}
/// <summary>
/// 创建公广播聊天树
/// </summary>
/// <param name="scene"></param>
/// <returns></returns>
public static ChatInfoTree Broadcast(Scene scene)
{
return new ChatInfoTree()
{
Scene = scene,
ChatChannelType = (int)ChatChannelType.Broadcast,
};
}
/// <summary>
/// 创建公告聊天树
/// </summary>
/// <param name="scene"></param>
/// <returns></returns>
public static ChatInfoTree Notice(Scene scene)
{
return new ChatInfoTree()
{
Scene = scene,
ChatChannelType = (int)ChatChannelType.Notice,
};
}
/// <summary>
/// 创建队伍聊天树
/// </summary>
/// <param name="scene"></param>
/// <returns></returns>
public static ChatInfoTree Team(Scene scene)
{
return new ChatInfoTree()
{
Scene = scene,
ChatChannelType = (int)ChatChannelType.Team,
};
}
/// <summary>
/// 创建附近人聊天树
/// </summary>
/// <param name="scene"></param>
/// <returns></returns>
public static ChatInfoTree Near(Scene scene)
{
return new ChatInfoTree()
{
Scene = scene,
ChatChannelType = (int)ChatChannelType.Near,
};
}
/// <summary>
/// 创建当前地图聊天树
/// </summary>
/// <param name="scene"></param>
/// <returns></returns>
public static ChatInfoTree CurrentMap(Scene scene)
{
return new ChatInfoTree()
{
Scene = scene,
ChatChannelType = (int)ChatChannelType.CurrentMap,
};
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f028a29029d74cbf8c264a7d4a245ecf
timeCreated: 1772694684

View File

@@ -1,179 +0,0 @@
// 本脚本只在不存在时会生成一次。已存在不会再次生成覆盖
using System.Collections.Generic;
using FairyGUI;
using Fantasy;
using Fantasy.Async;
using Fantasy.Network;
using NBC;
using UnityEngine;
using Log = NBC.Log;
using UIPanel = NBC.UIPanel;
namespace NBF
{
public partial class ChatTestPanel : UIPanel
{
private List<string> _messages = new List<string>();
protected override void OnInit()
{
this.AutoAddClick(OnClick);
}
protected override void OnShow()
{
SetLoginState(false);
}
private void RefreshList()
{
ChatList.RemoveChildrenToPool();
foreach (var item in _messages)
{
if(ChatList.AddItemFromPool() is ChatItem chatItem)
{
chatItem.InitData(item);
}
}
}
private void OnClick(GComponent btn)
{
if (btn == BtnLogin)
{
if (string.IsNullOrEmpty(InputAccount.text))
{
Notices.Info("没有输入账号密码");
return;
}
OnLoginButtonClick(InputAccount.text).Coroutine();
}
else if (btn == BtnSendWorld)
{
OnSendMessage(InputMessage.title).Coroutine();
}
}
private void SetLoginState(bool isLogin = false, string account = "")
{
if (isLogin)
{
BtnLogin.visible = false;
BtnLogout.visible = true;
TextLoginInfo.text = account;
}
else
{
BtnLogin.visible = true;
BtnLogout.visible = false;
}
}
#region
private Session _session;
private async FTask OnLoginButtonClick(string account)
{
// NBC.Platform.Unity.Entry.Initialize(GetType().Assembly);
// 根据用户名来选择目标的鉴权服务器
// 根据鉴权服务器地址来创建一个新的网络会话
_session = SessionHelper.CreateSession(Game.Main, "127.0.0.1:20001", OnConnectComplete,
OnConnectFail,
OnConnectDisconnect);
var acc = account;
// 发送登录的请求给服务器
var response = (A2C_LoginResponse)await _session.Call(new C2A_LoginRequest()
{
Username = acc,
Password = acc,
LoginType = 1
});
if (response.ErrorCode != 0)
{
Log.Error($"登录发生错误{response.ErrorCode}");
return;
}
if (!Game.Main.GetComponent<JWTParseComponent>().Parse(response.ToKen, out var payload))
{
return;
}
// 根据ToKen返回的Address登录到Gate服务器
_session = SessionHelper.CreateSession(Game.Main, payload.Address, OnConnectComplete, OnConnectFail,
OnConnectDisconnect);
// 发送登录请求到Gate服务器
var loginResponse = (G2C_LoginResponse)await _session.Call(new C2G_LoginRequest()
{
ToKen = response.ToKen
});
if (loginResponse.ErrorCode != 0)
{
Log.Error($"登录发生错误{loginResponse.ErrorCode}");
return;
}
SetLoginState(true, InputAccount.text);
Log.Info(
$"登录到Gate服务器成功ErrorCode:{loginResponse.ErrorCode}");
// Log.Debug(
// $"登录到Gate服务器成功LoginTime:{loginResponse.GameAccountInfo.LoginTime} CreateTime:{loginResponse.GameAccountInfo.CreateTime}");
// var getResponse = (G2C_GetAccountInfoResponse)await _session.Call(new C2G_GetAccountInfoRequest());
// var gameAcc = getResponse.GameAccountInfo;
// Log.Info($"gameAcc LoginTime:{gameAcc.LoginTime} CreateTime:{gameAcc.CreateTime}");
// getResponse.GameAccountInfo.LoginTime.ToString()
}
private void OnConnectComplete()
{
Log.Info("连接成功");
// 添加心跳组件给Session。
// Start(2000)就是2000毫秒。
_session.AddComponent<SessionHeartbeatComponent>().Start(5000);
}
private void OnConnectFail()
{
Log.Info("连接失败");
SetLoginState(false);
}
private void OnConnectDisconnect()
{
Log.Info("连接断开");
SetLoginState(false);
}
#endregion
#region
private async FTask OnSendMessage(string message)
{
var messageResponse = (S2C_SendMessageResponse)await _session.Call(new C2S_SendMessageRequest()
{
Message = message
});
if (messageResponse.ErrorCode != 0)
{
Notices.Info($"发送消息失败code={messageResponse.ErrorCode}");
}
}
public void Message(string message)
{
_messages.Add(message);
RefreshList();
}
#endregion
}
}

View File

@@ -32,7 +32,9 @@ namespace NBF
{
await LoginHelper.Login(InputAccount.text);
await Fishing.Instance.Go(RoleModel.Instance.Info.MapId);
// await Fishing.Instance.Go(RoleModel.Instance.Info.MapId);
ChatTestPanel.Show();
// FishingShopPanel.Show();