| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System;
- using System.Collections.Generic;
- using DevilLink2.Core;
- namespace DevilLink2.Gameplay
- {
- public enum ItemUseStatus { Success, InsufficientInventory, GameNotRunning, InteractionLocked, NoAvailablePair, UnsupportedMode, OperationFailed }
- public sealed class HintInfo
- {
- public const double DurationSeconds = 1.6;
- public Guid SessionId { get; }
- public int FirstId { get; }
- public int SecondId { get; }
- public IReadOnlyList<GridPosition> Path { get; }
- internal HintInfo(Guid sessionId, BoardMove move)
- {
- SessionId = sessionId; FirstId = move.First.Id; SecondId = move.Second.Id;
- Path = new List<GridPosition>(move.Path).AsReadOnly();
- }
- }
- public sealed class ItemUseResult
- {
- public ItemUseStatus Status { get; }
- public bool Succeeded => Status == ItemUseStatus.Success;
- public HintInfo Hint { get; }
- public string Error { get; }
- public ItemUseResult(ItemUseStatus status, HintInfo hint = null, string error = null)
- { Status = status; Hint = hint; Error = error; }
- }
- public sealed class ItemUseService
- {
- private readonly GameFlowController flow;
- private readonly Random random;
- private readonly BoardMoveFinder moves = new BoardMoveFinder();
- public event Action<HintInfo> HintRequested;
- public ItemUseService(GameFlowController flow, Random random = null)
- { this.flow = flow ?? throw new ArgumentNullException(nameof(flow)); this.random = random ?? new Random(); }
- public ItemUseResult TryUseHint() => flow.ExecuteItem(runtime =>
- {
- if (flow.Economy.Inventory.GetCount(ItemType.Hint) == 0) return new ItemUseResult(ItemUseStatus.InsufficientInventory);
- if (!moves.TryFindMove(runtime.Session.Board, out var move)) return new ItemUseResult(ItemUseStatus.NoAvailablePair);
- var hint = new HintInfo(runtime.Session.Id, move);
- flow.Economy.ConsumeItem(ItemType.Hint); flow.Economy.NotifyItemUsed();
- HintRequested?.Invoke(hint);
- return new ItemUseResult(ItemUseStatus.Success, hint);
- });
- public ItemUseResult TryUseShuffle() => flow.ExecuteItem(runtime =>
- {
- if (flow.Economy.Inventory.GetCount(ItemType.Shuffle) == 0) return new ItemUseResult(ItemUseStatus.InsufficientInventory);
- bool success = runtime.Interaction.TryManualShuffle(random, () => {
- flow.Economy.ConsumeItem(ItemType.Shuffle); flow.Economy.NotifyItemUsed();
- }, out _, out var error);
- if (!success) return new ItemUseResult(ItemUseStatus.OperationFailed, error: error);
- return new ItemUseResult(ItemUseStatus.Success);
- });
- public ItemUseResult TryUseTime() => flow.ExecuteItem(runtime =>
- {
- if (!runtime.Timer.HasTimer) return new ItemUseResult(ItemUseStatus.UnsupportedMode);
- if (flow.Economy.Inventory.GetCount(ItemType.Time) == 0) return new ItemUseResult(ItemUseStatus.InsufficientInventory);
- if (!runtime.AddTime(30)) return new ItemUseResult(ItemUseStatus.OperationFailed);
- flow.Economy.ConsumeItem(ItemType.Time); flow.Economy.NotifyItemUsed();
- return new ItemUseResult(ItemUseStatus.Success);
- });
- }
- }
|