Пример #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;
        }
Пример #2
0
 public static FFWorker Instance()
 {
     if (gInstance == null)
     {
         gInstance = new FFWorker();
     }
     return(gInstance);
 }
Пример #3
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);
 }
Пример #4
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);
        }
Пример #5
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);
                }
            }
        }
Пример #6
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);
 }
Пример #7
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();
        }
Пример #8
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);
        }
Пример #9
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;
        }
Пример #10
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);
                }
            });
        }
Пример #11
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);
            });
        }
Пример #12
0
 public void ClosePlayer()
 {
     FFWorker.Instance().SessionClose(this.nSessionID);
 }
Пример #13
0
        public static void Main(string[] args)
        {
            string   host     = "tcp://127.0.0.1:43210";
            FFBroker ffbroker = new FFBroker();

            ffbroker.Open(host);

            int      nWorkerIndex = 0;
            FFWorker worker       = new FFWorker();

            if (worker.Open(host, nWorkerIndex) == false)
            {
                FFLog.Trace("ffrpc open failed!");
            }

            //Console.ReadKey();
            //ffrpc.GetTaskQueue().Post(() =>
            //{
            //    SessionEnterWorkerReq reqMsg = new SessionEnterWorkerReq() { From_gate = "gate#0" };
            //    WorkerCallMsgReq reqWorkerCall = new WorkerCallMsgReq();
            //    //ffrpc.Call(strServiceName, reqMsg);
            //    reqMsg.From_gate = "gate#1";
            //    ffrpc.Call(strServiceName, reqWorkerCall, (SessionEnterWorkerReq retMsg) =>
            //    {
            //        FFLog.Trace(string.Format("ffrpc SessionEnterWorkerReq return£¡£¡£¡FromGate={0}", retMsg.From_gate));
            //    });
            //});
            FFGate ffGate = new FFGate();

            if (ffGate.Open(host, "tcp://*:44000", 0) == false)
            {
                FFLog.Trace("ffGate open failed!");
            }


            //FFNet.Timerout(1000, Theout);
            //FFNet.Timerout(2000, Theout);
            FFNet.Timerout(100000, () =>
            {
                FFLog.Debug("AAAAAAAAAAAAAAA1");
                //ffbroker.Close();
            });
            FFLog.Trace(string.Format("main! {0}", System.Threading.Thread.CurrentThread.ManagedThreadId.ToString()));

            AppDomain.CurrentDomain.ProcessExit += (sender, arg) =>
            {
                FFLog.Trace("exist!!");
            };
            Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs e) => {
                e.Cancel = true;
                FFLog.Trace("exist3!!");

                FFNet.Cleanup();
                FFLog.Cleanup();
            };
            Console.ReadKey();

            FFLog.Trace("exist!!");
            FFNet.Cleanup();
            FFLog.Cleanup();
        }
Пример #14
0
        public static void Main(string[] args)
        {
#if linux
            //if (args.Length >= 1 && (args[0] == "/daemon" || args[0] == "--daemon"))
            for (int i = 0; i < args.Length; ++i)
            {
                if (args[i] != "/daemon" && args[i] != "--daemon")
                {
                    continue;
                }
                FFLog.Trace(string.Format("config {0}", args[i]));

                int pid = fork();
                if (pid != 0)
                {
                    exit(0);
                }
                setsid();
                pid = fork();
                if (pid != 0)
                {
                    exit(0);
                }
                umask(022);

                int max = open("/dev/null", 0);
                for (var m = 0; m <= max; m++)
                {
                    close(m);
                }

                //!read write error
                int fd = open("/dev/null", 0);
                dup(fd);
                dup(fd);

                var executablePath = Environment.GetCommandLineArgs()[0];
                FFLog.Trace(string.Format("executablePath {0}", executablePath));
                string[] argsNew     = new string[args.Length + 1];//{"mono", executablePath};
                int      assignIndex = 0;
                argsNew[assignIndex] = "mono";
                assignIndex         += 1;
                argsNew[assignIndex] = executablePath;
                assignIndex         += 1;
                for (int j = 0; j < args.Length; ++j)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    argsNew[assignIndex] = args[j];
                    assignIndex         += 1;
                }
                execvp("mono", argsNew);
                return;
            }
#endif

            string   host     = "tcp://127.0.0.1:43210";
            FFBroker ffbroker = new FFBroker();
            ffbroker.Open(host);

            int      nWorkerIndex = 0;
            FFWorker worker       = new FFWorker();
            if (worker.Open(host, nWorkerIndex) == false)
            {
                FFLog.Trace("ffrpc open failed!");
            }

            //Console.ReadKey();
            //ffrpc.GetTaskQueue().Post(() =>
            //{
            //    SessionEnterWorkerReq reqMsg = new SessionEnterWorkerReq() { From_gate = "gate#0" };
            //    WorkerCallMsgReq reqWorkerCall = new WorkerCallMsgReq();
            //    //ffrpc.Call(strServiceName, reqMsg);
            //    reqMsg.From_gate = "gate#1";
            //    ffrpc.Call(strServiceName, reqWorkerCall, (SessionEnterWorkerReq retMsg) =>
            //    {
            //        FFLog.Trace(string.Format("ffrpc SessionEnterWorkerReq return!!!FromGate={0}", retMsg.From_gate));
            //    });
            //});
            FFGate ffGate = new FFGate();
            if (ffGate.Open(host, "tcp://*:44000", 0) == false)
            {
                FFLog.Trace("ffGate open failed!");
            }


            //FFNet.Timerout(1000, Theout);
            //FFNet.Timerout(2000, Theout);
            // FFNet.Timerout(1000000, () =>
            // {
            // FFLog.Debug("AAAAAAAAAAAAAAA1");
            // //ffbroker.Close();
            // });
            //FFLog.Trace(string.Format("main! {0}", System.Threading.Thread.CurrentThread.ManagedThreadId.ToString()));

            bool bExit = false;
            AppDomain.CurrentDomain.ProcessExit += (sender, arg) =>
            {
                FFLog.Trace("exist!!");
                bExit = true;
            };
            Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs e) => {
                e.Cancel = true;
                FFLog.Trace("exist3!!");

                FFNet.Cleanup();
                FFLog.Cleanup();
                bExit = true;
            };
            while (!bExit)
            {
                System.Threading.Thread.Sleep(300);
            }

            FFLog.Trace("exist!!");
            FFNet.Cleanup();
            FFLog.Cleanup();
        }