You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

98 regels
2.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using SUISS.Storage;
  4. public class TimeBoostReward : IOngoingReward, IStorable
  5. {
  6. public TimeBoostReward()
  7. {
  8. }
  9. public TimeBoostReward(double boostMultiplier, DateTime startTime, TimeSpan duration)
  10. {
  11. this.BoostMultiplier = boostMultiplier;
  12. this.StartTime = startTime;
  13. this.Duration = duration;
  14. this.IsActive = true;
  15. }
  16. public double BoostMultiplier { get; private set; }
  17. public DateTime StartTime { get; private set; }
  18. public TimeSpan Duration { get; private set; }
  19. public bool IsActive
  20. {
  21. get
  22. {
  23. return this._isActive && DateTime.UtcNow - this.StartTime <= this.Duration;
  24. }
  25. private set
  26. {
  27. this._isActive = value;
  28. }
  29. }
  30. public void FromStorage(IDictionary<string, object> dict)
  31. {
  32. this.BoostMultiplier = (double)dict["BoostMultiplier"];
  33. this.StartTime = new DateTime((long)dict["StartTime"], DateTimeKind.Utc);
  34. this.Duration = new TimeSpan((long)dict["Duration"]);
  35. this.IsActive = (bool)dict["IsActive"];
  36. }
  37. public void Run()
  38. {
  39. CityIsland current = CityIsland.Current;
  40. if (current != null)
  41. {
  42. current.TimeBoost(this.BoostMultiplier, this.Duration - (DateTime.UtcNow - this.StartTime));
  43. }
  44. }
  45. public IDictionary<string, object> ToStorage()
  46. {
  47. IDictionary<string, object> dictionary = new Dictionary<string, object>();
  48. dictionary["BoostMultiplier"] = this.BoostMultiplier;
  49. dictionary["StartTime"] = this.StartTime.Ticks;
  50. dictionary["Duration"] = this.Duration.Ticks;
  51. dictionary["IsActive"] = this.IsActive;
  52. return dictionary;
  53. }
  54. public override string ToString()
  55. {
  56. return string.Format("[Multiplier] {0}\n[StartTime] {1}\n[Duration] {2}/{3}", new object[]
  57. {
  58. this.BoostMultiplier,
  59. this.StartTime,
  60. DateTime.UtcNow - this.StartTime,
  61. this.Duration
  62. });
  63. }
  64. public bool Add(IOngoingReward reward)
  65. {
  66. TimeBoostReward timeBoostReward = reward as TimeBoostReward;
  67. if (timeBoostReward != null)
  68. {
  69. this.BoostMultiplier = Math.Max(this.BoostMultiplier, timeBoostReward.BoostMultiplier);
  70. this.Duration += timeBoostReward.Duration;
  71. timeBoostReward.Run();
  72. return true;
  73. }
  74. return false;
  75. }
  76. public const string BoostMultiplierKey = "BoostMultiplier";
  77. public const string StartTimeKey = "StartTime";
  78. public const string DurationKey = "Duration";
  79. public const string IsActiveKey = "IsActive";
  80. private bool _isActive;
  81. }