Exemple #1
0
        // 用户登录请求
        void OnUserLoginMsg(Connection conn, IReadableBuffer data, IWriteableBuffer buff, Action end)
        {
            var uid = data.ReadString();

            var deviceModel = "";

            // 检查版本

            var isNewVersion = false;
            var version      = "";
            var buildNo      = "";
            var platform     = "";

            if (data.Available != 0)
            {
                version  = data.ReadString();
                platform = data.ReadString();
            }

            if (data.Available != 0)
            {
                deviceModel = data.ReadString();
                buildNo     = data.ReadString();
            }

            isNewVersion = (version == SrvVersion) && (buildNo == SrvBuildNo);

            buff.Write(isNewVersion);
            if (!isNewVersion)
            {
                if (platform == "IOS")
                {
                    buff.Write("https://www.apple.com");
                }
                else if (platform == "ANDROID")
                {
                    buff.Write("https://www.google.com");
                }
                else
                {
                    buff.Write("https://www.baidu.com");
                }

                end();
                return;
            }

            UC.Retrieve(uid, (usr) =>
            {
                if (SC[uid] != null)
                {
                    KickOut(uid);
                    end();
                    return;
                }

                var isNew = usr == null;
                if (isNew) // 用户不存在就创建新的
                {
                    usr                  = new User();
                    usr.ID               = uid;
                    usr.Info             = new UserInfo();
                    usr.Info.DeviceModel = deviceModel;
                    UC.AddNew(usr);

                    UserManager.SetDefaultInfo(usr);
                }

                // 创建会话
                var s   = new Session();
                s.Usr   = usr;
                s.Conn  = conn;
                SC[uid] = s;

                // 登录日志
                SrvLogger.Log(new LoginInfo(uid, usr.Info.Name, conn.GetIP(), isNew));

                BeforeUserLogin.SC(s, isNew);

                // 通知登录成功
                buff.Write(true);
                buff.Write(usr.Info);

                // 问卷调查

                var totalCount = usr.Info.WinCount + usr.Info.LoseCount;
                if (totalCount >= 1 && totalCount < 5)
                {
                    buff.Write("1");
                    end();
                    OnUserLogin.SC(s, isNew);
                }
                else if (totalCount >= 5)
                {
                    QRC.Retrieve("1" + s.Usr.ID, (questionnaire) =>
                    {
                        if (questionnaire == null)
                        {
                            buff.Write("1");
                            end();
                            OnUserLogin.SC(s, isNew);
                        }
                        else
                        {
                            buff.Write("2");
                            end();
                            OnUserLogin.SC(s, isNew);
                        }
                    });
                }
                else
                {
                    buff.Write("0");
                    end();
                    OnUserLogin.SC(s, isNew);
                }
            });
        }