任务18:在大厅获取用户信息显示玩家昵称和金钱数

如果前面有反复多做几次,比较熟悉和尽量多的领会的话,从本节开始,你应该会觉得比较轻松了容易起来了。

服务端增加获取用户信息的消息指令、消息体和返回用户信息的Handler

\Server\Hotfix\Landlords\Handler\Gate\A1001_GetUserInfo_Handler.cs

using System;
using ETModel;

namespace ETHotfix
{
    //客户端已知玩家UserID 尝试读取玩家资料
    //消息内容 玩家的属性 世界位置 任务栏 技能栏 装备栏 背包
    [MessageHandler(AppType.Gate)]
    public class A1001_GetUserInfo_Handler : AMRpcHandler<A1001_GetUserInfo_C2G, A1001_GetUserInfo_G2C>
    {
        protected override async ETTask Run(Session session, A1001_GetUserInfo_C2G request,A1001_GetUserInfo_G2C response,Action reply)
        {
            try
            {
                //验证Session
                if (!GateHelper.SignSession(session))
                {
                    response.Error = ErrorCode.ERR_UserNotOnline;
                    reply();
                    return;
                }

                //获取玩家对象
                User user = session.GetComponent<SessionUserComponent>().User;
                DBProxyComponent dbProxyComponent = Game.Scene.GetComponent<DBProxyComponent>();
                UserInfo userInfo = await dbProxyComponent.Query<UserInfo>(user.UserID);

                response.UserName = userInfo.UserName;
                response.Money = userInfo.Money;

                reply();
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
    }
}

增加获取用户信息的消息定义

\Proto\HotfixMessage.proto

//获取用户信息
message A1001_GetUserInfo_C2G // IRequest
{
    int32 RpcId = 90;
}

//返回用户信息
//RpcID,Error,Message是actor RPC消息固有的
message A1001_GetUserInfo_G2C // IResponse
{
    int32 RpcId = 90;
    int32 Error = 91;
    string Message = 92;
    string UserName = 1;
    int32 Level = 2;
    int64 Money = 3;
}

更新 .proto 文件后,到unity中找到菜单点Tools>Ptoto2CS工具,确认下重新生成的消息体类与指令脚本。(记得复制消息文件到服务端哦!)

前端大厅界面预制体中增加Money的显示文本

前端大厅UI组件中添加请求与显示用户信息 

\Assets\Model\Landlords\LandUI\LandLoggy\LandLobbyComponent.cs

using System;
using ETModel;
using UnityEngine;
using UnityEngine.UI;

namespace ETModel
{
    [ObjectSystem]
    public class LandLobbyComponentAwakeSystem : AwakeSystem<LandLobbyComponent>
    {
        public override void Awake(LandLobbyComponent self)
        {
            self.Awake();
        }
    }

    /// <summary>
    /// 大厅界面组件
    /// </summary>
    public class LandLobbyComponent : Component
    {
        //提示文本
        public Text prompt;
        //玩家名称
        private Text name;
        //玩家金钱
        private Text money;

        public bool isMatching;

        public async void Awake()
        {
            ReferenceCollector rc = this.GetParent<UI>().GameObject.GetComponent<ReferenceCollector>();

            prompt = rc.Get<GameObject>("Prompt").GetComponent<Text>();
            name = rc.Get<GameObject>("Name").GetComponent<Text>();
            money = rc.Get<GameObject>("Money").GetComponent<Text>();

            //获取玩家数据
            A1001_GetUserInfo_C2G GetUserInfo_Req = new A1001_GetUserInfo_C2G();
            A1001_GetUserInfo_G2C GetUserInfo_Ack = (A1001_GetUserInfo_G2C)await SessionComponent.Instance.Session.Call(GetUserInfo_Req);

            //显示用户名和用户等级
            name.text = GetUserInfo_Ack.UserName;
            money.text = GetUserInfo_Ack.Money.ToString();
            
            //添加进入房间匹配事件
            //...

            //添加新的匹配目标
            //...

        }
    }
}

不能是写死在前端哦,得是从服务器获取到并显示出来

本节扩展练习(务必自己完成,遇到问题群里问)

1 前端大厅界面预制体中增加Level 等级的显示文本

2 前端大厅UI组件中添加显示用户等级信息

3 前后端获取用户信息消息体中增加一个Level字段(上面提供的消息体中已经有,如何增加减少字段并使用工具自动生成消息体,下节课讲)

4 后端返回用户信息的Handler中增加返回Level字段 \Server\Hotfix\Landlords\Handler\Gate\A1001_GetUserInfo_Handler.cs

response.UserName = userInfo.UserName;
response.Money = userInfo.Money;
//...增加返回Level字段

提示:用户信息实体类已经包含了Level字段,并且Awake初始时已经默认1级。下图中可以看到默认也有1000金钱了(去看看注册账号的代码,用户完成注册时用户初始数据已经保存到了数据库)。

\Server\ET.Core\Landlords\Entity\DB\UserInfo.cs

思考:如果你自己想添加一些字段,如何做,如何修改后端返回,前端获取,以及消息体定义中如何修改?大家可以自行练习掌握!

posted @ 2023-02-03 17:03  Domefy  阅读(41)  评论(0)    收藏  举报