Ejemplo n.º 1
0
        public void TestChangingCallbackAddLocks()
        {
            var obj  = new DummyLockable(1);
            var obj2 = new DummyLockable(2);
            var obj3 = new DummyLockable(3);

            int callCount = 0;

            CallbackLock.CallbackLockHandler lockFunc = delegate
            {
                if (callCount == 0)
                {
                    callCount++;
                    return(new ILockable[] { obj2 });
                }

                callCount++;
                return(new ILockable[] { obj3, obj2 });
            };
            var lck = new CallbackLock(multiObjectLockFactory).Lock(lockFunc, null, obj);

            DefaultMultiObjectLock.IsLocked(obj).Should().BeTrue();
            DefaultMultiObjectLock.IsLocked(obj2).Should().BeTrue();
            DefaultMultiObjectLock.IsLocked(obj3).Should().BeTrue();

            lck.UnlockAll();

            DefaultMultiObjectLock.IsLocked(obj).Should().BeFalse();
            DefaultMultiObjectLock.IsLocked(obj2).Should().BeFalse();
            DefaultMultiObjectLock.IsLocked(obj3).Should().BeFalse();
        }
Ejemplo n.º 2
0
        private void CheckUpdateMode()
        {
            if (!Global.Current.FireEvents)
            {
                return;
            }

            DefaultMultiObjectLock.ThrowExceptionIfNotLocked(BaseStation);
        }
Ejemplo n.º 3
0
        public void TestEmptyListFromCallback()
        {
            var obj = new DummyLockable(1);

            CallbackLock.CallbackLockHandler lockFunc = custom => new ILockable[] {};
            var lck = new CallbackLock(multiObjectLockFactory).Lock(lockFunc, null, obj);

            DefaultMultiObjectLock.IsLocked(obj).Should().BeTrue();
            lck.UnlockAll();
            DefaultMultiObjectLock.IsLocked(obj).Should().BeFalse();
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        public void IsLocked_ShouldTellIfObjectsAreLocked(ILockable lock1, ILockable lock2, ILockable lock3)
        {
            lock1.Lock.Returns(new object());
            lock2.Lock.Returns(new object());
            lock3.Lock.Returns(new object());

            lock1.Hash.Returns(100);
            lock2.Hash.Returns(200);
            lock3.Hash.Returns(300);

            locker.Lock(new[] { lock1, lock2 });

            DefaultMultiObjectLock.IsLocked(lock1).Should().BeTrue();
            DefaultMultiObjectLock.IsLocked(lock2).Should().BeTrue();
            DefaultMultiObjectLock.IsLocked(lock3).Should().BeFalse();
        }
Ejemplo n.º 8
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);
            }
        }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
0
        public DefaultMultiObjectLockTests()
        {
            dbManager    = Substitute.For <IDbManager>();
            enterObjects = new List <object>();
            exitObjects  = new List <object>();

            locker = new DefaultMultiObjectLock(
                item =>
            {
                Monitor.Enter(item);
                enterObjects.Add(item);
            },
                item =>
            {
                Monitor.Exit(item);
                exitObjects.Add(item);
            },
                dbManager);
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
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;
            }
        }
Ejemplo n.º 14
0
 public CallbackLockTest()
 {
     DefaultMultiObjectLock.ClearCurrentLock();
 }
Ejemplo n.º 15
0
 public void Dispose()
 {
     DefaultMultiObjectLock.ClearCurrentLock();
 }