Esempio n. 1
0
        private void CheckUpdateMode()
        {
            if (!Global.Current.FireEvents)
            {
                return;
            }

            DefaultMultiObjectLock.ThrowExceptionIfNotLocked(BaseStation);
        }
Esempio n. 2
0
        public Error RemoveTribesman(uint playerId, bool wasKicked, bool doNotRemoveIfOwner = true)
        {
            ITribesman tribesman;

            if (!tribesmen.TryGetValue(playerId, out tribesman))
            {
                return(Error.TribesmanNotFound);
            }

            IPlayer player = tribesman.Player;

            if (IsOwner(player))
            {
                if (doNotRemoveIfOwner)
                {
                    return(Error.TribesmanIsOwner);
                }

                Owner = null;
            }

            DefaultMultiObjectLock.ThrowExceptionIfNotLocked(tribesman);

            player.Tribesman = null;
            dbManager.Delete(tribesman);
            tribesmen.Remove(playerId);

            // Keep logs of who entered/left tribe. First clean up the list of no longer needed records though.
            LeavingTribesmates.RemoveAll(p => p.PlayerId == player.PlayerId || SystemClock.Now.Subtract(p.TimeLeft).TotalDays > DAYS_BEFORE_REJOIN_ALLOWED);
            LeavingTribesmates.Add(new LeavingTribesmate {
                PlayerId = player.PlayerId, TimeLeft = SystemClock.Now
            });

            // Save to save owner and leaving tribesmates
            dbManager.Save(this);

            // TODO: Move event out
            if (player.Session != null)
            {
                channel.Unsubscribe(player.Session, "/TRIBE/" + Id);

                if (wasKicked)
                {
                    player.Session.Write(new Packet(Command.TribesmanKicked));
                }
            }

            TribesmanRemoved.Raise(this, new TribesmanRemovedEventArgs {
                Player = player
            });

            return(Error.Ok);
        }
Esempio n. 3
0
        private void CheckUpdateMode()
        {
            if (!Global.Current.FireEvents || Id == 0 || !DbPersisted)
            {
                return;
            }

            if (!IsUpdating)
            {
                throw new Exception("Changed state outside of begin/end update block");
            }

            DefaultMultiObjectLock.ThrowExceptionIfNotLocked(this);
        }
Esempio n. 4
0
        protected override void CheckUpdateMode()
        {
            //If city is null then we dont care about being inside of a begin/end update block
            if (!Global.Current.FireEvents || City == null)
            {
                return;
            }

            if (!Updating)
            {
                throw new Exception("Changed state outside of begin/end update block");
            }

            DefaultMultiObjectLock.ThrowExceptionIfNotLocked(City);
        }
Esempio n. 5
0
        private void CheckUpdateMode(bool checkStationedCity = true)
        {
            if (!Global.Current.FireEvents || City == null)
            {
                return;
            }

            if (!isUpdating)
            {
                throw new Exception("Changed state outside of begin/end update block");
            }

            DefaultMultiObjectLock.ThrowExceptionIfNotLocked(this);

            if (checkStationedCity && station != null)
            {
                DefaultMultiObjectLock.ThrowExceptionIfNotLocked(station);
            }
        }
Esempio n. 6
0
        public Error Contribute(uint playerId, Resource resource)
        {
            ITribesman tribesman;

            if (!tribesmen.TryGetValue(playerId, out tribesman))
            {
                return(Error.TribesmanNotFound);
            }

            DefaultMultiObjectLock.ThrowExceptionIfNotLocked(tribesman);
            tribesman.Contribution += resource;
            Resource += resource;
            dbManager.Save(tribesman, this);

            TribesmanContributed.Raise(this, new TribesmanContributedEventArgs {
                Tribe = this, Player = tribesman.Player, Resource = resource
            });
            return(Error.Ok);
        }
Esempio n. 7
0
        public Error AddTribesman(ITribesman tribesman, bool ignoreRequirements = false)
        {
            if (tribesmen.ContainsKey(tribesman.Player.PlayerId))
            {
                return(Error.TribesmanAlreadyInTribe);
            }

            if (!ignoreRequirements)
            {
                if (LeavingTribesmates.Any(p =>
                                           p.PlayerId == tribesman.Player.PlayerId &&
                                           SystemClock.Now.Subtract(p.TimeLeft).TotalDays < DAYS_BEFORE_REJOIN_ALLOWED))
                {
                    return(Error.TribeCannotRejoinYet);
                }

                var totalSlots = Level * MEMBERS_PER_LEVEL -
                                 LeavingTribesmates.Count(p => SystemClock.Now.Subtract(p.TimeLeft).TotalHours < HOURS_BEFORE_SLOT_REOPENS);

                if (tribesmen.Count >= totalSlots)
                {
                    return(Error.TribeFull);
                }
            }

            DbLoaderAdd(tribesman);

            DefaultMultiObjectLock.ThrowExceptionIfNotLocked(tribesman);
            dbManager.Save(tribesman);

            if (tribesman.Player.Session != null)
            {
                channel.Subscribe(tribesman.Player.Session, "/TRIBE/" + Id);
            }

            TribesmanJoined.Raise(this, new TribesmanEventArgs {
                Tribe = this, Player = tribesman.Player
            });
            return(Error.Ok);
        }
Esempio n. 8
0
        public Error SetRank(uint playerId, byte rank)
        {
            ITribesman tribesman;
            ITribeRank tribeRank;

            if (rank == 0)
            {
                return(Error.TribesmanNotAuthorized);
            }

            if (!tribesmen.TryGetValue(playerId, out tribesman))
            {
                return(Error.TribesmanNotFound);
            }

            if (!ranks.TryGetValue(rank, out tribeRank))
            {
                return(Error.TribeRankNotFound);
            }

            DefaultMultiObjectLock.ThrowExceptionIfNotLocked(tribesman);

            if (IsOwner(tribesman.Player))
            {
                return(Error.TribesmanIsOwner);
            }

            tribesman.Rank = tribeRank;
            dbManager.Save(tribesman);
            tribesman.Player.TribeUpdate();

            TribesmanRankChanged.Raise(this, new TribesmanEventArgs {
                Tribe = this, Player = tribesman.Player
            });
            return(Error.Ok);
        }
Esempio n. 9
0
        private void NotifyActive(GameAction action, ActionState state)
        {
            DefaultMultiObjectLock.ThrowExceptionIfNotLocked(LockDelegate());

            ActiveAction actionStub;

            if (!active.TryGetValue(action.ActionId, out actionStub))
            {
                return;
            }

            switch (state)
            {
            case ActionState.Rescheduled:
                if (ActionRescheduled != null)
                {
                    ActionRescheduled(actionStub, state);
                }

                if (actionStub is ScheduledActiveAction)
                {
                    dbManager.Save(actionStub);
                    Schedule(action as ScheduledActiveAction);
                }
                break;

            case ActionState.Started:
                if (ActionStarted != null)
                {
                    ActionStarted(actionStub, state);
                }

                if (action is ScheduledActiveAction)
                {
                    dbManager.Save(actionStub);
                    Schedule(action as ScheduledActiveAction);
                }
                break;

            case ActionState.Completed:
                active.Remove(actionStub.ActionId);
                action.IsDone = true;

                if (ActionRemoved != null)
                {
                    ActionRemoved(actionStub, state);
                }

                if (action is ScheduledActiveAction)
                {
                    dbManager.Delete(actionStub);
                    scheduler.Remove(action as ISchedule);
                }
                break;

            case ActionState.Fired:
                if (action is ScheduledActiveAction)
                {
                    dbManager.Save(actionStub);
                    Schedule(action as ScheduledActiveAction);
                }
                break;

            case ActionState.Failed:
                active.Remove(actionStub.ActionId);
                action.IsDone = true;

                if (ActionRemoved != null)
                {
                    ActionRemoved(actionStub, state);
                }

                if (action is ScheduledActiveAction)
                {
                    dbManager.Delete(actionStub);
                    scheduler.Remove(action as ISchedule);
                }
                break;
            }
        }