public static CreateItemResult CalculateItemsResultExceptContainer( CreateItemResult result, IItemsContainer itemsContainer) { if (itemsContainer == null) { return(result); } var itemAmounts = new Dictionary <IItem, ushort>(result.ItemAmounts); foreach (var groundItem in itemsContainer.Items) { if (!itemAmounts.TryGetValue(groundItem, out var count)) { continue; } var newCount = count - groundItem.Count; if (newCount > 0) { itemAmounts[groundItem] = (ushort)newCount; } else { itemAmounts.Remove(groundItem); } } var newResult = new CreateItemResult(itemAmounts, (uint)itemAmounts.Sum(p => p.Value)); return(newResult); }
public static void ServerSendItemsNotification( ICharacter character, CreateItemResult createItemResult) { if (createItemResult.TotalCreatedCount == 0) { return; } // clone the result object to not modify the original createItemResult = new CreateItemResult(new Dictionary <IItem, ushort>(createItemResult.ItemAmounts), createItemResult.TotalCreatedCount); // do not send notifications for the items which were not added to the character createItemResult.ItemAmounts.RemoveAllByKey(k => k.Container.Owner != character); if (createItemResult.TotalCreatedCount == 0) { return; } var itemsChangedCount = SharedGetItemsChangedCount(createItemResult); if (itemsChangedCount != null) { ServerSendItemsNotification(character, itemsChangedCount); } }
public static Dictionary <IProtoItem, int> SharedGetItemsChangedCount(CreateItemResult createItemResult) { if (createItemResult.TotalCreatedCount == 0) { return(null); } var itemAmounts = createItemResult.ItemAmounts; if (itemAmounts == null || itemAmounts.Count == 0) { return(null); } var itemsChangedCount = new Dictionary <IProtoItem, int>(); foreach (var pair in itemAmounts) { var protoItem = pair.Key.ProtoItem; var count = (int)pair.Value; if (itemsChangedCount.TryGetValue(protoItem, out var currentCount)) { count += currentCount; } itemsChangedCount[protoItem] = count; } return(itemsChangedCount); }
public CreateItemResult Execute( DelegateSpawnDropItem delegateSpawnDropItem, DropItemContext dropItemContext, double probabilityMultiplier) { this.Freeze(); var result = new CreateItemResult() { IsEverythingCreated = true }; using var selectedEntries = Api.Shared.GetTempList <Entry>(); this.SelectRandomEntries(selectedEntries); // execute selected entries foreach (var entry in selectedEntries) { ExecuteEntry(entry, dropItemContext, out var entryResult, probabilityMultiplier, delegateSpawnDropItem); result.MergeWith(entryResult); } return(result); }
private static void ExecuteEntry( Entry entry, DropItemContext dropItemContext, out CreateItemResult createItemResult, double probabilityMultiplier, DelegateSpawnDropItem delegateSpawnDropItem) { if (entry.Condition != null) { try { if (!entry.Condition(dropItemContext)) { // condition not match createItemResult = null; return; } } catch (Exception ex) { Api.Logger.Exception(ex, "Exception during checking condition for droplist item " + entry); createItemResult = null; return; } } probabilityMultiplier *= entry.Probability; createItemResult = entry.EntryNestedList != null ? entry.EntryNestedList.Execute(delegateSpawnDropItem, dropItemContext, probabilityMultiplier) : ExecuteSpawnItem(entry.EntryItem, delegateSpawnDropItem, probabilityMultiplier); }
public async Task <CreateItemResult> CreateAsync(CreateItemModel createItemModel) { var authResult = await authenticatorsStore.CreateItemModelAuthenticator.AuthenticateAsync(createItemModel); if (!authResult.Succeed) { return(new CreateItemResult(authResult)); } var validResult = await validatorsStore.CreateItemModelValidator.ValidateAsync(createItemModel); if (!validResult.Succeed) { return(new CreateItemResult(validResult)); } var itemModel = mapper.Map <ItemModel>(createItemModel); await itemsCrudService.CreateAsync(itemModel); await collectionsManager.AttachItemToCollection(itemModel.Id, createItemModel.CollectionId); await tagsManager.AttachTagsToItemAsync(createItemModel.Tags, itemModel.Id); var result = new CreateItemResult { CreatedItemId = itemModel.Id }; return(result); }
public static void ServerSendItemsNotification( ICharacter character, CreateItemResult createItemResult, IItemsContainer exceptItemsContainer) { createItemResult = CalculateItemsResultExceptContainer(createItemResult, exceptItemsContainer); ServerSendItemsNotification(character, createItemResult); }
public static void ClientShowItemsNotification(CreateItemResult createItemResult) { var itemsChangedCount = SharedGetItemsChangedCount(createItemResult); if (itemsChangedCount != null) { ClientShowItemsNotification(itemsChangedCount); } }
public static void ServerSendItemsNotification( ICharacter character, CreateItemResult createItemResult) { var itemsChangedCount = SharedGetItemsChangedCount(createItemResult); if (itemsChangedCount != null) { ServerSendItemsNotification(character, itemsChangedCount); } }
public void CallApiNormal(PlayerData player, CreateItemResult result_type) { if (Network.Mode == Network.EConnectMode.Online) { this.ExecRequest((WebAPI) new ReqItemComposit(this.mItemParam.iname, result_type == CreateItemResult.CanCreateCommon, new Network.ResponseCallback(((FlowNode_Network)this).ResponseCallback))); ((Behaviour)this).set_enabled(true); } else { player.CreateItem(this.mItemParam); } }
public bool ServerGather(IStaticWorldObject worldObject, ICharacter character) { //MOD PublicState publicState = GetPublicState(worldObject); var lootDroplist = publicState.ProtoCharacterMob.LootDroplist; var dropItemContext = new DropItemContext(character, worldObject); CreateItemResult dropItemResultFinal = new CreateItemResult(); var level = publicState.Level; for (int i = 0; i < level; i++) { CreateItemResult dropItemResult; var attemptRemains = 200; do { dropItemResult = lootDroplist.TryDropToCharacterOrGround(character, character.TilePosition, dropItemContext, out _); dropItemResultFinal.MergeWith(dropItemResult); } // ensure that at least something is spawned... // perhaps that's not a good idea, but we have an attempts limit while (dropItemResult.TotalCreatedCount == 0 && --attemptRemains > 0); } if (!dropItemResultFinal.IsEverythingCreated) { Logger.Warning("Not all loot items were provided by " + worldObject + " - there is not enough space in inventory and around the character"); } // probably the attempts limit exceeded and nothing is spawned // we don't consider this as an issue as the probability of this is too rare Logger.Info(worldObject + " was gathered", character); Server.World.DestroyObject(worldObject); NotificationSystem.ServerSendItemsNotification(character, dropItemResultFinal); character.ServerAddSkillExperience <SkillHunting>(SkillHunting.ExperienceForGather * level); return(true); }
public static CreateItemResult TrySpawnToContainer( IReadOnlyOutputItems outputItems, IItemsContainer toContainer) { var itemsService = Api.Server.Items; var result = new CreateItemResult() { IsEverythingCreated = true }; var character = toContainer.Owner as ICharacter; foreach (var outputItem in outputItems.Items) { var count = GenerateCount(outputItem); if (count == 0) { continue; } CreateItemResult createItemResult; if (character is not null) { createItemResult = itemsService.CreateItem(outputItem.ProtoItem, character, count); } else { createItemResult = itemsService.CreateItem(outputItem.ProtoItem, toContainer, count); } result.MergeWith(createItemResult); } return(result); }
public Task <CreateItemResult> ExecuteAsync(CreateItemParameters parameters, CancellationToken cancellationToken = default) { return(Task.FromResult(CreateItemResult.Ok("Hallo world"))); }
private static TradingResult ServerExecuteTrade( TradingStationLot lot, IItemsContainerProvider sellerContainers, IItemsContainerProvider buyerContainers, bool isPlayerBuying) { // find items to buy by other party if (!SharedTryFindItemsOfType(sellerContainers, lot.ProtoItem, lot.LotQuantity, out var itemsToSell)) { return(isPlayerBuying ? TradingResult.ErrorNotEnoughItemsOnStation : TradingResult.ErrorNotEnoughItemsOnPlayer); } // try to find money to pay to other party var countCoinPenny = (uint)lot.PriceCoinPenny; var countCoinShiny = (uint)lot.PriceCoinShiny; if (!SharedTryFindItemsOfType(buyerContainers, ProtoItemCoinPenny.Value, countCoinPenny, out _) || !SharedTryFindItemsOfType(buyerContainers, ProtoItemCoinShiny.Value, countCoinShiny, out _)) { return(isPlayerBuying ? TradingResult.ErrorNotEnoughMoneyOnStation : TradingResult.ErrorNotEnoughMoneyOnPlayer); } // ensure there is enough space to store the sold items if (!ServerItems.CanCreateItem(buyerContainers, lot.ProtoItem, lot.LotQuantity)) { return(isPlayerBuying ? TradingResult.ErrorNotEnoughSpaceOnPlayerForPurchasedItem : TradingResult.ErrorNotEnoughSpaceOnStationForSoldItem); } // try create money in the source containers var sourceContainerResult = new CreateItemResult() { IsEverythingCreated = true }; if (lot.PriceCoinPenny > 0) { sourceContainerResult.MergeWith( ServerItems.CreateItem(ProtoItemCoinPenny.Value, sellerContainers, countCoinPenny)); } if (lot.PriceCoinShiny > 0) { sourceContainerResult.MergeWith( ServerItems.CreateItem(ProtoItemCoinShiny.Value, sellerContainers, countCoinShiny)); } if (!sourceContainerResult.IsEverythingCreated) { sourceContainerResult.Rollback(); // TODO: check this return(isPlayerBuying ? TradingResult.ErrorNotEnoughSpaceOnStationForSoldItem : TradingResult.ErrorNotEnoughSpaceOnPlayerForPurchasedItem); } // try moving (bought) items var itemsCountToDestroyRemains = (int)lot.LotQuantity; foreach (var item in itemsToSell) { if (itemsCountToDestroyRemains <= 0) { break; } ServerItems.MoveOrSwapItem(item, buyerContainers, out var movedCount, countToMove: (ushort)itemsCountToDestroyRemains); itemsCountToDestroyRemains -= movedCount; } if (itemsCountToDestroyRemains > 0) { // should be impossible Logger.Error( "Cannot move all sold items! But we've verified that the sellerContainers have them all..."); } if (countCoinPenny > 0) { ServerItems.DestroyItemsOfType(buyerContainers, ProtoItemCoinPenny.Value, countCoinPenny, out _); } if (countCoinShiny > 0) { ServerItems.DestroyItemsOfType(buyerContainers, ProtoItemCoinShiny.Value, countCoinShiny, out _); } Logger.Important($"Successfully completed trading transaction: {lot}"); return(TradingResult.Success); }
public override CreateItemResult ServerCreateItem( ICharacter character, IProtoItem protoItem, uint countToSpawn = 1) { if (character == null) { throw new ArgumentNullException(nameof(character)); } if (protoItem == null) { throw new ArgumentNullException(nameof(protoItem)); } if (countToSpawn == 0) { throw new Exception("Cannot add item to character: item count is 0"); } var countToSpawnRemains = countToSpawn; var privateState = GetPrivateState(character); var containerHotbar = privateState.ContainerHotbar; var containerInventory = privateState.ContainerInventory; var serverItemsService = Server.Items; var result = new CreateItemResult(); // define a function for spawning item at specified container bool TrySpawnItem(IItemsContainer container, bool onlyToExistingStacks) { var createItemResult = serverItemsService.CreateItem( protoItem, container, countToSpawnRemains, slotId: null, onlyAddToExistingStacks: onlyToExistingStacks); if (createItemResult.TotalCreatedCount == 0) { // cannot create item return(false); } // something created (perhaps all) result.MergeWith(createItemResult); countToSpawnRemains = (ushort)(countToSpawnRemains - createItemResult.TotalCreatedCount); var isEverythingCreated = countToSpawnRemains == 0; return(isEverythingCreated); } // 1. Try to add to existing stacks in hotbar. // 3. Try to add to existing stacks or spawn as new stacks in inventory. // 3. Try to spawn as new stacks in hotbar. // TODO: 4. Try to spawn on the ground. (actually implemented separately via scripting) if (TrySpawnItem(containerHotbar, onlyToExistingStacks: true) || TrySpawnItem(containerInventory, onlyToExistingStacks: false) || TrySpawnItem(containerHotbar, onlyToExistingStacks: false)) { // all items are created! result.IsEverythingCreated = true; } return(result); }
public override void OnActivate(int pinID) { if (pinID != 0 && pinID != 100) { return; } PlayerData player = MonoSingleton <GameManager> .Instance.Player; this.mItemParam = MonoSingleton <GameManager> .Instance.GetItemParam(GlobalVars.SelectedCreateItemID); if (!player.CheckItemCapacity(this.mItemParam, 1)) { ((Behaviour)this).set_enabled(false); this.ActivateOutputLinks(2); } else if (pinID == 0) { if (MonoSingleton <GameManager> .Instance.GetRecipeParam(this.mItemParam.recipe).cost > player.Gold) { ((Behaviour)this).set_enabled(false); this.ActivateOutputLinks(3); } else { CreateItemResult result_type = player.CheckCreateItem(this.mItemParam); if (result_type == CreateItemResult.NotEnough) { ((Behaviour)this).set_enabled(false); this.ActivateOutputLinks(4); } else if (result_type == CreateItemResult.CanCreateCommon) { int cost = 0; Dictionary <string, int> consumes = (Dictionary <string, int>)null; bool is_ikkatsu = false; NeedEquipItemList item_list = new NeedEquipItemList(); MonoSingleton <GameManager> .GetInstanceDirect().Player.CheckEnableCreateItem(this.mItemParam, ref is_ikkatsu, ref cost, ref consumes, item_list); UIUtility.ConfirmBox(LocalizedText.Get("sys.COMMON_EQUIP_CHECK_MADE", new object[1] { (object)item_list.GetCommonItemListString() }), (UIUtility.DialogResultEvent)(go => this.CallApiNormal(player, result_type)), (UIUtility.DialogResultEvent)null, (GameObject)null, false, -1, (string)null, (string)null); } else { this.CallApiNormal(player, result_type); } } } else { int cost = 0; Dictionary <string, int> consumes = (Dictionary <string, int>)null; bool is_ikkatsu = false; NeedEquipItemList need_euip_item = new NeedEquipItemList(); bool flag = player.CheckEnableCreateItem(this.mItemParam, ref is_ikkatsu, ref cost, ref consumes, need_euip_item); if (cost > player.Gold) { ((Behaviour)this).set_enabled(false); this.ActivateOutputLinks(3); } else if (!flag && !need_euip_item.IsEnoughCommon()) { ((Behaviour)this).set_enabled(false); this.ActivateOutputLinks(4); } else if (need_euip_item.IsEnoughCommon()) { UIUtility.ConfirmBox(LocalizedText.Get("sys.COMMON_EQUIP_CHECK_ONETAP", new object[1] { (object)need_euip_item.GetCommonItemListString() }), (UIUtility.DialogResultEvent)(go => this.CallApi(need_euip_item, player)), (UIUtility.DialogResultEvent)null, (GameObject)null, false, -1, (string)null, (string)null); } else { this.CallApi(need_euip_item, player); } } }