Beispiel #1
0
        public void HandleLogout(Int64 nSessionID)
        {
            FFLog.Trace(string.Format("PlayerHandler.HandleLogout {0}", nSessionID));
            Player player = RoleMgr.Instance().GetPlayerBySessionID(nSessionID);

            if (player == null)
            {
                return;
            }
            RoleMgr.Instance().RemoveRole(nSessionID);

            Pbmsg.LogoutRet retMsg = new Pbmsg.LogoutRet()
            {
                Id   = player.GetID(),
                Name = player.strName,
            };
            FFWorker.Instance().GateBroadcastMsg((int)Pbmsg.ServerCmdDef.SLogout, retMsg);

            if (player.playerYS != null && RoleMgr.Instance().GetPlayerBySessionID(player.playerYS.nSessionID) != null)
            {
                RoleMgr.Instance().RemoveRole(player.playerYS.nSessionID);;
                retMsg = new Pbmsg.LogoutRet()
                {
                    Id   = player.playerYS.GetID(),
                    Name = player.playerYS.strName,
                };
                FFWorker.Instance().GateBroadcastMsg((int)Pbmsg.ServerCmdDef.SLogout, retMsg);
            }
            return;
        }
Beispiel #2
0
 public bool Init()
 {
     FFWorker.Instance().BindHandler <Pbmsg.LoginReq>(Pbmsg.ClientCmdDef.CLogin, this.HandleLogin)
     .BindOffline(this.HandleLogout)
     .BindHandler <Player, Pbmsg.RunReq>(Pbmsg.ClientCmdDef.CRun, this.HandleRun)
     .BindHandler <Player, Pbmsg.AttackReq>(Pbmsg.ClientCmdDef.CAttack, this.HandleAttack)
     ;
     return(true);
 }
Beispiel #3
0
        public void SendPlayerMsg <T>(Pbmsg.ServerCmdDef cmd, T pbMsgData) where T : pb::IMessage, new()
        {
            Int64 nSessionID = this.nSessionID;
            Int16 nCmd       = (Int16)cmd;

            if (this.idZhuTi != 0)
            {
                nSessionID = this.idZhuTi;
                //nCmd |= 0x4000;
            }
            FFWorker.Instance().SessionSendMsg(nSessionID, (Int16)cmd, pbMsgData);
        }
Beispiel #4
0
        public void HandleAttack(Player player, Pbmsg.AttackReq reqMsg)
        {
            Role roleTarget = RoleMgr.Instance().GetRoleBySessionID(reqMsg.Targetid);

            if (roleTarget == null)
            {
                return;
            }
            Pbmsg.AttackRet retMsg = new Pbmsg.AttackRet()
            {
                Id       = player.GetID(),
                Targetid = reqMsg.Targetid,
                Magicid  = reqMsg.Magicid,
            };
            FFWorker.Instance().GateBroadcastMsg((int)Pbmsg.ServerCmdDef.SAttack, retMsg);
            Random rd         = new Random();
            int    hpChaneged = rd.Next(1, roleTarget.maxhp / 10);

            //hpChaneged = 1;

            if (roleTarget.hp >= hpChaneged)
            {
                roleTarget.hp -= hpChaneged;
            }
            else
            {
                roleTarget.hp = 0;
            }
            Pbmsg.HPChangedRet retMsg2 = new Pbmsg.HPChangedRet()
            {
                Id         = roleTarget.GetID(),
                Magicid    = reqMsg.Magicid,
                ValCur     = roleTarget.hp,
                ValChanged = hpChaneged,
            };
            FFWorker.Instance().GateBroadcastMsg((int)Pbmsg.ServerCmdDef.SHpChanged, retMsg2);
            roleTarget.nLastAttackedTime = Util.GetNowTimeMs();

            if (roleTarget is Monster)
            {
                Monster monster = roleTarget as Monster;
                monster.direction           = (player.direction + 4) % 8;
                monster.nLastAttackedRoleID = player.GetID();
                if (monster.hp == 0)
                {
                    Pbmsg.LeaveMapRet leaveMsg = new Pbmsg.LeaveMapRet()
                    {
                        Id = roleTarget.GetID(),
                    };
                    FFWorker.Instance().GateBroadcastMsg((int)Pbmsg.ServerCmdDef.SLeaveMap, leaveMsg);
                }
            }
        }
Beispiel #5
0
 public void HandleRun(Player player, Pbmsg.RunReq reqMsg)
 {
     player.x         = reqMsg.X;
     player.y         = reqMsg.Y;
     player.direction = reqMsg.Direction;
     Pbmsg.RunRet runRet = new Pbmsg.RunRet()
     {
         Id = player.GetID(),
         X  = player.x,
         Y  = player.y,
     };
     FFWorker.Instance().GateBroadcastMsg((int)Pbmsg.ServerCmdDef.SRun, runRet);
 }
Beispiel #6
0
        public static void Main(string[] args)
        {
            CfgTool.Instance().InitCfg(args);
            string strBrokerListen = CfgTool.Instance().GetCfgVal("BrokerListen", "tcp://127.0.0.1:4321");

            if (!FFBroker.Instance().Init(strBrokerListen))
            {
                FFLog.Error("FFBroker open failed!");
                return;
            }

            int nWorkerIndex = 0;

            if (FFWorker.Instance().Init(strBrokerListen, nWorkerIndex, listEnableClassNames) == false)
            {
                FFLog.Trace("FFWorker open failed!");
                return;
            }

            string strGateListen = CfgTool.Instance().GetCfgVal("GateListen", "tcp://*:44000");

            if (FFGate.Instance().Init(strBrokerListen, strGateListen) == false)
            {
                FFLog.Trace("ffGate open failed!");
                return;
            }

            bool bExit = false;

            AppDomain.CurrentDomain.ProcessExit += (sender, arg) =>
            {
                FFLog.Trace("exit!");
                bExit = true;
            };
            Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs e) => {
                e.Cancel = true;
                FFLog.Trace("exit!!");
                bExit = true;
            };
            while (!bExit)
            {
                System.Threading.Thread.Sleep(300);
            }

            FFLog.Trace("exit!!!");
            FFGate.Instance().Cleanup();
            FFBroker.Instance().Cleanup();
            FFWorker.Instance().Cleanup();
            FFNet.Cleanup();
            FFLog.Cleanup();
        }
Beispiel #7
0
        public bool Init()
        {
            Player player = null;

            FFWorker.Instance().funcSessionID2Object = (Int64 s, int cmd, byte[] data) => {
                Role ret = this.GetPlayerBySessionID(s);
                if (ret != null && ret is Player)
                {
                    player = ret as Player;
                    if ((cmd & 0x4000) != 0)
                    {
                        cmd &= ~(0x4000);
                        return(player.playerYS);
                    }
                }
                return(player);
            };
            return(true);
        }
Beispiel #8
0
        public void HandleRobotAI(Player player)
        {
            long nowtm = Util.GetNowTimeMs();

            if (nowtm - player.nLastRobotAITm < 1000)
            {
                return;
            }
            player.nLastRobotAITm = nowtm;
            Role targetRole = GetTargetRobot(player);

            if (targetRole == null)//!没有目标找一个离自己最近的目标
            {
                RoleMgr.Instance().ForeachRole((Role roleOther) => {
                    if (roleOther == player || roleOther.hp == 0 || roleOther is Monster)
                    {
                        return;
                    }
                    if (targetRole != null && Util.Distance(player.x, player.y, targetRole.x, targetRole.y) <= Util.Distance(player.x, player.y, roleOther.x, roleOther.y))
                    {
                        return;
                    }
                    targetRole        = roleOther;
                    player.nCurTarget = targetRole.GetID();
                });
            }
            int nDistance = Util.Distance(player.x, player.y, targetRole.x, targetRole.y);

            if (nDistance <= 1)
            {
                Pbmsg.AttackRet monsterAttackMsg = new Pbmsg.AttackRet()
                {
                    Magicid  = 2,
                    Id       = player.GetID(),
                    Targetid = targetRole.GetID(),
                };
                FFWorker.Instance().GateBroadcastMsg((int)Pbmsg.ServerCmdDef.SAttack, monsterAttackMsg);

                int hpChaneged = 200;

                //if (targetRole.hp >= hpChaneged)
                //{
                //    targetRole.hp -= hpChaneged;
                //}
                //else
                //{
                //    targetRole.hp = 0;
                //    targetRole.nLastAttackedTime = Util.GetNowTimeMs();
                //}
                Pbmsg.HPChangedRet retMsg2 = new Pbmsg.HPChangedRet()
                {
                    Id         = targetRole.GetID(),
                    Magicid    = 2,
                    ValCur     = targetRole.hp,
                    ValChanged = hpChaneged,
                };
                FFWorker.Instance().GateBroadcastMsg((int)Pbmsg.ServerCmdDef.SHpChanged, retMsg2);
                return;
            }
            //!追击
            int       nDir      = GameUtil.CalDirection(player.x, player.y, targetRole.x, targetRole.y);
            GamePoint offsetPos = GameUtil.CalPointByDirLen(nDir, 1);

            player.x = player.x + offsetPos.x;
            player.y = player.y + offsetPos.y;
            Pbmsg.RunRet runRet = new Pbmsg.RunRet()
            {
                Id = player.GetID(),
                X  = player.x,
                Y  = player.y,
            };
            FFWorker.Instance().GateBroadcastMsg((int)Pbmsg.ServerCmdDef.SRun, runRet);
            return;
        }
Beispiel #9
0
        public void HandleMonsterAI()
        {
            long now = Util.GetNowTimeMs();

            if (now - this.nLastAITick < 800)
            {
                return;
            }
            this.nLastAITick = Util.GetNowTimeMs();
            RoleMgr.Instance().ForeachRole((Role role) => {
                //if (gCount++ % 10 == 0)
                //{
                //    FFLog.Trace(string.Format("HandleMonsterAI! {0},hp:{1}", gCount, role.hp));
                //}
                if (role.hp <= 0)
                {
                    if (Util.GetNowTimeMs() - role.nLastAttackedTime < 5000 && gCount++ % 10 != 0)
                    {
                        return;
                    }
                    role.hp = role.maxhp;
                    Pbmsg.EnterMapRet enterMapRet = role.BuildEnterMsg();
                    FFWorker.Instance().GateBroadcastMsg((int)Pbmsg.ServerCmdDef.SEnterMap, enterMapRet);
                    return;
                }
                if (!(role is Monster))
                {
                    if (role is Player)
                    {
                        Player playerTmp = role as Player;
                        if (playerTmp.bIsRobot)
                        {
                            HandleRobotAI(playerTmp);
                        }
                    }
                    return;
                }

                Monster monster = role as Monster;
                if (monster.nLastAttackedRoleID == 0)
                {
                    return;
                }
                Player player = RoleMgr.Instance().GetPlayerBySessionID(monster.nLastAttackedRoleID);
                if (player == null || player.hp == 0)
                {
                    monster.nLastAttackedRoleID = 0;
                    return;
                }
                int nDistance = Util.Distance(monster.x, monster.y, player.x, player.y);
                if (nDistance <= 1)
                {
                    if (Util.GetNowTimeMs() - monster.nLastAttackTime < 1000)
                    {
                        return;
                    }
                    monster.nLastAttackTime          = Util.GetNowTimeMs();
                    Pbmsg.AttackRet monsterAttackMsg = new Pbmsg.AttackRet()
                    {
                        Id       = monster.GetID(),
                        Targetid = player.GetID(),
                    };
                    FFWorker.Instance().GateBroadcastMsg((int)Pbmsg.ServerCmdDef.SAttack, monsterAttackMsg);

                    Random rd = new Random();

                    Role roleTarget = player;
                    int hpChaneged  = rd.Next(1, roleTarget.maxhp / 10);
                    if (roleTarget.hp >= hpChaneged)
                    {
                        roleTarget.hp -= hpChaneged;
                    }
                    else
                    {
                        roleTarget.hp = 0;
                    }
                    roleTarget.nLastAttackedTime = Util.GetNowTimeMs();
                    Pbmsg.HPChangedRet retMsg3   = new Pbmsg.HPChangedRet()
                    {
                        Id         = roleTarget.GetID(),
                        ValCur     = roleTarget.hp,
                        ValChanged = hpChaneged,
                    };
                    FFWorker.Instance().GateBroadcastMsg((int)Pbmsg.ServerCmdDef.SHpChanged, retMsg3);
                }
                else//!怪物寻路追打角色
                {
                    GamePoint nextPos = GameUtil.FindPath(new GamePoint(monster.x, monster.y), new GamePoint(player.x, player.y));
                    if (RoleMgr.Instance().GetRoleByPos(nextPos.x, nextPos.y) != null)
                    {
                        return;
                    }
                    monster.x           = nextPos.x;
                    monster.y           = nextPos.y;
                    Pbmsg.RunRet runRet = new Pbmsg.RunRet()
                    {
                        Id = monster.GetID(),
                        X  = monster.x,
                        Y  = monster.y,
                    };
                    FFWorker.Instance().GateBroadcastMsg((int)Pbmsg.ServerCmdDef.SRun, runRet);
                }
            });
        }
Beispiel #10
0
        public void HandleLogin(Int64 nSessionID, Pbmsg.LoginReq reqMsg)
        {
            Player playerOld = RoleMgr.Instance().GetPlayerBySessionID(nSessionID);

            if (playerOld != null)
            {
                if (playerOld.nSessionID == nSessionID)
                {
                    FFLog.Error(string.Format("worker cmd invalid! {0} login twice", nSessionID));
                }
                return;
            }

            Player player = new Player()
            {
                nSessionID = nSessionID
            };

            RoleMgr.Instance().AddRole(player);

            player.strName = reqMsg.Name;
            player.nLevel  = 1;

            Pbmsg.LoginRet retMsg = new Pbmsg.LoginRet()
            {
                Id    = player.GetID(),
                Name  = reqMsg.Name,
                Level = player.nLevel,
            };

            player.SendPlayerMsg(Pbmsg.ServerCmdDef.SLogin, retMsg);

            {
                Pbmsg.EnterMapRet enterMapRet = player.BuildEnterMsg();
                FFWorker.Instance().GateBroadcastMsg((int)Pbmsg.ServerCmdDef.SEnterMap, enterMapRet);

                player.playerYS = new Player()
                {
                    nSessionID = player.nSessionID + 100000, strName = player.strName + "的元神", idZhuTi = player.GetID()
                };
                player.playerYS.x      = player.x - 1;
                player.playerYS.y      = player.y - 1;
                player.playerYS.apprID = player.apprID;
                RoleMgr.Instance().AddRole(player.playerYS);

                enterMapRet = player.playerYS.BuildEnterMsg();
                FFWorker.Instance().GateBroadcastMsg((int)Pbmsg.ServerCmdDef.SEnterMap, enterMapRet);
            }

            RoleMgr.Instance().ForeachRole((Role roleOther) => {
                if (roleOther.GetID() == player.GetID())
                {
                    return;
                }
                if (roleOther.hp == 0)
                {
                    return;
                }
                Pbmsg.EnterMapRet enterMapRet = roleOther.BuildEnterMsg();
                player.SendPlayerMsg(Pbmsg.ServerCmdDef.SEnterMap, enterMapRet);
            });
        }
Beispiel #11
0
 public void ClosePlayer()
 {
     FFWorker.Instance().SessionClose(this.nSessionID);
 }