public void PurchaseAuction(long auctionId, EntityAgent buyerEntity, Entity auctioneerEntity, bool withDelivery, out string failureCode) { if (auctions.TryGetValue(auctionId, out var auction)) { if ((buyerEntity as EntityPlayer)?.PlayerUID == auction.SellerUid) { failureCode = "ownauction"; return; } // Already purchased if (auction.BuyerName != null) { failureCode = "alreadypurchased"; return; } int monehs = InventoryTrader.GetPlayerAssets(buyerEntity); int deliveryCosts = withDelivery ? DeliveryCostsByDistance(auctioneerEntity.Pos.XYZ, auction.SrcAuctioneerEntityPos) : 0; int totalcost = auction.Price + deliveryCosts; if (monehs < totalcost) { failureCode = "notenoughgears"; return; } InventoryTrader.DeductFromEntity(sapi, buyerEntity, totalcost); (auctioneerEntity as EntityTrader).Inventory?.GiveToTrader((int)(auction.Price * SalesCutRate + deliveryCosts)); string buyerName = buyerEntity.GetBehavior <EntityBehaviorNameTag>()?.DisplayName; if (buyerName == null) { buyerName = buyerEntity.Properties.Code.ToShortString(); } auction.BuyerName = buyerName; auction.WithDelivery = withDelivery; auction.BuyerUid = (buyerEntity as EntityPlayer)?.PlayerUID; auction.RetrievableTotalHours = sapi.World.Calendar.TotalHours + 1 + 3 * deliveryCosts; auction.DstAuctioneerEntityId = withDelivery ? auctioneerEntity.EntityId : auction.SrcAuctioneerEntityId; auction.DstAuctioneerEntityPos = withDelivery ? auctioneerEntity.Pos.XYZ : auction.SrcAuctioneerEntityPos; auction.State = EnumAuctionState.Sold; sendAuctions(new Auction[] { auction }, null); failureCode = null; return; } failureCode = "nosuchauction"; return; }
public void PlaceAuction(ItemSlot slot, int quantity, int price, double durationHours, int depositCost, EntityAgent sellerEntity, Entity auctioneerEntity, out string failureCode) { if (slot.StackSize < quantity) { failureCode = "notenoughitems"; return; } if (GetAuctionsFrom((sellerEntity as EntityPlayer).Player).Count > 30) { failureCode = "toomanyauctions"; return; } int monehs = InventoryTrader.GetPlayerAssets(sellerEntity); if (monehs < GetDepositCost(slot) * depositCost) { failureCode = "notenoughgears"; return; } failureCode = null; InventoryTrader.DeductFromEntity(sapi, sellerEntity, depositCost); (auctioneerEntity as EntityTrader).Inventory?.GiveToTrader(depositCost); long id = ++auctionsData.nextAuctionId; string sellerName = sellerEntity.GetBehavior <EntityBehaviorNameTag>()?.DisplayName; if (sellerName == null) { sellerName = sellerEntity.Properties.Code.ToShortString(); } float debt; string uid = (sellerEntity as EntityPlayer)?.PlayerUID ?? ""; auctionsData.DebtToTraderByPlayer.TryGetValue(uid, out debt); float traderCutGears = price * SalesCutRate + debt; auctionsData.DebtToTraderByPlayer[uid] = traderCutGears - (int)traderCutGears; var auction = new Auction() { AuctionId = id, ExpireTotalHours = sapi.World.Calendar.TotalHours + durationHours, ItemStack = slot.TakeOut(quantity), PostedTotalHours = sapi.World.Calendar.TotalHours, Price = price, TraderCut = (int)traderCutGears, SellerName = sellerName, SellerUid = (sellerEntity as EntityPlayer)?.PlayerUID, SellerEntityId = sellerEntity.EntityId, SrcAuctioneerEntityPos = auctioneerEntity.Pos.XYZ, SrcAuctioneerEntityId = auctioneerEntity.EntityId }; auctions.Add(id, auction); slot.MarkDirty(); sendAuctions(new Auction[] { auction }, null); }