Unstable 0.17.4.0
This commit is contained in:
@@ -7,51 +7,57 @@ namespace Barotrauma
|
||||
{
|
||||
partial class CargoManager
|
||||
{
|
||||
public void SellBackPurchasedItems(List<PurchasedItem> itemsToSell, Client client = null)
|
||||
public void SellBackPurchasedItems(Identifier storeIdentifier, List<PurchasedItem> itemsToSell, Client client = null)
|
||||
{
|
||||
// Check all the prices before starting the transaction
|
||||
// to make sure the modifiers stay the same for the whole transaction
|
||||
Dictionary<ItemPrefab, int> buyValues = GetBuyValuesAtCurrentLocation(itemsToSell.Select(i => i.ItemPrefab));
|
||||
foreach (PurchasedItem item in itemsToSell)
|
||||
// Check all the prices before starting the transaction to make sure the modifiers stay the same for the whole transaction
|
||||
var buyValues = GetBuyValuesAtCurrentLocation(storeIdentifier, itemsToSell.Select(i => i.ItemPrefab));
|
||||
var store = Location.GetStore(storeIdentifier);
|
||||
if (store == null) { return; }
|
||||
var storeSpecificItems = GetPurchasedItems(storeIdentifier);
|
||||
foreach (var item in itemsToSell)
|
||||
{
|
||||
var itemValue = item.Quantity * buyValues[item.ItemPrefab];
|
||||
Location.StoreCurrentBalance -= itemValue;
|
||||
store.Balance -= itemValue;
|
||||
campaign.GetWallet(client).Give(itemValue);
|
||||
PurchasedItems.Remove(item);
|
||||
storeSpecificItems?.Remove(item);
|
||||
}
|
||||
}
|
||||
|
||||
public void BuyBackSoldItems(List<SoldItem> itemsToBuy, Client client)
|
||||
public void BuyBackSoldItems(Identifier storeIdentifier, List<SoldItem> itemsToBuy)
|
||||
{
|
||||
// Check all the prices before starting the transaction
|
||||
// to make sure the modifiers stay the same for the whole transaction
|
||||
var sellValues = GetSellValuesAtCurrentLocation(itemsToBuy.Select(i => i.ItemPrefab));
|
||||
var store = Location.GetStore(storeIdentifier);
|
||||
if (store == null) { return; }
|
||||
var storeSpecificItems = SoldItems.GetValueOrDefault(storeIdentifier);
|
||||
// Check all the prices before starting the transaction to make sure the modifiers stay the same for the whole transaction
|
||||
var sellValues = GetSellValuesAtCurrentLocation(storeIdentifier, itemsToBuy.Select(i => i.ItemPrefab));
|
||||
foreach (var item in itemsToBuy)
|
||||
{
|
||||
int itemValue = sellValues[item.ItemPrefab];
|
||||
if (Location.StoreCurrentBalance < itemValue || item.Removed) { continue; }
|
||||
Location.StoreCurrentBalance += itemValue;
|
||||
if (store.Balance < itemValue || item.Removed) { continue; }
|
||||
store.Balance += itemValue;
|
||||
campaign.Bank.TryDeduct(itemValue);
|
||||
SoldItems.Remove(item);
|
||||
storeSpecificItems.Remove(item);
|
||||
}
|
||||
}
|
||||
|
||||
public void SellItems(List<SoldItem> itemsToSell, Client client)
|
||||
public void SellItems(Identifier storeIdentifier, List<SoldItem> itemsToSell, Client client)
|
||||
{
|
||||
var store = Location.GetStore(storeIdentifier);
|
||||
if (store == null) { return; }
|
||||
bool canAddToRemoveQueue = (GameMain.NetworkMember == null || GameMain.NetworkMember.IsServer) && Entity.Spawner != null;
|
||||
IEnumerable<Item> sellableItemsInSub = Enumerable.Empty<Item>();
|
||||
if (canAddToRemoveQueue && itemsToSell.Any(i => i.Origin == SoldItem.SellOrigin.Submarine && i.ID == Entity.NullEntityID && !i.Removed))
|
||||
{
|
||||
sellableItemsInSub = GetSellableItemsFromSub();
|
||||
}
|
||||
// Check all the prices before starting the transaction
|
||||
// to make sure the modifiers stay the same for the whole transaction
|
||||
var sellValues = GetSellValuesAtCurrentLocation(itemsToSell.Select(i => i.ItemPrefab));
|
||||
var itemsSoldAtStore = SoldItems.GetValueOrDefault(storeIdentifier);
|
||||
// Check all the prices before starting the transaction to make sure the modifiers stay the same for the whole transaction
|
||||
var sellValues = GetSellValuesAtCurrentLocation(storeIdentifier, itemsToSell.Select(i => i.ItemPrefab));
|
||||
foreach (var item in itemsToSell)
|
||||
{
|
||||
int itemValue = sellValues[item.ItemPrefab];
|
||||
// check if the store can afford the item and if the item hasn't been removed already
|
||||
if (Location.StoreCurrentBalance < itemValue || item.Removed) { continue; }
|
||||
if (store.Balance < itemValue || item.Removed) { continue; }
|
||||
// Server determines the items that are sold from the sub in multiplayer
|
||||
if (item.Origin == SoldItem.SellOrigin.Submarine && item.ID == Entity.NullEntityID && !item.Removed)
|
||||
{
|
||||
@@ -66,8 +72,8 @@ namespace Barotrauma
|
||||
item.Removed = true;
|
||||
Entity.Spawner.AddItemToRemoveQueue(entity);
|
||||
}
|
||||
SoldItems.Add(item);
|
||||
Location.StoreCurrentBalance -= itemValue;
|
||||
itemsSoldAtStore?.Add(item);
|
||||
store.Balance -= itemValue;
|
||||
campaign.Bank.Give(itemValue);
|
||||
GameAnalyticsManager.AddMoneyGainedEvent(itemValue, GameAnalyticsManager.MoneySource.Store, item.ItemPrefab.Identifier.Value);
|
||||
}
|
||||
|
||||
+79
-89
@@ -616,8 +616,17 @@ namespace Barotrauma
|
||||
}
|
||||
|
||||
// Store balance
|
||||
msg.Write(true);
|
||||
msg.Write((UInt16)map.CurrentLocation.StoreCurrentBalance);
|
||||
bool hasStores = map.CurrentLocation.Stores != null && map.CurrentLocation.Stores.Any();
|
||||
msg.Write(hasStores);
|
||||
if (hasStores)
|
||||
{
|
||||
msg.Write((byte)map.CurrentLocation.Stores.Count);
|
||||
foreach (var store in map.CurrentLocation.Stores.Values)
|
||||
{
|
||||
msg.Write(store.Identifier);
|
||||
msg.Write((UInt16)store.Balance);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -626,36 +635,10 @@ namespace Barotrauma
|
||||
msg.Write(false);
|
||||
}
|
||||
|
||||
msg.Write((UInt16)CargoManager.ItemsInBuyCrate.Count);
|
||||
foreach (PurchasedItem pi in CargoManager.ItemsInBuyCrate)
|
||||
{
|
||||
msg.Write(pi.ItemPrefab.Identifier);
|
||||
msg.WriteRangedInteger(pi.Quantity, 0, CargoManager.MaxQuantity);
|
||||
}
|
||||
|
||||
msg.Write((UInt16)CargoManager.ItemsInSellFromSubCrate.Count);
|
||||
foreach (PurchasedItem pi in CargoManager.ItemsInSellFromSubCrate)
|
||||
{
|
||||
msg.Write(pi.ItemPrefab.Identifier);
|
||||
msg.WriteRangedInteger(pi.Quantity, 0, CargoManager.MaxQuantity);
|
||||
}
|
||||
|
||||
msg.Write((UInt16)CargoManager.PurchasedItems.Count);
|
||||
foreach (PurchasedItem pi in CargoManager.PurchasedItems)
|
||||
{
|
||||
msg.Write(pi.ItemPrefab.Identifier);
|
||||
msg.WriteRangedInteger(pi.Quantity, 0, CargoManager.MaxQuantity);
|
||||
}
|
||||
|
||||
msg.Write((UInt16)CargoManager.SoldItems.Count);
|
||||
foreach (SoldItem si in CargoManager.SoldItems)
|
||||
{
|
||||
msg.Write(si.ItemPrefab.Identifier);
|
||||
msg.Write((UInt16)si.ID);
|
||||
msg.Write(si.Removed);
|
||||
msg.Write(si.SellerID);
|
||||
msg.Write((byte)si.Origin);
|
||||
}
|
||||
WriteItems(msg, CargoManager.ItemsInBuyCrate);
|
||||
WriteItems(msg, CargoManager.ItemsInSellFromSubCrate);
|
||||
WriteItems(msg, CargoManager.PurchasedItems);
|
||||
WriteItems(msg, CargoManager.SoldItems);
|
||||
|
||||
msg.Write((ushort)UpgradeManager.PendingUpgrades.Count);
|
||||
foreach (var (prefab, category, level) in UpgradeManager.PendingUpgrades)
|
||||
@@ -700,44 +683,10 @@ namespace Barotrauma
|
||||
bool purchasedItemRepairs = msg.ReadBoolean();
|
||||
bool purchasedLostShuttles = msg.ReadBoolean();
|
||||
|
||||
UInt16 buyCrateItemCount = msg.ReadUInt16();
|
||||
List<PurchasedItem> buyCrateItems = new List<PurchasedItem>();
|
||||
for (int i = 0; i < buyCrateItemCount; i++)
|
||||
{
|
||||
string itemPrefabIdentifier = msg.ReadString();
|
||||
int itemQuantity = msg.ReadRangedInteger(0, CargoManager.MaxQuantity);
|
||||
buyCrateItems.Add(new PurchasedItem(ItemPrefab.Prefabs[itemPrefabIdentifier], itemQuantity, sender));
|
||||
}
|
||||
|
||||
UInt16 subSellCrateItemCount = msg.ReadUInt16();
|
||||
List<PurchasedItem> subSellCrateItems = new List<PurchasedItem>();
|
||||
for (int i = 0; i < subSellCrateItemCount; i++)
|
||||
{
|
||||
string itemPrefabIdentifier = msg.ReadString();
|
||||
int itemQuantity = msg.ReadRangedInteger(0, CargoManager.MaxQuantity);
|
||||
subSellCrateItems.Add(new PurchasedItem(ItemPrefab.Prefabs[itemPrefabIdentifier], itemQuantity, sender));
|
||||
}
|
||||
|
||||
UInt16 purchasedItemCount = msg.ReadUInt16();
|
||||
List<PurchasedItem> purchasedItems = new List<PurchasedItem>();
|
||||
for (int i = 0; i < purchasedItemCount; i++)
|
||||
{
|
||||
string itemPrefabIdentifier = msg.ReadString();
|
||||
int itemQuantity = msg.ReadRangedInteger(0, CargoManager.MaxQuantity);
|
||||
purchasedItems.Add(new PurchasedItem(ItemPrefab.Prefabs[itemPrefabIdentifier], itemQuantity, sender));
|
||||
}
|
||||
|
||||
UInt16 soldItemCount = msg.ReadUInt16();
|
||||
List<SoldItem> soldItems = new List<SoldItem>();
|
||||
for (int i = 0; i < soldItemCount; i++)
|
||||
{
|
||||
string itemPrefabIdentifier = msg.ReadString();
|
||||
UInt16 id = msg.ReadUInt16();
|
||||
bool removed = msg.ReadBoolean();
|
||||
byte sellerId = msg.ReadByte();
|
||||
byte origin = msg.ReadByte();
|
||||
soldItems.Add(new SoldItem(ItemPrefab.Prefabs[itemPrefabIdentifier], id, removed, sellerId, (SoldItem.SellOrigin)origin));
|
||||
}
|
||||
var buyCrateItems = ReadPurchasedItems(msg, sender);
|
||||
var subSellCrateItems = ReadPurchasedItems(msg, sender);
|
||||
var purchasedItems = ReadPurchasedItems(msg, sender);
|
||||
var soldItems = ReadSoldItems(msg);
|
||||
|
||||
ushort purchasedUpgradeCount = msg.ReadUInt16();
|
||||
List<PurchasedUpgrade> purchasedUpgrades = new List<PurchasedUpgrade>();
|
||||
@@ -839,42 +788,83 @@ namespace Barotrauma
|
||||
bool allowedToUseStore = AllowedToManageCampaign(sender, ClientPermissions.CampaignStore);
|
||||
if (allowedToManageCampaign || allowedToUseStore || AllowedToManageCampaign(sender, ClientPermissions.BuyItems))
|
||||
{
|
||||
var currentBuyCrateItems = new List<PurchasedItem>(CargoManager.ItemsInBuyCrate);
|
||||
currentBuyCrateItems.ForEach(i => CargoManager.ModifyItemQuantityInBuyCrate(i.ItemPrefab, -i.Quantity, sender));
|
||||
buyCrateItems.ForEach(i => CargoManager.ModifyItemQuantityInBuyCrate(i.ItemPrefab, i.Quantity, sender));
|
||||
CargoManager.SellBackPurchasedItems(new List<PurchasedItem>(CargoManager.PurchasedItems));
|
||||
CargoManager.PurchaseItems(purchasedItems, false, sender);
|
||||
var prevBuyCrateItems = new Dictionary<Identifier, List<PurchasedItem>>(CargoManager.ItemsInBuyCrate);
|
||||
foreach (var store in prevBuyCrateItems)
|
||||
{
|
||||
foreach (var item in store.Value)
|
||||
{
|
||||
CargoManager.ModifyItemQuantityInBuyCrate(store.Key, item.ItemPrefab, -item.Quantity, sender);
|
||||
}
|
||||
}
|
||||
foreach (var store in buyCrateItems)
|
||||
{
|
||||
foreach (var item in store.Value)
|
||||
{
|
||||
CargoManager.ModifyItemQuantityInBuyCrate(store.Key, item.ItemPrefab, item.Quantity, sender);
|
||||
}
|
||||
}
|
||||
var prevPurchasedItems = new Dictionary<Identifier, List<PurchasedItem>>(CargoManager.PurchasedItems);
|
||||
foreach (var store in prevPurchasedItems)
|
||||
{
|
||||
CargoManager.SellBackPurchasedItems(store.Key, store.Value);
|
||||
}
|
||||
foreach (var store in purchasedItems)
|
||||
{
|
||||
CargoManager.PurchaseItems(store.Key, store.Value, false, sender);
|
||||
}
|
||||
}
|
||||
|
||||
bool allowedToSellSubItems = AllowedToManageCampaign(sender, ClientPermissions.SellSubItems);
|
||||
if (allowedToManageCampaign || allowedToUseStore || allowedToSellSubItems)
|
||||
{
|
||||
var currentSubSellCrateItems = new List<PurchasedItem>(CargoManager.ItemsInSellFromSubCrate);
|
||||
currentSubSellCrateItems.ForEach(i => CargoManager.ModifyItemQuantityInSubSellCrate(i.ItemPrefab, -i.Quantity, sender));
|
||||
subSellCrateItems.ForEach(i => CargoManager.ModifyItemQuantityInSubSellCrate(i.ItemPrefab, i.Quantity, sender));
|
||||
var prevSubSellCrateItems = new Dictionary<Identifier, List<PurchasedItem>>(CargoManager.ItemsInSellFromSubCrate);
|
||||
foreach (var store in prevSubSellCrateItems)
|
||||
{
|
||||
foreach (var item in store.Value)
|
||||
{
|
||||
CargoManager.ModifyItemQuantityInSubSellCrate(store.Key, item.ItemPrefab, -item.Quantity, sender);
|
||||
}
|
||||
}
|
||||
foreach (var store in subSellCrateItems)
|
||||
{
|
||||
foreach (var item in store.Value)
|
||||
{
|
||||
CargoManager.ModifyItemQuantityInSubSellCrate(store.Key, item.ItemPrefab, item.Quantity, sender);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool allowedToSellInventoryItems = AllowedToManageCampaign(sender, ClientPermissions.SellInventoryItems);
|
||||
if (allowedToManageCampaign || allowedToUseStore || (allowedToSellInventoryItems && allowedToSellSubItems))
|
||||
{
|
||||
// for some reason CargoManager.SoldItem is never cleared by the server, I've added a check to SellItems that ignores all
|
||||
// sold items that are removed so they should be discarded on the next message
|
||||
CargoManager.BuyBackSoldItems(new List<SoldItem>(CargoManager.SoldItems), sender);
|
||||
CargoManager.SellItems(soldItems, sender);
|
||||
var prevSoldItems = new Dictionary<Identifier, List<SoldItem>>(CargoManager.SoldItems);
|
||||
foreach (var store in prevSoldItems)
|
||||
{
|
||||
CargoManager.BuyBackSoldItems(store.Key, store.Value);
|
||||
}
|
||||
foreach (var store in soldItems)
|
||||
{
|
||||
CargoManager.SellItems(store.Key, store.Value, sender);
|
||||
}
|
||||
}
|
||||
else if (allowedToSellInventoryItems || allowedToSellSubItems)
|
||||
{
|
||||
if (allowedToSellInventoryItems)
|
||||
var prevSoldItems = new Dictionary<Identifier, List<SoldItem>>(CargoManager.SoldItems);
|
||||
foreach (var store in prevSoldItems)
|
||||
{
|
||||
CargoManager.BuyBackSoldItems(new List<SoldItem>(CargoManager.SoldItems.Where(i => i.Origin == SoldItem.SellOrigin.Character)), sender);
|
||||
soldItems.RemoveAll(i => i.Origin != SoldItem.SellOrigin.Character);
|
||||
store.Value.RemoveAll(predicate);
|
||||
CargoManager.BuyBackSoldItems(store.Key, store.Value);
|
||||
}
|
||||
else
|
||||
foreach (var store in soldItems)
|
||||
{
|
||||
CargoManager.BuyBackSoldItems(new List<SoldItem>(CargoManager.SoldItems.Where(i => i.Origin == SoldItem.SellOrigin.Submarine)), sender);
|
||||
soldItems.RemoveAll(i => i.Origin != SoldItem.SellOrigin.Submarine);
|
||||
store.Value.RemoveAll(predicate);
|
||||
}
|
||||
CargoManager.SellItems(soldItems, sender);
|
||||
foreach (var store in soldItems)
|
||||
{
|
||||
CargoManager.SellItems(store.Key, store.Value, sender);
|
||||
}
|
||||
bool predicate(SoldItem i) => allowedToSellInventoryItems != (i.Origin == SoldItem.SellOrigin.Character);
|
||||
}
|
||||
|
||||
if (allowedToManageCampaign)
|
||||
@@ -960,7 +950,7 @@ namespace Barotrauma
|
||||
|
||||
public void ServerReadRewardDistribution(IReadMessage msg, Client sender)
|
||||
{
|
||||
NetWalletSalaryUpdate update = INetSerializableStruct.Read<NetWalletSalaryUpdate>(msg);
|
||||
NetWalletSetSalaryUpdate update = INetSerializableStruct.Read<NetWalletSetSalaryUpdate>(msg);
|
||||
|
||||
if (!AllowedToManageCampaign(sender)) { return; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user