Skip to content

Commit

Permalink
Merge pull request #511 from pacampbell/bbm_remove_items_on_death_hom…
Browse files Browse the repository at this point in the history
…e_point

feat: Items not equipped are lost in BBM on homepoint
  • Loading branch information
pacampbell authored Sep 1, 2024
2 parents 8ddd730 + 0ad3fe3 commit 260373c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 45 deletions.
37 changes: 37 additions & 0 deletions Arrowgene.Ddon.GameServer/Characters/ItemManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#nullable enable
using Arrowgene.Ddon.Database;
using Arrowgene.Ddon.Database.Model;
using Arrowgene.Ddon.Server;
using Arrowgene.Ddon.Shared.Entity.PacketStructure;
using Arrowgene.Ddon.Shared.Entity.Structure;
Expand All @@ -14,10 +15,17 @@ namespace Arrowgene.Ddon.GameServer.Characters
{
public class ItemManager
{
private DdonGameServer _Server;
public ItemManager(DdonGameServer server)
{
_Server = server;
}

private static readonly ServerLogger Logger = LogProvider.Logger<ServerLogger>(typeof(ItemManager));

private static readonly uint STACK_BOX_MAX = 999;

public static readonly List<StorageType> AllItemStorages = Enum.GetValues(typeof(StorageType)).Cast<StorageType>().ToList();
public static readonly List<StorageType> ItemBagStorageTypes = new List<StorageType> {
StorageType.ItemBagConsumable, StorageType.ItemBagMaterial, StorageType.ItemBagEquipment, StorageType.ItemBagJob,
StorageType.KeyItems
Expand Down Expand Up @@ -727,6 +735,35 @@ public static bool SendToItemBag(uint storageType)
}
return toBag;
}

public List<CDataItemUpdateResult> RemoveAllItemsFromInventory(Character character, Storages storages, List<StorageType> storageTypes, DbConnection connection = null)
{
var results = new List<CDataItemUpdateResult>();
foreach (var storageType in storageTypes)
{
if (!character.Storage.HasStorage(storageType))
{
continue;
}

for (int i = 0; i < character.Storage.GetStorage(storageType).Items.Count; i++)
{
ushort slotNo = (ushort)(i + 1);

var storageItem = storages.GetStorage(storageType).GetItem(slotNo);
if (storageItem != null)
{
results.Add(_Server.ItemManager.CreateItemUpdateResult(null, storageItem.Item1, storageType, slotNo, 0, 0));
results.Add(_Server.ItemManager.CreateItemUpdateResult(null, storageItem.Item1, storageType, slotNo, 0, storageItem.Item2));
}

character.Storage.GetStorage(storageType).SetItem(null, 0, slotNo);
_Server.Database.DeleteStorageItem(character.ContentCharacterId, storageType, slotNo, connection);
}
}

return results;
}
}

[Serializable]
Expand Down
2 changes: 1 addition & 1 deletion Arrowgene.Ddon.GameServer/DdonGameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public DdonGameServer(GameServerSetting setting, IDatabase database, AssetReposi
ClientLookup = new GameClientLookup();
ChatLogHandler = new ChatLogHandler();
ChatManager = new ChatManager(this, Router);
ItemManager = new ItemManager();
ItemManager = new ItemManager(this);
CraftManager = new CraftManager(this);
PartyManager = new PartyManager(this);
ExpManager = new ExpManager(this, ClientLookup);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,14 @@ public BattleContentContentResetHandler(DdonGameServer server) : base(server)

public override S2CBattleContentContentResetRes Handle(GameClient client, C2SBattleContentContentResetReq request)
{
// Reset Inventory
var updateItemList = RemoveAllItemsFromInventory(client.Character, client.Character.Storage, ItemManager.ItemBagStorageTypes);
client.Character.Storage.Clear();

// Flush Storage
S2CItemUpdateCharacterItemNtc updateCharacterItemNtc = new S2CItemUpdateCharacterItemNtc()
{
UpdateType = ItemNoticeType.SwitchingStorage,
UpdateItemList = updateItemList
};
client.Send(updateCharacterItemNtc);

// Add back equipment templates
client.Character.EquipmentTemplate = new EquipmentTemplate(Server.AssetRepository.BitterblackMazeAsset.GenerateStarterEquipment(), Server.AssetRepository.BitterblackMazeAsset.GenerateStarterJobEquipment());

List<CDataItemUpdateResult> updateItemList = null;
Server.Database.ExecuteInTransaction(connection =>
{
// Delete all existing storage items
Server.Database.DeleteAllStorageItems(connection, client.Character.ContentCharacterId);
// Remove all items from the player inventory
updateItemList = Server.ItemManager.RemoveAllItemsFromInventory(client.Character, client.Character.Storage, ItemManager.AllItemStorages, connection);

// Remove items equipped in the database
Server.Database.DeleteAllEquipItems(client.Character.CommonId, connection);
Expand All @@ -52,6 +41,13 @@ public override S2CBattleContentContentResetRes Handle(GameClient client, C2SBat
Server.Database.CreateItems(connection, client.Character);
});

S2CItemUpdateCharacterItemNtc updateCharacterItemNtc = new S2CItemUpdateCharacterItemNtc()
{
UpdateType = ItemNoticeType.SwitchingStorage,
UpdateItemList = updateItemList
};
client.Send(updateCharacterItemNtc);

// Add back equipment
client.Character.Equipment = client.Character.Storage.GetCharacterEquipment();

Expand Down Expand Up @@ -81,26 +77,5 @@ public override S2CBattleContentContentResetRes Handle(GameClient client, C2SBat

return new S2CBattleContentContentResetRes();
}

private List<CDataItemUpdateResult> RemoveAllItemsFromInventory(Character character, Storages storages, List<StorageType> storageTypes)
{
var results = new List<CDataItemUpdateResult>();
foreach (var storageType in storageTypes)
{
for (int i = 0; i < character.Storage.GetStorage(storageType).Items.Count; i++)
{
ushort slotNo = (ushort)(i + 1);

var storageItem = storages.GetStorage(storageType).GetItem(slotNo);
if (storageItem != null)
{
results.Add(Server.ItemManager.CreateItemUpdateResult(null, storageItem.Item1, storageType, slotNo, 0, 0));
results.Add(Server.ItemManager.CreateItemUpdateResult(null, storageItem.Item1, storageType, slotNo, 0, storageItem.Item2));
}
}
}

return results;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using System.Threading.Tasks;
using System;
using System.Threading;
using Arrowgene.Ddon.GameServer.Characters;
using Arrowgene.Ddon.Server;
using Arrowgene.Ddon.Server.Network;
using Arrowgene.Ddon.Shared.Entity.PacketStructure;
using Arrowgene.Ddon.Shared.Network;
using Arrowgene.Logging;
using Arrowgene.Ddon.Shared.Entity.Structure;
using Arrowgene.Ddon.Shared.Model;
using Arrowgene.Logging;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Arrowgene.Ddon.GameServer.Handler
{
public class CharacterCharacterPenaltyReviveHandler : StructurePacketHandler<GameClient, C2SCharacterCharacterPenaltyReviveReq>
public class CharacterCharacterPenaltyReviveHandler : GameRequestPacketHandler<C2SCharacterCharacterPenaltyReviveReq, S2CCharacterCharacterPenaltyReviveRes>
{
private static readonly ServerLogger Logger = LogProvider.Logger<ServerLogger>(typeof(CharacterCharacterPenaltyReviveHandler));

Expand All @@ -21,10 +21,9 @@ public CharacterCharacterPenaltyReviveHandler(DdonGameServer server) : base(serv
{
}

public override void Handle(GameClient client, StructurePacket<C2SCharacterCharacterPenaltyReviveReq> packet)
public override S2CCharacterCharacterPenaltyReviveRes Handle(GameClient client, C2SCharacterCharacterPenaltyReviveReq packet)
{
S2CCharacterCharacterPenaltyReviveRes res = new S2CCharacterCharacterPenaltyReviveRes();
client.Send(res);

if (client.GameMode != GameMode.BitterblackMaze)
{
Expand All @@ -37,6 +36,25 @@ public override void Handle(GameClient client, StructurePacket<C2SCharacterChara
// Restore after time passes
Task.Delay(WeaknessTimeSpan).ContinueWith(_ => client.Send(new S2CCharacterFinishDeathPenaltyNtc()));
}
else if (client.GameMode == GameMode.BitterblackMaze)
{
// The player will lose all items in their bag when homepoints after a death (equipped items stay)
List<CDataItemUpdateResult> updateItemList = new List<CDataItemUpdateResult>();
Server.Database.ExecuteInTransaction(connection =>
{
updateItemList = Server.ItemManager.RemoveAllItemsFromInventory(client.Character, client.Character.Storage, ItemManager.ItemBagStorageTypes, connection);
});

// Flush Storage
S2CItemUpdateCharacterItemNtc updateCharacterItemNtc = new S2CItemUpdateCharacterItemNtc()
{
UpdateType = ItemNoticeType.SwitchingStorage,
UpdateItemList = updateItemList
};
client.Send(updateCharacterItemNtc);
}

return res;
}
}
}
16 changes: 16 additions & 0 deletions Arrowgene.Ddon.Shared/Model/Storages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public Storage GetStorage(StorageType storageType)
return storages[storageType];
}

public bool HasStorage(StorageType storageType)
{
return storages.ContainsKey(storageType);
}

public void AddStorage(StorageType storageType, Storage storage)
{
storages[storageType] = storage;
Expand All @@ -68,6 +73,17 @@ public void Clear()
}
}

public void Clear(List<StorageType> storageTypes)
{
foreach (var (type, storage) in storages)
{
if (storageTypes.Contains(type))
{
storage.Clear();
}
}
}

public Equipment GetCharacterEquipment()
{
return new Equipment(GetStorage(StorageType.CharacterEquipment), 0);
Expand Down

0 comments on commit 260373c

Please sign in to comment.