ItemUseService.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections.Generic;
  3. using DevilLink2.Core;
  4. namespace DevilLink2.Gameplay
  5. {
  6. public enum ItemUseStatus { Success, InsufficientInventory, GameNotRunning, InteractionLocked, NoAvailablePair, UnsupportedMode, OperationFailed }
  7. public sealed class HintInfo
  8. {
  9. public const double DurationSeconds = 1.6;
  10. public Guid SessionId { get; }
  11. public int FirstId { get; }
  12. public int SecondId { get; }
  13. public IReadOnlyList<GridPosition> Path { get; }
  14. internal HintInfo(Guid sessionId, BoardMove move)
  15. {
  16. SessionId = sessionId; FirstId = move.First.Id; SecondId = move.Second.Id;
  17. Path = new List<GridPosition>(move.Path).AsReadOnly();
  18. }
  19. }
  20. public sealed class ItemUseResult
  21. {
  22. public ItemUseStatus Status { get; }
  23. public bool Succeeded => Status == ItemUseStatus.Success;
  24. public HintInfo Hint { get; }
  25. public string Error { get; }
  26. public ItemUseResult(ItemUseStatus status, HintInfo hint = null, string error = null)
  27. { Status = status; Hint = hint; Error = error; }
  28. }
  29. public sealed class ItemUseService
  30. {
  31. private readonly GameFlowController flow;
  32. private readonly Random random;
  33. private readonly BoardMoveFinder moves = new BoardMoveFinder();
  34. public event Action<HintInfo> HintRequested;
  35. public ItemUseService(GameFlowController flow, Random random = null)
  36. { this.flow = flow ?? throw new ArgumentNullException(nameof(flow)); this.random = random ?? new Random(); }
  37. public ItemUseResult TryUseHint() => flow.ExecuteItem(runtime =>
  38. {
  39. if (flow.Economy.Inventory.GetCount(ItemType.Hint) == 0) return new ItemUseResult(ItemUseStatus.InsufficientInventory);
  40. if (!moves.TryFindMove(runtime.Session.Board, out var move)) return new ItemUseResult(ItemUseStatus.NoAvailablePair);
  41. var hint = new HintInfo(runtime.Session.Id, move);
  42. flow.Economy.ConsumeItem(ItemType.Hint); flow.Economy.NotifyItemUsed();
  43. HintRequested?.Invoke(hint);
  44. return new ItemUseResult(ItemUseStatus.Success, hint);
  45. });
  46. public ItemUseResult TryUseShuffle() => flow.ExecuteItem(runtime =>
  47. {
  48. if (flow.Economy.Inventory.GetCount(ItemType.Shuffle) == 0) return new ItemUseResult(ItemUseStatus.InsufficientInventory);
  49. bool success = runtime.Interaction.TryManualShuffle(random, () => {
  50. flow.Economy.ConsumeItem(ItemType.Shuffle); flow.Economy.NotifyItemUsed();
  51. }, out _, out var error);
  52. if (!success) return new ItemUseResult(ItemUseStatus.OperationFailed, error: error);
  53. return new ItemUseResult(ItemUseStatus.Success);
  54. });
  55. public ItemUseResult TryUseTime() => flow.ExecuteItem(runtime =>
  56. {
  57. if (!runtime.Timer.HasTimer) return new ItemUseResult(ItemUseStatus.UnsupportedMode);
  58. if (flow.Economy.Inventory.GetCount(ItemType.Time) == 0) return new ItemUseResult(ItemUseStatus.InsufficientInventory);
  59. if (!runtime.AddTime(30)) return new ItemUseResult(ItemUseStatus.OperationFailed);
  60. flow.Economy.ConsumeItem(ItemType.Time); flow.Economy.NotifyItemUsed();
  61. return new ItemUseResult(ItemUseStatus.Success);
  62. });
  63. }
  64. }