using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using SUISS.Cloud.Extensions; namespace SUISS.Cloud { public class GiftController { public GiftController(ISSPStorageController storageController, IMessageController messageController, IPlayernameController playernameController, Dictionary playerIndependentStorage) { this._storageController = storageController; this._messageController = messageController; this._playernameController = playernameController; this._playerIndependentStorage = playerIndependentStorage; } private IDictionary CurrentStorage { get { if (this._sspStorage == null) { this._sspStorage = this._storageController.Get("SSP_Gift_Storage"); } return this._sspStorage.Contents; } } private List ClaimedGifts { get { if (!this.CurrentStorage.ContainsKey("Claimed_Gifts") || !(this.CurrentStorage["Claimed_Gifts"] is List)) { this.CurrentStorage.Add("Claimed_Gifts", new List()); } return (List)this.CurrentStorage["Claimed_Gifts"]; } set { this.CurrentStorage["Claimed_Gifts"] = value; } } private Dictionary PlayerIndependentSentGifts { get { if (!this._playerIndependentStorage.ContainsKey("PlayerIndependentSendGifts") || !(this._playerIndependentStorage["PlayerIndependentSendGifts"] is Dictionary)) { this._playerIndependentStorage.Add("PlayerIndependentSendGifts", new Dictionary()); } return (Dictionary)this._playerIndependentStorage["PlayerIndependentSendGifts"]; } set { this._playerIndependentStorage["PlayerIndependentSendGifts"] = value; } } private void AddSentGift(IFriend friend) { string value = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff"); this.SentGifts.Add(friend.AccountUuid, value); this.PlayerIndependentSentGifts.Add(friend.AccountUuid, value); } private Dictionary SentGifts { get { if (!this.CurrentStorage.ContainsKey("Sent_Gifts") || !(this.CurrentStorage["Sent_Gifts"] is Dictionary)) { this.CurrentStorage.Add("Sent_Gifts", new Dictionary()); } return (Dictionary)this.CurrentStorage["Sent_Gifts"]; } set { this.CurrentStorage["Sent_Gifts"] = value; } } public long GiftCurrencyAmount { get { return this._giftCurrencyAmount; } } public List Gifts { get { return this._gifts; } } public void SetGiftVariables(long currencyAmount, long sendLimit) { this._giftCurrencyAmount = currencyAmount; this._giftSendLimit = sendLimit; } public CloudRequest, GiftErrors?> Inbox() { return new CloudRequest, GiftErrors?>(this.CoInbox()); } public CloudRequest RefreshOutbox() { return new CloudRequest(this.CoRefreshOutbox()); } public CloudRequest, MessageErrors?> ClaimGifts(IList allGifts) { List list = new List(); foreach (IGift gift in allGifts) { if (!this.ClaimedGifts.Contains(gift.GiftId)) { list.Add(gift); this.ClaimedGifts.Add(gift.GiftId); } } if (list.Count > 0) { return new CloudRequest, MessageErrors?>(this.CoClaimGifts(list)); } return new CloudRequest, MessageErrors?>(this.DontCoClaimGifts()); } public CloudRequest SendGift(IGift gift, IFriend friend) { return new CloudRequest(this.CoSendGift(gift, friend)); } public bool CanSendGift(IFriend friend) { this.RemoveOldSentGifts(); return (long)this.SentGifts.Count < this._giftSendLimit && (long)this.PlayerIndependentSentGifts.Count < this._giftSendLimit && !this.SentGifts.ContainsKey(friend.AccountUuid) && !this.PlayerIndependentSentGifts.ContainsKey(friend.AccountUuid); } private IEnumerator CoSendGift(IGift gift, IFriend friend) { CloudRequest refreshCall = this.RefreshOutbox(); yield return refreshCall; if (refreshCall.Error != null) { yield return new YieldError(refreshCall.Error); } if (!this.CanSendGift(friend)) { yield return new YieldError(new GiftErrors?(GiftErrors.MaxReached)); } CloudRequest sendCall = this._messageController.SendMessage(new Message(friend.AccountUuid, "Gift", gift.ToStorage())); yield return sendCall; if (sendCall.Error != null) { yield return new YieldError(new GiftErrors?(this.MapErrorType(sendCall.Error.Value))); } this.AddSentGift(friend); yield break; } private void RemoveOldSentGifts() { this.SentGifts.RemoveAll(delegate(KeyValuePair kvp) { DateTime d = DateTime.ParseExact((string)kvp.Value, "MM/dd/yyyy hh:mm:ss.fff", CultureInfo.InvariantCulture); return (DateTime.UtcNow - d).Hours >= 24; }); this.PlayerIndependentSentGifts.RemoveAll(delegate(KeyValuePair kvp) { DateTime d = DateTime.ParseExact((string)kvp.Value, "MM/dd/yyyy hh:mm:ss.fff", CultureInfo.InvariantCulture); return (DateTime.UtcNow - d).Hours >= 24; }); } private IEnumerator CoInbox() { CloudRequest, MessageErrors?> inboxCall = this._messageController.Inbox("Gift"); yield return inboxCall; if (inboxCall.Error != null) { yield return new YieldError(new GiftErrors?(this.MapErrorType(inboxCall.Error.Value))); } this.Gifts.Clear(); foreach (IReceivedMessage receivedMessage in inboxCall.Result) { Gift gift = new Gift(string.Empty, string.Empty, string.Empty, -1L); gift.FromStorage(receivedMessage.Data); if (!this.ClaimedGifts.Contains(gift.GiftId)) { if (!(receivedMessage.FromInstallUuid == this._playernameController.InstallUuid)) { this.Gifts.Add(gift); } } } yield return new YieldResult>(this.Gifts); yield break; } private IEnumerator CoRefreshOutbox() { CloudRequest storageCall = this._storageController.Get("SSP_Gift_Storage").TrySendToServer(); yield return storageCall; if (storageCall.Error != null) { yield return new YieldError(new GiftErrors?(this.MapErrorType(storageCall.Error.Value))); } yield break; } private IEnumerator DontCoClaimGifts() { yield return new YieldResult>(new List()); yield break; } private IEnumerator CoClaimGifts(List newlyClaimedGifts) { CloudRequest sendCall = this._sspStorage.TrySendToServer(); yield return sendCall; if (sendCall.Error != null) { this.ClaimedGifts.RemoveAll((object element) => newlyClaimedGifts.Find((IGift giftEl) => giftEl.GiftId == (string)element) != null); yield return new YieldError(new MessageErrors?(MessageErrors.GeneralError)); } yield return new YieldResult>(newlyClaimedGifts); yield break; } private IEnumerator CoClaimGift(IGift gift) { if (this.ClaimedGifts.Contains(gift.GiftId)) { yield return new YieldError(new GiftErrors?(GiftErrors.AlreadyClaimed)); } else { this.ClaimedGifts.Add(gift.GiftId); CloudRequest sendCall = this._sspStorage.TrySendToServer(); yield return sendCall; if (sendCall.Error != null) { this.ClaimedGifts.Remove(gift.GiftId); yield return new YieldError(new GiftErrors?(GiftErrors.GeneralError)); } } yield break; } private GiftErrors MapErrorType(MessageErrors input) { switch (input) { case MessageErrors.NoInternet: return GiftErrors.NoInternet; case MessageErrors.GeneralError: return GiftErrors.GeneralError; case MessageErrors.PlayernamesNotLoggedIn: return GiftErrors.PlayernamesNotLoggedIn; case MessageErrors.ConcurrencyError: return GiftErrors.ConcurrencyError; } return GiftErrors.GeneralError; } private const string GiftController_SSPStorageKey = "SSP_Gift_Storage"; private const string SSP_Gift_ClaimedGiftsKey = "Claimed_Gifts"; private const string Sent_Gifts_Key = "Sent_Gifts"; public const string Gift_MessageType = "Gift"; private const int ExpireSentGiftsHours = 24; private SSPStorage _sspStorage; private ISSPStorageController _storageController; private IMessageController _messageController; private IPlayernameController _playernameController; private long _giftCurrencyAmount = 5L; private long _giftSendLimit = 5L; private List _gifts = new List(); private Dictionary _playerIndependentStorage; private const string PlayerIndependentSendGifts_Key = "PlayerIndependentSendGifts"; } }