|
- 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<string, object> playerIndependentStorage)
- {
- this._storageController = storageController;
- this._messageController = messageController;
- this._playernameController = playernameController;
- this._playerIndependentStorage = playerIndependentStorage;
- }
-
- private IDictionary<string, object> CurrentStorage
- {
- get
- {
- if (this._sspStorage == null)
- {
- this._sspStorage = this._storageController.Get("SSP_Gift_Storage");
- }
- return this._sspStorage.Contents;
- }
- }
-
- private List<object> ClaimedGifts
- {
- get
- {
- if (!this.CurrentStorage.ContainsKey("Claimed_Gifts") || !(this.CurrentStorage["Claimed_Gifts"] is List<object>))
- {
- this.CurrentStorage.Add("Claimed_Gifts", new List<object>());
- }
- return (List<object>)this.CurrentStorage["Claimed_Gifts"];
- }
- set
- {
- this.CurrentStorage["Claimed_Gifts"] = value;
- }
- }
-
- private Dictionary<string, object> PlayerIndependentSentGifts
- {
- get
- {
- if (!this._playerIndependentStorage.ContainsKey("PlayerIndependentSendGifts") || !(this._playerIndependentStorage["PlayerIndependentSendGifts"] is Dictionary<string, object>))
- {
- this._playerIndependentStorage.Add("PlayerIndependentSendGifts", new Dictionary<string, object>());
- }
- return (Dictionary<string, object>)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<string, object> SentGifts
- {
- get
- {
- if (!this.CurrentStorage.ContainsKey("Sent_Gifts") || !(this.CurrentStorage["Sent_Gifts"] is Dictionary<string, object>))
- {
- this.CurrentStorage.Add("Sent_Gifts", new Dictionary<string, object>());
- }
- return (Dictionary<string, object>)this.CurrentStorage["Sent_Gifts"];
- }
- set
- {
- this.CurrentStorage["Sent_Gifts"] = value;
- }
- }
-
- public long GiftCurrencyAmount
- {
- get
- {
- return this._giftCurrencyAmount;
- }
- }
-
- public List<IGift> Gifts
- {
- get
- {
- return this._gifts;
- }
- }
-
- public void SetGiftVariables(long currencyAmount, long sendLimit)
- {
- this._giftCurrencyAmount = currencyAmount;
- this._giftSendLimit = sendLimit;
- }
-
- public CloudRequest<IList<IGift>, GiftErrors?> Inbox()
- {
- return new CloudRequest<IList<IGift>, GiftErrors?>(this.CoInbox());
- }
-
- public CloudRequest<GiftErrors?> RefreshOutbox()
- {
- return new CloudRequest<GiftErrors?>(this.CoRefreshOutbox());
- }
-
- public CloudRequest<IList<IGift>, MessageErrors?> ClaimGifts(IList<IGift> allGifts)
- {
- List<IGift> list = new List<IGift>();
- 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<IList<IGift>, MessageErrors?>(this.CoClaimGifts(list));
- }
- return new CloudRequest<IList<IGift>, MessageErrors?>(this.DontCoClaimGifts());
- }
-
- public CloudRequest<GiftErrors?> SendGift(IGift gift, IFriend friend)
- {
- return new CloudRequest<GiftErrors?>(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<GiftErrors?> refreshCall = this.RefreshOutbox();
- yield return refreshCall;
- if (refreshCall.Error != null)
- {
- yield return new YieldError<GiftErrors?>(refreshCall.Error);
- }
- if (!this.CanSendGift(friend))
- {
- yield return new YieldError<GiftErrors?>(new GiftErrors?(GiftErrors.MaxReached));
- }
- CloudRequest<MessageErrors?> sendCall = this._messageController.SendMessage(new Message(friend.AccountUuid, "Gift", gift.ToStorage()));
- yield return sendCall;
- if (sendCall.Error != null)
- {
- yield return new YieldError<GiftErrors?>(new GiftErrors?(this.MapErrorType(sendCall.Error.Value)));
- }
- this.AddSentGift(friend);
- yield break;
- }
-
- private void RemoveOldSentGifts()
- {
- this.SentGifts.RemoveAll(delegate(KeyValuePair<string, object> 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<string, object> 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<List<IReceivedMessage>, MessageErrors?> inboxCall = this._messageController.Inbox("Gift");
- yield return inboxCall;
- if (inboxCall.Error != null)
- {
- yield return new YieldError<GiftErrors?>(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<IList<IGift>>(this.Gifts);
- yield break;
- }
-
- private IEnumerator CoRefreshOutbox()
- {
- CloudRequest<MessageErrors?> storageCall = this._storageController.Get("SSP_Gift_Storage").TrySendToServer();
- yield return storageCall;
- if (storageCall.Error != null)
- {
- yield return new YieldError<GiftErrors?>(new GiftErrors?(this.MapErrorType(storageCall.Error.Value)));
- }
- yield break;
- }
-
- private IEnumerator DontCoClaimGifts()
- {
- yield return new YieldResult<IList<IGift>>(new List<IGift>());
- yield break;
- }
-
- private IEnumerator CoClaimGifts(List<IGift> newlyClaimedGifts)
- {
- CloudRequest<MessageErrors?> 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<MessageErrors?>(new MessageErrors?(MessageErrors.GeneralError));
- }
- yield return new YieldResult<IList<IGift>>(newlyClaimedGifts);
- yield break;
- }
-
- private IEnumerator CoClaimGift(IGift gift)
- {
- if (this.ClaimedGifts.Contains(gift.GiftId))
- {
- yield return new YieldError<GiftErrors?>(new GiftErrors?(GiftErrors.AlreadyClaimed));
- }
- else
- {
- this.ClaimedGifts.Add(gift.GiftId);
- CloudRequest<MessageErrors?> sendCall = this._sspStorage.TrySendToServer();
- yield return sendCall;
- if (sendCall.Error != null)
- {
- this.ClaimedGifts.Remove(gift.GiftId);
- yield return new YieldError<GiftErrors?>(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<IGift> _gifts = new List<IGift>();
-
- private Dictionary<string, object> _playerIndependentStorage;
-
- private const string PlayerIndependentSendGifts_Key = "PlayerIndependentSendGifts";
- }
- }
|