Beispiel #1
0
 public void OnOperationRequest(ActorPeer sender, OperationRequest operationRequest, SendParameters sendParameters)
 {
     _executionFiber.Enqueue(() =>
     {
         this.ProcessRequest(sender,
                             operationRequest, sendParameters);
     });
 }
Beispiel #2
0
 public void Enter(ActorPeer peer)
 {
     _executionFiber.Enqueue(() =>
     {
         ListPeer.Add(peer);
         SpawnPlayer(peer);
     });
 }
Beispiel #3
0
        public void SpawnPlayer(ActorPeer peer)
        {
            IActor player = new IActor()
            {
                actorType = (byte)ActorType.Player,
                peerID    = peer.playerID,
                actorID   = AllocateActorId(),
                posX      = 10,
                posY      = 10
            };

            ListPlayer.Add(player);

            //#region Test Code
            ////List<MPlayer> listTest = new List<MPlayer>()
            ////{
            ////    new MPlayer() { id = 1,name = "duc anh"},
            ////    new MPlayer() {id = 2, name = "hoang anh"}
            ////};
            //EventData evtData = new EventData()
            //{
            //    Code = (byte)AckEventType.CreateActor,
            //    Parameters = new Dictionary<byte, object>()
            //    {
            //        {(byte)ActorType.Creep, GeneralFunc.Serialize(listCreep)}
            //    }
            //};
            //peer.SendEvent(evtData, new SendParameters());
            //#endregion

            Region startRegion = Region.GetRegionFromPosition(player);

            Console.WriteLine("aaaaaaaaaaaaaaaaaaaaaaa");
            startRegion.ActorEnter(player);

            EventData evtPlayer = new EventData()
            {
                Code       = (byte)AckEventType.AssignPlayerID,
                Parameters = new Dictionary <byte, object>()
                {
                    { 0, GeneralFunc.Serialize(player) }
                }
            };

            peer.SendEvent(evtPlayer, new SendParameters());
        }
Beispiel #4
0
        public void Exit(ActorPeer peer)
        {
            _executionFiber.Enqueue(() =>
            {
                ListPeer.Remove(peer);
            });

            IActor player = ListPlayer.FirstOrDefault(a => a.peerID == peer.playerID);

            if (player != null)
            {
                lock (ListPlayer)
                {
                    ListPlayer.Remove(player);
                }
                Region currentRegion = Region.GetRegionFromPosition(player);
                currentRegion.ActorExit(player);
            }
        }
Beispiel #5
0
 private void DeletePeerToOther(IActor player, List <IActor> listPlayerInRegion)
 {
     foreach (var playerInRegion in listPlayerInRegion)
     {
         ActorPeer actorPeer = ServerArena.Instance.ListPeer.Where(a => a.playerID == playerInRegion.actorID).FirstOrDefault();
         if (actorPeer != null)
         {
             EventData evtOtherPlayer = new EventData()
             {
                 Code       = (byte)AckEventType.DestroyActor,
                 Parameters = new Dictionary <byte, object>()
                 {
                     { (byte)Parameter.Data, GeneralFunc.Serialize(player) }
                 }
             };
             actorPeer.SendEvent(evtOtherPlayer, new SendParameters());
         }
     }
 }
Beispiel #6
0
        public void ActorEnter(IActor actor)
        {
            if (actor.actorType == (byte)ActorType.Player)
            {
                lock (listPlayer)
                {
                    listPlayer.Add(actor);
                }

                ActorPeer peer = ServerArena.Instance.ListPeer.Where(a => a.playerID == actor.peerID).First();

                SendNewInfoToPeer(peer, actor);
                SendNewPeerToTheOthers(actor, GetListPlayerInterest());
            }
            else
            {
                lock (listCreep)
                {
                    listCreep.Add(actor);
                }
            }
        }
Beispiel #7
0
        private void SendNewPeerToTheOthers(IActor player, List <IActor> listPlayerInRegion)
        {
            List <IActor> listOnePlayer = new List <IActor>();

            listOnePlayer.Add(player);

            foreach (var playerInRegion in listPlayerInRegion)
            {
                ActorPeer actorPeer = ServerArena.Instance.ListPeer.Where(a => a.playerID == playerInRegion.actorID).FirstOrDefault();
                if (actorPeer != null)
                {
                    EventData evtOtherPlayer = new EventData()
                    {
                        Code       = (byte)AckEventType.CreateActor,
                        Parameters = new Dictionary <byte, object>()
                        {
                            { (byte)ActorType.Player, GeneralFunc.Serialize(listOnePlayer) }
                        }
                    };
                    actorPeer.SendEvent(evtOtherPlayer, new SendParameters());
                }
            }
        }
Beispiel #8
0
        public void ProcessRequest(ActorPeer sender, OperationRequest operationRequest, SendParameters sendParameters)
        {
            if (operationRequest.OperationCode == (byte)AckRequestType.MoveCommand)
            {
                Movement movement = GeneralFunc.Deserialize <Movement>(operationRequest.Parameters[(byte)Parameter.Data] as byte[]);

                // find actor
                IActor player = (ListPlayer.Find(pl => pl.actorID == movement.actorID));

                Region oldRegion = Region.GetRegionFromPosition(player);

                player.posX = movement.posX;
                player.posY = movement.posY;

                Region newRegion = Region.GetRegionFromPosition(player);

                if (oldRegion != newRegion)
                {
                    oldRegion.ActorExit(player);

                    newRegion.ActorEnter(player);
                }
            }
        }
Beispiel #9
0
        private void SendNewInfoToPeer(ActorPeer peer, IActor player)
        {
            List <IActor> listPlayerInRegion = new List <IActor>();
            List <IActor> listCreepInRegion  = new List <IActor>();

            List <Region> listInterestRegion = GetInterestRegion();

            foreach (var region in listInterestRegion)
            {
                listPlayerInRegion.AddRange(region.listPlayer);
                listCreepInRegion.AddRange(region.listCreep);
            }
            listPlayerInRegion.Remove(player);

            InterestRegions dataInterest = GetDataInterestRegion(listInterestRegion);

            dataInterest.playerPosition = new MPosition()
            {
                x = player.posX,
                y = player.posY
            };


            EventData evtDataCrrepAndOtherPlayers = new EventData()
            {
                Code       = (byte)AckEventType.CreateActor,
                Parameters = new Dictionary <byte, object>()
                {
                    { (byte)Parameter.Player, GeneralFunc.Serialize(listPlayerInRegion) },
                    { (byte)Parameter.Creep, GeneralFunc.Serialize(listCreepInRegion) },
                    { (byte)Parameter.Regions, GeneralFunc.Serialize(dataInterest) }
                }
            };

            peer.SendEvent(evtDataCrrepAndOtherPlayers, new SendParameters());
        }