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(); }
private void CheckUpdateMode() { if (!Global.Current.FireEvents) { return; } DefaultMultiObjectLock.ThrowExceptionIfNotLocked(BaseStation); }
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(); }
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); }
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); }
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); }
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(); }
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); } }
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); }
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); }
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); }
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); }
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; } }
public CallbackLockTest() { DefaultMultiObjectLock.ClearCurrentLock(); }
public void Dispose() { DefaultMultiObjectLock.ClearCurrentLock(); }