public override void Save(BaseNetworkable.SaveInfo info) { base.Save(info); info.msg.vendingMachine = new ProtoBuf.VendingMachine() { ShouldPool = false, shopName = this.shopName }; if (this.sellOrders != null) { info.msg.vendingMachine.sellOrderContainer = new ProtoBuf.VendingMachine.SellOrderContainer() { ShouldPool = false, sellOrders = new List <ProtoBuf.VendingMachine.SellOrder>() }; foreach (ProtoBuf.VendingMachine.SellOrder sellOrder in this.sellOrders.sellOrders) { ProtoBuf.VendingMachine.SellOrder sellOrder1 = new ProtoBuf.VendingMachine.SellOrder() { ShouldPool = false }; sellOrder.CopyTo(sellOrder1); info.msg.vendingMachine.sellOrderContainer.sellOrders.Add(sellOrder1); } } }
public static void SerializeDelta(Stream stream, VendingMachine.SellOrder instance, VendingMachine.SellOrder previous) { MemoryStream memoryStream = Pool.Get <MemoryStream>(); if (instance.itemToSellID != previous.itemToSellID) { stream.WriteByte(8); ProtocolParser.WriteUInt64(stream, (ulong)instance.itemToSellID); } if (instance.itemToSellAmount != previous.itemToSellAmount) { stream.WriteByte(16); ProtocolParser.WriteUInt64(stream, (ulong)instance.itemToSellAmount); } if (instance.currencyID != previous.currencyID) { stream.WriteByte(24); ProtocolParser.WriteUInt64(stream, (ulong)instance.currencyID); } if (instance.currencyAmountPerItem != previous.currencyAmountPerItem) { stream.WriteByte(32); ProtocolParser.WriteUInt64(stream, (ulong)instance.currencyAmountPerItem); } if (instance.inStock != previous.inStock) { stream.WriteByte(40); ProtocolParser.WriteUInt64(stream, (ulong)instance.inStock); } stream.WriteByte(48); ProtocolParser.WriteBool(stream, instance.currencyIsBP); stream.WriteByte(56); ProtocolParser.WriteBool(stream, instance.itemToSellIsBP); Pool.FreeMemoryStream(ref memoryStream); }
public void AddSellOrder(int itemToSellID, int itemToSellAmount, int currencyToUseID, int currencyAmount, byte bpState) { ItemDefinition itemDefinition = ItemManager.FindItemDefinition(itemToSellID); ItemDefinition itemDefinition1 = ItemManager.FindItemDefinition(currencyToUseID); if (itemDefinition == null || itemDefinition1 == null) { return; } currencyAmount = Mathf.Clamp(currencyAmount, 1, 10000); itemToSellAmount = Mathf.Clamp(itemToSellAmount, 1, itemDefinition.stackable); ProtoBuf.VendingMachine.SellOrder sellOrder = new ProtoBuf.VendingMachine.SellOrder() { ShouldPool = false, itemToSellID = itemToSellID, itemToSellAmount = itemToSellAmount, currencyID = currencyToUseID, currencyAmountPerItem = currencyAmount, currencyIsBP = (bpState == 3 ? true : bpState == 2), itemToSellIsBP = (bpState == 3 ? true : bpState == 1) }; Interface.CallHook("OnAddVendingOffer", this, sellOrder); this.sellOrders.sellOrders.Add(sellOrder); this.RefreshSellOrderStockLevel(itemDefinition); this.UpdateMapMarker(); base.SendNetworkUpdate(BasePlayer.NetworkQueue.Update); }
public static VendingMachine.SellOrder Deserialize(Stream stream, VendingMachine.SellOrder instance, bool isDelta) { while (true) { int num = stream.ReadByte(); if (num == -1) { break; } if (num <= 24) { if (num == 8) { instance.itemToSellID = (int)ProtocolParser.ReadUInt64(stream); continue; } else if (num == 16) { instance.itemToSellAmount = (int)ProtocolParser.ReadUInt64(stream); continue; } else if (num == 24) { instance.currencyID = (int)ProtocolParser.ReadUInt64(stream); continue; } } else if (num <= 40) { if (num == 32) { instance.currencyAmountPerItem = (int)ProtocolParser.ReadUInt64(stream); continue; } else if (num == 40) { instance.inStock = (int)ProtocolParser.ReadUInt64(stream); continue; } } else if (num == 48) { instance.currencyIsBP = ProtocolParser.ReadBool(stream); continue; } else if (num == 56) { instance.itemToSellIsBP = ProtocolParser.ReadBool(stream); continue; } Key key = ProtocolParser.ReadKey((byte)num, stream); if (key.Field == 0) { throw new ProtocolBufferException("Invalid field id: 0, something went wrong in the stream"); } ProtocolParser.SkipKey(stream, key); } return(instance); }
public void Server_Purchase(RPCMessage msg) { if (!CanPlayerInteract(msg.player)) { return; } if (!_marketplace.IsValid(true)) { Debug.LogError("Marketplace is not set", this); return; } uint num = msg.read.UInt32(); int num2 = msg.read.Int32(); int num3 = msg.read.Int32(); VendingMachine vendingMachine = BaseNetworkable.serverEntities.Find(num) as VendingMachine; if (vendingMachine == null || !BaseEntityEx.IsValid(vendingMachine) || num2 < 0 || num2 >= vendingMachine.sellOrders.sellOrders.Count || num3 <= 0 || base.inventory.IsFull()) { return; } GetDeliveryEligibleVendingMachines(null); if (_deliveryEligible == null || !_deliveryEligible.Contains(num)) { return; } try { _transactionActive = true; int num4 = deliveryFeeAmount; ProtoBuf.VendingMachine.SellOrder sellOrder = vendingMachine.sellOrders.sellOrders[num2]; if (!CanPlayerAffordOrderAndDeliveryFee(msg.player, sellOrder, num3)) { return; } int num5 = msg.player.inventory.Take(null, deliveryFeeCurrency.itemid, num4); if (num5 != num4) { Debug.LogError($"Took an incorrect number of items for the delivery fee (took {num5}, should have taken {num4})"); } ClientRPCPlayer(null, msg.player, "Client_ShowItemNotice", deliveryFeeCurrency.itemid, -num4, false); if (!vendingMachine.DoTransaction(msg.player, num2, num3, base.inventory, _onCurrencyRemovedCached, _onItemPurchasedCached)) { Item item = ItemManager.CreateByItemID(deliveryFeeCurrency.itemid, num4, 0uL); if (!msg.player.inventory.GiveItem(item)) { item.Drop(msg.player.inventory.containerMain.dropPosition, msg.player.inventory.containerMain.dropVelocity); } } else { RestrictToPlayer(msg.player); RegisterOrder(msg.player, vendingMachine); } } finally { _transactionActive = false; } }
public static VendingMachine.SellOrder Deserialize(byte[] buffer, VendingMachine.SellOrder instance, bool isDelta = false) { using (MemoryStream memoryStream = new MemoryStream(buffer)) { VendingMachine.SellOrder.Deserialize(memoryStream, instance, isDelta); } return(instance); }
public virtual void WriteToStreamDelta(Stream stream, VendingMachine.SellOrder previous) { if (previous == null) { VendingMachine.SellOrder.Serialize(stream, this); return; } VendingMachine.SellOrder.SerializeDelta(stream, this, previous); }
public static VendingMachine.SellOrder Deserialize(byte[] buffer) { VendingMachine.SellOrder sellOrder = Pool.Get <VendingMachine.SellOrder>(); using (MemoryStream memoryStream = new MemoryStream(buffer)) { VendingMachine.SellOrder.Deserialize(memoryStream, sellOrder, false); } return(sellOrder); }
public void CopyTo(VendingMachine.SellOrder instance) { instance.itemToSellID = this.itemToSellID; instance.itemToSellAmount = this.itemToSellAmount; instance.currencyID = this.currencyID; instance.currencyAmountPerItem = this.currencyAmountPerItem; instance.inStock = this.inStock; instance.currencyIsBP = this.currencyIsBP; instance.itemToSellIsBP = this.itemToSellIsBP; }
public static byte[] SerializeToBytes(VendingMachine.SellOrder instance) { byte[] array; using (MemoryStream memoryStream = new MemoryStream()) { VendingMachine.SellOrder.Serialize(memoryStream, instance); array = memoryStream.ToArray(); } return(array); }
public static void ResetToPool(VendingMachine.SellOrder instance) { if (!instance.ShouldPool) { return; } instance.itemToSellID = 0; instance.itemToSellAmount = 0; instance.currencyID = 0; instance.currencyAmountPerItem = 0; instance.inStock = 0; instance.currencyIsBP = false; instance.itemToSellIsBP = false; Pool.Free <VendingMachine.SellOrder>(ref instance); }
public bool CanPlayerAffordOrderAndDeliveryFee(BasePlayer player, ProtoBuf.VendingMachine.SellOrder sellOrder, int numberOfTransactions) { int num = player.inventory.FindItemIDs(deliveryFeeCurrency.itemid).Sum((Item i) => i.amount); int num2 = deliveryFeeAmount; if (num < num2) { return(false); } if (sellOrder != null) { int num3 = sellOrder.currencyAmountPerItem * numberOfTransactions; if (sellOrder.currencyID == deliveryFeeCurrency.itemid && !sellOrder.currencyIsBP && num < num2 + num3) { return(false); } } return(true); }
public static void SerializeDelta(Stream stream, VendingMachine.SellOrderContainer instance, VendingMachine.SellOrderContainer previous) { MemoryStream memoryStream = Pool.Get <MemoryStream>(); if (instance.sellOrders != null) { for (int i = 0; i < instance.sellOrders.Count; i++) { VendingMachine.SellOrder item = instance.sellOrders[i]; stream.WriteByte(10); memoryStream.SetLength((long)0); VendingMachine.SellOrder.SerializeDelta(memoryStream, item, item); uint length = (uint)memoryStream.Length; ProtocolParser.WriteUInt32(stream, length); stream.Write(memoryStream.GetBuffer(), 0, (int)length); } } Pool.FreeMemoryStream(ref memoryStream); }
public static VendingMachine.SellOrder DeserializeLength(Stream stream, int length) { VendingMachine.SellOrder sellOrder = Pool.Get <VendingMachine.SellOrder>(); VendingMachine.SellOrder.DeserializeLength(stream, length, sellOrder, false); return(sellOrder); }
public static VendingMachine.SellOrder DeserializeLengthDelimited(Stream stream) { VendingMachine.SellOrder sellOrder = Pool.Get <VendingMachine.SellOrder>(); VendingMachine.SellOrder.DeserializeLengthDelimited(stream, sellOrder, false); return(sellOrder); }
public static VendingMachine.SellOrder DeserializeLengthDelimited(Stream stream, VendingMachine.SellOrder instance, bool isDelta) { long position = (long)ProtocolParser.ReadUInt32(stream); position += stream.Position; while (stream.Position < position) { int num = stream.ReadByte(); if (num == -1) { throw new EndOfStreamException(); } if (num <= 24) { if (num == 8) { instance.itemToSellID = (int)ProtocolParser.ReadUInt64(stream); continue; } else if (num == 16) { instance.itemToSellAmount = (int)ProtocolParser.ReadUInt64(stream); continue; } else if (num == 24) { instance.currencyID = (int)ProtocolParser.ReadUInt64(stream); continue; } } else if (num <= 40) { if (num == 32) { instance.currencyAmountPerItem = (int)ProtocolParser.ReadUInt64(stream); continue; } else if (num == 40) { instance.inStock = (int)ProtocolParser.ReadUInt64(stream); continue; } } else if (num == 48) { instance.currencyIsBP = ProtocolParser.ReadBool(stream); continue; } else if (num == 56) { instance.itemToSellIsBP = ProtocolParser.ReadBool(stream); continue; } Key key = ProtocolParser.ReadKey((byte)num, stream); if (key.Field == 0) { throw new ProtocolBufferException("Invalid field id: 0, something went wrong in the stream"); } ProtocolParser.SkipKey(stream, key); } if (stream.Position != position) { throw new ProtocolBufferException("Read past max limit"); } return(instance); }
public bool DoTransaction(BasePlayer buyer, int sellOrderId, int numberOfTransactions = 1) { Item item; Item item1; if (sellOrderId < 0 || sellOrderId > this.sellOrders.sellOrders.Count) { return(false); } if (Vector3.Distance(buyer.transform.position, base.transform.position) > 4f) { return(false); } object obj = Interface.CallHook("OnVendingTransaction", this, buyer, sellOrderId, numberOfTransactions); if (obj as bool) { return((bool)obj); } ProtoBuf.VendingMachine.SellOrder sellOrder = this.sellOrders.sellOrders[sellOrderId]; List <Item> list = this.inventory.FindItemsByItemID(sellOrder.itemToSellID); if (sellOrder.itemToSellIsBP) { list = ( from x in this.inventory.FindItemsByItemID(this.blueprintBaseDef.itemid) where x.blueprintTarget == sellOrder.itemToSellID select x).ToList <Item>(); } if (list == null || list.Count == 0) { return(false); } numberOfTransactions = Mathf.Clamp(numberOfTransactions, 1, (list[0].hasCondition ? 1 : 1000000)); int num = sellOrder.itemToSellAmount * numberOfTransactions; if (num > list.Sum <Item>((Item x) => x.amount)) { return(false); } List <Item> items = buyer.inventory.FindItemIDs(sellOrder.currencyID); if (sellOrder.currencyIsBP) { items = ( from x in buyer.inventory.FindItemIDs(this.blueprintBaseDef.itemid) where x.blueprintTarget == sellOrder.currencyID select x).ToList <Item>(); } items = items.Where <Item>((Item x) => { if (!x.hasCondition) { return(true); } if (x.conditionNormalized < 0.5f) { return(false); } return(x.maxConditionNormalized > 0.5f); }).ToList <Item>(); if (items.Count == 0) { return(false); } List <Item> items1 = items; int num1 = sellOrder.currencyAmountPerItem * numberOfTransactions; if (items1.Sum <Item>((Item x) => x.amount) < num1) { return(false); } this.transactionActive = true; int num2 = 0; foreach (Item item2 in items) { int num3 = Mathf.Min(num1 - num2, item2.amount); item = (item2.amount > num3 ? item2.SplitItem(num3) : item2); this.TakeCurrencyItem(item); num2 += num3; if (num2 < num1) { continue; } goto Label0; } Label0: int num4 = 0; foreach (Item item3 in list) { int num5 = num - num4; item1 = (item3.amount > num5 ? item3.SplitItem(num5) : item3); if (item1 != null) { num4 += item1.amount; this.GiveSoldItem(item1, buyer); } else { Debug.LogError("Vending machine error, contact developers!"); } if (num4 < num) { continue; } this.UpdateEmptyFlag(); this.transactionActive = false; return(true); } this.UpdateEmptyFlag(); this.transactionActive = false; return(true); }
public VendingMachine.SellOrder Copy() { VendingMachine.SellOrder sellOrder = Pool.Get <VendingMachine.SellOrder>(); this.CopyTo(sellOrder); return(sellOrder); }
public static void SerializeLengthDelimited(Stream stream, VendingMachine.SellOrder instance) { byte[] bytes = VendingMachine.SellOrder.SerializeToBytes(instance); ProtocolParser.WriteUInt32(stream, (uint)bytes.Length); stream.Write(bytes, 0, (int)bytes.Length); }