Пример #1
0
        public void DeactivateActors(HiveGame game)
        {
            if (!canPlayerTtlExpire(game.PlayerTTL))
            {
                // game should not contain any inactive actor
                return;
            }

            var actorsToCleanUp = new List <Actor>();
            var now             = DateTime.Now;

            foreach (var actor in this.InactiveActors)
            {
                var closureActor = actor;
                if (actor.DeactivationTime.HasValue)
                {
                    if (Log.IsDebugEnabled)
                    {
                        Log.DebugFormat("Actor {0} clean up time is '{1}', now is '{2}'", actor.ActorNr, actor.DeactivationTime, now);
                    }
                    var sceduleTime = (int)((DateTime)actor.DeactivationTime - now).TotalMilliseconds + game.PlayerTTL;
                    if (sceduleTime > 0)
                    {
                        actor.CleanUpTimer = game.ExecutionFiber.Schedule(() => game.RemoveInactiveActor(closureActor), sceduleTime);
                        if (Log.IsDebugEnabled)
                        {
                            Log.DebugFormat("Actor {0} has clean up time. Clean up will happen in {1} sec", actor.ActorNr, sceduleTime / 1000);
                        }
                    }
                    else
                    {
                        if (Log.IsDebugEnabled)
                        {
                            Log.DebugFormat("Actor's {0} cleanup time expired. Cleanup as fast as possible", actor.ActorNr);
                        }
                        actorsToCleanUp.Add(closureActor);
                    }
                }
                else
                {
                    if (Log.IsDebugEnabled)
                    {
                        Log.DebugFormat("Actor {0} does not have clean up time. use PlayerTtl:{1}", actor.ActorNr, game.PlayerTTL);
                    }
                    actor.DeactivationTime = now;
                    actor.CleanUpTimer     = game.ExecutionFiber.Schedule(() => game.RemoveInactiveActor(closureActor), game.PlayerTTL);
                }
            }

            foreach (var actor in actorsToCleanUp)
            {
                game.RemoveInactiveActor(actor);
            }
        }
Пример #2
0
        public int RemovePeerFromGame(HiveGame game, HivePeer peer, int playerTTL, bool isCommingBack)
        {
            //if (Log.IsDebugEnabled)
            //{
            //    Log.DebugFormat("RemovePeerFromGame: conId={0}", peer.ConnectionId);
            //}


            var actorIndex = this.allActors.FindIndex(a => a.Peer == peer);

            if (actorIndex == -1)
            {
                if (Log.IsDebugEnabled)
                {
                    Log.DebugFormat("RemovePeerFromGame - Cant remove actor. It was not found for peer: {0}", peer);
                }

                return(-1);
            }

            var actor = this.allActors[actorIndex];

            Debug.Assert(actor.IsActive);
            --this.activeActorsCount;

            if (playerTTL != 0 && isCommingBack)
            {
                // Note: deactive actor first, so it deosn't recieve its own leave event.
                // put to disconnectedActors collection
                //actor.Peer = null;
                // TBD - fix groups
                actor.Deactivate();

                game.OnActorDeactivated(actor);
                //Note: the player TTL can be set to never timeout (expected behavior specially for SavedGames)
                if (playerTTL > 0 && playerTTL != int.MaxValue)
                {
                    // setup cleanup timer
                    actor.CleanUpTimer = game.ExecutionFiber.Schedule(() => game.RemoveInactiveActor(actor), playerTTL);
                }

                if (Log.IsDebugEnabled)
                {
                    Log.DebugFormat("Actor {0}: {1} {2} to game: {3} -- peer:{4}", "deactivated", actor.ActorNr, actor.UserId, game.Name, peer.ToString());
                }
            }
            else
            {
                this.allActors.RemoveAt(actorIndex);
                // cleanup does raise leave event
                game.OnActorRemoved(actor);

                if (Log.IsDebugEnabled)
                {
                    Log.DebugFormat("Actor {0}: {1} {2} to game: {3} -- peer:{4}", "removed", actor.ActorNr, actor.UserId, game.Name, peer.ToString());
                }
            }

            return(actor.ActorNr);
        }