Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

303 rader
9.1 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using SUISS.Cloud.Extensions;
  6. namespace SUISS.Cloud
  7. {
  8. public class GiftController
  9. {
  10. public GiftController(ISSPStorageController storageController, IMessageController messageController, IPlayernameController playernameController, Dictionary<string, object> playerIndependentStorage)
  11. {
  12. this._storageController = storageController;
  13. this._messageController = messageController;
  14. this._playernameController = playernameController;
  15. this._playerIndependentStorage = playerIndependentStorage;
  16. }
  17. private IDictionary<string, object> CurrentStorage
  18. {
  19. get
  20. {
  21. if (this._sspStorage == null)
  22. {
  23. this._sspStorage = this._storageController.Get("SSP_Gift_Storage");
  24. }
  25. return this._sspStorage.Contents;
  26. }
  27. }
  28. private List<object> ClaimedGifts
  29. {
  30. get
  31. {
  32. if (!this.CurrentStorage.ContainsKey("Claimed_Gifts") || !(this.CurrentStorage["Claimed_Gifts"] is List<object>))
  33. {
  34. this.CurrentStorage.Add("Claimed_Gifts", new List<object>());
  35. }
  36. return (List<object>)this.CurrentStorage["Claimed_Gifts"];
  37. }
  38. set
  39. {
  40. this.CurrentStorage["Claimed_Gifts"] = value;
  41. }
  42. }
  43. private Dictionary<string, object> PlayerIndependentSentGifts
  44. {
  45. get
  46. {
  47. if (!this._playerIndependentStorage.ContainsKey("PlayerIndependentSendGifts") || !(this._playerIndependentStorage["PlayerIndependentSendGifts"] is Dictionary<string, object>))
  48. {
  49. this._playerIndependentStorage.Add("PlayerIndependentSendGifts", new Dictionary<string, object>());
  50. }
  51. return (Dictionary<string, object>)this._playerIndependentStorage["PlayerIndependentSendGifts"];
  52. }
  53. set
  54. {
  55. this._playerIndependentStorage["PlayerIndependentSendGifts"] = value;
  56. }
  57. }
  58. private void AddSentGift(IFriend friend)
  59. {
  60. string value = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff");
  61. this.SentGifts.Add(friend.AccountUuid, value);
  62. this.PlayerIndependentSentGifts.Add(friend.AccountUuid, value);
  63. }
  64. private Dictionary<string, object> SentGifts
  65. {
  66. get
  67. {
  68. if (!this.CurrentStorage.ContainsKey("Sent_Gifts") || !(this.CurrentStorage["Sent_Gifts"] is Dictionary<string, object>))
  69. {
  70. this.CurrentStorage.Add("Sent_Gifts", new Dictionary<string, object>());
  71. }
  72. return (Dictionary<string, object>)this.CurrentStorage["Sent_Gifts"];
  73. }
  74. set
  75. {
  76. this.CurrentStorage["Sent_Gifts"] = value;
  77. }
  78. }
  79. public long GiftCurrencyAmount
  80. {
  81. get
  82. {
  83. return this._giftCurrencyAmount;
  84. }
  85. }
  86. public List<IGift> Gifts
  87. {
  88. get
  89. {
  90. return this._gifts;
  91. }
  92. }
  93. public void SetGiftVariables(long currencyAmount, long sendLimit)
  94. {
  95. this._giftCurrencyAmount = currencyAmount;
  96. this._giftSendLimit = sendLimit;
  97. }
  98. public CloudRequest<IList<IGift>, GiftErrors?> Inbox()
  99. {
  100. return new CloudRequest<IList<IGift>, GiftErrors?>(this.CoInbox());
  101. }
  102. public CloudRequest<GiftErrors?> RefreshOutbox()
  103. {
  104. return new CloudRequest<GiftErrors?>(this.CoRefreshOutbox());
  105. }
  106. public CloudRequest<IList<IGift>, MessageErrors?> ClaimGifts(IList<IGift> allGifts)
  107. {
  108. List<IGift> list = new List<IGift>();
  109. foreach (IGift gift in allGifts)
  110. {
  111. if (!this.ClaimedGifts.Contains(gift.GiftId))
  112. {
  113. list.Add(gift);
  114. this.ClaimedGifts.Add(gift.GiftId);
  115. }
  116. }
  117. if (list.Count > 0)
  118. {
  119. return new CloudRequest<IList<IGift>, MessageErrors?>(this.CoClaimGifts(list));
  120. }
  121. return new CloudRequest<IList<IGift>, MessageErrors?>(this.DontCoClaimGifts());
  122. }
  123. public CloudRequest<GiftErrors?> SendGift(IGift gift, IFriend friend)
  124. {
  125. return new CloudRequest<GiftErrors?>(this.CoSendGift(gift, friend));
  126. }
  127. public bool CanSendGift(IFriend friend)
  128. {
  129. this.RemoveOldSentGifts();
  130. return (long)this.SentGifts.Count < this._giftSendLimit && (long)this.PlayerIndependentSentGifts.Count < this._giftSendLimit && !this.SentGifts.ContainsKey(friend.AccountUuid) && !this.PlayerIndependentSentGifts.ContainsKey(friend.AccountUuid);
  131. }
  132. private IEnumerator CoSendGift(IGift gift, IFriend friend)
  133. {
  134. CloudRequest<GiftErrors?> refreshCall = this.RefreshOutbox();
  135. yield return refreshCall;
  136. if (refreshCall.Error != null)
  137. {
  138. yield return new YieldError<GiftErrors?>(refreshCall.Error);
  139. }
  140. if (!this.CanSendGift(friend))
  141. {
  142. yield return new YieldError<GiftErrors?>(new GiftErrors?(GiftErrors.MaxReached));
  143. }
  144. CloudRequest<MessageErrors?> sendCall = this._messageController.SendMessage(new Message(friend.AccountUuid, "Gift", gift.ToStorage()));
  145. yield return sendCall;
  146. if (sendCall.Error != null)
  147. {
  148. yield return new YieldError<GiftErrors?>(new GiftErrors?(this.MapErrorType(sendCall.Error.Value)));
  149. }
  150. this.AddSentGift(friend);
  151. yield break;
  152. }
  153. private void RemoveOldSentGifts()
  154. {
  155. this.SentGifts.RemoveAll(delegate(KeyValuePair<string, object> kvp)
  156. {
  157. DateTime d = DateTime.ParseExact((string)kvp.Value, "MM/dd/yyyy hh:mm:ss.fff", CultureInfo.InvariantCulture);
  158. return (DateTime.UtcNow - d).Hours >= 24;
  159. });
  160. this.PlayerIndependentSentGifts.RemoveAll(delegate(KeyValuePair<string, object> kvp)
  161. {
  162. DateTime d = DateTime.ParseExact((string)kvp.Value, "MM/dd/yyyy hh:mm:ss.fff", CultureInfo.InvariantCulture);
  163. return (DateTime.UtcNow - d).Hours >= 24;
  164. });
  165. }
  166. private IEnumerator CoInbox()
  167. {
  168. CloudRequest<List<IReceivedMessage>, MessageErrors?> inboxCall = this._messageController.Inbox("Gift");
  169. yield return inboxCall;
  170. if (inboxCall.Error != null)
  171. {
  172. yield return new YieldError<GiftErrors?>(new GiftErrors?(this.MapErrorType(inboxCall.Error.Value)));
  173. }
  174. this.Gifts.Clear();
  175. foreach (IReceivedMessage receivedMessage in inboxCall.Result)
  176. {
  177. Gift gift = new Gift(string.Empty, string.Empty, string.Empty, -1L);
  178. gift.FromStorage(receivedMessage.Data);
  179. if (!this.ClaimedGifts.Contains(gift.GiftId))
  180. {
  181. if (!(receivedMessage.FromInstallUuid == this._playernameController.InstallUuid))
  182. {
  183. this.Gifts.Add(gift);
  184. }
  185. }
  186. }
  187. yield return new YieldResult<IList<IGift>>(this.Gifts);
  188. yield break;
  189. }
  190. private IEnumerator CoRefreshOutbox()
  191. {
  192. CloudRequest<MessageErrors?> storageCall = this._storageController.Get("SSP_Gift_Storage").TrySendToServer();
  193. yield return storageCall;
  194. if (storageCall.Error != null)
  195. {
  196. yield return new YieldError<GiftErrors?>(new GiftErrors?(this.MapErrorType(storageCall.Error.Value)));
  197. }
  198. yield break;
  199. }
  200. private IEnumerator DontCoClaimGifts()
  201. {
  202. yield return new YieldResult<IList<IGift>>(new List<IGift>());
  203. yield break;
  204. }
  205. private IEnumerator CoClaimGifts(List<IGift> newlyClaimedGifts)
  206. {
  207. CloudRequest<MessageErrors?> sendCall = this._sspStorage.TrySendToServer();
  208. yield return sendCall;
  209. if (sendCall.Error != null)
  210. {
  211. this.ClaimedGifts.RemoveAll((object element) => newlyClaimedGifts.Find((IGift giftEl) => giftEl.GiftId == (string)element) != null);
  212. yield return new YieldError<MessageErrors?>(new MessageErrors?(MessageErrors.GeneralError));
  213. }
  214. yield return new YieldResult<IList<IGift>>(newlyClaimedGifts);
  215. yield break;
  216. }
  217. private IEnumerator CoClaimGift(IGift gift)
  218. {
  219. if (this.ClaimedGifts.Contains(gift.GiftId))
  220. {
  221. yield return new YieldError<GiftErrors?>(new GiftErrors?(GiftErrors.AlreadyClaimed));
  222. }
  223. else
  224. {
  225. this.ClaimedGifts.Add(gift.GiftId);
  226. CloudRequest<MessageErrors?> sendCall = this._sspStorage.TrySendToServer();
  227. yield return sendCall;
  228. if (sendCall.Error != null)
  229. {
  230. this.ClaimedGifts.Remove(gift.GiftId);
  231. yield return new YieldError<GiftErrors?>(new GiftErrors?(GiftErrors.GeneralError));
  232. }
  233. }
  234. yield break;
  235. }
  236. private GiftErrors MapErrorType(MessageErrors input)
  237. {
  238. switch (input)
  239. {
  240. case MessageErrors.NoInternet:
  241. return GiftErrors.NoInternet;
  242. case MessageErrors.GeneralError:
  243. return GiftErrors.GeneralError;
  244. case MessageErrors.PlayernamesNotLoggedIn:
  245. return GiftErrors.PlayernamesNotLoggedIn;
  246. case MessageErrors.ConcurrencyError:
  247. return GiftErrors.ConcurrencyError;
  248. }
  249. return GiftErrors.GeneralError;
  250. }
  251. private const string GiftController_SSPStorageKey = "SSP_Gift_Storage";
  252. private const string SSP_Gift_ClaimedGiftsKey = "Claimed_Gifts";
  253. private const string Sent_Gifts_Key = "Sent_Gifts";
  254. public const string Gift_MessageType = "Gift";
  255. private const int ExpireSentGiftsHours = 24;
  256. private SSPStorage _sspStorage;
  257. private ISSPStorageController _storageController;
  258. private IMessageController _messageController;
  259. private IPlayernameController _playernameController;
  260. private long _giftCurrencyAmount = 5L;
  261. private long _giftSendLimit = 5L;
  262. private List<IGift> _gifts = new List<IGift>();
  263. private Dictionary<string, object> _playerIndependentStorage;
  264. private const string PlayerIndependentSendGifts_Key = "PlayerIndependentSendGifts";
  265. }
  266. }