C#服务端的微信小游戏——多人在线角色扮演(十一)

C#服务端的微信小游戏——多人在线角色扮演(十一)

账号是现实世界对用户的描述,而角色则是游戏世界当中用户的体现。
——茂叔

账号

上一篇,我们利用微信的api完成了用户的登录工作,那现在,我们来处理用户的账户和角色信息。
首先,我们在type.cs里面为用户的账户建立一个类。

public struct Account
    {
   
   
        public uint AID;//账户的编号
        public string openid;//微信的标识
        public string nickname;
        public DateTime CreateTime;
        public DateTime LastLogin;
    }

很简单吧。然后我们在GameServer里面去定义一个账号池,并在构造函数初始化,用来暂时储存账号信息。

……
public List<Account> AccountPool;

public GameServer(MainForm monitor)
        {
   
   
            Status = 0;
            Monitor = monitor;
            LOG("GameServer创建成功");
            gGame = new Game(LOG);
            gGame.Name = "像素寒冷";

            AccountPool = new List<Account>();

……

然后在GameServer类的ParseClientCommand方法里面,对GameCommand.LOGIN指令做一些修改,检查有没有对应openid的账号,没有就立即创建一个放进我们账号池里面。然后去取得对应账号的用户角色回传给客户端。

……
case GameCommand.LOGIN:
                    {
   
   
                        string requeststr = "https://api.weixin.qq.com/sns/jscode2session?appid=" + G.AppID + "&secret=" + G.Secret + "&js_code=" + data + "&grant_type=authorization_code";
                        HttpWebRequest wxRequest = (HttpWebRequest)WebRequest.Create(requeststr);
                        wxRequest.Method = "GET";
                        wxRequest.Timeout = 6000;
                        HttpWebResponse wxResponse = (HttpWebResponse)wxRequest.GetResponse();
                        Stream stream = wxResponse.GetResponseStream();
                        StreamReader reader = new StreamReader(stream);
                        string res = reader.ReadToEnd();
                        stream.Close();
                        reader.Close();

                        JObject json = JObject.Parse(res);
                        if (json.TryGetValue("openid", out JToken openid))
                        {
   
   
                            Account account;
                            lock (AccountPool)
                            {
   
   
                                account = AccountPool.Find(x => x.openid == openid.ToString());
                                if (account.openid == null)
                                {
   
   
                                    account = new Account()
                                    {
   
   
                                        AID = (uint)G.RND(10000000, 99999999),
                                        openid = openid.ToString(),
                                        CreateTime = DateTime.Now

                                    };

                                    while (AccountPool.Exists(x => x.AID == account.AID))
                                    {
   
   
                                        account.AID = (uint)G.RND(10000000, 99999999);
                                    }
                                    AccountPool.Add(account);
                                }
                                account.LastLogin = DateTime.Now;
                            }
                            JObject playerjson=gGame.GetPlayerJsonWithAccountID(account.AID);
                            ((GameConnection)connection
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值