Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.54 KB | None | 0 0
  1. // /* This Source Code Form is subject to the terms of the Mozilla Public
  2. //  * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. //  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Threading.Tasks;
  10. using Octgn.Core;
  11. using Octgn.Extentions;
  12. using Octgn.Site.Api;
  13. using Octgn.Site.Api.Models;
  14. using Octgn.Windows;
  15. using log4net;
  16.  
  17. namespace Octgn
  18. {
  19.     public class SleeveManager
  20.     {
  21.         internal static ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  22.  
  23.         #region Singleton
  24.  
  25.         internal static SleeveManager SingletonContext { get; set; }
  26.  
  27.         private static readonly object SleeveManagerSingletonLocker = new object();
  28.  
  29.         public static SleeveManager Instance
  30.         {
  31.             get
  32.             {
  33.                 if (SingletonContext == null)
  34.                 {
  35.                     lock (SleeveManagerSingletonLocker)
  36.                     {
  37.                         if (SingletonContext == null)
  38.                         {
  39.                             SingletonContext = new SleeveManager();
  40.                         }
  41.                     }
  42.                 }
  43.                 return SingletonContext;
  44.             }
  45.         }
  46.  
  47.         #endregion Singleton
  48.  
  49.         private ApiSleeve[] _sleeveCache;
  50.         private DateTime _lastGetTime = DateTime.MinValue;
  51.  
  52.         public Task<IEnumerable<ApiSleeve>> GetAllSleevesAsync()
  53.         {
  54.             return Task.Factory.StartNew<IEnumerable<ApiSleeve>>(GetAllSleeves);
  55.         }
  56.  
  57.         public Task<IEnumerable<ApiSleeve>> GetUserSleevesAsync()
  58.         {
  59.             return Task.Factory.StartNew<IEnumerable<ApiSleeve>>(GetUserSleeves);
  60.         }
  61.  
  62.         public Task<bool> AddSleeveToAccountAsync(int id)
  63.         {
  64.             return Task.Factory.StartNew(() => AddSleeveToAccount(id));
  65.         }
  66.  
  67.         public Task<ApiSleeve> SleeveFromIdAsync(int id)
  68.         {
  69.             return Task.Factory.StartNew(() => SleeveFromId(id));
  70.         }
  71.  
  72.         public IEnumerable<ApiSleeve> GetAllSleeves()
  73.         {
  74.             if (_sleeveCache != null)
  75.                 return _sleeveCache;
  76.             if (_lastGetTime >= DateTime.Now.AddMinutes(-1))
  77.                 return _sleeveCache ?? new ApiSleeve[0];
  78.             try
  79.             {
  80.                 _lastGetTime = DateTime.Now;
  81.                 var c = new Octgn.Site.Api.ApiClient();
  82.                 var sleeves = c.GetAllSleeves(0, 100);
  83.                 if (sleeves.Sleeves == null || sleeves.Sleeves.Length == 0)
  84.                 {
  85.                     WindowManager.GrowlWindow.AddNotification(new ErrorNotification("There was a problem downloading the sleeve list from the web. Please check your internet settings."));
  86.                     return new ApiSleeve[0];
  87.                 }
  88.                 _sleeveCache = sleeves.Sleeves.ToArray();
  89.                 return _sleeveCache;
  90.             }
  91.             catch (Exception e)
  92.             {
  93.                 Log.Error("GetAllSleeves", e);
  94.             }
  95.             WindowManager.GrowlWindow.AddNotification(new ErrorNotification("There was a problem downloading the sleeve list from the web. Please check your internet settings."));
  96.             return new ApiSleeve[0];
  97.         }
  98.  
  99.         public IEnumerable<ApiSleeve> GetUserSleeves()
  100.         {
  101.             try
  102.             {
  103.                 var c = new Octgn.Site.Api.ApiClient();
  104.  
  105.                 AuthorizedResponse<IEnumerable<ApiSleeve>> ownedSleeves;
  106.                 ownedSleeves = Program.LobbyClient.IsConnected == false ? c.GetUserSleeves(Prefs.Username, Prefs.Password.Decrypt()) : c.GetUserSleeves(Prefs.Username, Prefs.Password.Decrypt());
  107.  
  108.                 if (ownedSleeves.Authorized == false)
  109.                 {
  110.                     Log.Warn("Could not download user sleeves, the account was not authorized.");
  111.                     return new ApiSleeve[0];
  112.                 }
  113.                 if (ownedSleeves == null || ownedSleeves.Response == null)
  114.                 {
  115.                     WindowManager.GrowlWindow.AddNotification(new ErrorNotification("There was a problem downloading the user sleeve list from the web. Please check your internet settings."));
  116.                     return new ApiSleeve[0];
  117.                 }
  118.  
  119.                 return ownedSleeves.Response;
  120.             }
  121.             catch (Exception e)
  122.             {
  123.                 Log.Error("GetUserSleeves", e);
  124.             }
  125.             WindowManager.GrowlWindow.AddNotification(new ErrorNotification("There was a problem downloading the user sleeve list from the web. Please check your internet settings."));
  126.             return new ApiSleeve[0];
  127.         }
  128.  
  129.         public bool AddSleeveToAccount(int id)
  130.         {
  131.             try
  132.             {
  133.                 var req = new UpdateUserSleevesRequest();
  134.                 req.Add(new ApiSleeve()
  135.                 {
  136.                     Id = id
  137.                 });
  138.  
  139.                 var c = new ApiClient();
  140.                 AuthorizedResponse<int[]> result;
  141.                 result = Program.LobbyClient.IsConnected == false ? c.AddUserSleeves(Prefs.Username, Prefs.Password.Decrypt(), req) : c.AddUserSleeves(Prefs.Username, Prefs.Password.Decrypt(), req);
  142.                 if (result == null || result.Authorized == false || result.Response == null)
  143.                 {
  144.                     WindowManager.GrowlWindow.AddNotification(new ErrorNotification("There was a problem adding the sleeve to your account. Please check your internet settings."));
  145.                     return false;
  146.                 }
  147.                 if (result.Response.Contains(id))
  148.                     return true;
  149.             }
  150.             catch (Exception e)
  151.             {
  152.                 Log.Error("AddSleeveToAccount", e);
  153.             }
  154.             WindowManager.GrowlWindow.AddNotification(new ErrorNotification("There was a problem adding the sleeve to your account. Please check your internet settings."));
  155.             return false;
  156.         }
  157.  
  158.         public ApiSleeve SleeveFromId(int id)
  159.         {
  160.             var sleeves = GetAllSleeves();
  161.             var sleeve = sleeves.FirstOrDefault(x => x.Id == id);
  162.             return sleeve;
  163.         }
  164.  
  165.         public string GetSleeveString(int id)
  166.         {
  167.             if (id <= 0)
  168.                 return "";
  169.             var sleeve = GetAllSleeves().FirstOrDefault(x => x.Id == id);
  170.             if (sleeve == null)
  171.                 return "";
  172.             return id + "\t" + sleeve.Url;
  173.         }
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement