Exemplo n.º 1
0
        /// <summary>
        /// 销毁战场
        /// 主线程调用
        /// </summary>
        private void EndBattle(Battle battle)
        {
            battle.Stop();
            int count = battle.numChampions;

            //通知CS战场结束
            Protos.BS2CS_BattleEnd battleEnd = ProtoCreator.Q_BS2CS_BattleEnd();
            battleEnd.Bid = battle.id;
            for (int i = 0; i < count; ++i)
            {
                Champion champion = battle.GetChampionAt(i);
                var      info     = new Protos.BS2CS_BattleEndInfo
                {
                    Result       = (Protos.BS2CS_BattleEndInfo.Types.Result)champion.result,
                    Team         = champion.team,
                    Damage       = champion.damage,
                    Hurt         = champion.hurt,
                    Heal         = champion.heal,
                    OccupyTime   = champion.occupyTime,
                    Skill0Used   = champion.skill0Used,
                    Skill0Damage = champion.skill0Damage,
                    Skill1Used   = champion.skill1Used,
                    Skill1Damage = champion.skill1Damage,
                };
                battleEnd.Infos.Add(champion.user.gcNID, info);
            }
            BS.instance.netSessionMgr.Send(SessionType.ServerB2CS, battleEnd, RPCEntry.Pop(this.OnCSBattleEndRet, battle));
        }
Exemplo n.º 2
0
        private static void HandleLoginSuccess(Protos.LS2GC_AskLoginRet gcLoginRet, uint sid, LoginContext context)
        {
            ulong gcNID = GuidHash.GetUInt64();

            //通知cs,客户端登陆成功
            Protos.LS2CS_GCLogin csLogin = ProtoCreator.Q_LS2CS_GCLogin();
            csLogin.GcNID      = gcNID;
            csLogin.Id         = context.id;
            csLogin.Ukey       = context.ukey;
            csLogin.Channel    = context.channel;
            csLogin.Browser    = context.browser;
            csLogin.Platform   = context.platform;
            csLogin.SessionKey = context.sessionKey;
            csLogin.UnionID    = context.unionID;
            csLogin.Nickname   = context.nickname;
            csLogin.Avatar     = context.avatar;
            csLogin.Gender     = context.gender;
            csLogin.Money      = context.money;
            csLogin.Diamoned   = context.diamoned;
            csLogin.Rank       = context.rank;
            csLogin.Exp        = context.exp;
            csLogin.Champions  = context.champions;
            csLogin.ActorID    = context.actorID;
            LS.instance.netSessionMgr.Send(SessionType.ServerL2CS, csLogin,
                                           RPCEntry.Pop(OnGCLoginCSRet, gcLoginRet, sid, gcNID));
        }
Exemplo n.º 3
0
        /// <summary>
        /// 客户端请求购买英雄
        /// </summary>
        public ErrorCode OnGc2CsBuyChampion(NetSessionBase session, IMessage message)
        {
            Protos.GC2CS_BuyChampion    request  = (Protos.GC2CS_BuyChampion)message;
            Protos.CS2GC_BuyChampionRet response = ProtoCreator.R_GC2CS_BuyChampion(request.Opts.Pid);
            int    id    = request.Cid;
            ulong  gcNID = request.Opts.Transid;
            CSUser user  = CS.instance.userMgr.GetUser(gcNID);

            if (user == null)
            {
                return(ErrorCode.Success);
            }

            if (user.champions.Contains(id))                //已存在该英雄
            {
                response.Result = Protos.CS2GC_BuyChampionRet.Types.Result.Failed;
                user.Send(response);
                return(ErrorCode.Success);
            }
            Hashtable defs        = GoodsDefs.GetChampion(id);
            int       priceNeeded = ( int )(defs.GetInt("price") * (1 + defs.GetFloat("p_discount")));

            if (user.money < priceNeeded)              //没有足够金钱
            {
                response.Result = Protos.CS2GC_BuyChampionRet.Types.Result.NotEnoughMoney;
                user.Send(response);
                return(ErrorCode.Success);
            }
            int diamonedNeeded = ( int )(defs.GetInt("diamoned") * (1 + defs.GetFloat("d_discount")));

            if (user.diamoned < diamonedNeeded)
            {
                response.Result = Protos.CS2GC_BuyChampionRet.Types.Result.NotEnoughDiamoned;
                user.Send(response);
                return(ErrorCode.Success);
            }
            int expNeeded = defs.GetInt("exp");

            if (user.exp < expNeeded)
            {
                response.Result = Protos.CS2GC_BuyChampionRet.Types.Result.NotEnoughExp;
                user.Send(response);
                return(ErrorCode.Success);
            }

            int moneyResult    = user.money - priceNeeded;
            int diamonedResult = user.diamoned - diamonedNeeded;

            Protos.CS2DB_BuyChampion request2 = ProtoCreator.Q_CS2DB_BuyChampion();
            request2.Ukey     = user.ukey;
            request2.Money    = moneyResult;
            request2.Diamoned = diamonedResult;
            request2.Cids.Add(request.Cid);
            CS.instance.netSessionMgr.Send(SessionType.ServerC2DB, request2, RPCEntry.Pop(OnBuyChampion, user, moneyResult, diamonedResult, request.Cid, response));

            return(ErrorCode.Success);
        }
Exemplo n.º 4
0
        public ErrorCode OnGC2GSAskLogin(NetSessionBase session, Google.Protobuf.IMessage message)
        {
            Protos.GC2GS_AskLogin login = (Protos.GC2GS_AskLogin)message;

            Protos.GS2CS_GCAskLogin gcAskLogin = ProtoCreator.Q_GS2CS_GCAskLogin();
            gcAskLogin.SessionID = login.SessionID;
            Logger.Log($"client:{gcAskLogin.SessionID} ask login");

            //向CS请求客户端登陆
            GS.instance.netSessionMgr.Send(SessionType.ServerG2CS, gcAskLogin,
                                           RPCEntry.Pop(OnCS2GSAskLoginRet, session.id, login));
            return(ErrorCode.Success);
        }
Exemplo n.º 5
0
        public void BeginBattle(BattleUser[] users, BattleUser[][] tUsers)
        {
            BSInfo appropriateBSInfo = CS.instance.appropriateBSInfo;

            //没有找到合适的bs,通知客户端匹配失败
            if (appropriateBSInfo == null)
            {
                this.NotifyGCEnterBattleFailed(users, Protos.CS2GC_EnterBattle.Types.Result.BsnotFound);
                return;
            }

            //todo 现在先随机一张地图
            Random rnd      = new Random();
            int    mapCount = Defs.GetMapCount();
            int    mapID    = rnd.Next(0, mapCount);

            Protos.CS2BS_BattleInfo battleInfo = ProtoCreator.Q_CS2BS_BattleInfo();
            battleInfo.MapID       = mapID;
            battleInfo.ConnTimeout = ( int )Consts.WAITING_ROOM_TIME_OUT;
            int c1 = tUsers.Length;

            for (int i = 0; i < c1; i++)
            {
                Protos.CS2BS_TeamInfo ti = new Protos.CS2BS_TeamInfo();
                battleInfo.TeamInfos.Add(ti);
                BattleUser[] roomUsers = tUsers[i];
                int          c2        = roomUsers.Length;
                for (int j = 0; j < c2; j++)
                {
                    BattleUser roomUser        = roomUsers[j];
                    CSUser     user            = CS.instance.userMgr.GetUser(roomUser.id);
                    Protos.CS2BS_PlayerInfo pi = new Protos.CS2BS_PlayerInfo
                    {
                        GcNID        = user.ukey | ( ulong )appropriateBSInfo.lid << 32,
                            ActorID  = user.actorID,
                            Avatar   = user.avatar,
                            Nickname = user.nickname,
                            Gender   = user.gender,
                            Money    = user.money,
                            Diamoned = user.diamoned,
                            Rank     = user.rank,
                            Exp      = user.exp
                    };
                    ti.PlayerInfos.Add(pi);
                }
            }
            CS.instance.netSessionMgr.Send(appropriateBSInfo.sessionID, battleInfo, RPCEntry.Pop(this.OnBattleInfoRet, users, tUsers,
                                                                                                 appropriateBSInfo.ip, appropriateBSInfo.port,
                                                                                                 appropriateBSInfo.sessionID, appropriateBSInfo.lid));
        }
Exemplo n.º 6
0
        public ErrorCode OnGc2LsAskSmartLogin(NetSessionBase session, Google.Protobuf.IMessage message)
        {
            uint   sid    = session.id;
            string remote = session.connection.remoteEndPoint.ToString();

            Protos.GC2LS_AskSmartLogin login      = (Protos.GC2LS_AskSmartLogin)message;
            Protos.LS2GC_AskLoginRet   gcLoginRet = ProtoCreator.R_GC2LS_AskLogin(login.Opts.Pid);

            Logger.Log($"GC {login.Id}, {session.connection.remoteEndPoint} ask login");

            //检测用户名的合法性
            if (!CheckUsername(login.Id))
            {
                gcLoginRet.Result = Protos.LS2GC_AskLoginRet.Types.EResult.InvalidUname;
                session.Send(gcLoginRet);
                session.Close(true, "login failed");
                return(ErrorCode.Success);
            }

            LoginContext context = new LoginContext();

            context.id       = login.Id;
            context.channel  = login.Channel;
            context.browser  = login.Browser;
            context.platform = login.Platform;

            //查询数据库
            //若查询失败则自动注册
            Protos.LS2DB_QueryLogin queryLogin = ProtoCreator.Q_LS2DB_QueryLogin();
            queryLogin.Name     = login.Id;
            queryLogin.Nickname = login.Id;
            queryLogin.VertPwd  = false;
            queryLogin.Time     = TimeUtils.utcTime;
            queryLogin.Ip       = remote;
            queryLogin.Channel  = login.Channel;
            queryLogin.Browser  = login.Browser;
            queryLogin.Platform = login.Platform;
            queryLogin.Money    = LS.instance.config.initMoney;
            queryLogin.Diamoned = LS.instance.config.initDiamoned;
            queryLogin.Rank     = LS.instance.config.initRank;
            LS.instance.netSessionMgr.Send(SessionType.ServerL2DB, queryLogin,
                                           RPCEntry.Pop(OnSmartQueryLoginRet, gcLoginRet, sid, context));
            return(ErrorCode.Success);
        }
Exemplo n.º 7
0
        /// <summary>
        /// 玩家请求查询排行榜
        /// </summary>
        public ErrorCode OnGc2CsQueryRanking(NetSessionBase session, IMessage message)
        {
            Protos.GC2CS_QueryRanking request = (Protos.GC2CS_QueryRanking)message;
            ulong  gcNID = request.Opts.Transid;
            CSUser user  = CS.instance.userMgr.GetUser(gcNID);

            if (user != null)
            {
                Protos.CS2GC_QueryRankingRet resp         = ProtoCreator.R_GC2CS_QueryRanking(request.Opts.Pid);
                Protos.CS2DB_QueryRanking    queryRanking = ProtoCreator.Q_CS2DB_QueryRanking();
                queryRanking.SortType = (Protos.CS2DB_QueryRanking.Types.SortType)request.SortType;
                queryRanking.From     = request.From;
                queryRanking.Count    = request.Count;

                CS.instance.netSessionMgr.Send(SessionType.ServerC2DB, queryRanking, RPCEntry.Pop(OnQueryRankingRet, user, resp));
            }

            return(ErrorCode.Success);
        }
Exemplo n.º 8
0
 public void PingCS(NetSessionBase session)
 {
     Protos.G_AskPing msg = ProtoCreator.Q_G_AskPing();
     msg.Time = TimeUtils.utcTime;
     session.Send(msg, RPCEntry.Pop(OnGSAskPingRet));
 }
Exemplo n.º 9
0
        public ErrorCode OnGc2LsAskWxlogin(NetSessionBase session, Google.Protobuf.IMessage message)
        {
            uint   sid    = session.id;
            string remote = session.connection.remoteEndPoint.ToString();

            Protos.GC2LS_AskWXLogin  login      = (Protos.GC2LS_AskWXLogin)message;
            Protos.LS2GC_AskLoginRet gcLoginRet = ProtoCreator.R_GC2LS_AskLogin(login.Opts.Pid);
            string    reqUrl = string.Format(LS.instance.config.code2sessionUrl, LS.instance.config.wxAppID, LS.instance.config.wxAppSecret, login.Code);
            WebClient client = new WebClient();

            client.DownloadStringTaskAsync(reqUrl).ContinueWith((t, o) =>
            {
                Hashtable json = ( Hashtable )MiniJSON.JsonDecode(t.Result);
                int errorCode  = json.GetInt("errcode");
                if (errorCode != 0)
                {
                    //错误码,参考 https://developers.weixin.qq.com/minigame/dev/api/code2Session.html
                    switch (errorCode)
                    {
                    case -1:
                        gcLoginRet.Result = Protos.LS2GC_AskLoginRet.Types.EResult.Busy;
                        break;

                    case 40029:
                        gcLoginRet.Result = Protos.LS2GC_AskLoginRet.Types.EResult.InvalidCode;
                        break;

                    case 45011:
                        gcLoginRet.Result = Protos.LS2GC_AskLoginRet.Types.EResult.Frequent;
                        break;
                    }
                    session.Send(gcLoginRet);
                    session.Close(true, $"login fail:{errorCode}");
                }
                else
                {
                    string openID     = json.GetString("openid");
                    string sessionKey = json.GetString("session_key");
                    string unionID    = json.GetString("unionid");

                    Logger.Log($"wxLogin success, id:{openID}, sessionKey:{sessionKey}, unionID:{unionID}");

                    LoginContext context = new LoginContext();
                    context.id           = openID;
                    context.channel      = Protos.Global.Types.Channel.Wxmini;
                    context.browser      = login.Browser;
                    context.platform     = login.Platform;
                    context.sessionKey   = sessionKey;
                    context.unionID      = unionID;
                    context.nickname     = login.Nickname;
                    context.avatar       = login.Avatar;
                    context.gender       = ( byte )login.Gender;

                    //查询数据库
                    //若查询失败则自动注册
                    Protos.LS2DB_QueryLogin queryLogin = ProtoCreator.Q_LS2DB_QueryLogin();
                    queryLogin.Name     = openID;
                    queryLogin.VertPwd  = false;
                    queryLogin.Time     = TimeUtils.utcTime;
                    queryLogin.Ip       = remote;
                    queryLogin.Channel  = Protos.Global.Types.Channel.Wxmini;
                    queryLogin.Browser  = login.Browser;
                    queryLogin.Platform = login.Platform;
                    queryLogin.UnionID  = unionID;
                    queryLogin.Nickname = login.Nickname;
                    queryLogin.Avatar   = login.Avatar;
                    queryLogin.Gender   = login.Gender;
                    queryLogin.Money    = LS.instance.config.initMoney;
                    queryLogin.Diamoned = LS.instance.config.initDiamoned;
                    queryLogin.Rank     = LS.instance.config.initRank;
                    LS.instance.netSessionMgr.Send(SessionType.ServerL2DB, queryLogin,
                                                   RPCEntry.Pop(OnSmartQueryLoginRet, gcLoginRet, sid, context));
                }
            }, null, CancellationToken.None);
            return(ErrorCode.Success);
        }