Esempio n. 1
0
 // 새 클라이언트가 접속하여 세션이 열릴 때 불리는 함수
 public static void OnSessionOpened(Session session)
 {
     // 세션 접속  Activity Log 를 남깁니다.
     ActivityLog.SessionOpened(session.Id.ToString(), WallClock.Now);
 }
Esempio n. 2
0
        private static void OnLogin_Completed(string account_id, Session session, bool success, Session.EncodingScheme encoding)
        {
            if (!success)
            {
                // 로그인에 실패 응답을 보냅니다. 중복 로그인이 원인입니다.
                // (1. 같은 ID 로 이미 다른 Session 이 로그인 했거나,
                //  2. 이 Session 이 이미 로그인 되어 있는 경우)
                Log.Info("Failed to login: id={0}", account_id);
                if (encoding == Session.EncodingScheme.kJsonEncoding)
                {
                    session.SendMessage("login",
                                        Utility.MakeResponse("nop", "failed to login"),
                                        Session.Encryption.kDefault);
                }
                else
                {
                    Log.Assert(encoding == Session.EncodingScheme.kProtobufEncoding);
                    FunMessage      funmsg = new FunMessage();
                    LobbyLoginReply reply  = new LobbyLoginReply();
                    reply.result = "nop";
                    reply.msg    = "failed to login";
                    funmsg.AppendExtension_lobby_login_repl(reply);
                    session.SendMessage("login", funmsg);
                }

                // 아래 로그아웃 처리를 한 후 자동으로 로그인 시킬 수 있지만
                // 일단 클라이언트에서 다시 시도하도록 합니다.

                // 1. 이 ID 의 로그인을 풀어버립니다.(로그아웃)
                AccountManager.LogoutCallback logout_cb = (string param_account_id, Session param_session, bool param_success) => {
                    if (!param_success)
                    {
                        return;
                    }
                    if (param_session != null)
                    {
                        // 같은 서버에 로그인 되어 있었습니다.
                        Log.Info("Logged out(local) by duplicated login request: id={0}", param_account_id);
                        session.Close();
                    }
                    else
                    {
                        // 다른 서버에 로그인 되어 있었습니다.
                        // 해당 서버의 OnLoggedOutRemotely() 에서 처리합니다.
                        Log.Info("Logged out(remote) by duplicated login request: id={0}", param_account_id);
                    }
                };

                AccountManager.SetLoggedOutGlobalAsync(account_id, new AccountManager.LogoutCallback(logout_cb));

                // 2. 이 Session 의 로그인을 풀어버립니다.(로그아웃)
                string account_id_logged_in = AccountManager.FindLocalAccount(session);
                if (account_id_logged_in != string.Empty)
                {
                    // OnSessionClosed 에서 처리합니다.
                    Log.Info("Close session. by duplicated login request: id={0}", account_id);
                    session.Close();
                }

                return;
            }

            // User Object 를 가져옵니다.
            User user = User.FetchById(account_id);

            if (user == null)
            {
                // 새로운 유저를 생성합니다.
                user = User.Create(account_id);
                Log.Info("Registered new user: id={0}", account_id);
            }

            Event.AssertNoRollback();

            Log.Info("Succeed to login: id={0}", account_id);

            // 로그인 Activitiy Log 를 남깁니다.
            ActivityLog.PlayerLoggedIn(session.Id.ToString(), account_id, WallClock.Now);

            // Session 에 Login 한 ID 를 저장합니다.
            session.AddToContext("id", account_id);

            if (encoding == Session.EncodingScheme.kJsonEncoding)
            {
                JObject response = Utility.MakeResponse("ok");
                response ["id"]              = account_id;
                response ["winCount"]        = user.GetWinCount();
                response ["loseCount"]       = user.GetLoseCount();
                response ["curRecord"]       = Leaderboard.GetRecord(account_id);
                response ["singleWinCount"]  = user.GetWinCountSingle();
                response ["singleLoseCount"] = user.GetLoseCountSingle();
                response ["singleCurRecord"] = Leaderboard.GetRecord(account_id, true);

                session.SendMessage("login", response, Session.Encryption.kDefault);
            }
            else
            {
                Log.Assert(encoding == Session.EncodingScheme.kProtobufEncoding);
                FunMessage      funmsg = new FunMessage();
                LobbyLoginReply reply  = new LobbyLoginReply();
                reply.result            = "ok";
                reply.id                = account_id;
                reply.win_count         = (int)user.GetWinCount();
                reply.lose_count        = (int)user.GetLoseCount();
                reply.cur_record        = (int)Leaderboard.GetRecord(account_id);
                reply.win_count_single  = (int)user.GetWinCountSingle();
                reply.lose_count_single = (int)user.GetLoseCountSingle();
                reply.cur_record_single = Leaderboard.GetRecord(account_id, true);
                funmsg.AppendExtension_lobby_login_repl(reply);

                session.SendMessage("login", funmsg, Session.Encryption.kDefault);
            }
        }