Exemple #1
0
        public bool RemoveInactiveActor(HiveGame game, Actor actor)
        {
            var idx = this.allActors.IndexOf(actor);

            if (idx != -1)
            {
                this.allActors.RemoveAt(idx);
                var timer = actor.CleanUpTimer;
                if (timer != null)
                {
                    timer.Dispose();
                    actor.CleanUpTimer = null;
                }

                game.OnActorRemoved(actor);

                if (Log.IsDebugEnabled)
                {
                    Log.DebugFormat("Actor {0}: {1} {2} to game: {3} -- TTL expired", "removed", actor.ActorNr, actor.UserId, game.Name);
                }
            }
            else
            {
                Log.ErrorFormat("Can not find actor {0} in inactiveActors collection", actor);
            }
            return(idx != -1);
        }
Exemple #2
0
        public bool TryAddExpectedUsers(HiveGame hiveGame, JoinGameRequest joinRequest)
        {
            if (joinRequest.AddUsers == null)
            {
                return(true);
            }

            if (!this.CheckMayAddSlots(joinRequest.AddUsers, hiveGame.MaxPlayers))
            {
                return(false);
            }

            var added = new List <string>();

            foreach (var userName in joinRequest.AddUsers)
            {
                switch (this.TryAddExpectedUser(hiveGame, userName))
                {
                case SLOT_ADD_FAIL:
                    return(false);

                case SLOT_ADD_OK:
                    added.Add(userName);
                    break;
                }
            }
            joinRequest.AddUsers = added.Count > 0 ? added.ToArray() : null;
            return(true);
        }
Exemple #3
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);
        }
Exemple #4
0
        /// <summary>
        /// Tries to add a <see cref="HivePeer"/> to this game instance.
        /// </summary>
        /// <param name="game"></param>
        /// <param name="peer">
        /// The peer to add.
        /// </param>
        /// <param name="actorNr">
        /// The actor Nr.
        /// </param>
        /// <param name="actor">
        /// When this method returns this out param contains the <see cref="Actor"/> associated with the <paramref name="peer"/>.
        /// </param>
        /// <param name="isNewActor">indicates that new actor was created</param>
        /// <param name="reason">
        /// reason why player can not be added
        /// </param>
        /// <param name="joinRequest"></param>
        /// <returns>
        /// Returns true if no actor exists for the specified peer and a new actor for the peer has been successfully added.
        ///   The actor parameter is set to the newly created <see cref="Actor"/> instance.
        ///   Returns false if an actor for the specified peer already exists.
        ///   The actor parameter is set to the existing <see cref="Actor"/> for the specified peer.
        /// </returns>
        public bool TryAddPeerToGame(HiveGame game, HivePeer peer, int actorNr, out Actor actor, out bool isNewActor,
                                     out Photon.Common.ErrorCode errorcode, out string reason, JoinGameRequest joinRequest)
        {
            isNewActor = false;
            errorcode  = Photon.Common.ErrorCode.InternalServerError;

            if (!this.VerifyCanJoin(peer, actorNr, game.PlayerTTL, out actor, out errorcode, out reason, ref isNewActor, game.CheckUserOnJoin, joinRequest))
            {
                return(false);
            }

            if (isNewActor)
            {
                actor = new Actor(peer);
                this.actorNumberCounter++;
                actor.ActorNr = this.actorNumberCounter;

                this.SanityChecks(peer, actorNr, actor, ref reason, game.CheckUserOnJoin);

                this.allActors.Add(actor);
            }
            else
            {
                actor.Reactivate(peer);
            }
            ++this.activeActorsCount;

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

            reason = "";
            return(true);
        }
Exemple #5
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);
            }
        }
Exemple #6
0
        public int TryAddExpectedUser(HiveGame hiveGame, string userId)
        {
            if (this.IsExpectedUser(userId))
            {
                return(SLOT_ADD_SKIP);
            }

            // check if user already there
            var actor = GetActorByUserId(this.AllActors, userId);

            if (actor == null && this.allActors.Count + this.YetExpectedUsersCount == hiveGame.MaxPlayers)
            {
                return(SLOT_ADD_FAIL);
            }

            this.expectedActors.Add(userId);
            return(SLOT_ADD_OK);
        }
        public Dictionary <string, object> GetCreateGameSettings(HiveGame game)
        {
            var settings = new Dictionary <string, object>();

            // set default properties
            if (wellKnownPropertiesCache.MaxPlayer.HasValue && wellKnownPropertiesCache.MaxPlayer.Value != game.MaxPlayers)
            {
                settings[HiveHostGameState.MaxPlayers.ToString()] = wellKnownPropertiesCache.MaxPlayer.Value;
            }

            if (wellKnownPropertiesCache.IsOpen.HasValue && wellKnownPropertiesCache.IsOpen.Value != game.IsOpen)
            {
                settings[HiveHostGameState.IsOpen.ToString()] = wellKnownPropertiesCache.IsOpen.Value;
            }

            if (wellKnownPropertiesCache.IsVisible.HasValue && wellKnownPropertiesCache.IsVisible.Value != game.IsVisible)
            {
                settings[HiveHostGameState.IsVisible.ToString()] = wellKnownPropertiesCache.IsVisible.Value;
            }

            settings[HiveHostGameState.LobbyId.ToString()]   = this.LobbyName;
            settings[HiveHostGameState.LobbyType.ToString()] = this.LobbyType;

            if (wellKnownPropertiesCache.LobbyProperties != null)
            {
                settings[HiveHostGameState.CustomProperties.ToString()] =
                    GetLobbyGameProperties(this.GameProperties, new HashSet <object>(wellKnownPropertiesCache.LobbyProperties));
            }

            settings[HiveHostGameState.EmptyRoomTTL.ToString()]       = this.EmptyRoomLiveTime;
            settings[HiveHostGameState.PlayerTTL.ToString()]          = this.PlayerTTL;
            settings[HiveHostGameState.CheckUserOnJoin.ToString()]    = this.CheckUserOnJoin;
            settings[HiveHostGameState.DeleteCacheOnLeave.ToString()] = this.DeleteCacheOnLeave;
            settings[HiveHostGameState.SuppressRoomEvents.ToString()] = this.SuppressRoomEvents;
            settings[HiveHostGameState.PublishUserId.ToString()]      = this.PublishUserId;
            settings[HiveHostGameState.ExpectedUsers.ToString()]      = this.AddUsers;

            return(settings);
        }
        public bool RemoveInactiveActor(HiveGame game, Actor actor)
        {
            var idx = this.allActors.IndexOf(actor);

            if (idx != -1)
            {
                this.allActors.RemoveAt(idx);

                actor.RemovedFromInactive = DateTime.Now;

                game.OnActorRemoved(actor);

                if (Log.IsDebugEnabled)
                {
                    Log.DebugFormat("Actor {0}: {1} {2} to game: {3} -- TTL expired", "removed", actor.ActorNr, actor.UserId, game.Name);
                }
            }
            else
            {
                Log.ErrorFormat("Can not find actor {0} in inactiveActors collection. timer is {1}. game: '{2}'",
                                actor, actor.CleanUpTimer != null ? "NOT null" : "<null>", game);
            }
            return(idx != -1);
        }
        public Dictionary<string, object> GetCreateGameSettings(HiveGame game)
        {
            var settings = new Dictionary<string, object>();

            // set default properties
            if (newMaxPlayer.HasValue && newMaxPlayer.Value != game.MaxPlayers)
            {
                settings[HiveHostGameState.MaxPlayers.ToString()] = newMaxPlayer.Value;
            }

            if (newIsOpen.HasValue && newIsOpen.Value != game.IsOpen)
            {
                settings[HiveHostGameState.IsOpen.ToString()] = newIsOpen.Value;
            }

            if (newIsVisible.HasValue && newIsVisible.Value != game.IsVisible)
            {
                settings[HiveHostGameState.IsVisible.ToString()] = newIsVisible.Value;
            }

            settings[HiveHostGameState.LobbyId.ToString()] = this.LobbyName;
            settings[HiveHostGameState.LobbyType.ToString()] = this.LobbyType;

            if (newLobbyProperties != null)
            {
                settings[HiveHostGameState.CustomProperties.ToString()] =
                    GameParameterReader.GetLobbyGameProperties(this.GameProperties, new HashSet<object>(newLobbyProperties));
            }

            settings[HiveHostGameState.EmptyRoomTTL.ToString()] = this.EmptyRoomLiveTime;
            settings[HiveHostGameState.PlayerTTL.ToString()] = this.PlayerTTL;
            settings[HiveHostGameState.CheckUserOnJoin.ToString()] = this.CheckUserOnJoin;
            settings[HiveHostGameState.DeleteCacheOnLeave.ToString()] = this.DeleteCacheOnLeave;
            settings[HiveHostGameState.SuppressRoomEvents.ToString()] = this.SuppressRoomEvents;

            return settings;
        }