Esempio n. 1
0
        /// <summary>
        /// 客户端登陆服务器,采用简单模式,就只有2个参数
        /// </summary>
        /// <param name="netstate"></param>
        /// <param name="accountName"></param>
        /// <param name="password"></param>
        /// <param name="serverId"></param>
        public void OnLoginServer(NetState netstate, string accountName, string password, int serverId = 0)
        {
            var account = WorldEntityManager.AccountNames.GetValue(accountName);
            if (account == null)
            {
                //  账号不存在,则创建账号
                account = new Account
                {
                    Name = accountName,
                    Password = password,
                    CreateTime = OneServer.NowTime,
                    ServerId = serverId,
                    Id = WorldSeqGen.AccountSeq.NextSeq(),
                };

                WorldEntityManager.AccountNames.SetValue(accountName, account);
                WorldEntityManager.Accounts.SetValue(account.Id, account);

                ClientProxy.Login.LoginServerResult(netstate, LoginServerResult.Success);

                //  保存到数据
            }
            else
            {
                if (account.Password != password)
                {
                    //  账户存在,但是密码验证失败,返回错误
                    ClientProxy.Login.LoginServerResult(netstate, LoginServerResult.PassOrAccountError);
                    return;
                }
            }

            netstate.IsVerifyLogin = true;
        }
Esempio n. 2
0
        /// <summary>
        /// 客户端登陆服务器,采用简单模式,就只有2个参数
        /// </summary>
        /// <param name="netstate"></param>
        /// <param name="accountName"></param>
        /// <param name="password"></param>
        /// <param name="serverId"></param>
        public void OnLoginServer(NetState netstate, string accountName, string password, int serverId = 0)
        {
            var account = WorldEntityManager.AccountNames.GetValue(accountName);
            if (account == null)
            {
                //  账号不存在,则创建账号
                account = new Account
                {
                    Name = accountName,
                    Password = password,
                    CreateTime = OneServer.NowTime,
                    ServerId = serverId,
                    Id = WorldSeqGen.AccountSeq.NextSeq(),
                };

                WorldEntityManager.AccountNames.SetValue(accountName, account);
                WorldEntityManager.Accounts.SetValue(account.Id, account);

                //  保存到数据,demo阶段,已xml的方式写入文件里,减少对第三方系统的依赖
                DB.GameDB.InsertEntity(account);
            }
            else
            {
                if (account.Password != password)
                {
                    //  账户存在,但是密码验证失败,返回错误
                    ClientProxy.Login.LoginServerResult(netstate, LoginServerResult.PassOrAccountError);
                    return;
                }

               }

            netstate.BizId = account.Id;
            netstate.IsVerifyLogin = true;

            //  先瞅瞅内存里玩家数据有没有被缓存着
            var player = WorldEntityManager.PlayerCache.GetEntity(account.Id);

            if (player == null)
            {
                player = DB.GameDB.LoadEntity<Player>(account.Id);
                if(player != null)
                    WorldEntityManager.PlayerCache.AddOrReplace(player);
            }

            var isCreatePlayer = player != null;
            ClientProxy.Login.LoginServerResult(netstate, LoginServerResult.Success, isCreatePlayer);
            if (isCreatePlayer)
            {
                if (player.NetState != null)
                {
                    //  玩家重复登陆,需要将之前的玩家连接关闭
                    var oldCon = player.NetState;
                    oldCon.IsVerifyLogin = false;
                    oldCon.Player = null;
                    oldCon.BizId = 0;
                    oldCon.NetSocket.CloseSocket();
                }

                //  给玩家绑定网络连接账号
                player.NetState = netstate;
                netstate.Player = player;
                netstate.IsVerifyLogin = true;

                //  创建过角色,这里就发送玩家的数据到客户端
                PlayerEnterGame(player);

            }
        }