コード例 #1
0
 public static void OnSingleRanklistRequested2(Session session, FunMessage message)
 {
     Leaderboard.GetAndSendRankTop8(session, Session.EncodingScheme.kProtobufEncoding, true);
 }
コード例 #2
0
 public static void OnSingleRanklistRequested(Session session, JObject message)
 {
     Leaderboard.GetAndSendRankTop8(session, Session.EncodingScheme.kJsonEncoding, true);
 }
コード例 #3
0
        // 세션을 정리합니다.
        public static void FreeUser(Session session, Session.EncodingScheme encoding)
        {
            // 유저를 정리하기 위한 Context 를 읽어옵니다.
            string id;
            string opponent_id;

            session.GetFromContext("id", out id);
            session.GetFromContext("opponent", out opponent_id);

            // Session Context 를 초기화 합니다.
            session.Context = new JObject();

            // 로그아웃하고 세션을 종료합니다.
            if (id != string.Empty)
            {
                AccountManager.LogoutCallback logout_cb = (string param_account_id,
                                                           Session param_session,
                                                           bool param_result) => {
                    Log.InfoIf(param_result, "Logged out(local) by session close: id={0}", param_account_id);
                };
                AccountManager.SetLoggedOutAsync(id, logout_cb);
            }

            // 대전 상대가 있는 경우, 상대가 승리한 것으로 처리하고 로비서버로 보냅니다.
            if (opponent_id == string.Empty)
            {
                return;
            }
            Session opponent_session = AccountManager.FindLocalSession(opponent_id);

            if (opponent_session == null)
            {
                return;
            }

            Event.EventFunction update = () => {
                User user          = User.FetchById(id);
                User opponent_user = User.FetchById(opponent_id);

                Event.AssertNoRollback();

                // 편의상 아래 함수에서 ORM 데이터 업데이트 처리도 합니다.
                Leaderboard.OnWin(opponent_user);
                Leaderboard.OnLose(user);

                if (encoding == Session.EncodingScheme.kJsonEncoding)
                {
                    opponent_session.SendMessage("result", Utility.MakeResponse("win"));
                }
                else
                {
                    Log.Assert(encoding == Session.EncodingScheme.kProtobufEncoding);
                    FunMessage        funmsg = new FunMessage();
                    GameResultMessage msg    = new GameResultMessage();
                    msg.result = "win";
                    funmsg.AppendExtension_game_result(msg);
                    opponent_session.SendMessage("result", funmsg);
                }

                Common.Redirect(opponent_session, "lobby");
            };

            Event.Invoke(update);
        }
コード例 #4
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);
            }
        }
コード例 #5
0
        public static void OnHandleResult(Session session, Session.EncodingScheme encoding)
        {
            // 패배한 쪽만 result를 보내도록 되어있습니다.

            // 내 아이디를 가져옵니다.
            string id;

            session.GetFromContext("id", out id);

            // 상대방의 아이디와 세션을 가져옵니다.
            string opponent_id;

            Log.Verify(session.GetFromContext("opponent", out opponent_id));
            Session opponent_session = AccountManager.FindLocalSession(opponent_id);

            User user          = User.FetchById(id);
            User opponent_user = User.FetchById(opponent_id);

            Event.AssertNoRollback();

            // 편의상 아래 함수에서 ORM 데이터 업데이트 처리도 합니다.
            Leaderboard.OnWin(opponent_user);
            Leaderboard.OnLose(user);

            // 상대에게 승리했음을 알립니다.
            if (opponent_session != null)
            {
                if (encoding == Session.EncodingScheme.kJsonEncoding)
                {
                    opponent_session.SendMessage("result", Utility.MakeResponse("win"));
                }
                else
                {
                    Log.Assert(encoding == Session.EncodingScheme.kProtobufEncoding);
                    FunMessage        funmsg = new FunMessage();
                    GameResultMessage msg    = new GameResultMessage();
                    msg.result = "win";
                    funmsg.AppendExtension_game_result(msg);
                    opponent_session.SendMessage("result", funmsg);
                }

                opponent_session.DeleteFromContext("opponent");
                Common.Redirect(opponent_session, "lobby");
            }

            // 패배 확인 메세지를 보냅니다.
            if (encoding == Session.EncodingScheme.kJsonEncoding)
            {
                session.SendMessage("result", Utility.MakeResponse("lose"));
            }
            else
            {
                Log.Assert(encoding == Session.EncodingScheme.kProtobufEncoding);
                FunMessage        funmsg = new FunMessage();
                GameResultMessage msg    = new GameResultMessage();
                msg.result = "lose";
                funmsg.AppendExtension_game_result(msg);
                session.SendMessage("result", funmsg);
            }
            session.DeleteFromContext("opponent");
            Common.Redirect(session, "lobby");
        }