Пример #1
0
        private static void CancelMatchmaking(Session session, Session.EncodingScheme encoding)
        {
            // 로그인 한 Id 를 가져옵니다.
            string id;

            if (!session.GetFromContext("id", out id))
            {
                Log.Warning("Failed to cancel matchmaking. Not logged in.");
                if (encoding == Session.EncodingScheme.kJsonEncoding)
                {
                    session.SendMessage("error", Utility.MakeResponse("fail", "not logged in"));
                }
                else
                {
                    Log.Assert(encoding == Session.EncodingScheme.kProtobufEncoding);
                    FunMessage       funmsg = new FunMessage();
                    PongErrorMessage msg    = new PongErrorMessage();
                    msg.result = "fail";
                    msg.msg    = "not logged in";
                    funmsg.AppendExtension_pong_error(msg);
                    session.SendMessage("error", funmsg);
                }
                return;
            }

            // 매치메이킹 취소 상태로 변경합니다.
            session.AddToContext("matching", "cancel");

            funapi.Matchmaking.Client.CancelCallback cancel_cb = (string player_id,
                                                                  funapi.Matchmaking.CancelResult result) => {
                if (encoding == Session.EncodingScheme.kJsonEncoding)
                {
                    JObject response = new JObject();

                    if (result == funapi.Matchmaking.CancelResult.kSuccess)
                    {
                        Log.Info("Succeed to cancel matchmaking: id={0}", player_id);
                        response = Utility.MakeResponse("Cancel");
                    }
                    else if (result == funapi.Matchmaking.CancelResult.kNoRequest)
                    {
                        response = Utility.MakeResponse("NoRequest");
                    }
                    else
                    {
                        response = Utility.MakeResponse("Error");
                    }

                    session.SendMessage("match", response, Session.Encryption.kDefault);
                }
                else
                {
                    Log.Assert(encoding == Session.EncodingScheme.kProtobufEncoding);
                    FunMessage      funmsg = new FunMessage();
                    LobbyMatchReply reply  = new LobbyMatchReply();

                    if (result == funapi.Matchmaking.CancelResult.kSuccess)
                    {
                        Log.Info("Succeed to cancel matchmaking: id={0}", player_id);
                        reply.result = "Cancel";
                    }
                    else if (result == funapi.Matchmaking.CancelResult.kNoRequest)
                    {
                        reply.result = "NoRequest";
                    }
                    else
                    {
                        reply.result = "Error";
                    }

                    funmsg.AppendExtension_lobby_match_repl(reply);
                    session.SendMessage("match", funmsg, Session.Encryption.kDefault);
                }
            };

            funapi.Matchmaking.Client.Cancel((int)MatchmakingType.kMatch1vs1, id, (_1, _2) => { cancel_cb(_1, _2); });
        }
Пример #2
0
 public static void AppendExtension_lobby_match_repl(this FunMessage message, LobbyMatchReply value)
 {
     ProtoBuf.Extensible.AppendValue(message, (int)ExtendedMessageFieldNumber.FunMessage_lobby_match_repl, value);
 }
Пример #3
0
        private static void StartMatchmaking(Session session, Session.EncodingScheme encoding)
        {
            // 로그인 한 Id 를 가져옵니다.
            string id;

            if (!session.GetFromContext("id", out id))
            {
                Log.Warning("Failed to request matchmaking. Not logged in.");
                if (encoding == Session.EncodingScheme.kJsonEncoding)
                {
                    session.SendMessage("error", Utility.MakeResponse("fail", "not logged in"));
                }
                else
                {
                    Log.Assert(encoding == Session.EncodingScheme.kProtobufEncoding);
                    FunMessage       funmsg = new FunMessage();
                    PongErrorMessage msg    = new PongErrorMessage();
                    msg.result = "fail";
                    msg.msg    = "not logged in";
                    funmsg.AppendExtension_pong_error(msg);
                    session.SendMessage("error", funmsg);
                }
                return;
            }

            funapi.Matchmaking.Client.MatchCallback match_cb = (string player_id, funapi.Matchmaking.Match match,
                                                                funapi.Matchmaking.MatchResult result) => {
                JObject         response = new JObject();
                LobbyMatchReply reply    = new LobbyMatchReply();

                if (result == funapi.Matchmaking.MatchResult.kSuccess)
                {
                    // Matchmaking 에 성공했습니다.
                    Log.Info("Succeed in matchmaking: id={0}", player_id);

                    string player_a_id = Utility.ReadStringFromJsonObject(match.Context, "A");
                    string player_b_id = Utility.ReadStringFromJsonObject(match.Context, "B");
                    Log.Assert(player_a_id != null && player_b_id != null);

                    string opponent_id;
                    if (player_a_id == player_id)
                    {
                        opponent_id = player_b_id;
                    }
                    else
                    {
                        opponent_id = player_a_id;
                    }

                    session.AddToContext("opponent", opponent_id);
                    session.AddToContext("matching", "done");
                    session.AddToContext("ready", 0);

                    if (encoding == Session.EncodingScheme.kJsonEncoding)
                    {
                        response      = Utility.MakeResponse("Success");
                        response["A"] = player_a_id;
                        response["B"] = player_b_id;
                    }
                    else
                    {
                        Log.Assert(encoding == Session.EncodingScheme.kProtobufEncoding);
                        reply.result  = "Success";
                        reply.player1 = player_a_id;
                        reply.player2 = player_b_id;
                    }
                    // 유저를 Game 서버로 보냅니다.
                    Common.Redirect(session, "game");
                }
                else if (result == funapi.Matchmaking.MatchResult.kAlreadyRequested)
                {
                    // Matchmaking 요청을 중복으로 보냈습니다.
                    Log.Info("Failed in matchmaking. Already requested: id={0}", player_id);
                    session.AddToContext("matching", "failed");
                    if (encoding == Session.EncodingScheme.kJsonEncoding)
                    {
                        response = Utility.MakeResponse("AlreadyRequested");
                    }
                    else
                    {
                        Log.Assert(encoding == Session.EncodingScheme.kProtobufEncoding);
                        reply.result = "AlreadyRequested";
                    }
                }
                else if (result == funapi.Matchmaking.MatchResult.kTimeout)
                {
                    // Matchmaking 처리가 시간 초과되었습니다.
                    Log.Info("Failed in matchmaking. Timeout: id={0}", player_id);
                    session.AddToContext("matching", "failed");
                    if (encoding == Session.EncodingScheme.kJsonEncoding)
                    {
                        response = Utility.MakeResponse("Timeout");
                    }
                    else
                    {
                        Log.Assert(encoding == Session.EncodingScheme.kProtobufEncoding);
                        reply.result = "Timeout";
                    }
                }
                else
                {
                    // Matchmaking 에 오류가 발생했습니다.
                    Log.Error("Failed in matchmaking. Error: id={0}", player_id);
                    session.AddToContext("matching", "failed");
                    if (encoding == Session.EncodingScheme.kJsonEncoding)
                    {
                        response = Utility.MakeResponse("Error");
                    }
                    else
                    {
                        Log.Assert(encoding == Session.EncodingScheme.kProtobufEncoding);
                        reply.result = "Error";
                    }
                }

                if (encoding == Session.EncodingScheme.kJsonEncoding)
                {
                    session.SendMessage("match", response, Session.Encryption.kDefault);
                }
                else
                {
                    FunMessage funmsg = new FunMessage();
                    funmsg.AppendExtension_lobby_match_repl(reply);
                    session.SendMessage("match", funmsg, Session.Encryption.kDefault);
                }
            };

            // 빈 Player Context 를 만듭니다. 지금 구현에서는 Matchmaking 서버가
            // 조건 없이 Matching 합니다. Level 등의 조건으로 Matching 하려면
            // 여기에 Level 등의 Matching 에 필요한 정보를 넣습니다.
            JObject player_ctxt = new JObject();

            // Matchmaking 을 요청합니다.
            funapi.Matchmaking.Client.Start(
                (int)MatchmakingType.kMatch1vs1, id, player_ctxt, new funapi.Matchmaking.Client.MatchCallback(match_cb),
                funapi.Matchmaking.Client.TargetServerSelection.kMostNumberOfPlayers, null, kMatchmakingTimeout);
        }
Пример #4
0
 public static bool TryGetExtension_lobby_match_repl(this FunMessage message, out LobbyMatchReply value)
 {
     return(ProtoBuf.Extensible.TryGetValue(message, (int)ExtendedMessageFieldNumber.FunMessage_lobby_match_repl, out value));
 }