Merge branch 'dev' of https://github.com/Regalis11/Barotrauma.git into unstable-tests

This commit is contained in:
Evil Factory
2022-04-11 15:58:36 -03:00
38 changed files with 361 additions and 184 deletions
@@ -5,6 +5,7 @@ using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Barotrauma.Networking;
#if SERVER
@@ -15,7 +16,9 @@ namespace Barotrauma
{
class PurchasedItem
{
public ItemPrefab ItemPrefab { get; }
public ItemPrefab ItemPrefab => ItemPrefab.Prefabs[ItemPrefabIdentifier];
public Identifier ItemPrefabIdentifier { get; }
public int Quantity { get; set; }
public bool? IsStoreComponentEnabled { get; set; }
@@ -23,7 +26,7 @@ namespace Barotrauma
public PurchasedItem(ItemPrefab itemPrefab, int quantity, int buyerCharacterInfoId)
{
ItemPrefab = itemPrefab;
ItemPrefabIdentifier = itemPrefab.Identifier;
Quantity = quantity;
IsStoreComponentEnabled = null;
BuyerCharacterInfoId = buyerCharacterInfoId;
@@ -34,8 +37,11 @@ namespace Barotrauma
: this(itemPrefab, quantity, buyer: null) { }
#endif
public PurchasedItem(ItemPrefab itemPrefab, int quantity, Client buyer)
: this(itemPrefab.Identifier, quantity, buyer) { }
public PurchasedItem(Identifier itemPrefabId, int quantity, Client buyer)
{
ItemPrefab = itemPrefab;
ItemPrefabIdentifier = itemPrefabId;
Quantity = quantity;
IsStoreComponentEnabled = null;
BuyerCharacterInfoId = buyer?.Character?.Info?.ID ?? Character.Controlled?.Info?.ID ?? 0;
@@ -269,6 +275,24 @@ namespace Barotrauma
OnItemsInSellFromSubCrateChanged?.Invoke();
}
#if SERVER
public void OnNewItemsPurchased(Identifier storeIdentifier, List<PurchasedItem> newItems, Client client)
{
StringBuilder sb = new StringBuilder();
int price = 0;
Dictionary<ItemPrefab, int> buyValues = GetBuyValuesAtCurrentLocation(storeIdentifier, newItems.Select(i => i.ItemPrefab));
foreach (PurchasedItem item in newItems)
{
int itemValue = item.Quantity * buyValues[item.ItemPrefab];
sb.Append($"\n - {item.ItemPrefab.Name} x{item.Quantity}");
price += itemValue;
}
GameServer.Log($"{NetworkMember.ClientLogName(client, client?.Name ?? "Unknown")} purchased {newItems.Count} item(s) for {TextManager.FormatCurrency(price)}{sb.ToString()}", ServerLog.MessageType.Money);
}
#endif
public void PurchaseItems(Identifier storeIdentifier, List<PurchasedItem> itemsToPurchase, bool removeFromCrate, Client client = null)
{
var store = Location.GetStore(storeIdentifier);
@@ -32,7 +32,7 @@ namespace Barotrauma
/// </summary>
internal struct NetWalletUpdate : INetSerializableStruct
{
[NetworkSerialize(ArrayMaxSize = NetConfig.MaxPlayers + 1)]
[NetworkSerialize(ArrayMaxSize = 256)]
public NetWalletTransaction[] Transactions;
}
@@ -73,6 +73,9 @@ namespace Barotrauma
{
other.BalanceChanged = AddOptionalInt(other.BalanceChanged, BalanceChanged);
other.RewardDistributionChanged = AddOptionalInt(other.RewardDistributionChanged, RewardDistributionChanged);
other.BalanceChanged = TurnToNoneIfZero(other.BalanceChanged);
other.RewardDistributionChanged = TurnToNoneIfZero(other.RewardDistributionChanged);
return other;
static Option<int> AddOptionalInt(Option<int> a, Option<int> b)
@@ -94,6 +97,16 @@ namespace Barotrauma
_ => throw new ArgumentOutOfRangeException(nameof(a))
};
}
static Option<int> TurnToNoneIfZero(Option<int> option)
{
return option switch
{
Some<int> s => s.Value == 0 ? Option<int>.None() : option,
None<int> _ => option,
_ => throw new ArgumentOutOfRangeException(nameof(option))
};
}
}
}
@@ -251,7 +251,7 @@ namespace Barotrauma
msg.Write((UInt16)storeItems.Value.Count);
foreach (var item in storeItems.Value)
{
msg.Write(item.ItemPrefab.Identifier);
msg.Write(item.ItemPrefabIdentifier);
msg.WriteRangedInteger(item.Quantity, 0, CargoManager.MaxQuantity);
}
}
@@ -270,7 +270,7 @@ namespace Barotrauma
{
Identifier itemId = msg.ReadIdentifier();
int quantity = msg.ReadRangedInteger(0, CargoManager.MaxQuantity);
items[storeId].Add(new PurchasedItem(ItemPrefab.Prefabs[itemId], quantity, sender));
items[storeId].Add(new PurchasedItem(itemId, quantity, sender));
}
}
return items;
@@ -303,7 +303,7 @@ namespace Barotrauma
Identifier storeId = msg.ReadIdentifier();
soldItems.Add(storeId, new List<SoldItem>());
UInt16 itemCount = msg.ReadUInt16();
for (int j = 0; j < storeCount; j++)
for (int j = 0; j < itemCount; j++)
{
Identifier prefabId = msg.ReadIdentifier();
UInt16 itemId = msg.ReadUInt16();