public void ClearCollectionEventTest() { var itemDef = new ItemDefinition(Guid.NewGuid()) { maxStackSize = 10 }; var item = new ItemInstance(Guid.NewGuid(), itemDef); CollectionSlotsChangedResult changedResult = null; int changedEventCount = 0; _collection.Set(0, item, 3); _collection.Set(1, (ItemInstance)item.Clone(), 2); _collection.Set(3, (ItemInstance)item.Clone(), 4); _collection.OnSlotsChanged += (sender, e) => { changedResult = e; changedEventCount++; }; _collection.Clear(); Assert.AreEqual(1, changedEventCount); Assert.AreEqual(3, changedResult.affectedSlots.Length); Assert.AreEqual(0, changedResult.affectedSlots[0]); Assert.AreEqual(1, changedResult.affectedSlots[1]); Assert.AreEqual(3, changedResult.affectedSlots[2]); for (int i = 0; i < _collection.slotCount; i++) { Assert.AreEqual(0, _collection.GetAmount(i)); } }
// TODO: consider overwriting Add / Set methods for equipment -> Check if item we're trying to set / add is in a player collection. // TODO: Clients that join later need to get read permission to equipment collection of other clients and replicate the visually equipped items. protected void NotifyOnSlotsChanged(object sender, CollectionSlotsChangedResult data) { if (owner.isServer) { var clients = UNetPermissionsRegistry.collections.GetAllIdentitiesWithPermission(this); foreach (var client in clients) { // TODO: Validate if there's at least read access var actionBridge = client.GetComponent <UNetActionsBridge>(); if (actionBridge != null) { logger.LogVerbose($"[Server] Notify client with NetID: {actionBridge.identity.netId} of changed itemGuid: {data.affectedSlots.ToSimpleString()} on equipment collection: {collectionName}", this); // TODO: Combine all affected slots in single call to client! foreach (var slot in data.affectedSlots) { if (this[slot] != null) { // TODO: Remove this line - Client should request it by itself, or the server should check if the client already knows the item instance... actionBridge.Server_TellClientToRegisterItemInstance(this[slot]); } actionBridge.Server_SetSlotOnClient(new SlotDataMessage() { collectionGuid = ID, index = (ushort)slot, amount = (ushort)GetAmount(slot), itemInstanceGuid = this[slot]?.ID ?? System.Guid.Empty }); } } } } }
private void NotifyOnSlotsChanged(object sender, CollectionSlotsChangedResult data) { if (PhotonNetwork.IsMasterClient /*owner.IsServer() /*owner.isServer*/) { var clients = PUN2PermissionsRegistry.collections.GetAllIdentitiesWithPermission(this); foreach (var client in clients) { // TODO: Validate if there's at least read access var actionBridge = client.GetComponent <PUN2ActionsBridge>(); if (actionBridge != null) { logger.LogVerbose($"[Server] Notify client with ViewID: {actionBridge.photonView.ViewID} of changed itemGuid: {data.affectedSlots.ToSimpleString()} on equipment collection: {collectionName}", this); // TODO: Combine all affected slots in single call to client! foreach (var slot in data.affectedSlots) { if (this[slot] != null) { // TODO: Remove this line - Client should request it by itself, or the server should check if the client already knows the item instance... actionBridge.Server_TellClientToRegisterItemInstance(this[slot]); } actionBridge.Server_SetSlotOnClient( collectionGuid: ID, itemInstanceGuid: this[slot]?.ID ?? System.Guid.Empty, index: slot, amount: GetAmount(slot) ); } } } } }
protected virtual void OnCollectionSlotsChanged(object sender, CollectionSlotsChangedResult e) { logger.LogVerbose($"Collection {_collectionName} slots changed: {e.affectedSlots.Length} affected slots. - {e.affectedSlots.ToSimpleString()}"); foreach (var slot in e.affectedSlots) { RepaintOrQueue(slot); } }
public void ClearEmptyCollectionEventTest() { CollectionSlotsChangedResult changedResult = null; int changedEventCount = 0; _collection.OnSlotsChanged += (sender, e) => { changedResult = e; changedEventCount++; }; _collection.Clear(); Assert.AreEqual(0, changedEventCount); }
public void SwapOrMergeSlotsChangedEventsTest() { var itemDef = new ItemDefinition(Guid.NewGuid()) { maxStackSize = 3 }; var item = new CollectionItemInstance(Guid.NewGuid(), itemDef); var item2 = new CollectionItemInstance(Guid.NewGuid(), itemDef); CollectionSlotsChangedResult changeResult = null; object changeObj = null; int changeCallCount = 0; var set1 = _collection.Set(0, item, 3); var set2 = _collection.Set(1, item2, 2); Assert.IsTrue(set1.result); Assert.IsTrue(set2.result); _collection.OnSlotsChanged += (sender, result) => { changeObj = sender; changeResult = result; changeCallCount++; }; var swap = _collection.SwapOrMerge(0, _collection, 1, 3); Assert.IsTrue(swap.result); Assert.AreSame(item2, _collection[0]); Assert.AreSame(item, _collection[1]); Assert.AreEqual(2, _collection.GetAmount(0)); Assert.AreEqual(3, _collection.GetAmount(1)); Assert.AreEqual(1, changeCallCount); Assert.AreSame(changeObj, _collection); Assert.AreEqual(2, changeResult.affectedSlots.Length); Assert.IsTrue(changeResult.affectedSlots.Contains(0)); Assert.IsTrue(changeResult.affectedSlots.Contains(1)); }
public void GenerateSlotsChangedEventTest() { CollectionSlotsChangedResult changedResult = null; int changedEventCount = 0; _collection.OnSlotsChanged += (sender, e) => { changedResult = e; changedEventCount++; }; _collection.GenerateSlotsRange <CollectionSlot <IItemInstance> >(5, 9); Assert.AreEqual(1, changedEventCount); Assert.AreEqual(5, changedResult.affectedSlots.Length); Assert.AreEqual(5, changedResult.affectedSlots[0]); Assert.AreEqual(6, changedResult.affectedSlots[1]); Assert.AreEqual(7, changedResult.affectedSlots[2]); Assert.AreEqual(8, changedResult.affectedSlots[3]); Assert.AreEqual(9, changedResult.affectedSlots[4]); }
public void EquipmentCollectionOnEquipEventTest() { CollectionAddResult equipmentAddResult = null; CollectionRemoveResult <IEquippableItemInstance> equipmentRemoveResult = null; CollectionSlotsChangedResult equipmentChangeResult = null; int equipmentAddEventCount = 0; int equipmentRemoveEventCount = 0; int equipmentChangeEventCount = 0; CollectionAddResult restoreAddResult = null; CollectionRemoveResult <IItemInstance> restoreRemoveResult = null; CollectionSlotsChangedResult restoreChangeResult = null; int restoreAddEventCount = 0; int restoreRemoveEventCount = 0; int restoreChangeEventCount = 0; _equipmentCollection.OnAddedItem += (sender, result) => { equipmentAddResult = result; equipmentAddEventCount++; }; _equipmentCollection.OnRemovedItem += (sender, result) => { equipmentRemoveResult = result; equipmentRemoveEventCount++; }; _equipmentCollection.OnSlotsChanged += (sender, result) => { equipmentChangeResult = result; equipmentChangeEventCount++; }; _restoreToCollection.OnAddedItem += (sender, result) => { restoreAddResult = result; restoreAddEventCount++; }; _restoreToCollection.OnRemovedItem += (sender, result) => { restoreRemoveResult = result; restoreRemoveEventCount++; }; _restoreToCollection.OnSlotsChanged += (sender, result) => { restoreChangeResult = result; restoreChangeEventCount++; }; var set1 = _equippableCharacter.EquipAt(4, _arrowsItem); // Add event var set2 = _equippableCharacter.EquipAt(4, _shieldItem); // Unequips arrows + add event for shield Assert.IsNull(set1.error); Assert.IsNull(set2.error); Assert.AreEqual(2, equipmentAddEventCount); // Add arrows + shield Assert.AreEqual(1, equipmentRemoveEventCount); // Remove arrows Assert.AreEqual(3, equipmentChangeEventCount); // Arrow equip, unequip, shield equip Assert.AreEqual(1, restoreAddEventCount); // Add arrows Assert.AreEqual(0, restoreRemoveEventCount); Assert.AreEqual(1, restoreChangeEventCount); // Changed for arrows Assert.AreEqual(1, _restoreToCollection.GetAmount(_arrowsItem)); Assert.AreEqual(1, _equipmentCollection.GetAmount(_shieldItem)); }