Skip to content

Commit

Permalink
Merge pull request #557 from nitoygo/dev/recover-items-from-npc-on-di…
Browse files Browse the repository at this point in the history
…sconnect

Recover items placed in NPC when player is disconnected
  • Loading branch information
sven-n authored Dec 21, 2024
2 parents 48ed19e + 7a4af2c commit 4aa7cd4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/GameLogic/PlayerActions/Items/BaseItemCraftingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public abstract class BaseItemCraftingHandler : IItemCraftingHandler
if (await this.DoTheMixAsync(items, player, socketSlot).ConfigureAwait(false) is { } item)
{
player.Logger.LogInformation("Crafted item: {item}", item);
player.BackupInventory = null;

return (CraftingResult.Success, item);
}

Expand Down
5 changes: 5 additions & 0 deletions src/GameLogic/PlayerActions/TalkNpcAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ private async ValueTask ShowDialogOfOpenedNpcAsync(Player player)
}
});

break;
case NpcWindow.ChaosMachine:
case NpcWindow.RemoveJohOption:
player.BackupInventory = new BackupItemStorage(player.Inventory!.ItemStorage);
await player.InvokeViewPlugInAsync<IOpenNpcWindowPlugIn>(p => p.OpenNpcWindowAsync(npcStats.NpcWindow)).ConfigureAwait(false);
break;
default:
await player.InvokeViewPlugInAsync<IOpenNpcWindowPlugIn>(p => p.OpenNpcWindowAsync(npcStats.NpcWindow)).ConfigureAwait(false);
Expand Down
1 change: 1 addition & 0 deletions src/GameLogic/PlayerActions/Trade/BaseTradeAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected async ValueTask CancelTradeAsync(ITrader trader, bool checkState = tru
}

trader.Inventory.ItemStorage.Money = trader.BackupInventory.Money;
trader.BackupInventory = null;
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/GameServer/GameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,27 @@ private async ValueTask SaveSessionOfPlayerAsync(Player player)
{
try
{
// Recover items placed in an NPC or trade dialog when player is diconnected
if (player.BackupInventory is not null)
{
player.Inventory!.Clear();
player.BackupInventory.RestoreItemStates();
foreach (var item in player.BackupInventory.Items)
{
await player.Inventory.AddItemAsync(item.ItemSlot, item).ConfigureAwait(false);
}

player.Inventory.ItemStorage.Money = player.BackupInventory.Money;
}
else if (player is { TemporaryStorage: { } storage, Inventory: { } inventory })
{
if (!await inventory.TryTakeAllAsync(storage).ConfigureAwait(false))
{
// This should never happen since the space is checked before mixing
this._logger.LogWarning($"Could not take fit items of player {player}");
}
}

if (!await player.PersistenceContext.SaveChangesAsync().ConfigureAwait(false))
{
this._logger.LogWarning($"Could not save session of player {player}");
Expand Down

0 comments on commit 4aa7cd4

Please sign in to comment.