diff --git a/CocNET/CocNET.Test/CocCoreClansTest.cs b/CocNET/CocNET.Test/CocCoreClansTest.cs index c865ab7..3e630e4 100644 --- a/CocNET/CocNET.Test/CocCoreClansTest.cs +++ b/CocNET/CocNET.Test/CocCoreClansTest.cs @@ -16,7 +16,7 @@ public class CocCoreClansTest [OneTimeSetUp] public void InitializeCore() { - ClansCore = CocCore.Instance(TOKEN).Container.Resolve(); + ClansCore = new CocCore(TOKEN).Container.Resolve(); } [Test, Category("ClansTest")] diff --git a/CocNET/CocNET.Test/CocCoreLeaguesTest.cs b/CocNET/CocNET.Test/CocCoreLeaguesTest.cs index 5ed1864..8c273a3 100644 --- a/CocNET/CocNET.Test/CocCoreLeaguesTest.cs +++ b/CocNET/CocNET.Test/CocCoreLeaguesTest.cs @@ -18,7 +18,7 @@ public class CocCoreLeaguesTest [OneTimeSetUp] public void InitializeCore() { - LeaguesCore = CocCore.Instance(TOKEN).Container.Resolve(); + LeaguesCore = new CocCore(TOKEN).Container.Resolve(); } [Test, Category("LeaguesTest")] diff --git a/CocNET/CocNET.Test/CocCoreLocationsTest.cs b/CocNET/CocNET.Test/CocCoreLocationsTest.cs index 18da6bc..f29dcbd 100644 --- a/CocNET/CocNET.Test/CocCoreLocationsTest.cs +++ b/CocNET/CocNET.Test/CocCoreLocationsTest.cs @@ -18,7 +18,7 @@ public class CocCoreLocationsTest [OneTimeSetUp] public void InitializeCore() { - LocationsCore = CocCore.Instance(TOKEN).Container.Resolve(); + LocationsCore = new CocCore(TOKEN).Container.Resolve(); } [Test, Category("LocationsTest")] diff --git a/CocNET/CocNET.Test/CocCorePlayersTest.cs b/CocNET/CocNET.Test/CocCorePlayersTest.cs index fb38371..6c5cfe5 100644 --- a/CocNET/CocNET.Test/CocCorePlayersTest.cs +++ b/CocNET/CocNET.Test/CocCorePlayersTest.cs @@ -18,7 +18,7 @@ public class CocCorePlayersTest [OneTimeSetUp] public void InitializeCore() { - PlayersCore = CocCore.Instance(TOKEN).Container.Resolve(); + PlayersCore = new CocCore(TOKEN).Container.Resolve(); } [Test, Category("LocationsTest")] diff --git a/CocNET/CocNET/CocCore.cs b/CocNET/CocNET/CocCore.cs index 8eaf719..52a7192 100644 --- a/CocNET/CocNET/CocCore.cs +++ b/CocNET/CocNET/CocCore.cs @@ -1,6 +1,5 @@ using CocNET.Interfaces; using CocNET.Methods; -using System; using Funq; using CocNET.Services; @@ -8,37 +7,18 @@ namespace CocNET { public class CocCore { - private static object syncRoot = new Object(); - public Container Container { get; set; } - private static CocCore instance; - public static CocCore Instance(string token) - { - lock (syncRoot) - { - if (instance == null) - { - instance = new CocCore(token); - } - } - return instance; - } - /// - /// Initialize your core. - /// - private CocCore(string token) + + public CocCore(string token) { - if (string.IsNullOrEmpty(token)) + if (Container == null) { - throw new ArgumentException("Token is nullable or empty."); + Container = new Container(); } - Container = new Funq.Container(); - Container.Register("Request", new Request(token)); - Container.Register(new CocCoreClans(Container.ResolveNamed("Request"))); - Container.Register(new CocCoreLocations(Container.ResolveNamed("Request"))); - Container.Register(new CocCoreLeagues(Container.ResolveNamed("Request"))); + //Container.Register(new CocCoreLocations(Container.ResolveNamed("Request"))); + //Container.Register(new CocCoreLeagues(Container.ResolveNamed("Request"))); Container.Register(new CocCorePlayers(Container.ResolveNamed("Request"))); } } diff --git a/CocNET/CocNET/CocNET.csproj b/CocNET/CocNET/CocNET.csproj index bd372a5..14fc229 100644 --- a/CocNET/CocNET/CocNET.csproj +++ b/CocNET/CocNET/CocNET.csproj @@ -34,7 +34,6 @@ ..\packages\Funq.1.0.0\lib\net40\Funq.dll - True ..\packages\morelinq.1.4.0\lib\net35\MoreLinq.dll @@ -69,6 +68,7 @@ + @@ -85,6 +85,11 @@ + + + + + diff --git a/CocNET/CocNET/Exception/TokenExpiredException.cs b/CocNET/CocNET/Exception/TokenExpiredException.cs new file mode 100644 index 0000000..ea82066 --- /dev/null +++ b/CocNET/CocNET/Exception/TokenExpiredException.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CocNET +{ + public class TokenExpiredException:Exception + { + public TokenExpiredException() : base() + { + + } + public TokenExpiredException(string message):base(message) + { + + } + } +} diff --git a/CocNET/CocNET/Interfaces/ICocCoreClans.cs b/CocNET/CocNET/Interfaces/ICocCoreClans.cs index a43cadb..1ad7edf 100644 --- a/CocNET/CocNET/Interfaces/ICocCoreClans.cs +++ b/CocNET/CocNET/Interfaces/ICocCoreClans.cs @@ -1,5 +1,6 @@ using CocNET.Types.Clans; using CocNET.Types.Clans.CurrentWar; +using CocNET.Types.Clans.LeagueWar; using CocNET.Types.Other; using System; using System.Collections.Generic; @@ -17,5 +18,7 @@ public interface ICocCoreClans SearchClan GetClans(SearchFilter searchFilter, bool withMember); List GetClansMembers(string clanTag); War GetCurrentWar(string clanTag); + LeagueWar GetCurrentWarLeague(string clanTag); + LeagueWarRound GetCurrentWarLeagueRound(string warTag); } } diff --git a/CocNET/CocNET/Methods/Request.cs b/CocNET/CocNET/Methods/Request.cs index 7e8f83c..7086e18 100644 --- a/CocNET/CocNET/Methods/Request.cs +++ b/CocNET/CocNET/Methods/Request.cs @@ -1,14 +1,6 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading.Tasks; +using System.Net; using Newtonsoft.Json; -using CocNET.Types; using RestSharp; -using System.Collections.Specialized; namespace CocNET.Methods { @@ -36,7 +28,6 @@ public RestRequest GetRequest(string resource) RestRequest request = new RestRequest(resource, Method.GET); request.AddHeader("authorization", string.Format("Bearer {0}", Token)); request.AddHeader("Accept", "application/json"); - return request; } @@ -44,6 +35,10 @@ public T GetResponse(string call, string query) { var request = GetRequest(call+query); var response = Client.Execute(request); + if (response.StatusCode == HttpStatusCode.Forbidden) + { + throw new TokenExpiredException(response.Content); + } return JsonConvert.DeserializeObject(response.Content); } @@ -51,6 +46,10 @@ public T GetResponse(string call) { var request = GetRequest(call); var response = Client.Execute(request); + if (response.StatusCode == HttpStatusCode.Forbidden) + { + throw new TokenExpiredException(response.Content); + } return JsonConvert.DeserializeObject(response.Content); } public string GetCall(params object[] values) diff --git a/CocNET/CocNET/Services/CocCoreClans.cs b/CocNET/CocNET/Services/CocCoreClans.cs index a930a81..3ec2bed 100644 --- a/CocNET/CocNET/Services/CocCoreClans.cs +++ b/CocNET/CocNET/Services/CocCoreClans.cs @@ -3,14 +3,12 @@ using CocNET.Types.Clans; using CocNET.Types.Clans.CurrentWar; using CocNET.Types.Other; +using CocNET.Types.Clans.LeagueWar; using CocNET.Types.Other.Other; using System; using System.Collections.Generic; using System.Collections.Specialized; -using System.Linq; using System.Reflection; -using System.Text; -using System.Threading.Tasks; using System.Web; namespace CocNET.Services @@ -117,10 +115,32 @@ public List GetClanWarLogs(string clanTag) public War GetCurrentWar(string clanTag) { var call = Request.GetCall(API_URL_CLANS, HttpUtility.UrlEncode(clanTag), "currentwar"); - + var currentWar = Request.GetResponse(call); return currentWar; } + + public LeagueWar GetCurrentWarLeague(string clanTag) + { + var call = Request.GetCall(API_URL_CLANS, HttpUtility.UrlEncode(clanTag), "currentwar", "leaguegroup"); + + var currentWar = Request.GetResponse(call); + + return currentWar; + } + /// + /// Warning! This is war Tag not clan Tag!! + /// + /// + /// + public LeagueWarRound GetCurrentWarLeagueRound(string warTag) + { + var call = Request.GetCall("clanwarleagues", "wars", HttpUtility.UrlEncode(warTag)); + + var currentWar = Request.GetResponse(call); + + return currentWar; + } } } \ No newline at end of file diff --git a/CocNET/CocNET/Services/CocCorePlayers.cs b/CocNET/CocNET/Services/CocCorePlayers.cs index 9d1b8ee..a256793 100644 --- a/CocNET/CocNET/Services/CocCorePlayers.cs +++ b/CocNET/CocNET/Services/CocCorePlayers.cs @@ -2,11 +2,6 @@ using CocNET.Methods; using CocNET.Types.Players; using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading.Tasks; using System.Web; namespace CocNET.Services @@ -23,8 +18,8 @@ public CocCorePlayers(Request requestClient) public Player GetPlayer(string playerTag) { var call = REQUEST.GetCall(API_URL_PLAYERS, HttpUtility.UrlEncode(playerTag)); - - return REQUEST.GetResponse(call); ; + var result = REQUEST.GetResponse(call); + return result; } } } diff --git a/CocNET/CocNET/Types/Clans/CurrentWar/WarClan.cs b/CocNET/CocNET/Types/Clans/CurrentWar/WarClan.cs index 858527c..9b8fcf6 100644 --- a/CocNET/CocNET/Types/Clans/CurrentWar/WarClan.cs +++ b/CocNET/CocNET/Types/Clans/CurrentWar/WarClan.cs @@ -32,6 +32,9 @@ public class WarClan : BadRequest [JsonProperty("destructionPercentage")] public decimal DestructionPercentage { get; set; } + [JsonProperty("expEarned")] + public decimal ExpEarned { get; set; } + [JsonProperty("members")] public List Members { get; set; } } diff --git a/CocNET/CocNET/Types/Clans/LeagueWar/LeagueClan.cs b/CocNET/CocNET/Types/Clans/LeagueWar/LeagueClan.cs new file mode 100644 index 0000000..4a4f425 --- /dev/null +++ b/CocNET/CocNET/Types/Clans/LeagueWar/LeagueClan.cs @@ -0,0 +1,32 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CocNET.Types.Clans.LeagueWar +{ + public class LeagueClan + { + [JsonProperty("tag")] + public string Tag { get; set; } + + [JsonProperty("name")] + public string Name { get; set; } + + [JsonProperty("clanLevel")] + public int ClanLevel { get; set; } + + [JsonProperty("badgeUrls")] + public Dictionary BadgeUrls { get; set; } + + [JsonProperty("attacks")] + public int Attacks { get; set; } + + [JsonProperty("stars")] + public int Stars { get; set; } + + [JsonProperty("members")] + public LeagueMember[] Members { get; set; } + } +} diff --git a/CocNET/CocNET/Types/Clans/LeagueWar/LeagueMember.cs b/CocNET/CocNET/Types/Clans/LeagueWar/LeagueMember.cs new file mode 100644 index 0000000..8b7d117 --- /dev/null +++ b/CocNET/CocNET/Types/Clans/LeagueWar/LeagueMember.cs @@ -0,0 +1,23 @@ +using CocNET.Types.Clans.CurrentWar; +using Newtonsoft.Json; + +namespace CocNET.Types.Clans.LeagueWar +{ + public class LeagueMember + { + [JsonProperty("tag")] + public string Tag { get; set; } + + [JsonProperty("name")] + public string Name { get; set; } + + [JsonProperty("townhallLevel")] + public int TownhallLevel { get; set; } + + [JsonProperty("mapPosition")] + public int MapPosition { get; set; } + + [JsonProperty("attacks")] + public WarAttack[] Attacks { get; set; } + } +} diff --git a/CocNET/CocNET/Types/Clans/LeagueWar/LeagueRounds.cs b/CocNET/CocNET/Types/Clans/LeagueWar/LeagueRounds.cs new file mode 100644 index 0000000..4d90f64 --- /dev/null +++ b/CocNET/CocNET/Types/Clans/LeagueWar/LeagueRounds.cs @@ -0,0 +1,11 @@ +using CocNET.Types.Other; +using Newtonsoft.Json; + +namespace CocNET.Types.Clans.LeagueWar +{ + public class LeagueRounds : BadRequest + { + [JsonProperty("warTags")] + public string[] warTags { get; set; } + } +} diff --git a/CocNET/CocNET/Types/Clans/LeagueWar/LeagueWar.cs b/CocNET/CocNET/Types/Clans/LeagueWar/LeagueWar.cs new file mode 100644 index 0000000..7617007 --- /dev/null +++ b/CocNET/CocNET/Types/Clans/LeagueWar/LeagueWar.cs @@ -0,0 +1,20 @@ +using CocNET.Types.Other; +using Newtonsoft.Json; + +namespace CocNET.Types.Clans.LeagueWar +{ + public class LeagueWar : BadRequest + { + [JsonProperty("state")] + public string State { get; set; } + + [JsonProperty("season")] + public string Season { get; set; } + + [JsonProperty("clans")] + public LeagueClan[] Clans { get; set; } + + [JsonProperty("rounds")] + public LeagueRounds[] Rounds { get; set; } + } +} diff --git a/CocNET/CocNET/Types/Clans/LeagueWar/LeagueWarRounds.cs b/CocNET/CocNET/Types/Clans/LeagueWar/LeagueWarRounds.cs new file mode 100644 index 0000000..3e81542 --- /dev/null +++ b/CocNET/CocNET/Types/Clans/LeagueWar/LeagueWarRounds.cs @@ -0,0 +1,28 @@ +using CocNET.Types.Other; +using Newtonsoft.Json; +using System; + +namespace CocNET.Types.Clans.LeagueWar +{ + public class LeagueWarRound:BadRequest + { + public string state { get; set; } + + public int teamSize { get; set; } + + [JsonProperty("preparationStartTime"), JsonConverter(typeof(CustomDateTimeConverter))] + public DateTime preparationStartTime { get; set; } + + [JsonProperty("startTime"), JsonConverter(typeof(CustomDateTimeConverter))] + public DateTime StartTime { get; set; } + + [JsonProperty("endTime"), JsonConverter(typeof(CustomDateTimeConverter))] + public DateTime EndTime { get; set; } + + [JsonProperty("clan")] + public LeagueClan clan { get; set; } + + [JsonProperty("opponent")] + public LeagueClan opponent { get; set; } + } +} diff --git a/CocNET/CocNET/Types/Other/Game/Achievement.cs b/CocNET/CocNET/Types/Other/Game/Achievement.cs index 4b6224a..b917cab 100644 --- a/CocNET/CocNET/Types/Other/Game/Achievement.cs +++ b/CocNET/CocNET/Types/Other/Game/Achievement.cs @@ -20,6 +20,6 @@ public class Achievement : BadRequest [JsonProperty("completionInfo")] public string CompletionInfo { get; set; } [JsonProperty("village")] - public int Village { get; set; } + public string Village { get; set; } } } diff --git a/CocNET/CocNET/Types/Other/Game/Hero.cs b/CocNET/CocNET/Types/Other/Game/Hero.cs index 95f4864..273d36e 100644 --- a/CocNET/CocNET/Types/Other/Game/Hero.cs +++ b/CocNET/CocNET/Types/Other/Game/Hero.cs @@ -17,6 +17,6 @@ public class Hero : BadRequest public int MaxLevel { get; set; } [JsonProperty("village")] - public int Village { get; set; } + public string Village { get; set; } } } diff --git a/CocNET/CocNET/Types/Other/Game/Troop.cs b/CocNET/CocNET/Types/Other/Game/Troop.cs index 6613976..f0f0f1f 100644 --- a/CocNET/CocNET/Types/Other/Game/Troop.cs +++ b/CocNET/CocNET/Types/Other/Game/Troop.cs @@ -16,6 +16,6 @@ public class Troop : BadRequest [JsonProperty("maxLevel")] public int MaxLevel { get; set; } [JsonProperty("village")] - public int Village { get; set; } + public string Village { get; set; } } } diff --git a/README.md b/README.md index 0e54c0c..becd014 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ class Program static void Main(string[] args) { string token = "your token"; - Container container = CocCore.Instance(token).Container; + Container container = new CocCore(token).Container; ICocCoreLeagues leaguesCore = container.Resolve(); var leagues = leaguesCore.GetLeagues("crystal");