Skip to content

Commit

Permalink
Merge pull request #575 from nitoygo/bugfix/mini-game-ticket-handling
Browse files Browse the repository at this point in the history
Updated logic of minigame ticket consumption
  • Loading branch information
sven-n authored Jan 9, 2025
2 parents 268360a + 3db6f68 commit 19752a4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/GameLogic/IStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MUnique.OpenMU.GameLogic;

using MUnique.OpenMU.DataModel.Configuration.Items;
using MUnique.OpenMU.PlugIns;
using Nito.AsyncEx;

Expand Down Expand Up @@ -164,6 +165,13 @@ public interface IStorage
/// <returns>The item from the specified slot.</returns>
Item? GetItem(byte inventorySlot);

/// <summary>
/// Finds items that matches the given definition.
/// </summary>
/// <param name="definition">The item definition to be searched.</param>
/// <returns>The items with the same definition.</returns>
IEnumerable<Item> FindItemsByDefinition(ItemDefinition definition);

/// <summary>
/// Removes the item from this storage.
/// </summary>
Expand Down
35 changes: 32 additions & 3 deletions src/GameLogic/PlayerActions/MiniGames/EnterMiniGameAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MUnique.OpenMU.GameLogic.PlayerActions.MiniGames;

using MUnique.OpenMU.DataModel.Configuration.Items;
using MUnique.OpenMU.GameLogic.Attributes;
using MUnique.OpenMU.GameLogic.GuildWar;
using MUnique.OpenMU.GameLogic.MiniGames;
Expand All @@ -16,6 +17,8 @@ namespace MUnique.OpenMU.GameLogic.PlayerActions.MiniGames;
/// </summary>
public class EnterMiniGameAction
{
private const int UndefinedTicketSlot = 0xFF;

/// <summary>
/// Tries to enter the mini game event of the specified type and level with the specified ticket item.
/// </summary>
Expand Down Expand Up @@ -169,14 +172,40 @@ private async ValueTask ConsumeTicketItemAsync(Item? ticketItem, Player player)

private bool CheckTicketItem(MiniGameDefinition miniGameDefinition, Player player, byte gameTicketInventoryIndex, out Item? ticketItem)
{
ticketItem = null;

if (miniGameDefinition.TicketItem is not { } ticketItemDefinition)
{
ticketItem = null;
return true;
}

ticketItem = player.Inventory?.GetItem(gameTicketInventoryIndex);
return ticketItemDefinition.Equals(ticketItem?.Definition) && ticketItem.Durability > 0 && ticketItem.Level == miniGameDefinition.TicketItemLevel;
if (gameTicketInventoryIndex != UndefinedTicketSlot)
{
ticketItem = player.Inventory?.GetItem(gameTicketInventoryIndex);
if (this.IsValidTicket(ticketItem, ticketItemDefinition, miniGameDefinition))
{
return true;
}
}

foreach (var candidateItem in player.Inventory?.FindItemsByDefinition(ticketItemDefinition) ?? Enumerable.Empty<Item>())
{
if (this.IsValidTicket(candidateItem, ticketItemDefinition, miniGameDefinition))
{
ticketItem = candidateItem;
return true;
}
}

return false;
}

private bool IsValidTicket(Item? ticketItem, ItemDefinition requiredItemDefinition, MiniGameDefinition miniGameDefinition)
{
return ticketItem is not null
&& requiredItemDefinition.Equals(ticketItem.Definition)
&& ticketItem.Durability > 0
&& ticketItem.Level == miniGameDefinition.TicketItemLevel;
}

private bool CheckEntranceFee(MiniGameDefinition miniGameDefinition, Player player, out int entranceFee)
Expand Down
16 changes: 16 additions & 0 deletions src/GameLogic/Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace MUnique.OpenMU.GameLogic;

using MUnique.OpenMU.DataModel.Configuration.Items;

/// <summary>
/// This class wraps the access to the IItemStorage of an character.
/// </summary>
Expand Down Expand Up @@ -260,6 +262,20 @@ public async ValueTask<bool> TryTakeAllAsync(IStorage anotherStorage)
return extension?.GetItem(inventorySlot);
}

/// <inheritdoc />
public IEnumerable<Item> FindItemsByDefinition(ItemDefinition definition)
{
var primaryMatches = this.ItemArray
.Where(i => i != null && i.Definition == definition)
.Select(i => i!);

var extensionMatches = this.Extensions?
.SelectMany(extension => extension.FindItemsByDefinition(definition))
?? Enumerable.Empty<Item>();

return primaryMatches.Concat(extensionMatches);
}

/// <inheritdoc/>
public virtual async ValueTask RemoveItemAsync(Item item)
{
Expand Down

0 comments on commit 19752a4

Please sign in to comment.